/** * File: LeftAndMain.BatchActions.js */ (function($) { $.entwine('ss', function($){ /** * Class: #Form_BatchActionsForm * * Batch actions which take a bunch of selected pages, * usually from the CMS tree implementation, and perform serverside * callbacks on the whole set. We make the tree selectable when the jQuery.UI tab * enclosing this form is opened. * * Events: * register - Called before an action is added. * unregister - Called before an action is removed. */ $('#Form_BatchActionsForm').entwine({ /** * Variable: Tree * (DOMElement) */ Tree: null, /** * Variable: Actions * (Array) Stores all actions that can be performed on the collected IDs as * function closures. This might trigger filtering of the selected IDs, * a confirmation message, etc. */ Actions: [], /** * Constructor: onmatch */ onmatch: function() { var self = this; this.setTree($('#sitetree')[0]); $(this.getTree()).bind('select_node.jstree', function(e, data) { self._treeSelectionChanged(data.rslt.obj); }); // if tab which contains this form is shown, make the tree selectable $('#TreeActions').bind('tabsselect', function(e, ui) { // if we are selecting another tab, or the panel is visible (meaning about to be closed), // disable tree selection and reset any values. Otherwise enable it. if($(ui.panel).attr('id') != 'TreeActions-batchactions' || $(ui.panel).is(':visible')) { // @TODO: this is unneccessarily fired also when switching between two other tabs $(self.getTree()).removeClass('multiselect'); } else { self._multiselectTransform(); self.serializeFromTree(); } }); this._super(); }, /** * Function: register * * Parameters: * * (String) type - ... * (Function) callback - ... */ register: function(type, callback) { this.trigger('register', {type: type, callback: callback}); var actions = this.getActions(); actions[type] = callback; this.setActions(actions); }, /** * Function: unregister * * Remove an existing action. * * Parameters: * * {String} type */ unregister: function(type) { this.trigger('unregister', {type: type}); var actions = this.getActions(); if(actions[type]) delete actions[type]; this.setActions(actions); }, /** * Function: _isActive * * Determines if we should allow and track tree selections. * * Todo: * Too much coupling with tabset * * Returns: * (boolean) */ _isActive: function() { return $('#TreeActions-batchactions').is(':visible'); }, /** * Function: refreshSelected * * Ajax callbacks determine which pages is selectable in a certain batch action. * * Parameters: * {Object} rootNode */ refreshSelected : function(rootNode) { var self = this, st = this.getTree(), ids = this.getIDs(), allIds = []; // Default to refreshing the entire tree if(rootNode == null) rootNode = st; for(var idx in ids) { $(st.getTreeNodeByIdx(idx)).addClass('selected').attr('selected', 'selected'); } jQuery(rootNode).find('li').each(function() { var id = parseInt(this.id.replace('record-',''), 10); if(id) allIds.push(id); // Disable the nodes while the ajax request is being processed $(this).removeClass('nodelete').addClass('treeloading'); }); // Post to the server to ask which pages can have this batch action applied var applicablePagesURL = this.find(':input[name=Action]').val() + '/applicablepages/?csvIDs=' + allIds.join(','); jQuery.getJSON(applicablePagesURL, function(applicableIDs) { var i; var applicableIDMap = {}; for(i=0;i