1 | /* |
2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
3 | |
4 | This program is free software; |
5 | you can redistribute it and/or modify it under the terms of the |
6 | GNU General Public License as published by the Free Software Foundation; |
7 | either version 3 of the License, or any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | /* |
19 | Changes: |
20 | - v4.0.0: |
21 | - created from v3.6.0 |
22 | Doc reviewed 202208 |
23 | */ |
24 | |
25 | import { ZERO, ONE } from '../../main/Constants.js'; |
26 | |
27 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
28 | /** |
29 | A simple class used to detect if the device have a touch screen |
30 | */ |
31 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
32 | |
33 | class Device { |
34 | |
35 | /** |
36 | A flag withe touch status |
37 | @type {Boolean} |
38 | */ |
39 | |
40 | static #isTouch; |
41 | |
42 | /** |
43 | A touchstart event listener |
44 | */ |
45 | |
46 | static #touchStartEL ( ) { |
47 | Device.#isTouch = true; |
48 | document.removeEventListener ( 'touchstart', Device.#touchStartEL, true ); |
49 | } |
50 | |
51 | /** |
52 | The constructor |
53 | */ |
54 | |
55 | constructor ( ) { |
56 | Object.freeze ( this ); |
57 | Device.#isTouch = false; |
58 | document.addEventListener ( 'touchstart', Device.#touchStartEL, true ); |
59 | } |
60 | |
61 | /** |
62 | The touch status. True when the device have a touch screen |
63 | @type {Boolean} |
64 | */ |
65 | |
66 | get isTouch ( ) { return Device.#isTouch; } |
67 | |
68 | /** |
69 | get the width and height avaiable for menus and dialogs |
70 | @type {Object} a read only object with width and height properties |
71 | */ |
72 | |
73 | get screenAvailable ( ) { |
74 | |
75 | const testHTMLElement = document.createElement ( 'div' ); |
76 | testHTMLElement.style.position = 'absolute'; |
77 | testHTMLElement.style.bottom = ZERO; |
78 | testHTMLElement.style.right = ZERO; |
79 | testHTMLElement.style.height = ONE; |
80 | testHTMLElement.style.width = ONE; |
81 | document.body.appendChild ( testHTMLElement ); |
82 | const boundingClientRect = testHTMLElement.getBoundingClientRect ( ); |
83 | const screenAvailableHeight = boundingClientRect.bottom; |
84 | const screenAvailableWidth = boundingClientRect.right; |
85 | document.body.removeChild ( testHTMLElement ); |
86 | |
87 | return Object.freeze ( |
88 | { |
89 | height : screenAvailableHeight, |
90 | width : screenAvailableWidth |
91 | } |
92 | ); |
93 | } |
94 | } |
95 | |
96 | /** |
97 | The one and only one instance of Device class |
98 | */ |
99 | |
100 | const theDevice = new Device ( ); |
101 | |
102 | export default theDevice; |
103 | |
104 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
105 |