Add dialog for hacks without prefs, add prefs save/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                 /*
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                 enyo.log("Saving prefs: " + JSON.stringify(prefs));
87                 this.back();
88                 var view = this.$.hacksCarousel.fetchView('center');
89                 view.setPreferences(prefs);
90                 view.start();
91         },
92         windowActivated: function() {
93                 this.startHack();
94         },
95         windowDeactivated: function() {
96                 this.stopHack();
97         },
98         selectHack: function(inSender, inValue, inOldValue) {
99                 this.stopHack();
100                 this.index = inValue;
101                 this.lastScrollPos = 0;
102                 this.$.hacksCarousel.setCenterView(this.getHack(this.index));
103                 this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
104                 // For some reason, setCenterView above fires the startScroll
105                 // event without a subsequent stopScroll event.  To work around
106                 // this, we defer the start until later.
107                 setTimeout(function() { this.startHack() }.bind(this), 20);
108         },
109         startHack: function(direction) {
110                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
111                 //enyo.log('starting view ' + view);
112                 if (view)
113                         view.start();
114                 //localStorage.setItem('hack.index', this.index);
115         },
116         stopHack: function(direction) {
117                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
118                 //enyo.log('stopping view ' + view);
119                 if (view)
120                         view.stop();
121         },
122         resizeHack: function() {
123                 var view = this.$.hacksCarousel.fetchView('center');
124                 if (view)
125                         view.resize(window.innerWidth, window.innerHeight);
126                 var view = this.$.hacksCarousel.fetchView('left');
127                 if (view)
128                         view.resize(window.innerWidth, window.innerHeight);
129                 var view = this.$.hacksCarousel.fetchView('right');
130                 if (view)
131                         view.resize(window.innerWidth, window.innerHeight);
132         },
133         hackHidden: function(direction) {
134                 var view = this.$.hacksCarousel.fetchView(direction);
135                 //enyo.log('view ' + view + ' hidden');
136                 if (view)
137                         view.hidden();
138         },
139         getHack: function(index) {
140                 return {kind: this.hacksList[index].kind};
141         },
142         getLeft: function(inSender, inSnap) {
143                 if (inSnap && this.index > 0) {
144                         this.index--;
145                         this.$.hacksListSelector.setValue(this.index);
146                         this.hackHidden('right');
147                         this.infoFade();
148                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
149                 }
150                 if (this.index == 0)
151                         return null;
152                 else
153                         return this.getHack(this.index - 1);
154         },
155         getRight: function(inSender, inSnap) {
156                 if (inSnap && this.index < this.hacksList.length) {
157                         this.index++;
158                         this.$.hacksListSelector.setValue(this.index);
159                         this.hackHidden('left');
160                         this.infoFade();
161                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
162                 }
163                 if (this.index == this.hacksList.length - 1)
164                         return null;
165                 else
166                         return this.getHack(this.index + 1);
167         },
168         scrolling: function(inSender) {
169                 if (inSender.scrollLeft == 0 || inSender.scrollLeft == inSender.getBoundaries().right)
170                         this.startHack();
171         },
172         startScroll: function(inSender) {
173                 this.stopHack();
174         },
175         stopScroll: function(inSender) {
176                 this.lastScrollPos = this.$.hacksCarousel.scrollLeft;
177                 this.startHack();
178         },
179         closeNoPrefsDialog: function() {
180                 this.$.noPrefsDialog.close();
181         }
182 });