Add global color list
[Hacks.git] / hacks / Hack.js
index 28cfad7..13e1912 100644 (file)
@@ -3,6 +3,17 @@
 enyo.kind({
        name: "Hack",
        kind: "VFlexBox",
+       preferences: [],
+       create: function() {
+               this.inherited(arguments);
+               try {
+                       var str = localStorage['hacks/' + this.kind];
+                       if (str)
+                               this.setPreferences(JSON.parse(str), true, true);
+               } catch(e) {
+                       enyo.log("Could not load preferences: " + e.toString());
+               }
+       },
        stop: function() {
        },
        start: function() {
@@ -10,5 +21,38 @@ enyo.kind({
        resize: function() {
        },
        hidden: function() {
-       }
+       },
+       getPreferencesMetadata: function() {
+               return this.preferences;
+       },
+       getPreferences: function() {
+               var o = {};
+               for (var i = 0; i < this.preferences.length; i++) {
+                       var p = this.preferences[i];
+                       o[p.name] = this[p.name];
+               }
+               return o;
+       },
+       setPreferences: function(prefs, nosave, nonotify) {
+               for (var i in prefs) {
+                       if (this[i] == undefined)
+                               throw new Error('Cannot set nonexistent preference "' + i + '"');
+                       this[i] = prefs[i];
+               }
+               if (!nosave)
+                       this.savePreferences();
+               if (!nonotify)
+                       this.preferencesChanged();
+       },
+       savePreferences: function() {
+               try {
+                       localStorage['hacks/' + this.kind] = JSON.stringify(this.getPreferences());
+               } catch(e) {
+                       enyo.log("Could not save prefs: " + e.toString());
+               }
+       },
+       resetPreferences: function() {
+               delete localStorage['hacks/' + this.kind];
+       },
+       preferencesChanged: function() { }
 });