-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbreakpoints.js
165 lines (135 loc) · 3.1 KB
/
breakpoints.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* breakpoints.js
* o2web.ca
* 2015
* GPL v2 License
*/
// AMD MODULE LOADER DEFINITION
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// Define AMD module
define(['jquery', 'raf'], factory);
} else {
// JQUERY INIT
factory(jQuery);
}
}
// MAIN CODE
(this, function($){
var app = this;
if(!app.breakpoints) app.breakpoints = {
init: {},
resize: {},
event: 'afterwindowresize',
rafref: false
};
//
//
// BP object
Breakpoints = function(args, options){
var self = this;
//
//
// add breakpoint
this.add = function(minMax, width, callbacks, data, initial){
initial = !!initial;
var type = initial ? 'init' : 'resize';
if(typeof callbacks != 'object') callbacks = [callbacks];
// return if no width or no callbacks are defined
if(!width || !callbacks.length) return;
var bpWidth = width.toString();
// check if BP exists
if(!app.breakpoints[type][bpWidth]){
app.breakpoints[type][bpWidth] = {
active: false,
width: width,
min: [],
max: []
}
}
// push callbacks
for(var i=0; i<callbacks.length; i++){
if(typeof callbacks[i] == 'function'){
app.breakpoints[type][bpWidth][minMax].push({
callback: callbacks[i],
data: data
});
}
}
// trigger init callbacks right now if already initiated
if(initial && app.breakpoints.initiated) self.checkBreakpoints(true);
// hook resize loop
if(!app.breakpoints.rafref){
app.breakpoints.rafref = app.raf.on(app.breakpoints.event, self.checkBreakpoints).ref;
}
}
//
//
// SHORTHANDS
// min-width shorthand
this.min = function(width, callbacks, data){
self.add('min', width, callbacks, data);
}
// max-width shorthand
this.max = function(width, callbacks, data){
self.add('max', width, callbacks, data);
}
// initial BPs
this.initial = {
min: function(width, callbacks, data){
self.add('min', width, callbacks, data, true);
},
max: function(width, callbacks, data){
self.add('max', width, callbacks, data, true);
}
}
//
//
//
this.checkBreakpoints = function(init){
init = init === true;
var bps = app.breakpoints[init ? 'init' : 'resize' ];
var winWidth = app.innerWidth;
for(var bpKey in bps){
var bp = bps[bpKey];
if((init||!bp.active) && winWidth<=bp.width){
bp.active = true;
if(bp.max.length){
for(var i=0; i<bp.max.length; i++){
bp.max[i].callback(bp.max[i].data);
}
}
}
if((init||bp.active) && winWidth>bp.width){
bp.active = false;
if(bp.min.length){
for(var i=0; i<bp.min.length; i++){
bp.min[i].callback(bp.min[i].data);
}
}
}
}
// clear inital breakpoints
if(init){
for(var bpKey in bps){
if(app.breakpoints.resize[bpKey]){
app.breakpoints.resize[bpKey].active = bps[bpKey].active;
}
}
app.breakpoints.init = [];
app.breakpoints.initiated = true;
}
}
}
//
//
//
//
//
//
// INIT
$.breakpoints = new Breakpoints();
$(document).ready(function(){
$.breakpoints.checkBreakpoints(true);
});
}));