f70bc97f4f7abfbb19a3b99a0b137897ea2cbec1
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Storage.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Adds a public API for the browsers' localStorage called
16 * TYPO3.Storage.Client and the Backend Users "uc",
17 * available via TYPO3.Storage.Persistent
18 */
19 define('TYPO3/CMS/Backend/Storage', ['jquery'], function ($) {
20 var Storage = {
21 Client: {},
22 Persistent: {
23 _data: false
24 }
25 };
26
27 /**
28 * simple localStorage wrapper, common functions get/set/clear
29 */
30 Storage.Client.get = function(key) {
31 return localStorage.getItem('t3-' + key);
32 };
33 Storage.Client.set = function(key, value) {
34 return localStorage.setItem('t3-' + key, value);
35 };
36 Storage.Client.clear = function() {
37 localStorage.clear();
38 };
39 /**
40 * checks if a key was set before, useful to not do all the undefined checks all the time
41 */
42 Storage.Client.isset = function(key) {
43 var value = this.get(key);
44 return (typeof value !== 'undefined' && typeof value !== 'null' && value != 'undefined');
45 };
46
47 /**
48 * persistent storage, stores everything on the server
49 * via AJAX, does a greedy load on read
50 * common functions get/set/clear
51 */
52 Storage.Persistent.get = function(key) {
53 if (this._data === false) {
54 var value;
55 this._loadFromServer().done(function() {
56 value = Storage.Persistent._data[key];
57 });
58 return value;
59 } else {
60 return this._data[key];
61 }
62 };
63 Storage.Persistent.set = function(key, value) {
64 if (this._data === false) {
65 Storage.Persistent._storeOnServer(key, value);
66 } else {
67 this._data[key] = value;
68 this._storeOnServer(key, value);
69 }
70 };
71 Storage.Persistent.clear = function() {
72 $.ajax(TYPO3.settings.ajaxUrls['UserSettings::process'], {data: {'action': 'clear'}});
73 this._data = false;
74 };
75 /**
76 * checks if a key was set before, useful to not do all the undefined checks all the time
77 */
78 Storage.Persistent.isset = function(key) {
79 var value = this.get(key);
80 return (typeof value !== 'undefined' && typeof value !== 'null' && value != 'undefined');
81 };
82
83 /**
84 * loads the data from outside, only used for the initial call from BackendController
85 * @param data
86 */
87 Storage.Persistent.load = function(data) {
88 this._data = data;
89 };
90
91 /**
92 * loads all data from the server
93 * @returns jQuery Deferred
94 * @private
95 */
96 Storage.Persistent._loadFromServer = function() {
97 return $.ajax(TYPO3.settings.ajaxUrls['UserSettings::process'], {data: {'action': 'getAll'}, async: false}).done(function(data) {
98 Storage.Persistent._data = data;
99 });
100 };
101
102 /**
103 * stores data on the server, and gets the updated data on return
104 * to always be up-to-date inside the browser
105 * @returns jQuery Deferred
106 * @private
107 */
108 Storage.Persistent._storeOnServer = function(key, value) {
109 return $.ajax(TYPO3.settings.ajaxUrls['UserSettings::process'], {data: {'action': 'set', key: key, value: value}}).done(function(data) {
110 Storage.Persistent._data = data;
111 });
112 }
113
114 /**
115 * return the Storage object, and attach it to the global TYPO3 object on the global frame
116 */
117 return function() {
118 top.TYPO3.Storage = Storage;
119 return Storage;
120 }();
121 });