aboutsummaryrefslogtreecommitdiffstats
path: root/admin/themes/default/js
diff options
context:
space:
mode:
authormistic100 <mistic@piwigo.org>2014-05-26 22:03:57 +0000
committermistic100 <mistic@piwigo.org>2014-05-26 22:03:57 +0000
commita5b2cf82007ac1b2461ba86b79c48e43c2640053 (patch)
tree9cc97025f60101372081320a1fe9f56a8b69451e /admin/themes/default/js
parentb97c45b941170247107445ce4a8fffbae8267c1e (diff)
feature 3077 : factorize code for categories cache (TODO for other collections) + fix incorrect categories list for dissociation
git-svn-id: http://piwigo.org/svn/trunk@28542 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'admin/themes/default/js')
-rw-r--r--admin/themes/default/js/LocalStorageCache.js121
1 files changed, 121 insertions, 0 deletions
diff --git a/admin/themes/default/js/LocalStorageCache.js b/admin/themes/default/js/LocalStorageCache.js
index b720851be..747254349 100644
--- a/admin/themes/default/js/LocalStorageCache.js
+++ b/admin/themes/default/js/LocalStorageCache.js
@@ -1,4 +1,22 @@
+/**
+ * Base LocalStorage cache
+ *
+ * @param options {object}
+ * - key (required) identifier of the collection
+ * - serverId (recommended) identifier of the Piwigo instance
+ * - serverKey (required) state of collection server-side
+ * - lifetime (optional) cache lifetime in seconds
+ * - loader (required) function called to fetch data, takes a callback as first argument
+ * which must be called with the loaded date
+ */
var LocalStorageCache = function(options) {
+ this._init(options);
+};
+
+/*
+ * Constructor (deported for easy inheritance)
+ */
+LocalStorageCache.prototype._init = function(options) {
this.key = options.key + '_' + options.serverId;
this.serverKey = options.serverKey;
this.lifetime = options.lifetime ? options.lifetime*1000 : 3600*1000;
@@ -8,6 +26,10 @@ var LocalStorageCache = function(options) {
this.ready = !!this.storage;
};
+/*
+ * Get the cache content
+ * @param callback {function} called with the data as first parameter
+ */
LocalStorageCache.prototype.get = function(callback) {
var now = new Date().getTime(),
that = this;
@@ -27,6 +49,10 @@ LocalStorageCache.prototype.get = function(callback) {
});
};
+/*
+ * Manually set the cache content
+ * @param data {mixed}
+ */
LocalStorageCache.prototype.set = function(data) {
if (this.ready) {
this.storage[this.key] = JSON.stringify({
@@ -37,8 +63,103 @@ LocalStorageCache.prototype.set = function(data) {
}
};
+/*
+ * Manually clear the cache
+ */
LocalStorageCache.prototype.clear = function() {
if (this.ready) {
this.storage.removeItem(this.key);
}
+};
+
+
+/**
+ * Special LocalStorage for admin categories list
+ *
+ * @param options {object}
+ * - serverId (recommended) identifier of the Piwigo instance
+ * - serverKey (required) state of collection server-side
+ * - rootUrl (required) used for WS call
+ */
+var CategoriesCache = function(options) {
+ options.key = 'categoriesAdminList';
+
+ options.loader = function(callback) {
+ jQuery.getJSON(options.rootUrl + 'ws.php?format=json&method=pwg.categories.getAdminList', function(data) {
+ callback(data.result.categories);
+ });
+ };
+
+ this._init(options);
+};
+
+CategoriesCache.prototype = new LocalStorageCache({});
+
+/*
+ * Init Selectize with cache content
+ * @param $target {jQuery}
+ * @param options {object}
+ * - default (optional) default value which will be forced if the select is emptyed
+ * - filter (optional) function called for each select before applying the data
+ * takes two parameters: cache data, options
+ * must return new data
+ */
+CategoriesCache.prototype.selectize = function($target, options) {
+ options = options || {};
+
+ $target.selectize({
+ valueField: 'id',
+ labelField: 'fullname',
+ sortField: 'global_rank',
+ searchField: ['fullname'],
+ plugins: ['remove_button']
+ });
+
+ this.get(function(categories) {
+ $target.each(function() {
+ var data;
+ if (options.filter != undefined) {
+ data = options.filter.call(this, categories, options);
+ }
+ else {
+ data = categories;
+ }
+
+ this.selectize.load(function(callback) {
+ callback(data);
+ });
+
+ if (jQuery(this).data('value')) {
+ jQuery.each(jQuery(this).data('value'), jQuery.proxy(function(i, id) {
+ this.selectize.addItem(id);
+ }, this));
+ }
+
+ if (options.default != undefined) {
+ if (this.selectize.getValue() == '') {
+ this.selectize.addItem(options.default);
+ }
+
+ // if multiple: prevent item deletion
+ if (this.multiple) {
+ this.selectize.getItem(options.default).find('.remove').hide();
+
+ this.selectize.on('item_remove', function(id) {
+ if (id == options.default) {
+ this.addItem(id);
+ this.getItem(id).find('.remove').hide();
+ }
+ });
+ }
+ // if single: restore default on blur
+ else {
+ this.selectize.on('dropdown_close', function() {
+ if (this.getValue() == '') {
+ this.addItem(options.default);
+ }
+ });
+ }
+ }
+ });
+ });
}; \ No newline at end of file