-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.storage.js
104 lines (88 loc) · 3.34 KB
/
jquery.storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*!
* jQuery Storage plugin v1.0.2
* v1.0.1 - Fix bug when jQuery try read value to radio or checkbox;
* v1.0.2 - Change for Array the return value
* v1.0.3 - Change the way to call the function
*
* Copyright 2012, Thyago Quintas ([email protected])
* https://github.com/thyagoquintas/jquery-storage/
*
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function ($, document, undefined) {
var methods = {
init : function(content) {
if(content == undefined) $.error('Content is empty on jQuery.storage');
if(content['key'] == undefined) $.error('You need the key value');
var contents = $.extend({
'location' : $.storage('support') ? 'localStorage':'cookies'
}, content);
if(contents['location'] == 'localStorage'){
return localStorage[contents['key']];
}else if(contents['location'] == 'cookie') {
return $.cookie(contents['key']);
}else {
$.error('Location not accepted.');
}
},//Close return
// Remove the values in stored
remove : function(content) {
if(content == undefined) $.error('Content is empty on jQuery.storage');
if(content['key'] == undefined) $.error('You need the key value');
var contents = $.extend({
'location' : $.storage('support') ? 'localStorage':'cookies'
}, content);
if(contents['location'] == 'localStorage'){
localStorage.removeItem(contents['key']);
}else if(contents['location'] == 'cookie') {
$.removeCookie((contents['key']));
}else {
$.error('Location not accepted.');
}
},//Close remove
// Update the values in stored
update : function(content) {
if(content == undefined) $.error('Content is empty on jQuery.storage');
if(content['key'] == undefined) $.error('You need the key value');
var contents = $.extend( {
'location' : $.storage('support') ? 'localStorage':'cookies'
}, content);
if(contents['location'] == 'localStorage'){
localStorage[contents['key']] = JSON.stringify(contents['value']);
}else if(contents['location'] == 'cookie') {
$.cookie(contents['key'], JSON.stringify(contents['value']));
}else {
$.error('Location not accepted.');
}
},//Close update
// Verify the compatibility with localStorage
support : function(){
return ('localStorage' in window) && window['localStorage'] !== null;
}
clear : function(content){
var contents = $.extend( {
'location' : $.storage('support') ? 'localStorage':'cookies'
}, content);
if(contents['location'] == 'localStorage'){
localStorage.clear();
}else if(contents['location'] == 'cookie') {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
$.removeCookie(cookies[i].split("=")[0].replace(" ",""));
}else {
$.error('Location not accepted.');
}
}
};
$.storage = function(method) {
if (methods[method]) {
return methods[method].apply( this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.storage');
}
};
})(jQuery, document);