Add dialog for hacks without prefs, add prefs save/load
[Hacks.git] / HackPreferences.js
1 enyo.kind({
2         name: "HackPreferences",
3         kind: "VFlexBox",
4         events: {
5                 "onClose": ""
6         },
7         components: [
8                 {kind: "Header", content: "Preferences"},
9                 {kind: "Scroller", flex: 1, horizontal: false, components: [
10                         {kind: "VFlexBox", align: "center", components: [
11                                 {kind: "RowGroup", name: "prefsContainer", style: "width: 750px"}
12                         ]}
13                 ]},
14                 {kind: "Toolbar", className: "enyo-toolbar-light", components: [
15                         {kind: "Button", content: "Done", onclick: "close", className: "enyo-button-affirmative prefs-done-button"}
16                 ]}
17         ],
18         load: function(name, prefs, values) {
19                 this.$.prefsContainer.setCaption(name);
20                 this.prefs = prefs;
21
22                 for (var i = 0; i < this.prefs.length; i++) {
23                         var p = this.prefs[i];
24                         var c;
25
26                         switch(p.kind) {
27                         case 'Slider':
28                                 c = this.$.prefsContainer.createComponent({kind: "HFlexBox", align: "center", components: [{content: p.label, flex: 1}]});
29                                 var slider = c.createComponent(p, {owner: this.$.prefsContainer});
30                                 slider.setStyle('width: 400px');
31                                 slider.setPosition(values[p.name]);
32                                 break;
33                         default:
34                                 c = this.$.prefsContainer.createComponent(p, {owner: this.$.prefsContainer});
35                                 c.setValue(values[p.name]);
36                         }
37                 }
38                 this.$.prefsContainer.render();
39         },
40         close: function() {
41                 var o = {};
42                 for (var i = 0; i < this.prefs.length; i++) {
43                         var p = this.prefs[i];
44                         var v;
45
46                         switch (p.kind) {
47                         case 'Slider':
48                                 v = this.$.prefsContainer.$[p.name].getPosition();
49                                 break;
50                         default:
51                                 v = this.$.prefsContainer.$[p.name].getValue();
52                         }
53                         o[p.name] = v;
54                 }
55                 this.doClose(o);
56
57                 var controls = this.$.prefsContainer.getControls();
58                 for (var i = 0; i < controls.length; i++) {
59                         // We destroy the parent because we want to destroy the
60                         // row client, as well.  This is probably a bug in
61                         // Enyo.
62                         controls[i].parent.destroy();
63                         controls[i].destroy();
64                 }
65         }
66 });