root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / ExpandingSearchBar / classie.js @ fed367f0
History | View | Annotate | Download (1.8 kB)
1 | fed367f0 | Athina Bekakou | /*!
|
---|---|---|---|
2 | fed367f0 | Athina Bekakou | * classie - class helper functions
|
3 | fed367f0 | Athina Bekakou | * from bonzo https://github.com/ded/bonzo
|
4 | fed367f0 | Athina Bekakou | *
|
5 | fed367f0 | Athina Bekakou | * classie.has( elem, 'my-class' ) -> true/false
|
6 | fed367f0 | Athina Bekakou | * classie.add( elem, 'my-new-class' )
|
7 | fed367f0 | Athina Bekakou | * classie.remove( elem, 'my-unwanted-class' )
|
8 | fed367f0 | Athina Bekakou | * classie.toggle( elem, 'my-class' )
|
9 | fed367f0 | Athina Bekakou | */
|
10 | fed367f0 | Athina Bekakou | |
11 | fed367f0 | Athina Bekakou | /*jshint browser: true, strict: true, undef: true */
|
12 | fed367f0 | Athina Bekakou | /*global define: false */
|
13 | fed367f0 | Athina Bekakou | |
14 | fed367f0 | Athina Bekakou | ( function( window ) {
|
15 | fed367f0 | Athina Bekakou | |
16 | fed367f0 | Athina Bekakou | 'use strict';
|
17 | fed367f0 | Athina Bekakou | |
18 | fed367f0 | Athina Bekakou | // class helper functions from bonzo https://github.com/ded/bonzo
|
19 | fed367f0 | Athina Bekakou | |
20 | fed367f0 | Athina Bekakou | function classReg( className ) { |
21 | fed367f0 | Athina Bekakou | return new RegExp("(^|\\s+)" + className + "(\\s+|$)"); |
22 | fed367f0 | Athina Bekakou | } |
23 | fed367f0 | Athina Bekakou | |
24 | fed367f0 | Athina Bekakou | // classList support for class management
|
25 | fed367f0 | Athina Bekakou | // altho to be fair, the api sucks because it won't accept multiple classes at once
|
26 | fed367f0 | Athina Bekakou | var hasClass, addClass, removeClass;
|
27 | fed367f0 | Athina Bekakou | |
28 | fed367f0 | Athina Bekakou | if ( 'classList' in document.documentElement ) { |
29 | fed367f0 | Athina Bekakou | hasClass = function( elem, c ) { |
30 | fed367f0 | Athina Bekakou | return elem.classList.contains( c );
|
31 | fed367f0 | Athina Bekakou | }; |
32 | fed367f0 | Athina Bekakou | addClass = function( elem, c ) { |
33 | fed367f0 | Athina Bekakou | elem.classList.add( c ); |
34 | fed367f0 | Athina Bekakou | }; |
35 | fed367f0 | Athina Bekakou | removeClass = function( elem, c ) { |
36 | fed367f0 | Athina Bekakou | elem.classList.remove( c ); |
37 | fed367f0 | Athina Bekakou | }; |
38 | fed367f0 | Athina Bekakou | } |
39 | fed367f0 | Athina Bekakou | else {
|
40 | fed367f0 | Athina Bekakou | hasClass = function( elem, c ) { |
41 | fed367f0 | Athina Bekakou | return classReg( c ).test( elem.className );
|
42 | fed367f0 | Athina Bekakou | }; |
43 | fed367f0 | Athina Bekakou | addClass = function( elem, c ) { |
44 | fed367f0 | Athina Bekakou | if ( !hasClass( elem, c ) ) {
|
45 | fed367f0 | Athina Bekakou | elem.className = elem.className + ' ' + c;
|
46 | fed367f0 | Athina Bekakou | } |
47 | fed367f0 | Athina Bekakou | }; |
48 | fed367f0 | Athina Bekakou | removeClass = function( elem, c ) { |
49 | fed367f0 | Athina Bekakou | elem.className = elem.className.replace( classReg( c ), ' ' );
|
50 | fed367f0 | Athina Bekakou | }; |
51 | fed367f0 | Athina Bekakou | } |
52 | fed367f0 | Athina Bekakou | |
53 | fed367f0 | Athina Bekakou | function toggleClass( elem, c ) { |
54 | fed367f0 | Athina Bekakou | var fn = hasClass( elem, c ) ? removeClass : addClass;
|
55 | fed367f0 | Athina Bekakou | fn( elem, c ); |
56 | fed367f0 | Athina Bekakou | } |
57 | fed367f0 | Athina Bekakou | |
58 | fed367f0 | Athina Bekakou | var classie = {
|
59 | fed367f0 | Athina Bekakou | // full names
|
60 | fed367f0 | Athina Bekakou | hasClass: hasClass,
|
61 | fed367f0 | Athina Bekakou | addClass: addClass,
|
62 | fed367f0 | Athina Bekakou | removeClass: removeClass,
|
63 | fed367f0 | Athina Bekakou | toggleClass: toggleClass,
|
64 | fed367f0 | Athina Bekakou | // short names
|
65 | fed367f0 | Athina Bekakou | has: hasClass,
|
66 | fed367f0 | Athina Bekakou | add: addClass,
|
67 | fed367f0 | Athina Bekakou | remove: removeClass,
|
68 | fed367f0 | Athina Bekakou | toggle: toggleClass
|
69 | fed367f0 | Athina Bekakou | }; |
70 | fed367f0 | Athina Bekakou | |
71 | fed367f0 | Athina Bekakou | // transport
|
72 | fed367f0 | Athina Bekakou | if ( typeof define === 'function' && define.amd ) { |
73 | fed367f0 | Athina Bekakou | // AMD
|
74 | fed367f0 | Athina Bekakou | define( classie ); |
75 | fed367f0 | Athina Bekakou | } else {
|
76 | fed367f0 | Athina Bekakou | // browser global
|
77 | fed367f0 | Athina Bekakou | window.classie = classie; |
78 | fed367f0 | Athina Bekakou | } |
79 | fed367f0 | Athina Bekakou | |
80 | fed367f0 | Athina Bekakou | })( window ); |