62488573134d38a18cacbfbd022309cb314d102a
[platform/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     disableSelection: function (element) {
39         var self = this;
40         return self.enableSelection( element, 'none' );
41     },
42
43     enableSelection: function (element, value) {
44         return $(element).each( function () {
45                 switch( value ) {
46                         case 'text' :
47                         case 'auto' :
48                         case 'none' :
49                                 val = value;
50                         break;
51
52                         default :
53                                 val = 'auto';
54                         break;
55                 }
56             $(this).css( {
57                         'user-select': val,
58                         '-moz-user-select': val,
59                         '-webkit-user-select': val,
60                         '-o-user-select': val,
61                         '-ms-transform': val
62                         } );
63         } );
64     },
65
66     disableContextMenu: function(element) {
67         $(element).each( function() {
68                 $(this).bind("contextmenu", function( event ) {
69                         return false;
70                 } );
71         } );
72     },
73
74     enableContextMenu: function(element) {
75         $(element).each( function() {
76                 $(this).unbind( "contextmenu" );
77         } );
78     },
79
80     // Get document-relative mouse coordinates from a given event
81     // From: http://www.quirksmode.org/js/events_properties.html#position
82     documentRelativeCoordsFromEvent: function(ev) {
83         var e = ev ? ev : window.event,
84             client = { x: e.clientX, y: e.clientY },
85             page   = { x: e.pageX,   y: e.pageY   },
86             posx = 0,
87             posy = 0;
88
89         // Grab useful coordinates from touch events
90         if (e.type.match(/^touch/)) {
91             page = {
92                 x: e.originalEvent.targetTouches[0].pageX,
93                 y: e.originalEvent.targetTouches[0].pageY
94             };
95             client = {
96                 x: e.originalEvent.targetTouches[0].clientX,
97                 y: e.originalEvent.targetTouches[0].clientY
98             };
99         }
100
101         if (page.x || page.y) {
102             posx = page.x;
103             posy = page.y;
104         }
105         else
106         if (client.x || client.y) {
107             posx = client.x + document.body.scrollLeft + document.documentElement.scrollLeft;
108             posy = client.y + document.body.scrollTop  + document.documentElement.scrollTop;
109         }
110
111         return { x: posx, y: posy };
112     },
113
114         // TODO : offsetX, offsetY. touch events don't have offsetX and offsetY. support for touch devices.
115     // check algorithm...
116     targetRelativeCoordsFromEvent: function(e) {
117         var coords = { x: e.offsetX, y: e.offsetY };
118
119         if (coords.x === undefined || isNaN(coords.x) ||
120             coords.y === undefined || isNaN(coords.y)) {
121             var offset = $(e.target).offset();
122             //coords = documentRelativeCoordsFromEvent(e);      // Old code. Must be checked again.
123             coords = $.mobile.tizen.documentRelativeCoordsFromEvent(e);
124             coords.x -= offset.left;
125             coords.y -= offset.top;
126         }
127
128         return coords;
129     }
130 });
131
132 })();