aboutsummaryrefslogtreecommitdiffstats
path: root/admin/themes/default/js/LocalStorageCache.js
blob: 74725434929d8269ecf7d06b2a3653d758b37290 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * 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;
  this.loader = options.loader;
  
  this.storage = window.localStorage;
  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;
  
  if (this.ready && this.storage[this.key] != undefined) {
    var cache = JSON.parse(this.storage[this.key]);
    
    if (now - cache.timestamp <= this.lifetime && cache.key == this.serverKey) {
      callback(cache.data);
      return;
    }
  }
  
  this.loader(function(data) {
    that.set.call(that, data);
    callback(data);
  });
};

/*
 * Manually set the cache content
 * @param data {mixed}
 */
LocalStorageCache.prototype.set = function(data) {
  if (this.ready) {
    this.storage[this.key] = JSON.stringify({
      timestamp: new Date().getTime(),
      key: this.serverKey,
      data: 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);
            }
          });
        }
      }
    });
  });
};