|
1 | | -import { getWidth, setWidth } from '../../../../core/utils/size'; |
2 | | -import $ from '../../../../core/renderer'; |
3 | | -import { getWindow } from '../../../../core/utils/window'; |
4 | | -const window = getWindow(); |
5 | | -import eventsEngine from '../../events/core/events_engine'; |
6 | | -import { extend } from '../../../../core/utils/extend'; |
7 | | -import resizeCallbacks from '../../../../core/utils/resize_callbacks'; |
8 | | -import { styleProp } from '../../../../core/utils/style'; |
9 | | - |
10 | | -import devices from '../../../../__internal/core/m_devices'; |
11 | | -import domAdapter from '../../../../__internal/core/m_dom_adapter'; |
12 | | -import supportUtils from '../../../../__internal/core/utils/m_support'; |
13 | | - |
14 | | -export const initMobileViewport = function(options) { |
15 | | - options = extend({}, options); |
16 | | - let realDevice = devices.real(); |
17 | | - const allowZoom = options.allowZoom; |
18 | | - const allowPan = options.allowPan; |
19 | | - const allowSelection = ('allowSelection' in options) ? options.allowSelection : realDevice.platform === 'generic'; |
20 | | - |
21 | | - const metaSelector = 'meta[name=viewport]'; |
22 | | - if(!$(metaSelector).length) { |
23 | | - $('<meta>').attr('name', 'viewport').appendTo('head'); |
24 | | - } |
25 | | - |
26 | | - const metaVerbs = ['width=device-width']; |
27 | | - const msTouchVerbs = []; |
28 | | - |
29 | | - if(allowZoom) { |
30 | | - msTouchVerbs.push('pinch-zoom'); |
31 | | - } else { |
32 | | - metaVerbs.push('initial-scale=1.0', 'maximum-scale=1.0, user-scalable=no'); |
33 | | - } |
34 | | - |
35 | | - if(allowPan) { |
36 | | - msTouchVerbs.push('pan-x', 'pan-y'); |
37 | | - } |
38 | | - |
39 | | - if(!allowPan && !allowZoom) { |
40 | | - $('html, body').css({ |
41 | | - 'msContentZooming': 'none', |
42 | | - 'msUserSelect': 'none', |
43 | | - 'overflow': 'hidden' |
44 | | - }); |
45 | | - } else { |
46 | | - $('html').css('msOverflowStyle', '-ms-autohiding-scrollbar'); |
47 | | - } |
48 | | - |
49 | | - if(!allowSelection && supportUtils.supportProp('userSelect')) { |
50 | | - $('.dx-viewport').css(styleProp('userSelect'), 'none'); |
51 | | - } |
52 | | - |
53 | | - $(metaSelector).attr('content', metaVerbs.join()); |
54 | | - $('html').css('msTouchAction', msTouchVerbs.join(' ') || 'none'); |
55 | | - |
56 | | - realDevice = devices.real(); |
| 1 | +import $ from '@js/core/renderer'; |
| 2 | +import devices from '@ts/core/m_devices'; |
| 3 | +import domAdapter from '@ts/core/m_dom_adapter'; |
| 4 | +import { extend } from '@ts/core/utils/m_extend'; |
| 5 | +import resizeCallbacks from '@ts/core/utils/m_resize_callbacks'; |
| 6 | +import { getWidth, setWidth } from '@ts/core/utils/m_size'; |
| 7 | +import { styleProp } from '@ts/core/utils/m_style'; |
| 8 | +import supportUtils from '@ts/core/utils/m_support'; |
| 9 | +import { getWindow } from '@ts/core/utils/m_window'; |
| 10 | +import eventsEngine from '@ts/events/core/m_events_engine'; |
57 | 11 |
|
58 | | - if(supportUtils.touch) { |
59 | | - eventsEngine.off(domAdapter.getDocument(), '.dxInitMobileViewport'); |
60 | | - eventsEngine.on(domAdapter.getDocument(), 'dxpointermove.dxInitMobileViewport', function(e) { |
61 | | - const count = e.pointers.length; |
62 | | - const isTouchEvent = e.pointerType === 'touch'; |
63 | | - const zoomDisabled = !allowZoom && count > 1; |
64 | | - const panDisabled = !allowPan && count === 1 && !e.isScrollingEvent; |
| 12 | +const window = getWindow(); |
65 | 13 |
|
66 | | - if(isTouchEvent && (zoomDisabled || panDisabled)) { |
67 | | - e.preventDefault(); |
68 | | - } |
69 | | - }); |
| 14 | +export const initMobileViewport = function ( |
| 15 | + options: { allowZoom?: boolean; allowPan?: boolean; allowSelection?: boolean }, |
| 16 | +): void { |
| 17 | + // eslint-disable-next-line no-param-reassign |
| 18 | + options = extend({}, options); |
| 19 | + let realDevice = devices.real(); |
| 20 | + const { allowZoom } = options; |
| 21 | + const { allowPan } = options; |
| 22 | + const allowSelection = 'allowSelection' in options ? options.allowSelection : realDevice.platform === 'generic'; |
| 23 | + |
| 24 | + const metaSelector = 'meta[name=viewport]'; |
| 25 | + if (!$(metaSelector).length) { |
| 26 | + // @ts-expect-error |
| 27 | + $('<meta>').attr('name', 'viewport').appendTo('head'); |
| 28 | + } |
| 29 | + |
| 30 | + const metaVerbs = ['width=device-width']; |
| 31 | + const msTouchVerbs = []; |
| 32 | + |
| 33 | + if (allowZoom) { |
| 34 | + // @ts-expect-error |
| 35 | + msTouchVerbs.push('pinch-zoom'); |
| 36 | + } else { |
| 37 | + metaVerbs.push('initial-scale=1.0', 'maximum-scale=1.0, user-scalable=no'); |
| 38 | + } |
| 39 | + |
| 40 | + if (allowPan) { |
| 41 | + // @ts-expect-error |
| 42 | + msTouchVerbs.push('pan-x', 'pan-y'); |
| 43 | + } |
| 44 | + |
| 45 | + if (!allowPan && !allowZoom) { |
| 46 | + $('html, body').css({ |
| 47 | + msContentZooming: 'none', |
| 48 | + msUserSelect: 'none', |
| 49 | + overflow: 'hidden', |
| 50 | + }); |
| 51 | + } else { |
| 52 | + $('html').css('msOverflowStyle', '-ms-autohiding-scrollbar'); |
| 53 | + } |
| 54 | + |
| 55 | + if (!allowSelection && supportUtils.supportProp('userSelect')) { |
| 56 | + $('.dx-viewport').css(styleProp('userSelect'), 'none'); |
| 57 | + } |
| 58 | + |
| 59 | + $(metaSelector).attr('content', metaVerbs.join()); |
| 60 | + $('html').css('msTouchAction', msTouchVerbs.join(' ') || 'none'); |
| 61 | + |
| 62 | + realDevice = devices.real(); |
| 63 | + |
| 64 | + if (supportUtils.touch) { |
| 65 | + eventsEngine.off(domAdapter.getDocument(), '.dxInitMobileViewport'); |
| 66 | + eventsEngine.on(domAdapter.getDocument(), 'dxpointermove.dxInitMobileViewport', (e) => { |
| 67 | + const count = e.pointers.length; |
| 68 | + const isTouchEvent = e.pointerType === 'touch'; |
| 69 | + const zoomDisabled = !allowZoom && count > 1; |
| 70 | + const panDisabled = !allowPan && count === 1 && !e.isScrollingEvent; |
| 71 | + |
| 72 | + if (isTouchEvent && (zoomDisabled || panDisabled)) { |
| 73 | + e.preventDefault(); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + if (realDevice.ios) { |
| 79 | + // @ts-expect-error |
| 80 | + const isPhoneGap = domAdapter.getLocation().protocol === 'file:'; |
| 81 | + |
| 82 | + if (!isPhoneGap) { |
| 83 | + // NOTE: fix app size after device rotation in Safari when keyboard was shown |
| 84 | + resizeCallbacks.add(() => { |
| 85 | + const windowWidth = getWidth(window); |
| 86 | + setWidth($('body'), windowWidth); |
| 87 | + }); |
70 | 88 | } |
71 | | - |
72 | | - if(realDevice.ios) { |
73 | | - const isPhoneGap = (domAdapter.getLocation().protocol === 'file:'); |
74 | | - |
75 | | - if(!isPhoneGap) { |
76 | | - // NOTE: fix app size after device rotation in Safari when keyboard was shown |
77 | | - resizeCallbacks.add(function() { |
78 | | - const windowWidth = getWidth(window); |
79 | | - setWidth($('body'), windowWidth); |
80 | | - }); |
| 89 | + } |
| 90 | + |
| 91 | + if (realDevice.android) { |
| 92 | + resizeCallbacks.add(() => { |
| 93 | + // eslint-disable-next-line no-restricted-globals |
| 94 | + setTimeout(() => { |
| 95 | + const activeElement = domAdapter.getActiveElement(); |
| 96 | + |
| 97 | + // @ts-expect-error |
| 98 | + if (activeElement.scrollIntoViewIfNeeded) { |
| 99 | + // @ts-expect-error |
| 100 | + activeElement.scrollIntoViewIfNeeded(); |
| 101 | + } else { |
| 102 | + activeElement.scrollIntoView(false); |
81 | 103 | } |
82 | | - } |
83 | | - |
84 | | - if(realDevice.android) { |
85 | | - resizeCallbacks.add(function() { |
86 | | - setTimeout(function() { |
87 | | - const activeElement = domAdapter.getActiveElement(); |
88 | | - |
89 | | - activeElement.scrollIntoViewIfNeeded ? |
90 | | - activeElement.scrollIntoViewIfNeeded() : |
91 | | - activeElement.scrollIntoView(false); |
92 | | - }); |
93 | | - }); |
94 | | - } |
| 104 | + }); |
| 105 | + }); |
| 106 | + } |
95 | 107 | }; |
0 commit comments