PageSourceSearch

https://www.rummeles.com/blog/assets/plugins/header/slidebar.js

js rummeles.com collected 2026-10-05 11:40:09 UTC 11,016 bytes, 471 lines download raw bytes

vendor: 11,016 bytes, lines 1-471
1/*!
2 * Slidebars - A jQuery Framework for Off-Canvas Menus and Sidebars
3 * Version: 2 Development
4 * Url: http://www.adchsm.com/slidebars/
5 * Author: Adam Charles Smith
6 * Author url: http://www.adchsm.com/
7 * License: MIT
8 * License url: http://www.adchsm.com/slidebars/license/
9 */
10
11var slidebars = function () {
12
13	/**
14	 * Setup
15	 */
16
17	// Cache all canvas elements
18	var canvas = $( '[data-canvas]' ),
19
20	// Object of Slidebars
21	offCanvas = {},
22
23	// Variables, permitted sides and styles
24	init = false,
25	registered = false,
26	sides = [ 'top', 'right', 'bottom', 'left' ],
27	styles = [ 'reveal', 'push', 'overlay', 'shift' ],
28
29	/**
30	 * Get Animation Properties
31	 */
32
33	getAnimationProperties = function ( id ) {
34		// Variables
35		var elements = $(),
36		amount = '0px, 0px',
37		duration = parseFloat( offCanvas[ id ].element.css( 'transitionDuration' ), 10 ) * 1000;
38
39		// Elements to animate
40		if ( offCanvas[ id ].style === 'reveal' || offCanvas[ id ].style === 'push' || offCanvas[ id ].style === 'shift' ) {
41			elements = elements.add( canvas );
42		}
43
44		if ( offCanvas[ id ].style === 'push' || offCanvas[ id ].style === 'overlay' || offCanvas[ id ].style === 'shift' ) {
45			elements = elements.add( offCanvas[ id ].element );
46		}
47
48		// Amount to animate
49		if ( offCanvas[ id ].active ) {
50			if ( offCanvas[ id ].side === 'top' ) {
51				amount = '0px, ' + offCanvas[ id ].element.css( 'height' );
52			} else if ( offCanvas[ id ].side === 'right' ) {
53				amount = '-' + offCanvas[ id ].element.css( 'width' ) + ', 0px';
54			} else if ( offCanvas[ id ].side === 'bottom' ) {
55				amount = '0px, -' + offCanvas[ id ].element.css( 'height' );
56			} else if ( offCanvas[ id ].side === 'left' ) {
57				amount = offCanvas[ id ].element.css( 'width' ) + ', 0px';
58			}
59		}
60
61		// Return animation properties
62		return { 'elements': elements, 'amount': amount, 'duration': duration };
63	},
64
65	/**
66	 * Slidebars Registration
67	 */
68
69	registerSlidebar = function ( id, side, style, element ) {
70		// Check if Slidebar is registered
71		if ( isRegisteredSlidebar( id ) ) {
72			throw "Error registering Slidebar, a Slidebar with id '" + id + "' already exists.";
73		}
74
75		// Register the Slidebar
76		offCanvas[ id ] = {
77			'id': id,
78			'side': side,
79			'style': style,
80			'element': element,
81			'active': false
82		};
83	},
84
85	isRegisteredSlidebar = function ( id ) {
86		// Return if Slidebar is registered
87		if ( offCanvas.hasOwnProperty( id ) ) {
88			return true;
89		} else {
90			return false;
91		}
92	};
93
94	/**
95	 * Initialization
96	 */
97
98	this.init = function ( callback ) {
99		// Check if Slidebars has been initialized
100		if ( init ) {
101			throw "Slidebars has already been initialized.";
102		}
103
104		// Loop through and register Slidebars
105		if ( ! registered ) {
106			$( '[data-off-canvas]' ).each( function () {
107				// Get Slidebar parameters
108				var parameters = $( this ).data( 'off-canvas' ).split( ' ', 3 );
109				//console.log(parameters);
110				// Make sure a valid id, side and style are specified
111				if ( ! parameters || ! parameters[ 0 ] || sides.indexOf( parameters[ 1 ] ) === -1 || styles.indexOf( parameters[ 2 ] ) === -1 ) {
112					throw "Error registering Slidebar, please specifiy a valid id, side and style'.";
113				}
114
115				// Register Slidebar
116				registerSlidebar( parameters[ 0 ], parameters[ 1 ], parameters[ 2 ], $( this ) );
117			} );
118
119			// Set registered variable
120			registered = true;
121		}
122
123		// Set initialized variable
124		init = true;
125
126		// Set CSS
127		this.css();
128
129		// Trigger event
130		$( events ).trigger( 'init' );
131
132		// Run callback
133		if ( typeof callback === 'function' ) {
134			callback();
135		}
136	};
137
138	this.exit = function ( callback ) {
139		// Check if Slidebars has been initialized
140		if ( ! init ) {
141			throw "Slidebars hasn't been initialized.";
142		}
143
144		// Exit
145		var exit = function () {
146			// Set init variable
147			init = false;
148
149			// Trigger event
150			$( events ).trigger( 'exit' );
151
152			// Run callback
153			if ( typeof callback === 'function' ) {
154				callback();
155			}
156		};
157
158		// Call exit, close open Slidebar if active
159		if ( this.getActiveSlidebar() ) {
160			this.close( exit );
161		} else {
162			exit();
163		}
164	};
165
166	/**
167	 * CSS
168	 */
169
170	this.css = function ( callback ) {
171		// Check if Slidebars has been initialized
172		if ( ! init ) {
173			throw "Slidebars hasn't been initialized.";
174		}
175
176		// Loop through Slidebars to set negative margins
177		for ( var id in offCanvas ) {
178			// Check if Slidebar is registered
179			if ( isRegisteredSlidebar( id ) ) {
180				// Calculate offset
181				var offset;
182
183				if ( offCanvas[ id ].side === 'top' || offCanvas[ id ].side === 'bottom' ) {
184					offset = offCanvas[ id ].element.css( 'height' );
185				} else {
186					offset = offCanvas[ id ].element.css( 'width' );
187				}
188
189				// Apply negative margins
190				if ( offCanvas[ id ].style === 'push' || offCanvas[ id ].style === 'overlay' || offCanvas[ id ].style === 'shift' ) {
191					offCanvas[ id ].element.css( 'margin-' + offCanvas[ id ].side, '-' + offset );
192				}
193			}
194		}
195
196		// Reposition open Slidebars
197		if ( this.getActiveSlidebar() ) {
198			this.open( this.getActiveSlidebar() );
199		}
200
201		// Trigger event
202		$( events ).trigger( 'css' );
203
204		// Run callback
205		if ( typeof callback === 'function' ) {
206			callback();
207		}
208	};
209
210	/**
211	 * Controls
212	 */
213
214	this.open = function ( id, callback ) {
215		// Check if Slidebars has been initialized
216		if ( ! init ) {
217			throw "Slidebars hasn't been initialized.";
218		}
219
220		// Check if id wasn't passed
221		if ( ! id ) {
222			throw "You must pass a Slidebar id.";
223		}
224
225		// Check if Slidebar is registered
226		if ( ! isRegisteredSlidebar( id ) ) {
227			throw "Error opening Slidebar, there is no Slidebar with id '" + id + "'.";
228		}
229
230		// Open
231		var open = function () {
232			// Set active state to true
233			offCanvas[ id ].active = true;
234
235			// Display the Slidebar
236			offCanvas[ id ].element.css( 'display', 'block' );
237
238			// Trigger event
239			$( events ).trigger( 'opening', [ offCanvas[ id ].id ] );
240
241			// Get animation properties
242			var animationProperties = getAnimationProperties( id );
243
244			// Apply css
245			animationProperties.elements.css( {
246				'transition-duration': animationProperties.duration + 'ms',
247				'transform': 'translate(' + animationProperties.amount + ')'
248			} );
249
250			// Transition completed
251			setTimeout( function () {
252				// Trigger event
253				$( events ).trigger( 'opened', [ offCanvas[ id ].id ] );
254
255				// Run callback
256				if ( typeof callback === 'function' ) {
257					callback();
258				}
259			}, animationProperties.duration );
260		};
261
262		// Call open, close open Slidebar if active
263		if ( this.getActiveSlidebar() && this.getActiveSlidebar() !== id ) {
264			this.close( open );
265		} else {
266			open();
267		}
268	};
269
270	this.close = function ( id, callback ) {
271		// Shift callback arguments
272		if ( typeof id === 'function' ) {
273			callback = id;
274			id = null;
275		}
276
277		// Check if Slidebars has been initialized
278		if ( ! init ) {
279			throw "Slidebars hasn't been initialized.";
280		}
281
282		// Check if id was passed but isn't a registered Slidebar
283		if ( id && ! isRegisteredSlidebar( id ) ) {
284			throw "Error closing Slidebar, there is no Slidebar with id '" + id + "'.";
285		}
286
287		// If no id was passed, get the active Slidebar
288		if ( ! id ) {
289			id = this.getActiveSlidebar();
290		}
291
292		// Close a Slidebar
293		if ( id && offCanvas[ id ].active ) {
294			// Set active state to false
295			offCanvas[ id ].active = false;
296
297			// Trigger event
298			$( events ).trigger( 'closing', [ offCanvas[ id ].id ] );
299
300			// Get animation properties
301			var animationProperties = getAnimationProperties( id );
302
303			// Apply css
304			animationProperties.elements.css( 'transform', '' );
305
306			// Transition completetion
307			setTimeout( function () {
308				// Remove transition duration
309				animationProperties.elements.css( 'transition-duration', '' );
310
311				// Hide the Slidebar
312				offCanvas[ id ].element.css( 'display', '' );
313
314				// Trigger event
315				$( events ).trigger( 'closed', [ offCanvas[ id ].id ] );
316
317				// Run callback
318				if ( typeof callback === 'function' ) {
319					callback();
320				}
321			}, animationProperties.duration );
322		}
323	};
324
325	this.toggle = function ( id, callback ) {
326		// Check if Slidebars has been initialized
327		if ( ! init ) {
328			throw "Slidebars hasn't been initialized.";
329		}
330
331		// Check if id wasn't passed
332		if ( ! id ) {
333			throw "You must pass a Slidebar id.";
334		}
335
336		// Check if Slidebar is registered
337		if ( ! isRegisteredSlidebar( id ) ) {
338			throw "Error toggling Slidebar, there is no Slidebar with id '" + id + "'.";
339		}
340
341		// Check Slidebar state
342		if ( offCanvas[ id ].active ) {
343			// It's open, close it
344			this.close( id, function () {
345				// Run callback
346				if ( typeof callback === 'function' ) {
347					callback();
348				}
349			} );
350		} else {
351			// It's closed, open it
352			this.open( id, function () {
353				// Run callback
354				if ( typeof callback === 'function' ) {
355					callback();
356				}
357			} );
358		}
359	};
360
361	/**
362	 * Active States
363	 */
364
365	this.isActive = function () {
366		// Return init state
367		return init;
368	};
369
370	this.isActiveSlidebar = function ( id ) {
371		// Check if Slidebars has been initialized
372		if ( ! init ) {
373			throw "Slidebars hasn't been initialized.";
374		}
375
376		// Check if id wasn't passed
377		if ( ! id ) {
378			throw "You must provide a Slidebar id.";
379		}
380
381		// Check if Slidebar is registered
382		if ( ! isRegisteredSlidebar( id ) ) {
383			throw "Error retrieving Slidebar, there is no Slidebar with id '" + id + "'.";
384		}
385
386		// Return the active state
387		return offCanvas[ id ].active;
388	};
389
390	this.getActiveSlidebar = function () {
391		// Check if Slidebars has been initialized
392		if ( ! init ) {
393			throw "Slidebars hasn't been initialized.";
394		}
395
396		// Variable to return
397		var active = false;
398
399		// Loop through Slidebars
400		for ( var id in offCanvas ) {
401			// Check if Slidebar is registered
402			if ( isRegisteredSlidebar( id ) ) {
403				// Check if it's active
404				if ( offCanvas[ id ].active ) {
405					// Set the active id
406					active = offCanvas[ id ].id;
407					break;
408				}
409			}
410		}
411		//console.log(active);
412		// Return
413		return active;
414
415	};
416
417	this.getSlidebars = function () {
418		// Check if Slidebars has been initialized
419		if ( ! init ) {
420			throw "Slidebars hasn't been initialized.";
421		}
422
423		// Create an array for the Slidebars
424		var slidebarsArray = [];
425
426		// Loop through Slidebars
427		for ( var id in offCanvas ) {
428			// Check if Slidebar is registered
429			if ( isRegisteredSlidebar( id ) ) {
430				// Add Slidebar id to array
431				slidebarsArray.push( offCanvas[ id ].id );
432			}
433		}
434
435		// Return
436		return slidebarsArray;
437	};
438
439	this.getSlidebar = function ( id ) {
440		// Check if Slidebars has been initialized
441		if ( ! init ) {
442			throw "Slidebars hasn't been initialized.";
443		}
444
445		// Check if id wasn't passed
446		if ( ! id ) {
447			throw "You must pass a Slidebar id.";
448		}
449
450		// Check if Slidebar is registered
451		if ( ! isRegisteredSlidebar( id ) ) {
452			throw "Error retrieving Slidebar, there is no Slidebar with id '" + id + "'.";
453		}
454
455		// Return the Slidebar's properties
456		return offCanvas[ id ];
457	};
458
459	/**
460	 * Events
461	 */
462
463	this.events = {};
464	var events = this.events;
465
466	/**
467	 * Resizes
468	 */
469
470	$( window ).on( 'resize', this.css.bind( this ) );
471};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.