Fix list selector on load
[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                 try {
36                         this.index = parseInt(localStorage.getItem('hack.index'));
37                         if (!this.index)
38                                 this.index = 0;
39                 } catch(e) {
40                         enyo.log("Could not load last hack index");
41                         this.index = 0;
42                 }
43                 this.lastScrollPos = 0;
44                 this.$.hacksCarousel.setCenterView(this.getHack(this.index));
45         },
46         ready: function() {
47                 var displayList = [];
48                 for (var i = 0; i < this.hacksList.length; i++) {
49                         displayList.push({
50                                 caption: this.hacksList[i].name,
51                                 value: i
52                         });
53                 }
54                 this.$.hacksListSelector.setItems(displayList);
55                 this.$.hacksListSelector.setValue(this.index);
56                 this.startHack();
57                 window.addEventListener('resize', this.resizeHack.bind(this), false);
58                 this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
59                 this.setNotice('Swipe for more...');
60         },
61         setNotice: function(notice) {
62                 this.$.notice.setContent(notice);
63                 this.infoFade();
64         },
65         infoFade: function() {
66                 this.$.info.setStyle('opacity: 1');
67
68                 clearTimeout(this.noticeTimer);
69                 this.noticeTimer = setTimeout(function() {
70                         this.$.info.setStyle('opacity: 0');
71                         setTimeout(function() {
72                                 this.$.notice.setContent('');
73                         }.bind(this), 600);
74                 }.bind(this), 5000);
75         },
76         openPreferences: function() {
77                 var view = this.$.hacksCarousel.fetchView('center');
78
79                 var meta = view.getPreferencesMetadata();
80                 if (meta.length) {
81                         view.stop();
82                         var values = view.getPreferences();
83                         this.$.preferencesView.load(this.hacksList[this.index].name, meta, values);
84                         this.selectView(this.$.preferencesView);
85                 } else {
86                         this.$.noPrefsDialog.openAtCenter();
87                 }
88         },
89         savePreferences: function(inSender, prefs) {
90                 var view = this.$.hacksCarousel.fetchView('center');
91                 if (prefs == null) {
92                         enyo.log("Clearing prefs");
93                         view.resetPreferences(prefs);
94                         this.selectHack(this, this.index);
95                 } else {
96                         enyo.log("Saving prefs: " + JSON.stringify(prefs));
97                         view.setPreferences(prefs);
98                         view.start();
99                 }
100                 this.back();
101         },
102         windowActivated: function() {
103                 this.startHack();
104         },
105         windowDeactivated: function() {
106                 this.stopHack();
107         },
108         selectHack: function(inSender, inValue) {
109                 this.stopHack();
110                 this.index = inValue;
111                 this.lastScrollPos = 0;
112                 this.$.hacksCarousel.setCenterView(this.getHack(this.index));
113                 this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
114                 // For some reason, setCenterView above fires the startScroll
115                 // event without a subsequent stopScroll event.  To work around
116                 // this, we defer the start until later.
117                 setTimeout(function() { this.startHack() }.bind(this), 20);
118         },
119         startHack: function(direction) {
120                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
121                 //enyo.log('starting view ' + view);
122                 if (view)
123                         view.start();
124                 try {
125                         localStorage.setItem('hack.index', this.index);
126                 } catch(e) {
127                         enyo.log("Could not set hack index");
128                 }
129         },
130         stopHack: function(direction) {
131                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
132                 //enyo.log('stopping view ' + view);
133                 if (view)
134                         view.stop();
135         },
136         resizeHack: function() {
137                 var view = this.$.hacksCarousel.fetchView('center');
138                 if (view)
139                         view.resize(window.innerWidth, window.innerHeight);
140                 var view = this.$.hacksCarousel.fetchView('left');
141                 if (view)
142                         view.resize(window.innerWidth, window.innerHeight);
143                 var view = this.$.hacksCarousel.fetchView('right');
144                 if (view)
145                         view.resize(window.innerWidth, window.innerHeight);
146         },
147         hackHidden: function(direction) {
148                 var view = this.$.hacksCarousel.fetchView(direction);
149                 //enyo.log('view ' + view + ' hidden');
150                 if (view)
151                         view.hidden();
152         },
153         getHack: function(index) {
154                 return {kind: this.hacksList[index].kind};
155         },
156         getLeft: function(inSender, inSnap) {
157                 if (inSnap && this.index > 0) {
158                         this.index--;
159                         this.$.hacksListSelector.setValue(this.index);
160                         this.hackHidden('right');
161                         this.infoFade();
162                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
163                 }
164                 if (this.index == 0)
165                         return null;
166                 else
167                         return this.getHack(this.index - 1);
168         },
169         getRight: function(inSender, inSnap) {
170                 if (inSnap && this.index < this.hacksList.length) {
171                         this.index++;
172                         this.$.hacksListSelector.setValue(this.index);
173                         this.hackHidden('left');
174                         this.infoFade();
175                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
176                 }
177                 if (this.index == this.hacksList.length - 1)
178                         return null;
179                 else
180                         return this.getHack(this.index + 1);
181         },
182         scrolling: function(inSender) {
183                 if (inSender.scrollLeft == 0 || inSender.scrollLeft == inSender.getBoundaries().right)
184                         this.startHack();
185         },
186         startScroll: function(inSender) {
187                 this.stopHack();
188         },
189         stopScroll: function(inSender) {
190                 this.lastScrollPos = this.$.hacksCarousel.scrollLeft;
191                 this.startHack();
192         },
193         closeNoPrefsDialog: function() {
194                 this.$.noPrefsDialog.close();
195         }
196 });