550dbc1a38a5cab4b6e696d2b20b1517af8f4281
[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, components: [
10                         {kind: "Control", className: "prefs-center", components: [
11                                 {kind: "RowGroup", name: "prefsContainer"},
12                                 {kind: "HFlexBox", pack: "center", components: [
13                                         {kind: "Button", className: "enyo-button-negative prefs-done-button", caption: "Reset to Defaults", onclick: "resetToDefaults"}
14                                 ]}
15                         ]}
16                 ]},
17                 {kind: "Toolbar", className: "enyo-toolbar-light", components: [
18                         {kind: "Button", content: "Done", onclick: "close", className: "enyo-button-affirmative prefs-done-button"}
19                 ]}
20         ],
21         load: function(name, prefs, values) {
22                 this.$.prefsContainer.setCaption(name);
23                 this.prefs = prefs;
24
25                 for (var i = 0; i < this.prefs.length; i++) {
26                         var p = this.prefs[i];
27                         var c;
28
29                         switch(p.kind) {
30                         case 'Slider':
31                                 c = this.$.prefsContainer.createComponent({kind: "HFlexBox", align: "center", components: [{content: p.label, flex: 1}]});
32                                 var slider = c.createComponent(p, {owner: this.$.prefsContainer});
33                                 slider.setPosition(values[p.name]);
34                                 break;
35                         default:
36                                 c = this.$.prefsContainer.createComponent(p, {owner: this.$.prefsContainer});
37                                 c.setValue(values[p.name]);
38                         }
39                 }
40                 this.$.prefsContainer.render();
41         },
42         close: function() {
43                 var o = {};
44                 for (var i = 0; i < this.prefs.length; i++) {
45                         var p = this.prefs[i];
46                         var v;
47
48                         switch (p.kind) {
49                         case 'Slider':
50                                 v = this.$.prefsContainer.$[p.name].getPosition();
51                                 break;
52                         default:
53                                 v = this.$.prefsContainer.$[p.name].getValue();
54                         }
55                         o[p.name] = v;
56                 }
57                 this.doClose(o);
58
59                 this.cleanup();
60         },
61         resetToDefaults: function() {
62                 this.doClose(null);
63                 this.cleanup();
64         },
65         cleanup: function() {
66                 var controls = this.$.prefsContainer.getControls();
67                 for (var i = 0; i < controls.length; i++) {
68                         // We destroy the parent because we want to destroy the
69                         // row client, as well.  This is probably a bug in
70                         // Enyo.
71                         controls[i].parent.destroy();
72                         controls[i].destroy();
73                 }
74         }
75 });