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 pathPreferencesManager.js
More file actions
211 lines (180 loc) · 6.95 KB
/
PreferencesManager.js
File metadata and controls
211 lines (180 loc) · 6.95 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
/*
* 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, $, localStorage */
/**
* PreferencesManager
*
*/
define(function (require, exports, module) {
"use strict";
var PreferenceStorage = require("preferences/PreferenceStorage").PreferenceStorage,
FileUtils = require("file/FileUtils"),
ExtensionLoader = require("utils/ExtensionLoader"),
CollectionUtils = require("utils/CollectionUtils");
/**
* The local storage ID
* @const
* @type {string}
*/
var PREFERENCES_CLIENT_ID = "com.adobe.brackets.preferences";
/**
* The prefix used in the generated client ID
* @const
* @type {string}
*/
var CLIENT_ID_PREFIX = "com.adobe.brackets.";
// Private Properties
var preferencesKey,
prefStorage,
persistentStorage,
doLoadPreferences = false;
/**
* This method returns a standardized ClientID for a given requireJS module object
* @param {!{id: string, uri: string}} module - A requireJS module object
* @return {string} The ClientID
*/
function getClientID(module) {
var pathExp, pathUrl, clientID,
dirPath = FileUtils.getNativeBracketsDirectoryPath(),
paths = [
dirPath + "/extensions/default/",
dirPath + "/extensions/dev/",
ExtensionLoader.getUserExtensionPath() + "/"
];
paths.some(function (path) {
pathExp = new RegExp("^" + path);
if (module.uri.match(pathExp)) {
pathUrl = path;
return true;
}
});
if (pathUrl) {
clientID = CLIENT_ID_PREFIX + module.uri.replace(pathUrl, "");
} else {
clientID = CLIENT_ID_PREFIX + module.id;
}
return clientID;
}
/**
* Retreive the preferences data for the given clientID.
* @param {string|{id: string, uri: string}} clientID - A unique identifier or a requireJS module object
* @param {string} defaults - Default preferences stored as JSON
* @return {PreferenceStorage}
*/
function getPreferenceStorage(clientID, defaults) {
if (!clientID || (typeof clientID === "object" && (!clientID.id || !clientID.uri))) {
console.error("Invalid clientID");
return;
}
if (typeof (clientID) === "object") {
clientID = getClientID(clientID);
}
var prefs = prefStorage[clientID];
if (prefs === undefined) {
// create a new empty preferences object
prefs = (defaults && JSON.stringify(defaults)) ? defaults : {};
prefStorage[clientID] = prefs;
} else if (defaults) {
// add new defaults
CollectionUtils.forEach(defaults, function (value, key) {
if (prefs[key] === undefined) {
prefs[key] = value;
}
});
}
return new PreferenceStorage(clientID, prefs);
}
/**
* Save all preference clients.
*/
function savePreferences() {
// save all preferences
persistentStorage.setItem(preferencesKey, JSON.stringify(prefStorage));
}
/**
* @private
* Reset preferences and callbacks
*/
function _reset() {
prefStorage = {};
// Note that storage.clear() is not used. Production and unit test code
// both rely on the same backing storage but unique item keys.
persistentStorage.setItem(preferencesKey, JSON.stringify(prefStorage));
}
/**
* @private
* Initialize persistent storage implementation
*/
function _initStorage(storage) {
persistentStorage = storage;
if (doLoadPreferences) {
prefStorage = JSON.parse(persistentStorage.getItem(preferencesKey));
}
// initialize empty preferences if none were found in storage
if (!prefStorage) {
_reset();
}
}
/**
* This method handles the copy of all old prefs to the new prefs
* TODO: remove All calls to this function and the function itself
*
* @param {!PreferenceStorage} newPrefs The new PreferenceStorage
* @param {!string} oldID The id of the old PreferenceStorage
* @param {?obj} defaults The defaults to add
*/
function handleClientIdChange(newPrefs, oldID, defaults) {
var oldPrefs = getPreferenceStorage(oldID);
defaults = defaults || {};
if (!newPrefs.getValue("newClientID")) {
var data = oldPrefs.getAllValues();
if ($.isEmptyObject(data)) {
data = defaults;
}
newPrefs.setAllValues(data, false);
newPrefs.setValue("newClientID", true);
}
delete prefStorage[oldID];
}
// Check localStorage for a preferencesKey. Production and unit test keys
// are used to keep preferences separate within the same storage implementation.
preferencesKey = localStorage.getItem("preferencesKey");
if (!preferencesKey) {
// use default key if none is found
preferencesKey = PREFERENCES_CLIENT_ID;
doLoadPreferences = true;
} else {
// using a non-default key, check for additional settings
doLoadPreferences = !!(localStorage.getItem("doLoadPreferences"));
}
// Use localStorage by default
_initStorage(localStorage);
// Public API
exports.getPreferenceStorage = getPreferenceStorage;
exports.savePreferences = savePreferences;
exports.handleClientIdChange = handleClientIdChange;
exports.getClientID = getClientID;
// Unit test use only
exports._reset = _reset;
});