Tile Munch at native res to fill screen.
[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
89                 //enyo.log('resizing view ' + view);
90                 if (view)
91                         view.resize(window.innerWidth, window.innerHeight);
92         },
93         hackHidden: function(direction) {
94                 var view = this.$.hacksCarousel.fetchView(direction);
95                 //enyo.log('view ' + view + ' hidden');
96                 if (view)
97                         view.hidden();
98         },
99         getHack: function(index) {
100                 return {kind: this.hacksList[index].kind};
101         },
102         getLeft: function(inSender, inSnap) {
103                 if (inSnap && this.index > 0) {
104                         this.index--;
105                         this.hackHidden('right');
106                         this.setTitle(this.hacksList[this.index].name);
107                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
108                 }
109                 if (this.index == 0)
110                         return null;
111                 else
112                         return this.getHack(this.index - 1);
113         },
114         getRight: function(inSender, inSnap) {
115                 if (inSnap && this.index < this.hacksList.length) {
116                         this.index++;
117                         this.hackHidden('left');
118                         this.setTitle(this.hacksList[this.index].name);
119                         this.$.info.addRemoveClass('dark', this.hacksList[this.index].dark);
120                 }
121                 if (this.index == this.hacksList.length - 1)
122                         return null;
123                 else
124                         return this.getHack(this.index + 1);
125         },
126         scrolling: function(inSender) {
127                 if (inSender.scrollLeft == 0 || inSender.scrollLeft == inSender.getBoundaries().right)
128                         this.startHack();
129                 //this.$.hacksSelector.index = this.index + ((inSender.scrollLeft - this.lastScrollPos) / window.innerWidth);
130                 //this.$.hacksSelector.draw();
131         },
132         startScroll: function(inSender) {
133                 this.stopHack();
134         },
135         stopScroll: function(inSender) {
136                 this.lastScrollPos = this.$.hacksCarousel.scrollLeft;
137                 this.startHack();
138         }
139 });