Resize all loaded views to prevent left/right overlapping center.
[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: "hacksSelector", kind: "HacksSelector"}
11                 {name: "info", kind: "HFlexBox", className: "info", style: "opacity: 0", showing: false, components: [
12                         {name: "title"},
13                         {kind: "Spacer"},
14                         {name: "notice", className: "notice"}
15                 ]}
16         ],
17         hacksList: [
18                 {name: "Nimbus", kind: "Nimbus", 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                 //this.$.hacksSelector.setHacksList(this.hacksList);
38         },
39         ready: function() {
40                 this.startHack();
41                 window.addEventListener('resize', this.resizeHack.bind(this), false);
42                 this.setNotice('Swipe for more...');
43                 this.setTitle(this.hacksList[this.index].name);
44         },
45         setTitle: function(title) {
46                 this.$.title.setContent(title);
47                 this.infoFade();
48         },
49         setNotice: function(notice) {
50                 this.$.notice.setContent(notice);
51                 this.infoFade();
52         },
53         infoFade: function() {
54                 this.$.info.setStyle('opacity: 1');
55                 this.$.info.show();
56
57                 clearTimeout(this.noticeTimer);
58                 this.noticeTimer = setTimeout(function() {
59                         this.$.info.setStyle('opacity: 0');
60                         setTimeout(function() {
61                                 this.$.info.hide();
62                                 this.setNotice('');
63                                 this.setTitle('');
64                         }.bind(this), 600);
65                 }.bind(this), 5000);
66         },
67         windowActivated: function() {
68                 this.startHack();
69         },
70         windowDeactivated: function() {
71                 this.stopHack();
72         },
73         startHack: function(direction) {
74                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
75                 //enyo.log('starting view ' + view);
76                 if (view)
77                         view.start();
78                 //localStorage.setItem('hack.index', this.index);
79         },
80         stopHack: function(direction) {
81                 var view = this.$.hacksCarousel.fetchView(direction || 'center');
82                 //enyo.log('stopping view ' + view);
83                 if (view)
84                         view.stop();
85         },
86         resizeHack: function() {
87                 var view = this.$.hacksCarousel.fetchView('center');
88                 if (view)
89                         view.resize(window.innerWidth, window.innerHeight);
90                 var view = this.$.hacksCarousel.fetchView('left');
91                 if (view)
92                         view.resize(window.innerWidth, window.innerHeight);
93                 var view = this.$.hacksCarousel.fetchView('right');
94                 if (view)
95                         view.resize(window.innerWidth, window.innerHeight);
96         },
97         hackHidden: function(direction) {
98                 var view = this.$.hacksCarousel.fetchView(direction);
99                 //enyo.log('view ' + view + ' hidden');
100                 if (view)
101                         view.hidden();
102         },
103         getHack: function(index) {
104                 return {kind: this.hacksList[index].kind};
105         },
106         getLeft: function(inSender, inSnap) {
107                 if (inSnap && this.index > 0) {
108                         this.index--;
109                         this.hackHidden('right');
110                         this.setTitle(this.hacksList[this.index].name);
111                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
112                 }
113                 if (this.index == 0)
114                         return null;
115                 else
116                         return this.getHack(this.index - 1);
117         },
118         getRight: function(inSender, inSnap) {
119                 if (inSnap && this.index < this.hacksList.length) {
120                         this.index++;
121                         this.hackHidden('left');
122                         this.setTitle(this.hacksList[this.index].name);
123                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
124                 }
125                 if (this.index == this.hacksList.length - 1)
126                         return null;
127                 else
128                         return this.getHack(this.index + 1);
129         },
130         scrolling: function(inSender) {
131                 if (inSender.scrollLeft == 0 || inSender.scrollLeft == inSender.getBoundaries().right)
132                         this.startHack();
133                 //this.$.hacksSelector.index = this.index + ((inSender.scrollLeft - this.lastScrollPos) / window.innerWidth);
134                 //this.$.hacksSelector.draw();
135         },
136         startScroll: function(inSender) {
137                 this.stopHack();
138         },
139         stopScroll: function(inSender) {
140                 this.lastScrollPos = this.$.hacksCarousel.scrollLeft;
141                 this.startHack();
142         }
143 });