Changed web samples' name and updated snapshots
[profile/ivi/sdk/web-sample-build.git] / samples / web / Sample / Tizen / Web App / ModelloHomescreen / project / js / keyControl.js
1 /*global areasDefinitions, onFrameClick */
2
3 /**
4  * @module HomescreenApplication
5  **/
6 /**
7  * Allows to select radial menu items and start selected application by keyboard. Following keys are intecepted:
8  * * A - Previous application
9  * * D - Next application
10  * * Enter - Launch application
11  * * 1 - 7 - Launch application 1-7 from radial menu starting from 12 o'clock application in clockwise direction
12  * @class keyControl
13  * @static
14  **/
15 var KeyControl = {
16         /**
17          * Define selected sector (pie, app).
18          * @property homeScreenselectedIndex
19          * @type int
20          * @default 1
21          **/
22         homeScreenselectedIndex: 1,
23         /**
24          * Indicate if some sector is selected.
25          * @property sectionHighlited
26          * @type bool
27          * @default false
28          **/
29         sectionHighlited: false,
30         /**
31          * Timer for reset highlited pie after predefined timer interval in timerInteval property.
32          * @property timer
33          * @type Object
34          * @default null
35          **/
36         timer: null,
37         /**
38          * Define interval for unhighlight and unselect selected sector if keyboard is inactive more than defined interval.
39          * @property timerIterval
40          * @type int
41          * @default 5000 (ms)
42          **/
43         timerIterval: 5000,
44         /**
45          * Provides changing of selected sector, highlights it and unhighlight previous higlighted. Also provides setting of timer.
46          * @method changeIndex
47          * @param direction {string} Could be 'prev', 'next' for change highigting for prev or next sector and 'null' only for unhighligting sector
48          **/
49         changeIndex: function (direction) {
50                 "use strict";
51                 var i = 0;
52                 for (i = 0; i < areasDefinitions.length; i++) {
53                         if (areasDefinitions[i].sectorId !== null) {
54                                 $('.sector' + areasDefinitions[i].sectorId).removeClass('selected');
55                         }
56                 }
57                 if (direction !== null) {
58                         if (KeyControl.sectionHighlited) {
59                                 clearTimeout(KeyControl.timer);
60                                 KeyControl.timer = setTimeout(function() { KeyControl.changeIndex(); }, KeyControl.timerIterval);
61                                 switch (direction) {
62                                 case 'prev':
63                                         KeyControl.prevIndex();
64                                         break;
65                                 case 'next':
66                                         KeyControl.nextIndex();
67                                         break;
68                                 }
69                                 $('.sector' + KeyControl.homeScreenselectedIndex).addClass('selected');
70                         } else {
71                                 KeyControl.sectionHighlited = true;
72                                 $('.sector' + KeyControl.homeScreenselectedIndex).addClass('selected');
73                                 KeyControl.timer = setTimeout(function() { KeyControl.changeIndex(); }, KeyControl.timerIterval);
74                         }
75                 } else {
76                         KeyControl.sectionHighlited = false;
77                 }
78         },
79         /**
80          * Provides changing index of selected sector to next one in circle.
81          * @method nextIndex
82          * **/
83         nextIndex: function () {
84                 "use strict";
85                 KeyControl.homeScreenselectedIndex++;
86                 if (KeyControl.homeScreenselectedIndex > 7) {
87                         KeyControl.homeScreenselectedIndex = 1;
88                 }
89         },
90         /**
91          * Provides changing index of selected sector to previous one in circle.
92          * @method prevIndex
93          **/
94         prevIndex: function () {
95                 "use strict";
96                 KeyControl.homeScreenselectedIndex--;
97                 if (KeyControl.homeScreenselectedIndex < 1) {
98                         KeyControl.homeScreenselectedIndex = 7;
99                 }
100         },
101         /**
102          * Provides start of app by confirmation of selected sector with keyboard.
103          * @method prevIndex
104          **/
105         confirmed: function () {
106                 "use strict";
107                 if (KeyControl.sectionHighlited) {
108                         switch (KeyControl.homeScreenselectedIndex) {
109                         case 1:
110                                 onFrameClick(areasDefinitions[1]);
111                                 break;//maps
112                         case 2:
113                                 onFrameClick(areasDefinitions[2]);
114                                 break;//maps
115                         case 3:
116                                 onFrameClick(areasDefinitions[3]);
117                                 break;//musicPlayer
118                         case 4:
119                                 onFrameClick(areasDefinitions[4]);
120                                 break;//sdl
121                         case 5:
122                                 onFrameClick(areasDefinitions[5]);
123                                 break;//phone
124                         case 6:
125                                 onFrameClick(areasDefinitions[6]);
126                                 break;//store
127                         case 7:
128                                 onFrameClick(areasDefinitions[7]);
129                                 break;//dashboard
130                         }
131                 }
132         }
133 };
134
135 /**
136  * Provides catching and sorting of keyboard events.
137  * @method $(document).keypress
138  * @param callback {function} For calling functions on specific key press.
139  * @static
140  **/
141 $(document).keypress(function (event) {
142         "use strict";
143         switch (event.keyCode) {
144         case 97:
145                 KeyControl.changeIndex('prev'); // a
146                 break;
147         case 100:
148                 KeyControl.changeIndex('next');  // d
149                 break;
150         case 13:
151                 KeyControl.confirmed();
152                 break;
153         }
154 });