@@ -33,28 +33,91 @@ define(function (require, exports, module) {
33
33
"use strict" ;
34
34
35
35
var PreferenceStorage = require ( "preferences/PreferenceStorage" ) . PreferenceStorage ,
36
- CollectionUtils = require ( "utils/CollectionUtils" ) ;
36
+ FileUtils = require ( "file/FileUtils" ) ,
37
+ ExtensionLoader = require ( "utils/ExtensionLoader" ) ,
38
+ CollectionUtils = require ( "utils/CollectionUtils" ) ;
37
39
40
+ /**
41
+ * The local storage ID
42
+ * @const
43
+ * @type {string }
44
+ */
38
45
var PREFERENCES_CLIENT_ID = "com.adobe.brackets.preferences" ;
39
-
46
+
47
+ /**
48
+ * The prefix used in the generated client ID
49
+ * @const
50
+ * @type {string }
51
+ */
52
+ var CLIENT_ID_PREFIX = "com.adobe.brackets." ;
53
+
54
+
40
55
// Private Properties
41
56
var preferencesKey ,
42
57
prefStorage ,
43
58
persistentStorage ,
59
+ extensionPaths ,
44
60
doLoadPreferences = false ;
45
-
61
+
62
+
63
+ /**
64
+ * @private
65
+ * Returns an array with the extension paths used in Brackets. The result is stored on a
66
+ * private variable on the first call and used to return the value on the next calls.
67
+ * @return {Array.<string> }
68
+ */
69
+ function _getExtensionPaths ( ) {
70
+ if ( ! extensionPaths ) {
71
+ var dirPath = FileUtils . getNativeBracketsDirectoryPath ( ) ;
72
+
73
+ extensionPaths = [
74
+ dirPath + "/extensions/default/" ,
75
+ dirPath + "/extensions/dev/" ,
76
+ ExtensionLoader . getUserExtensionPath ( ) + "/"
77
+ ] ;
78
+ }
79
+ return extensionPaths ;
80
+ }
81
+
82
+ /**
83
+ * This method returns a standardized ClientID for a given requireJS module object
84
+ * @param {!{id: string, uri: string} } module - A requireJS module object
85
+ * @return {string } The ClientID
86
+ */
87
+ function getClientID ( module ) {
88
+ var paths = _getExtensionPaths ( ) ;
89
+ var pathExp , pathUrl , clientID ;
90
+
91
+ paths . some ( function ( path ) {
92
+ pathExp = new RegExp ( "^" + path ) ;
93
+ if ( module . uri . match ( pathExp ) ) {
94
+ pathUrl = path ;
95
+ return true ;
96
+ }
97
+ } ) ;
98
+
99
+ if ( pathUrl ) {
100
+ clientID = CLIENT_ID_PREFIX + module . uri . replace ( pathUrl , "" ) ;
101
+ } else {
102
+ clientID = CLIENT_ID_PREFIX + module . id ;
103
+ }
104
+ return clientID ;
105
+ }
106
+
46
107
/**
47
- * Retreive preferences data for the given clientID.
48
- *
49
- * @param {string } clientID Unique identifier
50
- * @param {string } defaults Default preferences stored as JSON
108
+ * Retreive the preferences data for the given clientID.
109
+ * @param {string|{id: string, uri: string} } clientID - A unique identifier or a requireJS module object
110
+ * @param {string } defaults - Default preferences stored as JSON
51
111
* @return {PreferenceStorage }
52
112
*/
53
113
function getPreferenceStorage ( clientID , defaults ) {
54
- if ( ( clientID === undefined ) || ( clientID === null ) ) {
114
+ if ( ! clientID || ( typeof clientID === "object" && ( ! clientID . id || ! clientID . uri ) ) ) {
55
115
console . error ( "Invalid clientID" ) ;
56
116
return ;
57
117
}
118
+ if ( typeof clientID === "object" ) {
119
+ clientID = getClientID ( clientID ) ;
120
+ }
58
121
59
122
var prefs = prefStorage [ clientID ] ;
60
123
@@ -117,34 +180,21 @@ define(function (require, exports, module) {
117
180
*
118
181
* @param {!PreferenceStorage } newPrefs The new PreferenceStorage
119
182
* @param {!string } oldID The id of the old PreferenceStorage
120
- * @param {?obj } defaults The defaults to add
121
183
*/
122
- function handleClientIdChange ( newPrefs , oldID , defaults ) {
123
- var oldPrefs = getPreferenceStorage ( oldID ) ;
124
-
125
- defaults = defaults || { } ;
126
-
127
- if ( ! newPrefs . getValue ( "newClientID" ) ) {
128
- var data = oldPrefs . getAllValues ( ) ;
129
-
130
- if ( $ . isEmptyObject ( data ) ) {
131
- data = defaults ;
132
- }
184
+ function handleClientIdChange ( newPrefs , oldID ) {
185
+ if ( prefStorage [ oldID ] ) {
186
+ var oldPrefs = getPreferenceStorage ( oldID ) ;
133
187
134
- newPrefs . setAllValues ( data , false ) ;
135
- newPrefs . setValue ( "newClientID" , true ) ;
188
+ if ( ! newPrefs . getValue ( "newClientID" ) ) {
189
+ var data = oldPrefs . getAllValues ( ) ;
190
+
191
+ if ( ! $ . isEmptyObject ( data ) ) {
192
+ newPrefs . setAllValues ( data , false ) ;
193
+ }
194
+ newPrefs . setValue ( "newClientID" , true ) ;
195
+ }
196
+ delete prefStorage [ oldID ] ;
136
197
}
137
- delete prefStorage [ oldID ] ;
138
- }
139
-
140
- /**
141
- * This method returns a standardized ClientId for a given moduleId
142
- *
143
- * @param {!string } moduleId a given moduleId
144
- * @return {string } the ClientId
145
- */
146
- function getClientId ( moduleId ) {
147
- return "com.adobe.brackets." + moduleId ;
148
198
}
149
199
150
200
// Check localStorage for a preferencesKey. Production and unit test keys
@@ -167,7 +217,7 @@ define(function (require, exports, module) {
167
217
exports . getPreferenceStorage = getPreferenceStorage ;
168
218
exports . savePreferences = savePreferences ;
169
219
exports . handleClientIdChange = handleClientIdChange ;
170
- exports . getClientId = getClientId ;
220
+ exports . getClientID = getClientID ;
171
221
172
222
// Unit test use only
173
223
exports . _reset = _reset ;
0 commit comments