upload tizen1.0 source
[framework/web/web-ui-fw.git] / src / widgets / common / js / jquery.mobile.tizen.core.js
1 /*!
2  * jQuery Mobile Widget @VERSION
3  *
4  * TODO: remove unnecessary codes....
5  *
6  * This software is licensed under the MIT licence (as defined by the OSI at
7  * http://www.opensource.org/licenses/mit-license.php)
8  * 
9  * ***************************************************************************
10  * Copyright (C) 2011 by Intel Corporation Ltd.
11  * 
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  * 
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  * 
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  * ***************************************************************************
30  *
31  * Authors: Kalyan Kondapally <kalyan.kondapally@intel.com>
32  */
33
34 ensureNS("jQuery.mobile.tizen");
35
36 (function () {
37 jQuery.extend(jQuery.mobile.tizen, {
38     Point: function (x, y) {
39         var X = isNaN(x) ? 0 : x;
40         var Y = isNaN(y) ? 0 : y;
41
42         this.add = function (Point) {
43             this.setX(X + Point.x());
44             this.setY(Y + Point.y());
45             return this;
46         }
47
48         this.subtract = function (Point) {
49             this.setX(X - Point.x());
50             this.setY(Y - Point.y());
51             return this;
52         }
53
54         this.multiply = function (Point) {
55             this.setX(Math.round(X * Point.x()));
56             this.setY(Math.round(Y * Point.y()));
57             return this;
58         }
59
60         this.divide = function (Point) {
61             this.setX(Math.round(X / Point.x()));
62             this.setY(Math.round(Y / Point.y()));
63             return this;
64         }
65
66         this.isNull = function () {
67             return (X === 0 && Y === 0);
68         }
69
70         this.x = function () {
71             return X;
72         }
73
74         this.setX = function (val) {
75             isNaN(val) ? X = 0 : X = val;
76         }
77
78         this.y = function () {
79             return Y;
80         }
81
82         this.setY = function (val) {
83             isNaN(val) ? Y = 0 : Y = val;
84         }
85
86         this.setNewPoint = function (point) {
87             this.setX(point.x());
88             this.setY(point.y());
89         }
90
91         this.isEqualTo = function (point) {
92             return (X === point.x() && Y === point.y());
93         }
94     },
95
96     Rect: function (left,top,width,height) {
97         var Left = left;
98         var Top = top;
99         var Right = Left+width;
100         var Bottom = Top+height;
101
102         this.setRect = function(varL,varR,varT,varB) {
103             this.setLeft(varL);
104             this.setRight(varR);
105             this.setTop(varT);
106             this.setBottom(varB);
107         }
108
109         this.right = function () {
110             return Right;
111         }
112
113         this.setRight = function (val) {
114             Right = val;
115         }
116
117         this.top = function () {
118             return Top;
119         }
120
121         this.setTop = function (val) {
122             Top = val;
123         }
124
125         this.bottom = function () {
126             return Bottom;
127         }
128
129         this.setBottom = function (val) {
130             Bottom = val;
131         }
132
133         this.left = function () {
134             return Left;
135         }
136
137         this.setLeft = function (val) {
138             Left = val;
139         }
140
141         this.moveTop = function(valY) {
142             var h = this.height();
143             Top = valY;
144             Bottom = Top + h;
145         }
146
147         this.isNull = function () {
148             return Right === Left && Bottom === Top;
149         }
150
151         this.isValid = function () {
152             return Left <= Right && Top <= Bottom;
153         }
154
155         this.isEmpty = function () {
156             return Left > Right || Top > Bottom;
157         }
158
159         this.contains = function (valX,valY) {
160             if (this.containsX(valX) && this.containsY(valY))
161                 return true;
162             return false;
163         }
164
165         this.width = function () {
166             return Right - Left;
167         }
168
169         this.height = function () {
170             return Bottom - Top;
171         }
172
173         this.containsX = function(val) {
174             var l = Left,
175             r = Right;
176             if (Right<Left) {
177                 l = Right;
178                 r = Left;
179             }
180             if (l > val || r < val)
181                 return false;
182         return true;
183         }
184
185         this.containsY = function(val) {
186             var t = Top,
187             b = Bottom;
188             if (Bottom<Top) {
189                 t = Bottom;
190                 b = Top;
191             }
192             if (t > val || b < val)
193                 return false;
194           return true;
195         }
196     },
197
198     disableSelection: function (element) {
199         return $(element).each(function () {
200             jQuery(element).css('-webkit-user-select', 'none');
201         });
202     },
203
204     enableSelection: function (element, value) {
205         return $(element).each(function () {
206             val = value == "text" ? val = 'text' : val = 'auto';
207             jQuery(element).css('-webkit-user-select', val);
208         });
209     },
210
211     // Set the height of the content area to fill the space between a
212     // page's header and footer
213     fillPageWithContentArea: function (page) {
214         var $page = $(page);
215         var $content = $page.children(".ui-content:first");
216         var hh = $page.children(".ui-header").outerHeight(); hh = hh ? hh : 0;
217         var fh = $page.children(".ui-footer").outerHeight(); fh = fh ? fh : 0;
218         var pt = parseFloat($content.css("padding-top"));
219         var pb = parseFloat($content.css("padding-bottom"));
220         var wh = window.innerHeight;
221         var height = wh - (hh + fh) - (pt + pb);
222         $content.height(height);
223     },
224
225     // Get document-relative mouse coordinates from a given event
226     // From: http://www.quirksmode.org/js/events_properties.html#position
227     documentRelativeCoordsFromEvent: function(ev) {
228         var e = ev ? ev : window.event,
229             client = { x: e.clientX, y: e.clientY },
230             page   = { x: e.pageX,   y: e.pageY   },
231             posx = 0,
232             posy = 0;
233
234         // Grab useful coordinates from touch events
235         if (e.type.match(/^touch/)) {
236             page = {
237                 x: e.originalEvent.targetTouches[0].pageX,
238                 y: e.originalEvent.targetTouches[0].pageY
239             };
240             client = {
241                 x: e.originalEvent.targetTouches[0].clientX,
242                 y: e.originalEvent.targetTouches[0].clientY
243             };
244         }
245
246         if (page.x || page.y) {
247             posx = page.x;
248             posy = page.y;
249         }
250         else
251         if (client.x || client.y) {
252             posx = client.x + document.body.scrollLeft + document.documentElement.scrollLeft;
253             posy = client.y + document.body.scrollTop  + document.documentElement.scrollTop;
254         }
255
256         return { x: posx, y: posy };
257     },
258
259         // TODO : offsetX, offsetY. touch events don't have offsetX and offsetY. support for touch devices.
260     // check algorithm...
261     targetRelativeCoordsFromEvent: function(e) {
262         var coords = { x: e.offsetX, y: e.offsetY };
263
264         if (coords.x === undefined || isNaN(coords.x) ||
265             coords.y === undefined || isNaN(coords.y)) {
266             var offset = $(e.target).offset();
267             //coords = documentRelativeCoordsFromEvent(e);      // Old code. Must be checked again.
268             coords = $.mobile.tizen.documentRelativeCoordsFromEvent(e);
269             coords.x -= offset.left;
270             coords.y -= offset.top;
271         }
272
273         return coords;
274     }
275 });
276
277 })();