1919
2020$ ( document ) . ready ( function ( ) {
2121
22- $ ( 'form#edit_role, form#create_role' ) . submit ( function ( event ) {
23- var form = $ ( this ) ;
24- var to_role = $ ( 'ul#for_role' ) . sortable ( 'toArray' ) ;
25- if ( form . attr ( 'id' ) == 'edit_role' ) {
26- form . append ( '<input type="hidden" name="_method" value="put">' ) ;
27- }
28- form . append ( $ ( 'input#role_name' ) ) . css ( 'display' , 'none' ) ;
29- form . append ( $ ( 'textarea#role_description' ) ) . css ( 'display' , 'none' ) ;
22+ var buildHiddenFormFromDragDrop = function ( form , runListSet ) {
23+ runListSet . each ( function ( i , envRunListUl ) {
24+ $ ( envRunListUl ) . find ( 'li.runListItem' ) . each ( function ( i , field ) {
25+ form . append ( '<input type="hidden" name="env_run_lists[' + envRunListUl . id + '][]" value="' + field . id + '"/>' ) ;
26+ } ) ;
27+ } ) ;
28+ } ;
29+
30+ var buildHiddenFormFromJSONEditor = function ( form ) {
3031 form . append ( '<input type="hidden" id="default_attributes" name="default_attributes"/>' ) ;
31- $ ( 'input#default_attributes' ) . attr ( 'value' , BCJTEP . save ( 'json["defaults"]' ) )
32+ $ ( 'input#default_attributes' ) . attr ( 'value' , BCJTEP . save ( 'json["defaults"]' ) ) ;
3233 form . append ( '<input type="hidden" id="override_attributes" name="override_attributes"/>' ) ;
3334 $ ( 'input#override_attributes' ) . attr ( 'value' , BCJTEP . save ( 'json["overrides"]' ) ) ;
34- jQuery . each ( to_role , function ( i , field ) {
35- form . append ( '<input type="hidden" name="for_role[]" value="' + field + '"/>' ) ;
36- } ) ;
37- } ) ;
35+ } ;
3836
37+ $ ( 'a#debugFormBuild' ) . click ( function ( event ) { buildHiddenFormFromDragDrop ( event ) } ) ;
38+
39+
40+ $ ( 'form.roleForm' ) . submit ( function ( event ) {
41+ buildHiddenFormFromDragDrop ( $ ( this ) , $ ( 'ul.runListItemList' ) ) ;
42+ buildHiddenFormFromJSONEditor ( $ ( this ) ) ;
43+ } ) ;
44+
3945 $ ( 'form#edit_node, form#create_node' ) . submit ( function ( event ) {
4046 var form = $ ( this ) ;
4147 var to_node = $ ( 'ul#for_node' ) . sortable ( 'toArray' ) ;
@@ -108,10 +114,10 @@ $(document).ready(function(){
108114 return ;
109115 }
110116
111- if ( method == 'post' || method == 'put' || method == 'delete' ) {
117+ if ( method === 'post' || method === 'put' || method = == 'delete' ) {
112118 event . preventDefault ( ) ;
113119 var form = $ ( "<form/>" ) . attr ( 'method' , 'post' ) . attr ( 'action' , this . href ) . attr ( 'style' , 'display: none' ) ;
114- if ( method != "post" ) {
120+ if ( method !== "post" ) {
115121 form . append ( $ ( '<input type="hidden" name="_method"/>' ) . attr ( 'value' , method ) ) ;
116122 }
117123 form . insertAfter ( this ) . submit ( ) ;
@@ -129,10 +135,18 @@ $(document).ready(function(){
129135 $ ( 'a[rel*=facebox]' ) . facebox ( ) ;
130136
131137
132- $ ( '.connectedSortable' ) . sortable ( {
133- placeholder : 'ui-state-highlight' ,
134- connectWith : $ ( '.connectedSortable' )
135- } ) . disableSelection ( ) ;
138+ var enableDragDropBehavior = function ( ) {
139+ $ ( '.connectedSortable' ) . sortable ( {
140+ placeholder : 'ui-state-highlight' ,
141+ connectWith : $ ( '.connectedSortable' )
142+ } ) . disableSelection ( ) ;
143+ } ;
144+
145+ var disableDragDropBehavior = function ( ) {
146+ $ ( '.connectedSortable' ) . sortable ( "destroy" ) ;
147+ } ;
148+
149+ enableDragDropBehavior ( ) ;
136150
137151 // The table tree!
138152 $ ( 'table.tree' ) . treeTable ( { expandable : true } ) ;
@@ -158,17 +172,62 @@ $(document).ready(function(){
158172 $ ( "#sidebar_block" ) . fadeIn ( ) ;
159173 }
160174
175+ // Run list editor with per-env run lists for roles
161176
162-
163- if ( $ ( 'ul#role_available_recipes' ) . size ( ) !== 0 ) {
164- var currentEnvironment = $ ( '#environment_run_list_selection_control select' ) . val ( ) ;
177+ var populateAvailableRecipesForEnv = function ( currentEnvironment , callback ) {
165178 $ . getJSON ( '/environments/' + currentEnvironment + '/recipes' , function ( data ) {
166- $ ( 'div#available_recipes_container img .spinner' ) . remove ( ) ;
167- for ( var i = data [ 'recipes' ] . length - 1 ; i >= 0 ; i -- ) {
179+ $ ( 'div#available_recipes_container .spinner' ) . hide ( ) ;
180+ for ( var i = 0 ; i < data [ 'recipes' ] . length ; i ++ ) {
168181 var recipe = data [ 'recipes' ] [ i ] ;
169182 $ ( 'ul#role_available_recipes' ) . append ( '<li id="recipe[' + recipe + ']" class="ui-state-default runListItem">' + recipe + '</li>' ) ;
170183 } ;
171184 } )
185+ if ( callback ) { callback ( ) ; }
186+ } ;
187+
188+ var depopulateAvailableRecipesForEnv = function ( ) {
189+ $ ( 'ul#role_available_recipes li' ) . remove ( ) ;
190+ $ ( 'div#available_recipes_container .spinner' ) . show ( ) ;
172191 } ;
173192
193+ if ( $ ( 'ul#role_available_recipes' ) . size ( ) !== 0 ) {
194+ populateAvailableRecipesForEnv ( window . initialCurrentEnv ) ;
195+ }
196+
197+ $ ( 'a.createEmptyRunListControl' ) . each ( function ( i ) {
198+ var environment = $ ( this ) . attr ( 'id' ) ;
199+ $ ( this ) . click ( function ( event ) {
200+ $ ( 'div.emptyRunListControlsContainer#' + environment ) . remove ( ) ;
201+ var runListContainerForEnv = $ ( 'div.runListContainer#' + environment + 'RunListContainer' ) ;
202+ runListContainerForEnv . append ( '<ul class="ui-sortable connectedSortable runListItemList ohai" id="' + environment + '"></ul>' ) ;
203+ // remove all previous drag/drop events/behavior/whatever, then re-add it for the new run list container
204+ disableDragDropBehavior ( ) ;
205+ enableDragDropBehavior ( ) ;
206+ } ) ;
207+ } ) ;
208+
209+ var setActiveRunListLabelFor = function ( environment ) {
210+ var labelElement = $ ( 'span#selectedRunListEditorLabel' ) ;
211+ if ( environment === '_default' ) {
212+ labelElement . html ( 'Default Run List' ) ;
213+ }
214+ else {
215+ labelElement . html ( 'Run List for ' + environment ) ;
216+ }
217+ } ;
218+
219+
220+ $ ( 'select#activeEnvironment' ) . change ( function ( ) {
221+ // set the active run list editor
222+ var newActiveEnvironment = $ ( this ) . val ( ) ;
223+ if ( newActiveEnvironment !== 'noop' ) {
224+ $ ( 'div.runListWithControlsContainer.active' ) . removeClass ( 'active' ) . addClass ( 'inactive' ) ;
225+ $ ( 'div.runListWithControlsContainer#' + newActiveEnvironment ) . addClass ( 'active' , 10000 ) . removeClass ( 'inactive' ) ;
226+ setActiveRunListLabelFor ( newActiveEnvironment ) ;
227+ depopulateAvailableRecipesForEnv ( ) ;
228+ var selector = $ ( this ) ;
229+ populateAvailableRecipesForEnv ( newActiveEnvironment , function ( ) { selector . val ( 'noop' ) ; } ) ;
230+ }
231+ } ) ;
232+
174233} ) ;
0 commit comments