Modifying spec file to rely on Common module, and copying the code from it once insta...
[profile/ivi/html5_UI_Homescreen.git] / js / actionCatcher.js
1 /*global  areasDefinitions, onFrameClick, centerPoint */
2
3 /**
4  * @module HomescreenApplication
5  **/
6
7 /**
8  * Compute distance between two points in 2D.
9  * @method pointsDistance
10  * @for window
11  * @private
12  * @param pointA {object} Contains X and Y coordinate of first point.
13  * @param pointB {object} Contains X and Y coordinate of second point.
14  * @return float distance in points
15  **/
16 var pointsDistance = function (pointA, pointB) {
17         "use strict";
18         return Math.sqrt(Math.pow(Math.abs(pointA.y - pointB.y), 2) + Math.pow(Math.abs(pointA.x - pointB.x), 2));
19 };
20
21 /**
22  * Compute angle of click point from  zero angle (zero angle has a line, which is horizontal and starts in the center point).
23  * @method pointAngle
24  * @for window
25  * @private
26  * @param point {object} Contains X and Y coordinate of clicked/touched point.
27  * @param centerPoint {object} Contains X and Y coordinate of center point.
28  * @return float angle in degrees
29  **/
30 var pointAngle = function (point, centerPoint) {
31         "use strict";
32         var tmpPoint = {};
33         tmpPoint.x = centerPoint.x + 200; //to define zero vector
34         tmpPoint.y = centerPoint.y;
35         var u1 = tmpPoint.x - centerPoint.x,
36                 u2 = tmpPoint.y - centerPoint.y,
37                 v1 = point.x - centerPoint.x,
38                 v2 = point.y - centerPoint.y,
39                 su = pointsDistance(centerPoint, tmpPoint),
40                 sv = pointsDistance(centerPoint, point),
41                 angle = Math.acos((u1 * v1 + u2 * v2) / (su * sv)) * (180 / Math.PI);
42         if (point.y > centerPoint.y) {
43                 angle = 360 - angle;
44         }
45         return angle;
46 };
47
48 /**
49  * Gets app sector from click based on predefAppModel, clickDistance from center and click angle.
50  * @method getClickedItem
51  * @for window
52  * @private
53  * @param touchPoint {object} Contains X and Y coordinate of clicked/touched point.
54  **/
55 var getClickedItem = function (touchPoint) {
56         "use strict";
57         var dst = pointsDistance(touchPoint, centerPoint),
58                 angle = pointAngle(touchPoint, centerPoint),
59                 i = 0;
60         for (i = 0; i < areasDefinitions.length; i++) {
61                 switch (areasDefinitions[i].shape) {
62                 case 'cenerCircle':
63                         if (dst < areasDefinitions[i].lc) {
64                                 return areasDefinitions[i];
65                         }
66                         break;
67                 case 'pieWithoutCenter':
68                         if ((areasDefinitions[i].sc < dst) && (areasDefinitions[i].lc > dst) && (areasDefinitions[i].sa < angle) && (areasDefinitions[i].la > angle)) {
69                                 return areasDefinitions[i];
70                         }
71                         break;
72                 }
73         }
74         return null;
75 };
76
77
78 /**
79  * Provides functions for catching mouse events and translating them to correct calls for launching applications displayed in radial menu. Applications and locations
80  * which should be displayed are defined in class [predefAppModel](../classes/predefAppModel.html). Reading applications from Tizen system is handled by class
81  * [installedApps](../classes/installedApps.html). As alternative method to launching applicatins with mouse events applications can be launched by key events which are
82  * processed by class [keyControl](../classes/keyControl.html).
83  * @class actionCatcher
84  * @static
85  **/
86 var actionCatcher = {
87         /**
88          * Holds clicked item Object.
89          * @property clickedItem
90          * @type Object
91          * @default null
92          **/
93         clickedItem: null,
94         /**
95          * Indicates if mouse button is pressed.
96          * @property mouseDown
97          * @type bool
98          * @default false
99          **/
100         mouseDown: false,
101
102         /**
103          * Provides highlighting sectors if mouse cursor is over.
104          * @method over
105          **/
106         over: function () {
107                 "use strict";
108                 var i = 0;
109                 if (!actionCatcher.mouseDown) {
110                         var mousePosition = {x: window.event.pageX, y: window.event.pageY};
111                         var overItem = getClickedItem(mousePosition);
112                         if (overItem !== actionCatcher.clickedItem) {
113                                 for (i = 0; i < areasDefinitions.length; i++) {
114                                         if (areasDefinitions[i].sectorId !== null) {
115                                                 $('.sector' + areasDefinitions[i].sectorId).removeClass('selected');
116                                         }
117                                 }
118                                 if (overItem !== null) {
119                                         $('.sector' + overItem.sectorId).addClass('selected');
120                                 }
121                                 actionCatcher.clickedItem = overItem;
122                         }
123                 }
124         },
125         /**
126          * Sets mouseDown property to true and sets clickedItem property if click to some sector.
127          * @method touchStart
128          **/
129         touchStart: function () {
130                 "use strict";
131                 actionCatcher.mouseDown = true;
132                 var mousePosition = {x: window.event.pageX, y: window.event.pageY};
133                 actionCatcher.clickedItem = getClickedItem(mousePosition);
134         },
135         /**
136          * Sets mouseDown property to false and starts app if click to some sector.
137          * @method touchEnd
138          **/
139         touchEnd: function () {
140                 "use strict";
141                 actionCatcher.mouseDown = false;
142                 if (actionCatcher.clickedItem !== null) {
143                         switch (actionCatcher.clickedItem.shape) {
144                         case 'cenerCircle':
145                                 actionCatcher.clickedItem.action();
146                                 break;
147                         case 'pieWithoutCenter':
148                                 if (actionCatcher.clickedItem.id !== null) {
149                                         onFrameClick(actionCatcher.clickedItem);
150                                 }
151                                 break;
152                         }
153                 }
154         }
155 };