-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferences.js
More file actions
126 lines (124 loc) · 3.49 KB
/
Preferences.js
File metadata and controls
126 lines (124 loc) · 3.49 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
/**
* Preferences v0.5 by Fourth Draft Software
*
* A class that uses cookies manage versioned Preferences.
* See README for more information.
*/
var Preferences = Class.create({
/**
* A hash of defaults. Uses Prototype Hash.
*/
defaults: $H(),
/**
* The actual preferences hash. Uses Prototype Hash.
*/
preferences: $H(),
/**
* @param {string} cookieName
* @param {hash} defaults
*
* The initialize function, which is called when "new Preferences()" is
* called.
*
* Loads the defaults and creates the cookie, then calls load(). Defaults
* needs to be a built-in hash in the form of
* {
* verNum: {
* pref1: default,
* pref2: default
* },
* nextVerNum: {
* pref3: default,
* pref4: default
* }
* }
*/
initialize: function(cookieName, defaults) {
if (!cookieName) {
throw "InvalidCookieName";
}
if (defaults) {
this.defaults = $H(defaults);
}
this.cookie = new Mojo.Model.Cookie(cookieName);
this.load(true);
},
/**
* Sets the version if it doesn't exist, then loops through the defaults
* and sets preferences that didn't previously exist to their default,
* increasing the version number as it goes.
*/
handleVersionDifferences: function() {
if (!this.preferences.get('version')) {
this.preferences.set('version', 0);
}
var myver = parseInt(this.preferences.get('version'));
this.defaults.each(function(p) {
var ver = parseInt(p.key);
if (myver < ver) {
this.preferences.set('version', ver);
$H(p.value).each(function(sp) {
// I'm not sure why this if check is here...
if (sp.key != 'value') {
this.preferences.set(sp.key, sp.value);
}
}.bind(this));
}
}.bind(this));
try {
this.save();
} catch (error) {
throw error;
}
},
/**
* @param {string} key
* @return preferences[key]
*/
get: function(key) {
return this.preferences.get(key);
},
/**
* @param {string} key
* @param {mixed} value
* sets key to value. value can be anything.
*/
set: function(key, value) {
this.preferences.set(key, value);
},
/**
* Save the data as JSON to the cookie.
*/
save: function() {
this.cookie.put(Object.toJSON(this.preferences));
},
/**
* Unset the cookie.
*/
clear: function() {
this.cookie.remove();
},
/**
* Return the preferences as a JSON string.
*/
toJSON: function() {
return this.preferences.toJSON();
},
/**
* @param {boolean} versionUpdate
* Load the cookie; if the cookie doesn't exist, load the defaults
* through handleVersionDifferences; if it does, set the preferences,
* but if versionUpdate is true still parse through handleVersionDifferences.
*/
load: function(versionUpdate) {
var cv = this.cookie.get();
if (typeof cv == 'undefined' && this.defaults != null) {
this.handleVersionDifferences();
} else {
this.preferences = $H(cv.evalJSON());
if (versionUpdate === true) {
this.handleVersionDifferences();
}
}
}
});