(function($) { $.entwine('ss', function($){ /** * @class 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. * @name ss.Form_BatchActionsForm * * Events: * - register: Called before an action is added. * - unregister: Called before an action is removed. */ $('#Form_BatchActionsForm').entwine(/** @lends ss.Form_BatchActionsForm */{ /** * @type {DOMElement} */ Tree: null, /** * @type {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: [], onmatch: function() { var self = this; this.setTree($('#sitetree')[0]); $(this.getTree()).bind('selectionchanged', function(e, data) { self._treeSelectionChanged(data.node); }); // 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(); }, /** * @param {String} type * @param {Function} callback */ register: function(type, callback) { this.trigger('register', {type: type, callback: callback}); var actions = this.getActions(); actions[type] = callback; this.setActions(actions); }, /** * Remove an existing action. * * @param {String} type */ unregister: function(type) { this.trigger('unregister', {type: type}); var actions = this.getActions(); if(actions[type]) delete actions[type]; this.setActions(actions); }, /** * Determines if we should allow and track tree selections. * * @todo Too much coupling with tabset * @return boolean */ _isActive: function() { return $('#TreeActions-batchactions').is(':visible'); }, /** * Ajax callbacks determine which pages is selectable in a certain batch action. * * @param {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