2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* File: LeftAndMain.BatchActions.js
|
|
|
|
*/
|
2009-11-21 04:15:40 +01:00
|
|
|
(function($) {
|
2010-04-13 07:55:56 +02:00
|
|
|
$.entwine('ss', function($){
|
2009-11-21 04:19:59 +01:00
|
|
|
|
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Class: #Form_BatchActionsForm
|
|
|
|
*
|
|
|
|
* Batch actions which take a bunch of selected pages,
|
2009-11-21 04:19:59 +01:00
|
|
|
* 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:
|
2010-05-08 04:45:05 +02:00
|
|
|
* register - Called before an action is added.
|
|
|
|
* unregister - Called before an action is removed.
|
2009-11-21 04:19:59 +01:00
|
|
|
*/
|
2010-05-08 04:45:05 +02:00
|
|
|
$('#Form_BatchActionsForm').entwine({
|
2009-11-21 04:15:40 +01:00
|
|
|
|
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Variable: Tree
|
|
|
|
* (DOMElement)
|
2009-11-21 04:15:40 +01:00
|
|
|
*/
|
|
|
|
Tree: null,
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Variable: Actions
|
|
|
|
* (Array) Stores all actions that can be performed on the collected IDs as
|
2009-11-21 04:15:40 +01:00
|
|
|
* function closures. This might trigger filtering of the selected IDs,
|
|
|
|
* a confirmation message, etc.
|
|
|
|
*/
|
|
|
|
Actions: [],
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Constructor: onmatch
|
|
|
|
*/
|
2009-11-21 04:15:40 +01:00
|
|
|
onmatch: function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.setTree($('#sitetree')[0]);
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2011-03-10 06:30:41 +01:00
|
|
|
$(this.getTree()).bind('select_node.jstree', function(e, data) {
|
|
|
|
self._treeSelectionChanged(data.rslt.obj);
|
2009-11-21 04:15:40 +01:00
|
|
|
});
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// if tab which contains this form is shown, make the tree selectable
|
|
|
|
$('#TreeActions').bind('tabsselect', function(e, ui) {
|
2009-12-17 00:20:39 +01:00
|
|
|
// if we are selecting another tab, or the panel is visible (meaning about to be closed),
|
2009-11-21 04:15:40 +01:00
|
|
|
// disable tree selection and reset any values. Otherwise enable it.
|
2009-12-17 00:20:39 +01:00
|
|
|
if($(ui.panel).attr('id') != 'TreeActions-batchactions' || $(ui.panel).is(':visible')) {
|
|
|
|
// @TODO: this is unneccessarily fired also when switching between two other tabs
|
2009-11-21 04:19:59 +01:00
|
|
|
$(self.getTree()).removeClass('multiselect');
|
2009-11-21 04:15:40 +01:00
|
|
|
} else {
|
|
|
|
self._multiselectTransform();
|
2010-04-13 08:42:21 +02:00
|
|
|
self.serializeFromTree();
|
2009-11-21 04:15:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-11-21 04:19:59 +01:00
|
|
|
this._super();
|
2009-11-21 04:15:40 +01:00
|
|
|
},
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: register
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* (String) type - ...
|
|
|
|
* (Function) callback - ...
|
2009-11-21 04:15:40 +01:00
|
|
|
*/
|
|
|
|
register: function(type, callback) {
|
|
|
|
this.trigger('register', {type: type, callback: callback});
|
2009-11-21 04:19:59 +01:00
|
|
|
var actions = this.getActions();
|
2009-11-21 04:15:40 +01:00
|
|
|
actions[type] = callback;
|
|
|
|
this.setActions(actions);
|
|
|
|
},
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: unregister
|
|
|
|
*
|
2009-11-21 04:15:40 +01:00
|
|
|
* Remove an existing action.
|
|
|
|
*
|
2010-05-08 04:45:05 +02:00
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* {String} type
|
2009-11-21 04:15:40 +01:00
|
|
|
*/
|
|
|
|
unregister: function(type) {
|
|
|
|
this.trigger('unregister', {type: type});
|
2009-11-21 04:19:59 +01:00
|
|
|
|
|
|
|
var actions = this.getActions();
|
2009-11-21 04:15:40 +01:00
|
|
|
if(actions[type]) delete actions[type];
|
|
|
|
this.setActions(actions);
|
|
|
|
},
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: _isActive
|
|
|
|
*
|
2009-11-21 04:15:40 +01:00
|
|
|
* Determines if we should allow and track tree selections.
|
|
|
|
*
|
2010-05-08 04:45:05 +02:00
|
|
|
* Todo:
|
|
|
|
* Too much coupling with tabset
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* (boolean)
|
2009-11-21 04:15:40 +01:00
|
|
|
*/
|
|
|
|
_isActive: function() {
|
|
|
|
return $('#TreeActions-batchactions').is(':visible');
|
|
|
|
},
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2010-04-13 08:42:21 +02:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: refreshSelected
|
|
|
|
*
|
2010-04-13 08:42:21 +02:00
|
|
|
* Ajax callbacks determine which pages is selectable in a certain batch action.
|
|
|
|
*
|
2010-05-08 04:45:05 +02:00
|
|
|
* Parameters:
|
|
|
|
* {Object} rootNode
|
2010-04-13 08:42:21 +02:00
|
|
|
*/
|
|
|
|
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<applicableIDs.length;i++) applicableIDMap[applicableIDs[i]] = true;
|
|
|
|
|
|
|
|
// Set a CSS class on each tree node indicating which can be batch-actioned and which can't
|
|
|
|
jQuery(rootNode).find('li').each(function() {
|
|
|
|
$(this).removeClass('treeloading');
|
|
|
|
|
|
|
|
var id = parseInt(this.id.replace('record-',''), 10);
|
|
|
|
if(id) {
|
|
|
|
if(applicableIDMap[id] === true) {
|
|
|
|
$(this).removeClass('nodelete');
|
|
|
|
} else {
|
|
|
|
// De-select the node if it's non-applicable
|
|
|
|
$(this).removeClass('selected').addClass('nodelete');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.serializeFromTree();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Function: serializeFromTree
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* (boolean)
|
|
|
|
*/
|
2010-04-13 08:42:21 +02:00
|
|
|
serializeFromTree: function() {
|
|
|
|
var tree = this.getTree(), ids = [];
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// find all explicitly selected IDs
|
|
|
|
$(tree).find('li.selected').each(function() {
|
|
|
|
ids.push(tree.getIdxOf(this));
|
2010-04-13 08:42:21 +02:00
|
|
|
// // find implicitly selected children
|
|
|
|
// $(this).find('li').each(function() {
|
|
|
|
// ids.push(tree.getIdxOf(this));
|
|
|
|
// });
|
2009-11-21 04:15:40 +01:00
|
|
|
});
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2010-04-13 08:42:21 +02:00
|
|
|
// if no IDs are selected, stop here. This is an implict way for the
|
|
|
|
// callback to cancel the actions
|
|
|
|
if(!ids || !ids.length) return false;
|
|
|
|
|
|
|
|
// write IDs to the hidden field
|
|
|
|
this.setIDs(ids);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: setIDS
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* {Array} ids
|
2010-04-13 08:42:21 +02:00
|
|
|
*/
|
|
|
|
setIDs: function(ids) {
|
|
|
|
this.find(':input[name=csvIDs]').val(ids.join(','));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: getIDS
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* {Array}
|
2010-04-13 08:42:21 +02:00
|
|
|
*/
|
|
|
|
getIDs: function() {
|
|
|
|
return this.find(':input[name=csvIDs]').val().split(',');
|
|
|
|
},
|
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Function: onsubmit
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Event) e
|
|
|
|
*/
|
2010-04-13 08:42:21 +02:00
|
|
|
onsubmit: function(e) {
|
|
|
|
var ids = this.getIDs();
|
|
|
|
var tree = this.getTree();
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// if no nodes are selected, return with an error
|
|
|
|
if(!ids || !ids.length) {
|
|
|
|
alert(ss.i18n._t('CMSMAIN.SELECTONEPAGE'));
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-13 08:42:21 +02:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// apply callback, which might modify the IDs
|
|
|
|
var type = this.find(':input[name=Action]').val();
|
2009-11-21 04:19:59 +01:00
|
|
|
if(this.getActions()[type]) ids = this.getActions()[type].apply(this, [ids]);
|
|
|
|
|
2010-04-13 08:42:21 +02:00
|
|
|
// write (possibly modified) IDs back into to the hidden field
|
|
|
|
this.setIDs(ids);
|
2010-04-13 08:57:12 +02:00
|
|
|
|
|
|
|
// Reset failure states
|
|
|
|
$(tree).find('li').removeClass('failed');
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
var button = this.find(':submit:first');
|
|
|
|
button.addClass('loading');
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
jQuery.ajax({
|
|
|
|
// don't use original form url
|
|
|
|
url: type,
|
|
|
|
type: 'POST',
|
|
|
|
data: this.serializeArray(),
|
|
|
|
complete: function(xmlhttp, status) {
|
|
|
|
button.removeClass('loading');
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// status message
|
|
|
|
var msg = (xmlhttp.getResponseHeader('X-Status')) ? xmlhttp.getResponseHeader('X-Status') : xmlhttp.statusText;
|
|
|
|
statusMessage(msg, (status == 'success') ? 'good' : 'bad');
|
|
|
|
},
|
|
|
|
success: function(data, status) {
|
2010-04-13 08:42:21 +02:00
|
|
|
var id;
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// TODO This should use a more common serialization in a new tree library
|
|
|
|
if(data.modified) {
|
2010-04-13 08:42:21 +02:00
|
|
|
for(id in data.modified) {
|
2009-11-21 04:15:40 +01:00
|
|
|
tree.setNodeTitle(id, data.modified[id]['TreeTitle']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(data.deleted) {
|
2010-04-13 08:42:21 +02:00
|
|
|
for(id in data.deleted) {
|
2009-11-21 04:15:40 +01:00
|
|
|
var node = tree.getTreeNodeByIdx(id);
|
|
|
|
if(node && node.parentTreeNode) node.parentTreeNode.removeTreeNode(node);
|
|
|
|
}
|
|
|
|
}
|
2010-04-13 08:57:12 +02:00
|
|
|
if(data.error) {
|
|
|
|
for(id in data.error) {
|
|
|
|
var node = tree.getTreeNodeByIdx(id);
|
|
|
|
$(node).addClass('failed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deselect all nodes
|
|
|
|
$(tree).find('li').removeClass('selected');
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// reset selection state
|
|
|
|
// TODO Should unselect all selected nodes as well
|
2010-04-13 08:57:12 +02:00
|
|
|
// jQuery(tree).removeClass('multiselect');
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// Check if current page still exists, and refresh it.
|
|
|
|
// Otherwise remove the current form
|
|
|
|
var selectedNode = tree.firstSelected();
|
|
|
|
if(selectedNode) {
|
|
|
|
var selectedNodeId = tree.getIdxOf(selectedNode);
|
|
|
|
if(data.modified[selectedNode.getIdx()]) {
|
|
|
|
// only if the current page was modified
|
|
|
|
selectedNode.selectTreeNode();
|
|
|
|
} else if(data.deleted[selectedNode.getIdx()]) {
|
2010-04-13 07:55:56 +02:00
|
|
|
jQuery('#Form_EditForm').entwine('ss').removeForm();
|
2009-11-21 04:15:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2010-04-13 07:55:56 +02:00
|
|
|
jQuery('#Form_EditForm').entwine('ss').removeForm();
|
2009-11-21 04:15:40 +01:00
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// close panel
|
|
|
|
// TODO Coupling with tabs
|
2010-04-13 08:57:12 +02:00
|
|
|
// jQuery('#TreeActions').tabs('select', -1);
|
2009-11-21 04:15:40 +01:00
|
|
|
},
|
|
|
|
dataType: 'json'
|
|
|
|
});
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: _multiselectTransform
|
|
|
|
*
|
|
|
|
* Todo:
|
|
|
|
* This is simulating MultiselectTree functionality, and shouldn't be necessary.
|
2009-11-21 04:15:40 +01:00
|
|
|
*/
|
|
|
|
_multiselectTransform : function() {
|
|
|
|
// make tree selectable
|
2009-11-21 04:19:59 +01:00
|
|
|
jQuery(this.getTree()).addClass('multiselect');
|
2009-11-21 04:15:40 +01:00
|
|
|
|
|
|
|
// auto-select the current node
|
2009-11-21 04:19:59 +01:00
|
|
|
var node = this.getTree().firstSelected();
|
2009-11-21 04:15:40 +01:00
|
|
|
if(node){
|
2010-04-13 08:57:12 +02:00
|
|
|
$(node).removeClass('current').addClass('selected');
|
2009-11-21 04:15:40 +01:00
|
|
|
node.open();
|
|
|
|
|
|
|
|
// Open all existing children, which might trigger further
|
|
|
|
// ajaxExansion calls to ensure all nodes are selectable
|
|
|
|
var children = $(node).find('li').each(function() {
|
|
|
|
this.open();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Function: _treeSelectionChanged
|
|
|
|
*
|
2009-11-21 04:15:40 +01:00
|
|
|
* Only triggers if the field is considered 'active'.
|
2010-05-08 04:45:05 +02:00
|
|
|
*
|
|
|
|
* Todo:
|
|
|
|
* Most of this is basically simulating broken behaviour of the MultiselectTree mixin,
|
2009-11-21 04:15:40 +01:00
|
|
|
* and should be removed.
|
|
|
|
*/
|
|
|
|
_treeSelectionChanged: function(node) {
|
|
|
|
if(!this._isActive()) return;
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
if(node.selected) {
|
2010-04-13 08:57:12 +02:00
|
|
|
$(node).removeClass('selected').attr('selected', false);
|
2009-11-21 04:15:40 +01:00
|
|
|
} else {
|
|
|
|
// Select node
|
2010-04-13 08:57:12 +02:00
|
|
|
$(node).addClass('selected').attr('selected', true);
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
// Open node in order to allow proper selection of children
|
|
|
|
if($(node).hasClass('unexpanded')) {
|
|
|
|
node.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open all existing children, which might trigger further
|
|
|
|
// ajaxExansion calls to ensure all nodes are selectable
|
|
|
|
var children = $(node).find('li').each(function() {
|
|
|
|
this.open();
|
|
|
|
});
|
|
|
|
}
|
2010-04-13 08:42:21 +02:00
|
|
|
|
|
|
|
this.serializeFromTree();
|
2009-11-21 04:15:40 +01:00
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
});
|
2009-11-21 04:15:40 +01:00
|
|
|
});
|
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Class: #Form_BatchActionsForm :select[name=Action]
|
|
|
|
*/
|
2011-02-24 05:10:01 +01:00
|
|
|
$('#Form_BatchActionsForm select[name=Action]').entwine({
|
2010-05-08 04:45:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: onchange
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Event) e
|
|
|
|
*/
|
2010-04-13 08:42:21 +02:00
|
|
|
onchange: function(e) {
|
|
|
|
$(e.target.form).entwine('ss').refreshSelected();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-11-21 04:15:40 +01:00
|
|
|
$(document).ready(function() {
|
|
|
|
/**
|
|
|
|
* Publish selected pages action
|
|
|
|
*/
|
2010-04-13 07:55:56 +02:00
|
|
|
$('#Form_BatchActionsForm').entwine('ss').register('admin/batchactions/publish', function(ids) {
|
2009-11-21 04:15:40 +01:00
|
|
|
var confirmed = confirm(
|
|
|
|
"You have " + ids.length + " pages selected.\n\n"
|
|
|
|
+ "Do your really want to publish?"
|
|
|
|
);
|
|
|
|
return (confirmed) ? ids : false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unpublish selected pages action
|
|
|
|
*/
|
2010-04-13 07:55:56 +02:00
|
|
|
$('#Form_BatchActionsForm').entwine('ss').register('admin/batchactions/unpublish', function(ids) {
|
2009-11-21 04:15:40 +01:00
|
|
|
var confirmed = confirm(
|
|
|
|
"You have " + ids.length + " pages selected.\n\n"
|
|
|
|
+ "Do your really want to unpublish?"
|
|
|
|
);
|
|
|
|
return (confirmed) ? ids : false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete selected pages action
|
|
|
|
*/
|
2010-04-13 07:55:56 +02:00
|
|
|
$('#Form_BatchActionsForm').entwine('ss').register('admin/batchactions/delete', function(ids) {
|
2009-11-21 04:15:40 +01:00
|
|
|
var confirmed = confirm(
|
|
|
|
"You have " + ids.length + " pages selected.\n\n"
|
|
|
|
+ "Do your really want to delete?"
|
|
|
|
);
|
|
|
|
return (confirmed) ? ids : false;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete selected pages from live action
|
|
|
|
*/
|
2010-04-13 07:55:56 +02:00
|
|
|
$('#Form_BatchActionsForm').entwine('ss').register('admin/batchactions/deletefromlive', function(ids) {
|
2009-11-21 04:15:40 +01:00
|
|
|
var confirmed = confirm(
|
|
|
|
"You have " + ids.length + " pages selected.\n\n"
|
|
|
|
+ "Do your really want to delete these pages from live?"
|
|
|
|
);
|
|
|
|
return (confirmed) ? ids : false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|