1 | /** |
---|
2 | * Base LocalStorage cache |
---|
3 | * |
---|
4 | * @param options {object} |
---|
5 | * - key (required) identifier of the collection |
---|
6 | * - serverId (recommended) identifier of the Piwigo instance |
---|
7 | * - serverKey (required) state of collection server-side |
---|
8 | * - lifetime (optional) cache lifetime in seconds |
---|
9 | * - loader (required) function called to fetch data, takes a callback as first argument |
---|
10 | * which must be called with the loaded date |
---|
11 | */ |
---|
12 | var LocalStorageCache = function(options) { |
---|
13 | this._init(options); |
---|
14 | }; |
---|
15 | |
---|
16 | /* |
---|
17 | * Constructor (deported for easy inheritance) |
---|
18 | */ |
---|
19 | LocalStorageCache.prototype._init = function(options) { |
---|
20 | this.key = options.key + '_' + options.serverId; |
---|
21 | this.serverKey = options.serverKey; |
---|
22 | this.lifetime = options.lifetime ? options.lifetime*1000 : 3600*1000; |
---|
23 | this.loader = options.loader; |
---|
24 | |
---|
25 | this.storage = window.localStorage; |
---|
26 | this.ready = !!this.storage; |
---|
27 | }; |
---|
28 | |
---|
29 | /* |
---|
30 | * Get the cache content |
---|
31 | * @param callback {function} called with the data as first parameter |
---|
32 | */ |
---|
33 | LocalStorageCache.prototype.get = function(callback) { |
---|
34 | var now = new Date().getTime(), |
---|
35 | that = this; |
---|
36 | |
---|
37 | if (this.ready && this.storage[this.key] != undefined) { |
---|
38 | var cache = JSON.parse(this.storage[this.key]); |
---|
39 | |
---|
40 | if (now - cache.timestamp <= this.lifetime && cache.key == this.serverKey) { |
---|
41 | callback(cache.data); |
---|
42 | return; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | this.loader(function(data) { |
---|
47 | that.set.call(that, data); |
---|
48 | callback(data); |
---|
49 | }); |
---|
50 | }; |
---|
51 | |
---|
52 | /* |
---|
53 | * Manually set the cache content |
---|
54 | * @param data {mixed} |
---|
55 | */ |
---|
56 | LocalStorageCache.prototype.set = function(data) { |
---|
57 | if (this.ready) { |
---|
58 | this.storage[this.key] = JSON.stringify({ |
---|
59 | timestamp: new Date().getTime(), |
---|
60 | key: this.serverKey, |
---|
61 | data: data |
---|
62 | }); |
---|
63 | } |
---|
64 | }; |
---|
65 | |
---|
66 | /* |
---|
67 | * Manually clear the cache |
---|
68 | */ |
---|
69 | LocalStorageCache.prototype.clear = function() { |
---|
70 | if (this.ready) { |
---|
71 | this.storage.removeItem(this.key); |
---|
72 | } |
---|
73 | }; |
---|
74 | |
---|
75 | |
---|
76 | /** |
---|
77 | * Special LocalStorage for admin categories list |
---|
78 | * |
---|
79 | * @param options {object} |
---|
80 | * - serverId (recommended) identifier of the Piwigo instance |
---|
81 | * - serverKey (required) state of collection server-side |
---|
82 | * - rootUrl (required) used for WS call |
---|
83 | */ |
---|
84 | var CategoriesCache = function(options) { |
---|
85 | options.key = 'categoriesAdminList'; |
---|
86 | |
---|
87 | options.loader = function(callback) { |
---|
88 | jQuery.getJSON(options.rootUrl + 'ws.php?format=json&method=pwg.categories.getAdminList', function(data) { |
---|
89 | callback(data.result.categories); |
---|
90 | }); |
---|
91 | }; |
---|
92 | |
---|
93 | this._init(options); |
---|
94 | }; |
---|
95 | |
---|
96 | CategoriesCache.prototype = new LocalStorageCache({}); |
---|
97 | |
---|
98 | /* |
---|
99 | * Init Selectize with cache content |
---|
100 | * @param $target {jQuery} |
---|
101 | * @param options {object} |
---|
102 | * - default (optional) default value which will be forced if the select is emptyed |
---|
103 | * - filter (optional) function called for each select before applying the data |
---|
104 | * takes two parameters: cache data, options |
---|
105 | * must return new data |
---|
106 | */ |
---|
107 | CategoriesCache.prototype.selectize = function($target, options) { |
---|
108 | options = options || {}; |
---|
109 | |
---|
110 | $target.selectize({ |
---|
111 | valueField: 'id', |
---|
112 | labelField: 'fullname', |
---|
113 | sortField: 'global_rank', |
---|
114 | searchField: ['fullname'], |
---|
115 | plugins: ['remove_button'] |
---|
116 | }); |
---|
117 | |
---|
118 | this.get(function(categories) { |
---|
119 | $target.each(function() { |
---|
120 | var data; |
---|
121 | if (options.filter != undefined) { |
---|
122 | data = options.filter.call(this, categories, options); |
---|
123 | } |
---|
124 | else { |
---|
125 | data = categories; |
---|
126 | } |
---|
127 | |
---|
128 | this.selectize.load(function(callback) { |
---|
129 | callback(data); |
---|
130 | }); |
---|
131 | |
---|
132 | if (jQuery(this).data('value')) { |
---|
133 | jQuery.each(jQuery(this).data('value'), jQuery.proxy(function(i, id) { |
---|
134 | this.selectize.addItem(id); |
---|
135 | }, this)); |
---|
136 | } |
---|
137 | |
---|
138 | if (options.default != undefined) { |
---|
139 | if (this.selectize.getValue() == '') { |
---|
140 | this.selectize.addItem(options.default); |
---|
141 | } |
---|
142 | |
---|
143 | // if multiple: prevent item deletion |
---|
144 | if (this.multiple) { |
---|
145 | this.selectize.getItem(options.default).find('.remove').hide(); |
---|
146 | |
---|
147 | this.selectize.on('item_remove', function(id) { |
---|
148 | if (id == options.default) { |
---|
149 | this.addItem(id); |
---|
150 | this.getItem(id).find('.remove').hide(); |
---|
151 | } |
---|
152 | }); |
---|
153 | } |
---|
154 | // if single: restore default on blur |
---|
155 | else { |
---|
156 | this.selectize.on('dropdown_close', function() { |
---|
157 | if (this.getValue() == '') { |
---|
158 | this.addItem(options.default); |
---|
159 | } |
---|
160 | }); |
---|
161 | } |
---|
162 | } |
---|
163 | }); |
---|
164 | }); |
---|
165 | }; |
---|