-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediawatcher.js
142 lines (114 loc) · 4.81 KB
/
mediawatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* @license MIT
* cinetech (muvisho)
* Copyright (c) 2023 Abraham Ukachi. The Muvisho Project Contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @name: Media Watcher
* @type: helper
* @author: Abraham Ukachi <abraham.ukachi@laplateforme.io>
*
* A basic media-query watcher that calls two separate callbacks (narrow & wide) whenever the screen changes
* relative to the specified *breakpoint*.
*
* Example usage:
*
* 1-|> import { installMediaQueryWatcher } from './src/helpers/mediawatcher.js';
* -|>
* -|> class App extends Engine {
* -|> ...
* -|> firstUpdated() {
* -|> installMediaQueryWatcher(this, 460,
* -|> (firstNarrowQuery) => this._handleNarrowLayout(firstNarrowQuery),
* -|> (firstWideQuery) => this._handleWideLayout(firstWideQuery)
* -|> );
* -|> }
* -|> }
* -|>
*
* ?-|> // TODO: Make this watcher calls `onNarrowLayout()` and `onWideLayout()` methods of the given `controller`;
* -|> ...
* -|> installMediaQueryWatcher(this, 460);
* -|> ...
* -|> onNarrowLayout() { console.log(`our screen is narrow`) }
* -|> ...
* -|> onWideLayout() { console.log(`our screen is wide`) }
* -|>
*/
"use strict";
// ^^^^^^^^^ This keeps us on our toes, as it forces us to use all pre-defined variables, among other things 😅
// defining some constant variables...
/**
* Method used to handle the media query `matches`
*
* @param { Boolean } matches
* @param { Function } narrowLayoutCallback
* @param { Function } wideLayoutCallback
* @param { Boolean } firstQuery
*/
const handleMediaMatches = (matches, narrowLayoutCallback, wideLayoutCallback, firstQuery) => {
// if the data matches (ie. width is more than the breakpoint)...
if (matches) {
// if theres a wide layout callback or function...
if (typeof(wideLayoutCallback) == 'function') {
// run the wide layout callback function w/ the query data
wideLayoutCallback(firstQuery);
}
// DEBUG [4dbsmaster]: tell me about it :)
console.log(`\x1b[35m[_handleMediaMatches](1):\x1b[0m \
typeof(wideLayoutCallback) => ${typeof(wideLayoutCallback)}`);
} else {
// if theres a narrow layout callback or function...
if (typeof(narrowLayoutCallback) == 'function') {
// run the narrow layout callback function w/ the query data
narrowLayoutCallback(firstQuery);
}
// DEBUG [4dbsmaster]: tell me about it :)
console.log(`\x1b[35m[_handleMediaMatches](2):\x1b[0m \
typeof(narrowLayoutCallback) => ${typeof(narrowLayoutCallback)}`);
}
// DEBUG [4dbsmaster]: tell me about it :)
console.log(`\x1b[35m[_handleMediaMatches](3):\x1b[0m firstQuery => ${firstQuery}`);
}; // <- end of `handleMediaMatches()`
/**
* `installMediaQueryWatcher`
*
* Example usage:
* (See above examples)
*
* @param { Object } controller
* @param { Number } breakpoint
* @param { Function } narrowLayoutCallback
* @param { Function } wideLayoutCallback
*/
export const installMediaQueryWatcher = (controller, breakpoint, narrowLayoutCallback, wideLayoutCallback) => {
// Create a width media query with the given `breakpoint` as `widthMediaQuery`
let widthMediaQuery = window.matchMedia(`(min-width: ${breakpoint}px)`);
// Handle the media query matches
handleMediaMatches(widthMediaQuery.matches, narrowLayoutCallback, wideLayoutCallback, true);
// Add a listener to `widthMediaQuery`
widthMediaQuery.addListener((data) => {
handleMediaMatches(data.matches, narrowLayoutCallback, wideLayoutCallback, false);
// DEBUG [4dbsmaster]: tell me about it :)
console.log(`\x1b[34m[installMediaQueryWatcher](2):\x1b[0m data => `, data);
});
// DEBUG [4dbsmaster]: tell me about it :)
console.log(`\x1b[34m[installMediaQueryWatcher](1):\x1b[0m widthMediaQuery => `, widthMediaQuery);
};