This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathPreferenceStorage.js
More file actions
273 lines (243 loc) · 10.3 KB
/
PreferenceStorage.js
File metadata and controls
273 lines (243 loc) · 10.3 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, $ */
/**
* PreferenceStorage defines an interface for persisting preference data as
* name/value pairs for a module or plugin.
*/
define(function (require, exports, module) {
"use strict";
var _ = require("thirdparty/lodash");
var PreferencesManager = require("preferences/PreferencesManager"),
DeprecationWarning = require("utils/DeprecationWarning");
/**
* @private
* Validate JSON keys and values.
*/
function _validateJSONPair(key, value) {
if (typeof key === "string") {
// validate temporary JSON
var temp = {},
error = null;
temp[key] = value;
try {
temp = JSON.parse(JSON.stringify(temp));
} catch (err) {
error = err;
}
// set value to JSON storage if no errors occurred
if (!error && (temp[key] !== undefined)) {
return true;
} else {
console.error("Value '" + value + "' for key '" + key + "' must be a valid JSON value");
return false;
}
} else {
console.error("Preference key '" + key + "' must be a string");
return false;
}
}
/**
* @private
* Save to persistent storage.
*/
function _commit() {
PreferencesManager.savePreferences();
}
/**
* Creates a new PreferenceStorage object.
* @param {!string} clientID Unique identifier for PreferencesManager to
* associate this PreferenceStorage data with.
* @param {!object} json JSON object to persist preference data.
*/
function PreferenceStorage(clientID, json) {
this._clientID = clientID;
this._json = json;
}
/**
* Unique clientID for this PreferenceStorage object.
* @return {!string} clientID
*/
PreferenceStorage.prototype.getClientID = function () {
return this._clientID;
};
/**
* Removes a preference from this PreferenceStorage object.
* @param {!string} key A unique identifier
*/
PreferenceStorage.prototype.remove = function (key) {
DeprecationWarning.deprecationWarning("remove is called to remove a preference '" + key + ",' use PreferencesManager.set (with value of undefined) instead.");
// remove value from JSON storage
delete this._json[key];
_commit();
};
/**
* Assigns a value for a key. Overwrites existing value if present.
* @param {!string} key A unique identifier
* @param {object} value A valid JSON value
*/
PreferenceStorage.prototype.setValue = function (key, value) {
DeprecationWarning.deprecationWarning("setValue is called to set preference '" + key + ",' use PreferencesManager.set instead.");
if (_validateJSONPair(key, value)) {
this._json[key] = value;
_commit();
}
};
/**
* Retreive the value associated with the specified key.
* @param {!string} key Key name to lookup.
* @return {object} Returns the value for the key or undefined.
*/
PreferenceStorage.prototype.getValue = function (key) {
DeprecationWarning.deprecationWarning("getValue is called to get preference '" + key + ",' use PreferencesManager.get instead.");
return this._json[key];
};
/**
* Return all name-value pairs as a single JSON object.
* @return {!object} JSON object containing name/value pairs for all keys
* in this PreferenceStorage object.
*/
PreferenceStorage.prototype.getAllValues = function () {
return JSON.parse(JSON.stringify(this._json));
};
/**
* Writes name-value pairs from a JSON object as preference properties.
* Invalid JSON values report an error and all changes are discarded.
*
* @param {!object} obj A JSON object with zero or more preference properties to write.
* @param {boolean} append Defaults to false. When true, properties in the JSON object
* overwrite and/or append to the existing set of preference properties. When false,
* all existing preferences are deleted before writing new properties from the JSON object.
*/
PreferenceStorage.prototype.setAllValues = function (obj, append) {
DeprecationWarning.deprecationWarning("setAllValues is called to set preferences '" + Object.keys(obj) + ",' use PreferencesManager.set (probably with doNotSave flag) instead.");
var self = this,
error = null;
// validate all name/value pairs before committing
_.some(obj, function (value, key) {
try {
_validateJSONPair(key, value);
} catch (err) {
// fail fast
error = err;
return true;
}
});
// skip changes if any error is detected
if (error) {
console.error(error);
return;
}
// delete all exiting properties if not appending
if (!append) {
_.forEach(this._json, function (value, key) {
delete self._json[key];
});
}
// copy properties from incoming JSON object
_.forEach(obj, function (value, key) {
self._json[key] = value;
});
_commit();
};
/**
* Converts preferences to the new-style file-based preferences according to the
* rules. (See PreferencesManager.ConvertPreferences for information about the rules).
*
* @param {Object} rules Conversion rules.
* @param {Array.<string>} convertedKeys List of keys that were previously converted
* (will not be reconverted)
* @param {boolean=} isViewState If it is undefined or false, then the preferences
* listed in 'rules' are those normal user-editable preferences. Otherwise,
* they are view state settings.
* @param {function(string)=} prefCheckCallback Optional callback function that
* examines each preference key for migration.
* @return {Promise} promise that is resolved once the conversion is done. Callbacks are given a
* `complete` flag that denotes whether everything from this object
* was converted (making it safe to delete entirely).
*/
PreferenceStorage.prototype.convert = function (rules, convertedKeys, isViewState, prefCheckCallback) {
var prefs = this._json,
self = this,
complete = true,
manager = isViewState ? PreferencesManager.stateManager : PreferencesManager,
deferred = new $.Deferred();
if (!convertedKeys) {
convertedKeys = [];
}
Object.keys(prefs).forEach(function (key) {
if (convertedKeys.indexOf(key) > -1) {
return;
}
var rule = rules[key];
if (!rule && prefCheckCallback) {
rule = prefCheckCallback(key);
} else if (prefCheckCallback) {
// Check whether we have a new preference key-value pair
// for an old preference.
var newRule = prefCheckCallback(key, prefs[key]);
if (newRule) {
rule = _.cloneDeep(newRule);
}
}
if (!rule) {
console.warn("Preferences conversion for ", self._clientID, " has no rule for", key);
complete = false;
} else if (_.isString(rule)) {
var parts = rule.split(" ");
if (parts[0] === "user") {
var newKey = parts.length > 1 ? parts[1] : key;
var options = null;
if (parts.length > 2 && parts[2].indexOf("/") !== -1) {
var projectPath = rule.substr(rule.indexOf(parts[2]));
options = { location: { scope: "user",
layer: "project",
layerID: projectPath } };
}
manager.set(newKey, prefs[key], options);
convertedKeys.push(key);
}
} else if (_.isObject(rule)) {
Object.keys(rule).forEach(function (ruleKey) {
manager.set(ruleKey, rule[ruleKey]);
});
convertedKeys.push(key);
} else {
complete = false;
}
});
if (convertedKeys.length > 0) {
manager.save().done(function () {
_commit();
deferred.resolve(complete, convertedKeys);
}).fail(function (error) {
deferred.reject(error);
});
} else {
deferred.resolve(complete, convertedKeys);
}
return deferred.promise();
};
exports.PreferenceStorage = PreferenceStorage;
});