-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (95 loc) · 2.62 KB
/
index.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
(function () {
var WindowMode = function () {
/*
* A place to store the modes that a user defines
*/
this.modes = [];
/*
* Some sane defaults for most responsive apps
*/
this.init = function () {
this.register({name: 'mobile', width: 767, condition: "<="});
this.register({name: 'tablet', width: 979, condition: "<="});
this.register({name: 'desktop', width: 1199, condition: "<="});
this.register({name: 'big', width: 1200, condition: ">="});
return this;
}
/*
* Register a mode with windowMode
*
* ex.
*
* windowMode.register({name: 'desktop', width: 980, condition: '>='});
*
*/
this.register = function (opts) {
this.modes[opts.width] = {name: opts.name, condition: opts.condition};
return this;
};
/*
* Check the current window size to see what mode we're in
*
* ex.
*
* // returns the name of the mode - e.g. "desktop"
* windowMode.check()
*
* // returns true if the passed in name matches the current mode name
* windowMode.check('mobile')
*
*/
this.check = function (name) {
for(var i in this.modes) {
var mode = this.modes[i];
var condition = mode.condition;
var test;
switch (condition) {
case '<':
test = (window.innerWidth < i);
break;
case '>':
test = (window.innerWidth > i);
break;
case '<=':
test = (window.innerWidth <= i);
break;
case '>=':
test = (window.innerWidth >= i);
break;
case '==':
test = (window.innerWidth == i);
break;
case '===':
test = (window.innerWidth === i);
break;
default:
test = (window.innerWidth >= i);
}
if (test) {
// does this mode match the one we want to check for?
if (name && (mode.name == name)) {
return true;
}
if (name && (mode.name != name)) {
return false;
}
// we're not checking a specific name
// just return the mode that we found
return mode.name;
}
}
// we didn't find any matches
return false;
}
};
var wm = new WindowMode();
// if we've got a window and we don't have a module
// create a global;
if ((typeof window != 'undefined') && (typeof module == 'undefined')) {
window.windowMode = wm;
}
// otherwise, export it.
else {
module.exports = wm;
}
})();