Debian Codegraph @

dojox/mobile/common.js

raw symbols SCIP
1 define([
2 "dojo/_base/array",
3 "dojo/_base/config",
4 "dojo/_base/connect",
5 "dojo/_base/lang",
6 "dojo/_base/window",
7 "dojo/_base/kernel",
8 "dojo/dom",
9 "dojo/dom-class",
10 "dojo/dom-construct",
11 "dojo/domReady",
12 "dojo/ready",
13 "dojo/touch",
14 "dijit/registry",
15 "./sniff",
16 "./uacss" // (no direct references)
17 ], function(array, config, connect, lang, win, kernel, dom, domClass, domConstruct, domReady, ready, touch, registry, has){
18
19 // module:
20 // dojox/mobile/common
21
22 var dm = lang.getObject("dojox.mobile", true);
23
24 // tell dojo/touch to generate synthetic clicks immediately
25 // and regardless of preventDefault() calls on touch events
26 win.doc.dojoClick = true;
27 /// ... but let user disable this by removing dojoClick from the document
28 if(has("touch")){
29 // Do we need to send synthetic clicks when preventDefault() is called on touch events?
30 // This is normally true on anything except Android 4.1+ and IE10+, but users reported
31 // exceptions like Galaxy Note 2. So let's use a has("clicks-prevented") flag, and let
32 // applications override it through data-dojo-config="has:{'clicks-prevented':true}" if needed.
33 has.add("clicks-prevented", !(has("android") >= 4.1 || (has("ie") === 10) || (!has("ie") && has("trident") > 6)));
34 if(has("clicks-prevented")){
35 dm._sendClick = function(target, e){
36 // dojo/touch will send a click if dojoClick is set, so don't do it again.
37 for(var node = target; node; node = node.parentNode){
38 if(node.dojoClick){
39 return;
40 }
41 }
42 var ev = win.doc.createEvent("MouseEvents");
43 ev.initMouseEvent("click", true, true, win.global, 1, e.screenX, e.screenY, e.clientX, e.clientY);
44 target.dispatchEvent(ev);
45 };
46 }
47 }
48
49 dm.getScreenSize = function(){
50 // summary:
51 // Returns the dimensions of the browser window.
52 return {
53 h: win.global.innerHeight || win.doc.documentElement.clientHeight,
54 w: win.global.innerWidth || win.doc.documentElement.clientWidth
55 };
56 };
57
58 dm.updateOrient = function(){
59 // summary:
60 // Updates the orientation specific CSS classes, 'dj_portrait' and
61 // 'dj_landscape'.
62 var dim = dm.getScreenSize();
63 domClass.replace(win.doc.documentElement,
64 dim.h > dim.w ? "dj_portrait" : "dj_landscape",
65 dim.h > dim.w ? "dj_landscape" : "dj_portrait");
66 };
67 dm.updateOrient();
68
69 dm.tabletSize = 500;
70 dm.detectScreenSize = function(/*Boolean?*/force){
71 // summary:
72 // Detects the screen size and determines if the screen is like
73 // phone or like tablet. If the result is changed,
74 // it sets either of the following css class to `<html>`:
75 //
76 // - 'dj_phone'
77 // - 'dj_tablet'
78 //
79 // and it publishes either of the following events:
80 //
81 // - '/dojox/mobile/screenSize/phone'
82 // - '/dojox/mobile/screenSize/tablet'
83
84 var dim = dm.getScreenSize();
85 var sz = Math.min(dim.w, dim.h);
86 var from, to;
87 if(sz >= dm.tabletSize && (force || (!this._sz || this._sz < dm.tabletSize))){
88 from = "phone";
89 to = "tablet";
90 }else if(sz < dm.tabletSize && (force || (!this._sz || this._sz >= dm.tabletSize))){
91 from = "tablet";
92 to = "phone";
93 }
94 if(to){
95 domClass.replace(win.doc.documentElement, "dj_"+to, "dj_"+from);
96 connect.publish("/dojox/mobile/screenSize/"+to, [dim]);
97 }
98 this._sz = sz;
99 };
100 dm.detectScreenSize();
101
102 // dojox/mobile.hideAddressBarWait: Number
103 // The time in milliseconds to wait before the fail-safe hiding address
104 // bar runs. The value must be larger than 800.
105 dm.hideAddressBarWait = typeof(config.mblHideAddressBarWait) === "number" ?
106 config.mblHideAddressBarWait : 1500;
107
108 dm.hide_1 = function(){
109 // summary:
110 // Internal function to hide the address bar.
111 // tags:
112 // private
113 scrollTo(0, 1);
114 dm._hidingTimer = (dm._hidingTimer == 0) ? 200 : dm._hidingTimer * 2;
115 setTimeout(function(){ // wait for a while for "scrollTo" to finish
116 if(dm.isAddressBarHidden() || dm._hidingTimer > dm.hideAddressBarWait){
117 // Succeeded to hide address bar, or failed but timed out
118 dm.resizeAll();
119 dm._hiding = false;
120 }else{
121 // Failed to hide address bar, so retry after a while
122 setTimeout(dm.hide_1, dm._hidingTimer);
123 }
124 }, 50); //50ms is an experiential value
125 };
126
127 dm.hideAddressBar = function(/*Event?*/evt){
128 // summary:
129 // Hides the address bar.
130 // description:
131 // Tries to hide the address bar a couple of times. The purpose is to do
132 // it as quick as possible while ensuring the resize is done after the hiding
133 // finishes.
134 if(dm.disableHideAddressBar || dm._hiding){ return; }
135 dm._hiding = true;
136 dm._hidingTimer = has("ios") ? 200 : 0; // Need to wait longer in case of iPhone
137 var minH = screen.availHeight;
138 if(has('android')){
139 minH = outerHeight / devicePixelRatio;
140 // On some Android devices such as Galaxy SII, minH might be 0 at this time.
141 // In that case, retry again after a while. (200ms is an experiential value)
142 if(minH == 0){
143 dm._hiding = false;
144 setTimeout(function(){ dm.hideAddressBar(); }, 200);
145 }
146 // On some Android devices such as HTC EVO, "outerHeight/devicePixelRatio"
147 // is too short to hide address bar, so make it high enough
148 if(minH <= innerHeight){ minH = outerHeight; }
149 // On Android 2.2/2.3, hiding address bar fails when "overflow:hidden" style is
150 // applied to html/body element, so force "overflow:visible" style
151 if(has('android') < 3){
152 win.doc.documentElement.style.overflow = win.body().style.overflow = "visible";
153 }
154 }
155 if(win.body().offsetHeight < minH){ // to ensure enough height for scrollTo to work
156 win.body().style.minHeight = minH + "px";
157 dm._resetMinHeight = true;
158 }
159 setTimeout(dm.hide_1, dm._hidingTimer);
160 };
161
162 dm.isAddressBarHidden = function(){
163 return pageYOffset === 1;
164 };
165
166 dm.resizeAll = function(/*Event?*/evt, /*Widget?*/root){
167 // summary:
168 // Calls the resize() method of all the top level resizable widgets.
169 // description:
170 // Finds all widgets that do not have a parent or the parent does not
171 // have the resize() method, and calls resize() for them.
172 // If a widget has a parent that has resize(), calling widget's
173 // resize() is its parent's responsibility.
174 // evt:
175 // Native event object
176 // root:
177 // If specified, searches the specified widget recursively for top-level
178 // resizable widgets.
179 // root.resize() is always called regardless of whether root is a
180 // top level widget or not.
181 // If omitted, searches the entire page.
182 if(dm.disableResizeAll){ return; }
183 connect.publish("/dojox/mobile/resizeAll", [evt, root]); // back compat
184 connect.publish("/dojox/mobile/beforeResizeAll", [evt, root]);
185 if(dm._resetMinHeight){
186 win.body().style.minHeight = dm.getScreenSize().h + "px";
187 }
188 dm.updateOrient();
189 dm.detectScreenSize();
190 var isTopLevel = function(w){
191 var parent = w.getParent && w.getParent();
192 return !!((!parent || !parent.resize) && w.resize);
193 };
194 var resizeRecursively = function(w){
195 array.forEach(w.getChildren(), function(child){
196 if(isTopLevel(child)){ child.resize(); }
197 resizeRecursively(child);
198 });
199 };
200 if(root){
201 if(root.resize){ root.resize(); }
202 resizeRecursively(root);
203 }else{
204 array.forEach(array.filter(registry.toArray(), isTopLevel),
205 function(w){ w.resize(); });
206 }
207 connect.publish("/dojox/mobile/afterResizeAll", [evt, root]);
208 };
209
210 dm.openWindow = function(url, target){
211 // summary:
212 // Opens a new browser window with the given URL.
213 win.global.open(url, target || "_blank");
214 };
215
216 dm._detectWindowsTheme = function(){
217 // summary:
218 // Detects if the "windows" theme is used,
219 // if it is used, set has("windows-theme") and
220 // add the .windows_theme class on the document.
221
222 // Avoid unwanted (un)zoom on some WP8 devices (at least Nokia Lumia 920)
223 if(navigator.userAgent.match(/IEMobile\/10\.0/)){
224 domConstruct.create("style",
225 {innerHTML: "@-ms-viewport {width: auto !important}"}, win.doc.head);
226 }
227
228 var setWindowsTheme = function(){
229 domClass.add(win.doc.documentElement, "windows_theme");
230 kernel.experimental("Dojo Mobile Windows theme", "Behavior and appearance of the Windows theme are experimental.");
231 };
232
233 // First see if the "windows-theme" feature has already been set explicitly
234 // in that case skip aut-detect
235 var windows = has("windows-theme");
236 if(windows !== undefined){
237 if(windows){
238 setWindowsTheme();
239 }
240 return;
241 }
242
243 // check css
244 var i, j;
245
246 var check = function(href){
247 // TODO: find a better regexp to match?
248 if(href && href.indexOf("/windows/") !== -1){
249 has.add("windows-theme", true);
250 setWindowsTheme();
251 return true;
252 }
253 return false;
254 };
255
256 // collect @import
257 var s = win.doc.styleSheets;
258 for(i = 0; i < s.length; i++){
259 if(s[i].href){ continue; }
260 var r = s[i].cssRules || s[i].imports;
261 if(!r){ continue; }
262 for(j = 0; j < r.length; j++){
263 if(check(r[j].href)){
264 return;
265 }
266 }
267 }
268
269 // collect <link>
270 var elems = win.doc.getElementsByTagName("link");
271 for(i = 0; i < elems.length; i++){
272 if(check(elems[i].href)){
273 return;
274 }
275 }
276 };
277
278 if(config.mblApplyPageStyles !== false){
279 domClass.add(win.doc.documentElement, "mobile");
280 }
281 if(has('chrome')){
282 // dojox/mobile does not load uacss (only _compat does), but we need dj_chrome.
283 domClass.add(win.doc.documentElement, "dj_chrome");
284 }
285
286 if(win.global._no_dojo_dm){
287 // deviceTheme seems to be loaded from a script tag (= non-dojo usage)
288 var _dm = win.global._no_dojo_dm;
289 for(var i in _dm){
290 dm[i] = _dm[i];
291 }
292 dm.deviceTheme.setDm(dm);
293 }
294
295 // flag for Android transition animation flicker workaround
296 has.add('mblAndroidWorkaround',
297 config.mblAndroidWorkaround !== false && has('android') < 3, undefined, true);
298 has.add('mblAndroid3Workaround',
299 config.mblAndroid3Workaround !== false && has('android') >= 3, undefined, true);
300
301 dm._detectWindowsTheme();
302
303 dm.setSelectable = function(/*Node*/node, /*Boolean*/selectable){
304 var nodes, i;
305 node = dom.byId(node);
306 if (has("ie") <= 9){
307 // (IE < 10) Fall back to setting/removing the
308 // unselectable attribute on the element and all its children
309 // except the input element (see https://bugs.dojotoolkit.org/ticket/13846)
310 nodes = node.getElementsByTagName("*");
311 i = nodes.length;
312 if(selectable){
313 node.removeAttribute("unselectable");
314 while(i--){
315 nodes[i].removeAttribute("unselectable");
316 }
317 }else{
318 node.setAttribute("unselectable", "on");
319 while(i--){
320 if (nodes[i].tagName !== "INPUT"){
321 nodes[i].setAttribute("unselectable", "on");
322 }
323 }
324 }
325 }else{
326 domClass.toggle(node, "unselectable", !selectable);
327 }
328 };
329
330 var touchActionProp = has("pointer-events") ? "touchAction" : has("MSPointer") ? "msTouchAction" : null;
331 dm._setTouchAction = touchActionProp ? function(/*Node*/node, /*Boolean*/value){
332 node.style[touchActionProp] = value;
333 } : function(){};
334
335 // Set the background style using dojo/domReady, not dojo/ready, to ensure it is already
336 // set at widget initialization time. (#17418)
337 domReady(function(){
338 if(config.mblApplyPageStyles !== false){
339 domClass.add(win.body(), "mblBackground");
340 }
341 });
342
343 ready(function(){
344 dm.detectScreenSize(true);
345 if(config.mblAndroidWorkaroundButtonStyle !== false && has('android')){
346 // workaround for the form button disappearing issue on Android 2.2-4.0
347 domConstruct.create("style", {innerHTML:"BUTTON,INPUT[type='button'],INPUT[type='submit'],INPUT[type='reset'],INPUT[type='file']::-webkit-file-upload-button{-webkit-appearance:none;} audio::-webkit-media-controls-play-button,video::-webkit-media-controls-play-button{-webkit-appearance:media-play-button;} video::-webkit-media-controls-fullscreen-button{-webkit-appearance:media-fullscreen-button;}"}, win.doc.head, "first");
348 }
349 if(has('mblAndroidWorkaround')){
350 // add a css class to show view offscreen for android flicker workaround
351 domConstruct.create("style", {innerHTML:".mblView.mblAndroidWorkaround{position:absolute;top:-9999px !important;left:-9999px !important;}"}, win.doc.head, "last");
352 }
353
354 var f = dm.resizeAll;
355 // Address bar hiding
356 var isHidingPossible =
357 navigator.appVersion.indexOf("Mobile") != -1 && // only mobile browsers
358 // #17455: hiding Safari's address bar works in iOS < 7 but this is
359 // no longer possible since iOS 7. Hence, exclude iOS 7 and later:
360 !(has("ios") >= 7);
361 // You can disable the hiding of the address bar with the following dojoConfig:
362 // var dojoConfig = { mblHideAddressBar: false };
363 // If unspecified, the flag defaults to true.
364 if((config.mblHideAddressBar !== false && isHidingPossible) ||
365 config.mblForceHideAddressBar === true){
366 dm.hideAddressBar();
367 if(config.mblAlwaysHideAddressBar === true){
368 f = dm.hideAddressBar;
369 }
370 }
371
372 var ios6 = has("ios") >= 6; // Full-screen support for iOS6 or later
373 if((has('android') || ios6) && win.global.onorientationchange !== undefined){
374 var _f = f;
375 var curSize, curClientWidth, curClientHeight;
376 if(ios6){
377 curClientWidth = win.doc.documentElement.clientWidth;
378 curClientHeight = win.doc.documentElement.clientHeight;
379 }else{ // Android
380 // Call resize for the first resize event after orientationchange
381 // because the size information may not yet be up to date when the
382 // event orientationchange occurs.
383 f = function(evt){
384 var _conn = connect.connect(null, "onresize", null, function(e){
385 connect.disconnect(_conn);
386 _f(e);
387 });
388 };
389 curSize = dm.getScreenSize();
390 };
391 // Android: Watch for resize events when the virtual keyboard is shown/hidden.
392 // The heuristic to detect this is that the screen width does not change
393 // and the height changes by more than 100 pixels.
394 //
395 // iOS >= 6: Watch for resize events when entering or existing the new iOS6
396 // full-screen mode. The heuristic to detect this is that clientWidth does not
397 // change while the clientHeight does change.
398 connect.connect(null, "onresize", null, function(e){
399 if(ios6){
400 var newClientWidth = win.doc.documentElement.clientWidth,
401 newClientHeight = win.doc.documentElement.clientHeight;
402 if(newClientWidth == curClientWidth && newClientHeight != curClientHeight){
403 // full-screen mode has been entered/exited (iOS6)
404 _f(e);
405 }
406 curClientWidth = newClientWidth;
407 curClientHeight = newClientHeight;
408 }else{ // Android
409 var newSize = dm.getScreenSize();
410 if(newSize.w == curSize.w && Math.abs(newSize.h - curSize.h) >= 100){
411 // keyboard has been shown/hidden (Android)
412 _f(e);
413 }
414 curSize = newSize;
415 }
416 });
417 }
418
419 connect.connect(null, win.global.onorientationchange !== undefined
420 ? "onorientationchange" : "onresize", null, f);
421 win.body().style.visibility = "visible";
422 });
423
424 // TODO: return functions declared above in this hash, rather than
425 // dojox.mobile.
426
427 /*=====
428 return {
429 // summary:
430 // A common module for dojox/mobile.
431 // description:
432 // This module includes common utility functions that are used by
433 // dojox/mobile widgets. Also, it provides functions that are commonly
434 // necessary for mobile web applications, such as the hide address bar
435 // function.
436 };
437 =====*/
438 return dm;
439 });