forked from brunosimon/burno.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
breakpoints.class.js
306 lines (265 loc) · 8.25 KB
/
breakpoints.class.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* @class Breakpoints
* @author Bruno SIMON / http://bruno-simon.com
* @fires update
* @fires change
* @requires B.Tools.Viewport
*/
B.Tools.Breakpoints = B.Core.Event_Emitter.extend(
{
static : 'breakpoints',
options :
{
breakpoints : []
},
/**
* Initialise and merge options
* @constructor
* @param {object} options Properties to merge with defaults
*/
construct : function( options )
{
this._super( options );
// Set up
this.viewport = new B.Tools.Viewport();
this.all = {};
this.actives = {};
this.first_test = true;
// Initial breakpoints
this.add( this.options.breakpoints );
// Init
this.init_events();
},
/**
* Listen to events
* @return {object} Context
*/
init_events : function()
{
var that = this;
// Viewport resize event
this.viewport.on( 'resize', function()
{
// Test breakpoints
that.test();
} );
return this;
},
/**
* Add one breakpoint
* @param {object} breakpoint Breakpoint informations
* @return {object} Context
* @example
* add( {
* name : 'large',
* width :
* {
* value : 960,
* extreme : 'min',
* included : false
* }
* } )
* @example
* add_breakpoints( [
* {
* name : 'large',
* width :
* {
* value : 960,
* extreme : 'min',
* included : false
* }
* },
* {
* name : 'medium',
* width :
* {
* value : 960,
* extreme : 'max',
* included : true
* }
* },
* {
* name : 'small',
* width :
* {
* value : 500,
* extreme : 'max',
* included : true
* },
* height :
* {
* value : 500,
* extreme : 'max',
* included : true
* }
* }
* ] )
*
*/
add : function( breakpoints, silent )
{
// Default
silent = typeof silent === 'undefined' ? true : false;
// Force array
if( !( breakpoints instanceof Array ) )
breakpoints = [ breakpoints ];
// Add each one to breakpoints
for( var i = 0; i < breakpoints.length; i++ )
{
var breakpoint = breakpoints[ i ];
this.all[ breakpoint.name ] = breakpoint;
}
// Test breakpoints
if( !silent )
this.test();
return this;
},
/**
* Remove one breakpoint
* @param {string} breakpoint Breakpoint name (can be the breakpoint object itself)
* @return {object} Context
*/
remove : function( breakpoints, silent )
{
// Force array
if( !( breakpoints instanceof Array ) )
breakpoints = [ breakpoints ];
// Object breakpoint
if( typeof breakpoint === 'object' && typeof breakpoint.name === 'string' )
breakpoint = breakpoint.name;
// Default
silent = typeof silent === 'undefined' ? false : true;
// Add each one to breakpoints
for( var i = 0; i < breakpoints.length; i++ )
{
delete this.all[ breakpoints[ i ] ];
}
// Test breakpoints
if( !silent )
this.test();
return this;
},
/**
* Test every breakpoint and trigger 'update' event if current breakpoint changed
* @return {object} Context
*/
test : function()
{
// Set up
var current_breakpoints = {},
all_names = Object.keys( this.all );
// Each breakpoint
for( var i = 0, len = all_names.length; i < len; i++ )
{
// Set up
var breakpoint = this.all[ all_names[ i ] ],
width = !breakpoint.width,
height = !breakpoint.height;
// Width must be tested
if( !width )
{
// Min
if( breakpoint.width.extreme === 'min' )
{
if(
// Included
( breakpoint.width.included && this.viewport.width >= breakpoint.width.value ) ||
// Not included
( !breakpoint.width.included && this.viewport.width > breakpoint.width.value )
)
width = true;
}
// Max
else
{
if(
// Included
( breakpoint.width.included && this.viewport.width <= breakpoint.width.value ) ||
// Not included
( !breakpoint.width.included && this.viewport.width < breakpoint.width.value )
)
width = true;
}
}
// Height must be tested
if( !height )
{
// Min
if( breakpoint.height.extreme === 'min' )
{
if(
// Included
( breakpoint.height.included && this.viewport.height >= breakpoint.height.value ) ||
// Not included
( !breakpoint.height.included && this.viewport.height > breakpoint.height.value )
)
height = true;
}
// Max
else
{
if(
// Included
( breakpoint.height.included && this.viewport.height <= breakpoint.height.value ) ||
// Not included
( !breakpoint.height.included && this.viewport.height < breakpoint.height.value )
)
height = true;
}
}
if( width && height )
{
current_breakpoints[ breakpoint.name ] = breakpoint;
}
}
// Set up
var current_names = Object.keys( current_breakpoints ),
old_names = Object.keys( this.actives ),
difference = this.get_arrays_differences( current_names, old_names );
if( difference.length || this.first_test )
{
// Set actives
this.actives = current_breakpoints;
this.first_test = false;
// Trigger
this.trigger( 'update change', [ this.actives ] );
}
return this;
},
/**
* Test if breakpoint is active
* @param {string} breakpoint Breakpoint name (can be the breakpoint object itself)
* @return {boolean} True or false depending on if the breakpoint is active
*/
is_active : function( breakpoint )
{
// Object breakpoint
if( typeof breakpoint === 'object' && typeof breakpoint.name === 'string' )
breakpoint = breakpoint.name;
return typeof this.actives[ breakpoint ] !== 'undefined';
},
/**
* Get differences between two arrays
* @param {array} a First array
* @param {array} b Second array
* @return {array} Items in one but not in the other
*/
get_arrays_differences : function( a, b )
{
var a_new = [],
diff = [];
for( var i = 0; i < a.length; i++ )
a_new[ a[ i ] ] = true;
for( i = 0; i < b.length; i++ )
{
if( a_new[ b[ i ] ] )
delete a_new[ b[ i ] ];
else
a_new[ b[ i ] ] = true;
}
for( var k in a_new )
diff.push( k );
return diff;
}
} );