Excise HacksSelectorCanvas
[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: "VFlexBox",
6         style: "background-color: black",
7         components: [
8                 {kind: "ApplicationEvents", onWindowActivated: "windowActivated", onWindowDeactivated: "windowDeactivated"},
9                 {name: "hacksCarousel", kind: "Carousel", flex: 1, onGetLeft: "getLeft", onGetRight: "getRight", onScroll: "scrolling", onScrollStart: "startScroll", onScrollStop: "stopScroll"},
10                 {name: "info", kind: "HFlexBox", className: "info", style: "opacity: 0", showing: false, components: [
11                         {name: "title"},
12                         {kind: "Spacer"},
13                         {name: "notice", className: "notice"}
14                 ]}
15         ],
16         hacksList: [
17                 {name: "Nimbus", kind: "Nimbus", dark: false},
18                 {name: "HexaSpinner", kind: "HexaSpinnerHack", dark: false},
19                 {name: "Landscape", kind: "Landscape", dark: false},
20                 {name: "Munch", kind: "Munch", dark: true},
21                 {name: "Orbit", kind: "Orbit", dark: false},
22                 {name: "Pixelfade", kind: "Pixelfade", dark: true},
23                 //{name: "Swarm", kind: "Swarm", dark: false},       // crashy
24                 //{name: "Spinner.CGA", kind: "XSpinnerCGA", dark: true}, // no webfont support
25                 {name: "Spinner", kind: "XSpinner", dark: true}
26         ],
27         create: function() {
28                 this.inherited(arguments);
29                 /*
30                 this.index = localStorage.getItem('hack.index');
31                 if (!this.index)
32                         this.index = 0;
33                 */
34                 this.index = 0;
35                 this.lastScrollPos = 0;
36                 this.$.hacksCarousel.setCenterView(this.getHack(this.index));
37         },
38         ready: function() {
39                 this.startHack();
40                 window.addEventListener('resize', this.resizeHack.bind(this), false);
41                 this.setNotice('Swipe for more...');
42                 this.setTitle(this.hacksList[this.index].name);
43         },
44         setTitle: function(title) {
45                 this.$.title.setContent(title);
46                 this.infoFade();
47         },
48         setNotice: function(notice) {
49                 this.$.notice.setContent(notice);
50                 this.infoFade();
51         },
52         infoFade: function() {
53                 this.$.info.setStyle('opacity: 1');
54                 this.$.info.show();
55
56                 clearTimeout(this.noticeTimer);
57                 this.noticeTimer = setTimeout(function() {
58                         this.$.info.setStyle('opacity: 0');
59                         setTimeout(function() {
60                                 this.$.info.hide();
61                                 this.setNotice('');
62                                 this.setTitle('');
63                         }.bind(this), 600);
64                 }.bind(this), 5000);
65         },
66         windowActivated: function() {
67                 this.startHack();
68         },
69         windowDeactivated: function() {
70                 this.stopHack();
71         },
72         startHack: function(direction) {
73                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
74                 //enyo.log('starting view ' + view);
75                 if (view)
76                         view.start();
77                 //localStorage.setItem('hack.index', this.index);
78         },
79         stopHack: function(direction) {
80                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
81                 //enyo.log('stopping view ' + view);
82                 if (view)
83                         view.stop();
84         },
85         resizeHack: function() {
86                 var view = this.$.hacksCarousel.fetchView('center');
87                 if (view)
88                         view.resize(window.innerWidth, window.innerHeight);
89                 var view = this.$.hacksCarousel.fetchView('left');
90                 if (view)
91                         view.resize(window.innerWidth, window.innerHeight);
92                 var view = this.$.hacksCarousel.fetchView('right');
93                 if (view)
94                         view.resize(window.innerWidth, window.innerHeight);
95         },
96         hackHidden: function(direction) {
97                 var view = this.$.hacksCarousel.fetchView(direction);
98                 //enyo.log('view ' + view + ' hidden');
99                 if (view)
100                         view.hidden();
101         },
102         getHack: function(index) {
103                 return {kind: this.hacksList[index].kind};
104         },
105         getLeft: function(inSender, inSnap) {
106                 if (inSnap && this.index > 0) {
107                         this.index--;
108                         this.hackHidden('right');
109                         this.setTitle(this.hacksList[this.index].name);
110                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
111                 }
112                 if (this.index == 0)
113                         return null;
114                 else
115                         return this.getHack(this.index - 1);
116         },
117         getRight: function(inSender, inSnap) {
118                 if (inSnap && this.index < this.hacksList.length) {
119                         this.index++;
120                         this.hackHidden('left');
121                         this.setTitle(this.hacksList[this.index].name);
122                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
123                 }
124                 if (this.index == this.hacksList.length - 1)
125                         return null;
126                 else
127                         return this.getHack(this.index + 1);
128         },
129         scrolling: function(inSender) {
130                 if (inSender.scrollLeft == 0 || inSender.scrollLeft == inSender.getBoundaries().right)
131                         this.startHack();
132         },
133         startScroll: function(inSender) {
134                 this.stopHack();
135         },
136         stopScroll: function(inSender) {
137                 this.lastScrollPos = this.$.hacksCarousel.scrollLeft;
138                 this.startHack();
139         }
140 });