MyAccount: Generate keys+Upload keys Ok [users needs to have account on myslice to...
[myslice.git] / third-party / jquery-html5storage-1.0 / jquery.html5storage.js
1 /*!
2 * jQuery Plugin to use Local Storage or Session Storage without worrying
3 * about HTML5 support. It uses Cookies for backward compatibility.
4 *
5 * @author Alberto Varela Sánchez (http://www.berriart.com)
6 * @version 1.0 (17th January 2013)
7 *
8 * Released under the MIT License (http://opensource.org/licenses/MIT)
9 *
10 * Copyright (c) 2013 Alberto Varela Sánchez (alberto@berriart.com)
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 */
30
31 ;(function(window, $ ) {
32     "use strict";
33
34     var types = ['localStorage','sessionStorage'],
35         support = [];
36
37     $.each(types, function( i, type ) {
38         try {
39             support[type] = type in window && window[type] !== null;
40         } catch (e) {
41             support[type] = false;
42         }
43
44         $[type] = {
45             settings : {
46                 cookiePrefix : 'html5fallback:' + type + ':',
47                 cookieOptions : {
48                     path : '/',
49                     domain : document.domain,
50                     expires : ('localStorage' === type) ? { expires: 365 } : undefined
51                 }
52             },
53             
54             getItem : function( key ) {
55                 var response;
56                 if(support[type]) {
57                     response = window[type].getItem(key);
58                 }
59                 else {
60                     response = $.cookie(this.settings.cookiePrefix + key);
61                 }
62                 
63                 return response;
64             },
65             
66             setItem : function( key, value ) {
67                 if(support[type]) {
68                     return window[type].setItem(key, value);
69                 }
70                 else {
71                     return $.cookie(this.settings.cookiePrefix + key, value, this.settings.cookieOptions);
72                 }
73             },
74
75             removeItem : function( key ) {
76                 if(support[type]) {
77                     return window[type].removeItem(key);
78                 }
79                 else {
80                     var options = $.extend(this.settings.cookieOptions, {
81                         expires: -1
82                     });
83                     return $.cookie(this.settings.cookiePrefix + key, null, options);
84                 }
85             },
86
87             clear : function() {
88                 if(support[type]) {
89                     return window[type].clear();
90                 }
91                 else {
92                     var reg = new RegExp('^' + this.settings.cookiePrefix, ''),
93                         options = $.extend(this.settings.cookieOptions, {
94                             expires: -1
95                         });
96
97                     if(document.cookie && document.cookie !== ''){
98                         $.each(document.cookie.split(';'), function( i, cookie ){
99                             if(reg.test(cookie = $.trim(cookie))) {
100                                  $.cookie( cookie.substr(0,cookie.indexOf('=')), null, options);
101                             }
102                         });
103                     }
104                 }
105             }
106         };
107     });
108 })(window, jQuery);