11'use strict' ;
2- ( function ( ) {
3- angular . module ( 'uiAccordion' , [ ] ) ;
4- angular . module ( 'uiAccordion' )
5- . directive ( 'ngAccordion' , function ( $q , $log ) {
6- return {
7- restrict : 'EA' ,
8- controller : function ( ) {
9- var groups = [ ] ;
10- this . addGroup = function ( group ) {
11- groups . push ( group ) ;
12- }
13- this . getGroups = function ( ) {
14- return groups ;
2+ angular . module ( 'uiAccordion' , [ ] ) ;
3+ function fetchFromObject ( obj , prop ) {
4+
5+ if ( typeof obj === 'undefined' || typeof prop === 'undefined' ) {
6+ return ;
7+ }
8+
9+ var _index = prop . indexOf ( '.' )
10+ if ( _index > - 1 ) {
11+ return fetchFromObject ( obj [ prop . substring ( 0 , _index ) ] , prop . substr ( _index + 1 ) ) ;
12+ }
13+
14+ return obj [ prop ] ;
15+ }
16+
17+
18+ 'use strict' ;
19+
20+ /**
21+ * @ngdoc directive
22+ * @name uiAccordion.directive:ngAccordion
23+ * @description
24+ * # ngAccordion
25+ */
26+ function Accordion ( ) {
27+ this . groups = [ ] ;
28+ this . options = {
29+ closeOthers :true
30+ }
31+ }
32+ Accordion . prototype . addGroup = function ( group ) {
33+ this . groups . push ( group ) ;
34+ }
35+ Accordion . prototype . getGroups = function ( group ) {
36+ return this . groups ;
37+ }
38+ Accordion . prototype . toggleState = function ( group ) {
39+ if ( group ) {
40+ if ( group . options . open ) {
41+ group . up ( 'slideUp' ) ;
42+ } else {
43+ group . down ( 'slideDown' ) ;
44+ if ( this . options . closeOthers ) {
45+ for ( var a = 0 ; a < this . groups . length ; a ++ ) {
46+ if ( group == this . groups [ a ] ) {
47+ continue ;
48+ } else {
49+ this . groups [ a ] . up ( 'slideUp' ) ;
50+ }
51+ }
52+ }
1553 }
16- this . toggleState = function ( group ) {
17- for ( var a = 0 ; a < groups . length ; a ++ ) {
18- if ( group == groups [ a ] ) {
19- if ( ! group . open ) {
20- group . beforeOpen ( ) . then ( function ( ) {
21- group . body . show ( ) ;
22- group . open = ! group . open ;
23- } , function ( error ) {
24- $log . error ( error ) ;
25- } )
26- } else {
27- group . beforeHide ( ) . then ( function ( ) {
28- group . open = false ;
29- group . body . hide ( ) ;
30- } , function ( error ) {
31- $log . error ( error ) ;
32- } )
33- }
34- } else {
35- ( function ( group ) {
36- group . beforeHide ( ) . then ( function ( ) {
37- group . open = false ;
38- group . body . hide ( ) ;
39- } , function ( error ) {
40- $log . error ( error ) ;
41- } )
42- } ) ( groups [ a ] ) ;
54+ }
55+
56+ }
57+ Accordion . prototype . applyState = function ( state ) {
58+ var method , group ;
59+ method = ( state ) ?'up' :'down' ;
60+ for ( var a = 0 ; a < this . groups . length ; a ++ ) {
61+ group = this . groups [ a ] ;
62+ group . options . open = state ;
63+ group [ method ] ( ) ;
64+ }
65+ }
66+ Accordion . prototype . allUp = function ( ) {
67+ this . applyState ( true ) ;
68+ }
69+ Accordion . prototype . allDown = function ( group ) {
70+ this . applyState ( false ) ;
71+ }
72+ function getDefaultAccordion ( ) {
73+ return new Accordion ( ) ;
74+ }
75+ angular . module ( 'uiAccordion' )
76+ . directive ( 'ngAccordion' , function ( $q , $log ) {
77+ return {
78+ restrict : 'A' ,
79+ link :function ( scope , element , attrs , controller ) {
80+ var options = fetchFromObject ( scope , attrs . options ) ;
81+ if ( options ) {
82+ angular . extend ( controller . accordion . options , options ) ;
4383 }
44- }
84+ scope . $watchCollection ( attrs . options , function ( n , o ) {
85+ if ( n && n !== o ) {
86+ angular . extend ( controller . accordion . options , n ) ;
87+ }
88+ } )
89+ } ,
90+ controller : function ( ) {
91+ this . accordion = getDefaultAccordion ( ) ;
4592 }
46- }
47- } ;
93+ } ;
94+ } )
95+
96+ 'use strict' ;
97+
98+ /**
99+ * @ngdoc directive
100+ * @name uiAccordion.directive:ngAccordionGroup
101+ * @description
102+ * # ngAccordionGroup
103+ */
104+ function getService ( name ) {
105+ return angular . injector ( [ 'ng' ] ) . invoke ( [ name , function ( service ) {
106+ return service ;
107+ } ] ) ;
108+ }
109+ function noopPromise ( ) {
110+ var def = getService ( '$q' ) . defer ( ) ;
111+ def . resolve ( ) ;
112+ return def . promise ;
113+ }
114+ function AccordionGroup ( ) {
115+
116+ }
117+ AccordionGroup . prototype . beforeOpen = noopPromise ;
118+ AccordionGroup . prototype . beforeHide = noopPromise ;
119+ AccordionGroup . prototype . down = function ( animationFn ) {
120+ var _self = this ;
121+ _self . beforeOpen ( ) . then ( function ( ) {
122+ _self . body [ animationFn ] ( 'slow' ) ; //slideDown
123+ _self . options . open = true ;
124+ } , function ( error ) {
125+ getService ( '$log' ) . error ( error ) ;
126+ } )
127+ } ;
128+ AccordionGroup . prototype . up = function ( animationFn ) {
129+ var _self = this ;
130+ _self . beforeHide ( ) . then ( function ( ) {
131+ _self . options . open = false ;
132+ _self . body [ animationFn ] ( ) ; //slideUp
133+ } , function ( error ) {
134+ getService ( '$log' ) . error ( error ) ;
48135 } )
49- . directive ( 'ngAccordionGroup' , function ( $q ) {
50- function defaultOptions ( ) {
136+ } ;
137+ function defaultAccordionGroupOptions ( ) {
138+ return {
139+ open :false ,
140+ disabled :false
141+ }
142+ }
143+ angular . module ( 'uiAccordion' )
144+ . directive ( 'ngAccordionGroup' , function ( $q ) {
51145 return {
52- beforeOpen :function ( ) {
53- var def = $q . defer ( ) ;
54- def . resolve ( ) ;
55- return def . promise ;
56- } ,
57- beforeHide :function ( ) {
58- var def = $q . defer ( ) ;
59- def . resolve ( ) ;
60- return def . promise ;
146+ require :[ '^ngAccordion' , 'ngAccordionGroup' ] ,
147+ restrict :'EA' ,
148+ controller :function ( ) {
149+ var headerEl , bodyEl , headerDef , bodyDef ;
150+ headerDef = $q . defer ( ) ;
151+ bodyDef = $q . defer ( ) ;
152+ this . setHeaderElement = function ( element ) {
153+ headerDef . resolve ( element ) ;
154+ }
155+ this . getHeaderElement = function ( ) {
156+ return headerDef . promise ;
157+ }
158+ this . setBodyElement = function ( element ) {
159+ bodyDef . resolve ( element ) ;
160+ }
161+ this . getBodyElement = function ( ) {
162+ return bodyDef . promise ;
163+ }
61164 } ,
62- open :false
63- }
64- }
65- return {
66- require :[ '^ngAccordion' , 'ngAccordionGroup' ] ,
67- restrict :'EA' ,
68- controller :function ( ) {
69- var headerEl ;
70- var bodyEl ;
71- var headerDef = $q . defer ( ) ;
72- var bodyDef = $q . defer ( ) ;
73- this . setHeaderElement = function ( element ) {
74- headerDef . resolve ( element ) ;
75- }
76- this . getHeaderElement = function ( ) {
77- return headerDef . promise ;
78- }
79- this . setBodyElement = function ( element ) {
80- bodyDef . resolve ( element ) ;
165+ link : function ( scope , element , attrs , controllers ) {
166+ var accordionCtrl , controller , accordion , accordionGroup , options ;
167+ controller = controllers [ 1 ] ;
168+ accordionCtrl = controllers [ 0 ] ;
169+ accordion = accordionCtrl . accordion ;
170+ accordionGroup = new AccordionGroup ( ) ;
171+ options = fetchFromObject ( scope , attrs . options ) ;
172+
173+ if ( options ) {
174+ accordionGroup . options = angular . extend ( defaultAccordionGroupOptions ( ) , options ) ;
175+ } else {
176+ accordionGroup . options = defaultAccordionGroupOptions ( ) ;
177+ }
178+ scope . $watchCollection ( function ( ) {
179+ return options ;
180+ } , function ( n , o ) {
181+ accordionGroup . options = angular . extend ( defaultAccordionGroupOptions ( ) , n ) ;
182+ if ( n !== o && n . open !== o . open ) {
183+ accordionGroup . options . open = ! n . open ;
184+ accordion . toggleState ( accordionGroup ) ;
185+ }
186+ } )
187+
188+ $q . all ( [ controller . getHeaderElement ( ) , controller . getBodyElement ( ) ] ) . then ( function ( results ) {
189+ accordionGroup . header = results [ 0 ] ;
190+ accordionGroup . body = results [ 1 ] ;
191+ if ( accordionGroup . options . open ) {
192+ accordionGroup . down ( 'show' ) ;
193+ } else {
194+ accordionGroup . up ( 'hide' ) ;
195+ }
196+ accordionGroup . header . on ( 'click' , function ( ) {
197+ if ( ! accordionGroup . options . disabled ) {
198+ accordion . toggleState ( accordionGroup ) ;
199+ }
200+ } ) ;
201+ accordion . addGroup ( accordionGroup ) ;
202+ } )
203+
81204 }
82- this . getBodyElement = function ( ) {
83- return bodyDef . promise ;
205+ } ;
206+ } )
207+
208+ 'use strict' ;
209+
210+ /**
211+ * @ngdoc directive
212+ * @name uiAccordion.directive:ngAccordionHeading
213+ * @description
214+ * # ngAccordionHeading
215+ */
216+ angular . module ( 'uiAccordion' )
217+ . directive ( 'ngAccordionHeading' , function ( ) {
218+ return {
219+ require :'^ngAccordionGroup' ,
220+ restrict : 'EA' ,
221+ link : function ( scope , element , attrs , accordionCtrl ) {
222+ accordionCtrl . setHeaderElement ( element ) ;
84223 }
85- } ,
86- link : function ( scope , element , attrs , controllers ) {
87- var controller = controllers [ 1 ] ;
88- var accordionCtrl = controllers [ 0 ] ;
89-
90- var accordion = angular . extend ( defaultOptions ( ) , scope . $eval ( attrs . options ) )
91- $q . all ( [ controller . getHeaderElement ( ) , controller . getBodyElement ( ) ] ) . then ( function ( results ) {
92- accordion . header = results [ 0 ] ;
93- accordion . body = results [ 1 ] ;
94- accordion . body . hide ( ) ;
95- accordion . header . on ( 'click' , function ( ) {
96- accordionCtrl . toggleState ( accordion ) ;
97- } ) ;
98- accordionCtrl . addGroup ( accordion ) ;
99- } )
100-
101- }
102- } ;
103- } )
104- . directive ( 'ngAccordionHeading' , function ( ) {
105- return {
106- require :'^ngAccordionGroup' ,
107- restrict : 'EA' ,
108- link : function ( scope , element , attrs , accordionCtrl ) {
109- accordionCtrl . setHeaderElement ( element ) ;
110- }
111- } ;
112- } )
113- . directive ( 'ngAccordionBody' , function ( ) {
114- return {
115- require :'^ngAccordionGroup' ,
116- restrict : 'EA' ,
117- link : function ( scope , element , attrs , accordionCtrl ) {
118- accordionCtrl . setBodyElement ( element ) ;
119- }
120- } ;
121- } )
122- } ) ( )
224+ } ;
225+ } ) ;
226+
227+ 'use strict' ;
123228
229+ /**
230+ * @ngdoc directive
231+ * @name uiAccordion.directive:ngAccordionBody
232+ * @description
233+ * # ngAccordionBody
234+ */
235+ angular . module ( 'uiAccordion' )
236+ . directive ( 'ngAccordionBody' , function ( ) {
237+ return {
238+ require :'^ngAccordionGroup' ,
239+ restrict : 'EA' ,
240+ link : function ( scope , element , attrs , accordionCtrl ) {
241+ accordionCtrl . setBodyElement ( element ) ;
242+ }
243+ } ;
244+ } ) ;
0 commit comments