Clean up preferences style, add prefs reset
[Hacks.git] / Main.js
1 /* Copyright 2011 The Dominion of Awesome
2  * See COPYING for licensing information */
3 enyo.kind({
4         name: "Main",
5         kind: "Pane",
6         components: [
7                 {kind: "ApplicationEvents", onWindowActivated: "windowActivated", onWindowDeactivated: "windowDeactivated"},
8                 {name: "mainView", kind: "VFlexBox", components: [
9                         {name: "hacksCarousel", kind: "Carousel", flex: 1, onGetLeft: "getLeft", onGetRight: "getRight", onScroll: "scrolling", onScrollStart: "startScroll", onScrollStop: "stopScroll"},
10                         {name: "info", kind: "HFlexBox", className: "info", align: "center", style: "opacity: 0", onclick: "infoFade", components: [
11                                 {className: "wrench", onclick: "openPreferences"},
12                                 {name: "hacksListSelector", kind: "ListSelector", popupAlign: "left", onChange: "selectHack", style: "width: 200px"},
13                                 {kind: "Spacer"},
14                                 {name: "notice", className: "notice"}
15                         ]},
16                         {name: "noPrefsDialog", kind: "ModalDialog", caption: "No Preferences", components: [
17                                 {kind: "Button", content: "Okay", onclick: "closeNoPrefsDialog"}
18                         ]}
19                 ]},
20                 {name: "preferencesView", kind: "HackPreferences", onClose: "savePreferences"}
21         ],
22         hacksList: [
23                 {name: "Nimbus", kind: "Nimbus", dark: false},
24                 {name: "HexaSpinner", kind: "HexaSpinnerHack", dark: false},
25                 {name: "Landscape", kind: "Landscape", dark: false},
26                 {name: "Munch", kind: "Munch", dark: true},
27                 {name: "Orbit", kind: "Orbit", dark: false},
28                 {name: "Pixelfade", kind: "Pixelfade", dark: true},
29                 //{name: "Swarm", kind: "Swarm", dark: false},       // crashy
30                 //{name: "Spinner.CGA", kind: "XSpinnerCGA", dark: true}, // no webfont support
31                 {name: "Spinner", kind: "XSpinner", dark: true}
32         ],
33         create: function() {
34                 this.inherited(arguments);
35                 /*
36                 this.index = localStorage.getItem('hack.index');
37                 if (!this.index)
38                         this.index = 0;
39                 */
40                 this.index = 0;
41                 this.lastScrollPos = 0;
42                 this.$.hacksCarousel.setCenterView(this.getHack(this.index));
43         },
44         ready: function() {
45                 var displayList = [];
46                 for (var i = 0; i < this.hacksList.length; i++) {
47                         displayList.push({
48                                 caption: this.hacksList[i].name,
49                                 value: i
50                         });
51                 }
52                 this.$.hacksListSelector.setItems(displayList);
53                 this.startHack();
54                 window.addEventListener('resize', this.resizeHack.bind(this), false);
55                 this.setNotice('Swipe for more...');
56         },
57         setNotice: function(notice) {
58                 this.$.notice.setContent(notice);
59                 this.infoFade();
60         },
61         infoFade: function() {
62                 this.$.info.setStyle('opacity: 1');
63
64                 clearTimeout(this.noticeTimer);
65                 this.noticeTimer = setTimeout(function() {
66                         this.$.info.setStyle('opacity: 0');
67                         setTimeout(function() {
68                                 this.$.notice.setContent('');
69                         }.bind(this), 600);
70                 }.bind(this), 5000);
71         },
72         openPreferences: function() {
73                 var view = this.$.hacksCarousel.fetchView('center');
74
75                 var meta = view.getPreferencesMetadata();
76                 if (meta.length) {
77                         view.stop();
78                         var values = view.getPreferences();
79                         this.$.preferencesView.load(this.hacksList[this.index].name, meta, values);
80                         this.selectView(this.$.preferencesView);
81                 } else {
82                         this.$.noPrefsDialog.openAtCenter();
83                 }
84         },
85         savePreferences: function(inSender, prefs) {
86                 var view = this.$.hacksCarousel.fetchView('center');
87                 if (prefs == null) {
88                         enyo.log("Clearing prefs");
89                         view.resetPreferences(prefs);
90                         this.selectHack(this, this.index);
91                 } else {
92                         enyo.log("Saving prefs: " + JSON.stringify(prefs));
93                         view.setPreferences(prefs);
94                         view.start();
95                 }
96                 this.back();
97         },
98         windowActivated: function() {
99                 this.startHack();
100         },
101         windowDeactivated: function() {
102                 this.stopHack();
103         },
104         selectHack: function(inSender, inValue) {
105                 this.stopHack();
106                 this.index = inValue;
107                 this.lastScrollPos = 0;
108                 this.$.hacksCarousel.setCenterView(this.getHack(this.index));
109                 this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
110                 // For some reason, setCenterView above fires the startScroll
111                 // event without a subsequent stopScroll event.  To work around
112                 // this, we defer the start until later.
113                 setTimeout(function() { this.startHack() }.bind(this), 20);
114         },
115         startHack: function(direction) {
116                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
117                 //enyo.log('starting view ' + view);
118                 if (view)
119                         view.start();
120                 //localStorage.setItem('hack.index', this.index);
121         },
122         stopHack: function(direction) {
123                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
124                 //enyo.log('stopping view ' + view);
125                 if (view)
126                         view.stop();
127         },
128         resizeHack: function() {
129                 var view = this.$.hacksCarousel.fetchView('center');
130                 if (view)
131                         view.resize(window.innerWidth, window.innerHeight);
132                 var view = this.$.hacksCarousel.fetchView('left');
133                 if (view)
134                         view.resize(window.innerWidth, window.innerHeight);
135                 var view = this.$.hacksCarousel.fetchView('right');
136                 if (view)
137                         view.resize(window.innerWidth, window.innerHeight);
138         },
139         hackHidden: function(direction) {
140                 var view = this.$.hacksCarousel.fetchView(direction);
141                 //enyo.log('view ' + view + ' hidden');
142                 if (view)
143                         view.hidden();
144         },
145         getHack: function(index) {
146                 return {kind: this.hacksList[index].kind};
147         },
148         getLeft: function(inSender, inSnap) {
149                 if (inSnap && this.index > 0) {
150                         this.index--;
151                         this.$.hacksListSelector.setValue(this.index);
152                         this.hackHidden('right');
153                         this.infoFade();
154                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
155                 }
156                 if (this.index == 0)
157                         return null;
158                 else
159                         return this.getHack(this.index - 1);
160         },
161         getRight: function(inSender, inSnap) {
162                 if (inSnap && this.index < this.hacksList.length) {
163                         this.index++;
164                         this.$.hacksListSelector.setValue(this.index);
165                         this.hackHidden('left');
166                         this.infoFade();
167                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
168                 }
169                 if (this.index == this.hacksList.length - 1)
170                         return null;
171                 else
172                         return this.getHack(this.index + 1);
173         },
174         scrolling: function(inSender) {
175                 if (inSender.scrollLeft == 0 || inSender.scrollLeft == inSender.getBoundaries().right)
176                         this.startHack();
177         },
178         startScroll: function(inSender) {
179                 this.stopHack();
180         },
181         stopScroll: function(inSender) {
182                 this.lastScrollPos = this.$.hacksCarousel.scrollLeft;
183                 this.startHack();
184         },
185         closeNoPrefsDialog: function() {
186                 this.$.noPrefsDialog.close();
187         }
188 });