2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* File: LeftAndMain.Tree.js
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function($) {
|
2011-05-08 06:02:28 +02:00
|
|
|
|
2012-05-18 02:07:40 +02:00
|
|
|
$.entwine('ss.tree', function($){
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2011-04-17 09:55:23 +02:00
|
|
|
$('.cms-tree').entwine({
|
2011-05-08 06:02:28 +02:00
|
|
|
|
|
|
|
Hints: null,
|
2012-06-12 12:48:08 +02:00
|
|
|
|
|
|
|
onadd: function(){
|
2011-03-23 10:51:00 +01:00
|
|
|
this._super();
|
2012-03-06 20:17:57 +01:00
|
|
|
|
|
|
|
// Don't reapply (expensive) tree behaviour if already present
|
2012-05-10 06:21:11 +02:00
|
|
|
if($.isNumeric(this.data('jstree_instance_id'))) return;
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2011-12-18 16:01:36 +01:00
|
|
|
var hints = this.attr('data-hints');
|
|
|
|
if(hints) this.setHints($.parseJSON(hints));
|
2011-05-08 06:02:28 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* @todo Icon and page type hover support
|
|
|
|
* @todo Sorting of sub nodes (originally placed in context menu)
|
|
|
|
* @todo Refresh after language <select> change (with Translatable enabled)
|
|
|
|
* @todo Automatic load of full subtree via ajax on node checkbox selection (minNodeCount = 0)
|
|
|
|
* to avoid doing partial selection with "hidden nodes" (unloaded markup)
|
|
|
|
* @todo Disallow drag'n'drop when node has "noChildren" set (see siteTreeHints)
|
|
|
|
* @todo Disallow moving of pages marked as deleted
|
|
|
|
* most likely by server response codes rather than clientside
|
|
|
|
* @todo "defaultChild" when creating a page (sitetreeHints)
|
|
|
|
* @todo Duplicate page (originally located in context menu)
|
|
|
|
* @todo Update tree node title information and modified state after reordering (response is a JSON array)
|
|
|
|
*
|
|
|
|
* Tasks most likely not required after moving to a standalone tree:
|
|
|
|
*
|
|
|
|
* @todo Context menu - to be replaced by a bezel UI
|
|
|
|
* @todo Refresh form for selected tree node if affected by reordering (new parent relationship)
|
|
|
|
* @todo Cancel current form load via ajax when new load is requested (synchronous loading)
|
|
|
|
* @todo When new edit form is loaded, automatically: Select matching node, set correct parent,
|
|
|
|
* update icon and title
|
|
|
|
*/
|
|
|
|
var self = this;
|
|
|
|
this
|
2012-03-12 11:40:08 +01:00
|
|
|
.jstree(this.getTreeConfig())
|
2011-03-23 10:51:00 +01:00
|
|
|
.bind('loaded.jstree', function(e, data) {
|
2012-06-03 18:10:57 +02:00
|
|
|
self.updateFromEditForm();
|
2011-12-14 16:54:52 +01:00
|
|
|
self.css('visibility', 'visible');
|
2011-03-23 10:51:00 +01:00
|
|
|
// Add ajax settings after init period to avoid unnecessary initial ajax load
|
|
|
|
// of existing tree in DOM - see load_node_html()
|
|
|
|
data.inst._set_settings({'html_data': {'ajax': {
|
2012-02-23 16:07:38 +01:00
|
|
|
'url': self.data('urlTree'),
|
2011-03-23 10:51:00 +01:00
|
|
|
'data': function(node) {
|
|
|
|
var params = self.data('searchparams') || [];
|
|
|
|
// Avoid duplication of parameters
|
|
|
|
params = $.grep(params, function(n, i) {return (n.name != 'ID' && n.name != 'value');});
|
|
|
|
params.push({name: 'ID', value: $(node).data("id") ? $(node).data("id") : 0});
|
|
|
|
params.push({name: 'ajax', value: 1});
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
}}});
|
|
|
|
|
|
|
|
// Only show checkboxes with .multiple class
|
|
|
|
data.inst.hide_checkboxes();
|
|
|
|
})
|
|
|
|
.bind('before.jstree', function(e, data) {
|
|
|
|
if(data.func == 'start_drag') {
|
2011-04-22 13:34:01 +02:00
|
|
|
// Don't allow drag'n'drop if multi-select is enabled'
|
|
|
|
if(!self.hasClass('draggable') || self.hasClass('multiselect')) {
|
2011-03-23 10:51:00 +01:00
|
|
|
e.stopImmediatePropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($.inArray(data.func, ['check_node', 'uncheck_node'])) {
|
2012-02-08 07:19:32 +01:00
|
|
|
//Don't allow check and uncheck if parent is disabled
|
2011-03-23 10:51:00 +01:00
|
|
|
var node = $(data.args[0]).parents('li:first');
|
|
|
|
if(node.hasClass('disabled')) {
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.bind('move_node.jstree', function(e, data) {
|
|
|
|
var movedNode = data.rslt.o, newParentNode = data.rslt.np, oldParentNode = data.inst._get_parent(movedNode);
|
|
|
|
var siblingIDs = $.map($(movedNode).siblings().andSelf(), function(el) {
|
|
|
|
return $(el).data('id');
|
|
|
|
});
|
|
|
|
|
|
|
|
$.ajax({
|
2012-02-23 16:07:38 +01:00
|
|
|
'url': self.data('urlSavetreenode'),
|
2011-03-23 10:51:00 +01:00
|
|
|
'data': {
|
|
|
|
ID: $(movedNode).data('id'),
|
|
|
|
ParentID: $(newParentNode).data('id') || 0,
|
|
|
|
SiblingIDs: siblingIDs
|
2012-06-14 07:17:27 +02:00
|
|
|
},
|
|
|
|
statusCode: {
|
|
|
|
403: function() {
|
|
|
|
$.jstree.rollback(data.rlbk);
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
});
|
2012-06-18 00:23:04 +02:00
|
|
|
})
|
|
|
|
// Make some jstree events delegatable
|
2012-06-22 00:46:03 +02:00
|
|
|
.bind('select_node.jstree check_node.jstree uncheck_node.jstree', function(e, data) {
|
2012-06-18 00:23:04 +02:00
|
|
|
$(document).triggerHandler(e, data);
|
|
|
|
})
|
2011-03-23 10:51:00 +01:00
|
|
|
},
|
2012-06-12 12:48:08 +02:00
|
|
|
onremove: function(){
|
|
|
|
this.jstree('destroy');
|
2012-05-11 07:36:18 +02:00
|
|
|
this._super();
|
|
|
|
},
|
2012-03-12 11:40:08 +01:00
|
|
|
|
2012-06-12 12:48:08 +02:00
|
|
|
'from .cms-container': {
|
|
|
|
onafterstatechange: function(e){
|
|
|
|
this.updateFromEditForm(e.origData);
|
|
|
|
},
|
|
|
|
|
|
|
|
onaftersubmitform: function(e){
|
|
|
|
this.updateFromEditForm(e.origData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-03-12 11:40:08 +01:00
|
|
|
getTreeConfig: function() {
|
2012-03-12 14:44:09 +01:00
|
|
|
var self = this;
|
2012-03-12 11:40:08 +01:00
|
|
|
return {
|
|
|
|
'core': {
|
|
|
|
'initially_open': ['record-0'],
|
|
|
|
'animation': 0,
|
|
|
|
'html_titles': true
|
|
|
|
},
|
|
|
|
'html_data': {
|
|
|
|
// 'ajax' will be set on 'loaded.jstree' event
|
|
|
|
},
|
|
|
|
'ui': {
|
|
|
|
"select_limit" : 1,
|
|
|
|
'initially_select': [this.find('.current').attr('id')]
|
|
|
|
},
|
|
|
|
"crrm": {
|
|
|
|
'move': {
|
|
|
|
// Check if a node is allowed to be moved.
|
|
|
|
// Caution: Runs on every drag over a new node
|
|
|
|
'check_move': function(data) {
|
|
|
|
var movedNode = $(data.o), newParent = $(data.np),
|
|
|
|
isMovedOntoContainer = data.ot.get_container()[0] == data.np[0],
|
|
|
|
movedNodeClass = movedNode.getClassname(),
|
|
|
|
newParentClass = newParent.getClassname(),
|
|
|
|
// Check allowedChildren of newParent or against root node rules
|
|
|
|
hints = self.getHints(),
|
|
|
|
disallowedChildren = [],
|
|
|
|
hintKey = newParentClass ? newParentClass : 'Root',
|
2012-06-13 00:51:53 +02:00
|
|
|
hint = (hints && typeof hints[hintKey] != 'undefined') ? hints[hintKey] : null;
|
2012-03-12 11:40:08 +01:00
|
|
|
|
|
|
|
// Special case for VirtualPage: Check that original page type is an allowed child
|
|
|
|
if(hint && movedNode.attr('class').match(/VirtualPage-([^\s]*)/)) movedNodeClass = RegExp.$1;
|
|
|
|
|
|
|
|
if(hint) disallowedChildren = (typeof hint.disallowedChildren != 'undefined') ? hint.disallowedChildren : [];
|
|
|
|
var isAllowed = (
|
|
|
|
// Don't allow moving the root node
|
|
|
|
movedNode.data('id') !== 0
|
|
|
|
// Only allow moving node inside the root container, not before/after it
|
|
|
|
&& (!isMovedOntoContainer || data.p == 'inside')
|
|
|
|
// Children are generally allowed on parent
|
|
|
|
&& !newParent.hasClass('nochildren')
|
|
|
|
// movedNode is allowed as a child
|
|
|
|
&& (!disallowedChildren.length || $.inArray(movedNodeClass, disallowedChildren) == -1)
|
|
|
|
);
|
|
|
|
|
|
|
|
return isAllowed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'dnd': {
|
|
|
|
"drop_target" : false,
|
|
|
|
"drag_target" : false
|
|
|
|
},
|
|
|
|
'checkbox': {
|
|
|
|
'two_state': true
|
|
|
|
},
|
|
|
|
'themes': {
|
|
|
|
'theme': 'apple',
|
2012-03-24 04:38:57 +01:00
|
|
|
'url': $('body').data('frameworkpath') + '/thirdparty/jstree/themes/apple/style.css'
|
2012-03-12 11:40:08 +01:00
|
|
|
},
|
|
|
|
// Caution: SilverStripe has disabled $.vakata.css.add_sheet() for performance reasons,
|
2012-03-24 04:38:57 +01:00
|
|
|
// which means you need to add any CSS manually to framework/admin/scss/_tree.css
|
2012-03-12 11:40:08 +01:00
|
|
|
'plugins': [
|
|
|
|
'html_data', 'ui', 'dnd', 'crrm', 'themes',
|
|
|
|
'checkbox' // checkboxes are hidden unless .multiple is set
|
|
|
|
]
|
|
|
|
};
|
|
|
|
},
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function:
|
|
|
|
* search
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) data Pass empty data to cancel search
|
|
|
|
* (Function) callback Success callback
|
|
|
|
*/
|
|
|
|
search: function(params, callback) {
|
|
|
|
if(params) this.data('searchparams', params);
|
|
|
|
else this.removeData('searchparams');
|
|
|
|
this.jstree('refresh', -1, callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: getNodeByID
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Int) id
|
|
|
|
*
|
|
|
|
* Returns
|
|
|
|
* DOMElement
|
|
|
|
*/
|
|
|
|
getNodeByID: function(id) {
|
2012-03-06 21:36:53 +01:00
|
|
|
return this.find('*[data-id='+id+']');
|
2011-03-23 10:51:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-03-12 11:40:08 +01:00
|
|
|
* Assumes to be triggered by a form element with the following input fields:
|
2012-06-12 17:13:36 +02:00
|
|
|
* ID, ParentID, TreeTitle (or Title), ClassName.
|
|
|
|
*
|
|
|
|
* @todo Serverside node refresh, see http://open.silverstripe.org/ticket/7450
|
2012-03-12 11:40:08 +01:00
|
|
|
*/
|
2012-06-03 18:10:57 +02:00
|
|
|
updateFromEditForm: function(origData) {
|
|
|
|
var self = this,
|
|
|
|
form = $('.cms-edit-form').get(0),
|
2012-06-13 00:51:53 +02:00
|
|
|
id = form ? $(form.ID).val() : null,
|
|
|
|
urlEditPage = this.data('urlEditpage');
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-12 11:40:08 +01:00
|
|
|
// check if a form with a valid ID exists
|
|
|
|
if(id) {
|
2012-06-03 18:10:57 +02:00
|
|
|
var parentID = $(form.ParentID).val(),
|
2011-03-23 10:51:00 +01:00
|
|
|
parentNode = this.find('li[data-id='+parentID+']');
|
|
|
|
node = this.find('li[data-id='+id+']'),
|
2012-06-03 18:10:57 +02:00
|
|
|
title = $((form.TreeTitle) ? form.TreeTitle : form.Title).val(),
|
|
|
|
className = $(form.ClassName).val();
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-12 11:40:08 +01:00
|
|
|
// set title (either from TreeTitle or from Title fields)
|
|
|
|
// Treetitle has special HTML formatting to denote the status changes.
|
2012-06-20 11:58:23 +02:00
|
|
|
// only update immediate text element, we don't want to update all the nested ones
|
|
|
|
if(title) node.find('.text:first').html(title);
|
2012-06-12 19:16:34 +02:00
|
|
|
|
|
|
|
// Collect flag classes and also apply to parent
|
|
|
|
var statusFlags = [];
|
|
|
|
node.children('a').find('.badge').each(function() {
|
|
|
|
statusFlags = statusFlags.concat($(this).attr('class').replace('badge', '').split(' '));
|
|
|
|
});
|
|
|
|
// TODO Doesn't remove classes, gets too complex: Best handled through complete serverside replacement
|
|
|
|
node.addClass(statusFlags.join(' '));
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-12 11:40:08 +01:00
|
|
|
// check if node exists, might have been created instead
|
2012-06-13 00:51:53 +02:00
|
|
|
if(!node.length && urlEditPage) {
|
2011-03-23 10:51:00 +01:00
|
|
|
this.jstree(
|
|
|
|
'create_node',
|
|
|
|
parentNode,
|
|
|
|
'inside',
|
2012-06-03 18:10:57 +02:00
|
|
|
{
|
|
|
|
data: '',
|
|
|
|
attr: {
|
|
|
|
'data-class': className,
|
|
|
|
'class': 'class-' + className,
|
|
|
|
'data-id': id
|
|
|
|
}
|
|
|
|
},
|
2011-03-23 10:51:00 +01:00
|
|
|
function() {
|
|
|
|
var newNode = self.find('li[data-id='+id+']');
|
|
|
|
// TODO Fix replacement of jstree-icon inside <a> tag
|
2012-06-03 18:10:57 +02:00
|
|
|
newNode.find('a:first').html(title).attr('href', ss.i18n.sprintf(
|
2012-06-13 00:51:53 +02:00
|
|
|
urlEditPage, id
|
2012-06-03 18:10:57 +02:00
|
|
|
));
|
2012-06-04 11:05:05 +02:00
|
|
|
self.jstree('deselect_all');
|
2011-03-23 10:51:00 +01:00
|
|
|
self.jstree('select_node', newNode);
|
|
|
|
}
|
|
|
|
);
|
2012-03-12 11:40:08 +01:00
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-06-04 11:05:05 +02:00
|
|
|
if(node.length) {
|
|
|
|
// set correct parent (only if it has changed)
|
|
|
|
if(parentID && parentID != node.parents('li:first').data('id')) {
|
|
|
|
this.jstree('move_node', node, parentNode.length ? parentNode : -1, 'last');
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-06-04 11:05:05 +02:00
|
|
|
// Only single selection is supported on initial load
|
|
|
|
this.jstree('deselect_all');
|
|
|
|
this.jstree('select_node', node);
|
|
|
|
}
|
2012-03-12 11:40:08 +01:00
|
|
|
} else {
|
2012-06-03 18:10:57 +02:00
|
|
|
// If no ID exists in a form view, we're displaying the tree on its own,
|
|
|
|
// hence to page should show as active
|
|
|
|
this.jstree('deselect_all');
|
|
|
|
|
|
|
|
if(typeof origData != 'undefined') {
|
|
|
|
var node = this.find('li[data-id='+origData.ID+']');
|
|
|
|
if(node && node.data('id') !== 0) this.jstree('delete_node', node);
|
2012-03-12 11:40:08 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-12 11:40:08 +01:00
|
|
|
}
|
2012-05-11 06:07:07 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
});
|
|
|
|
|
2012-03-06 20:17:42 +01:00
|
|
|
$('.cms-tree.multiple').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
this._super();
|
|
|
|
this.jstree('show_checkboxes');
|
|
|
|
},
|
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
this.jstree('uncheck_all');
|
|
|
|
this.jstree('hide_checkboxes');
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Function: getSelectedIDs
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* (Array)
|
|
|
|
*/
|
|
|
|
getSelectedIDs: function() {
|
|
|
|
return $.map($(this).jstree('get_checked'), function(el, i) {return $(el).data('id');});
|
|
|
|
}
|
|
|
|
});
|
2011-03-23 10:51:00 +01:00
|
|
|
|
2012-03-06 20:17:42 +01:00
|
|
|
$('.cms-tree li').entwine({
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: setEnabled
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (bool)
|
|
|
|
*/
|
|
|
|
setEnabled: function(bool) {
|
|
|
|
this.toggleClass('disabled', !(bool));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: getClassname
|
|
|
|
*
|
|
|
|
* Returns PHP class for this element. Useful to check business rules like valid drag'n'drop targets.
|
|
|
|
*/
|
|
|
|
getClassname: function() {
|
|
|
|
var matches = this.attr('class').match(/class-([^\s]*)/i);
|
|
|
|
return matches ? matches[1] : '';
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: getID
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* (Number)
|
|
|
|
*/
|
|
|
|
getID: function() {
|
|
|
|
return this.data('id');
|
|
|
|
}
|
|
|
|
});
|
2011-04-05 23:50:20 +02:00
|
|
|
|
2012-03-06 20:17:42 +01:00
|
|
|
$('.cms-tree-view-modes input.view-mode').entwine({
|
|
|
|
onmatch: function() {
|
|
|
|
// set active by default
|
2012-05-01 14:10:09 +02:00
|
|
|
this.redraw();
|
2012-03-06 20:17:42 +01:00
|
|
|
this._super();
|
|
|
|
},
|
2012-05-14 01:43:36 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
|
|
|
},
|
2012-03-06 20:17:42 +01:00
|
|
|
onclick: function(e) {
|
2012-05-01 14:10:09 +02:00
|
|
|
this.redraw();
|
|
|
|
},
|
|
|
|
redraw: function(type) {
|
2012-05-30 20:57:18 +02:00
|
|
|
if(window.debug) console.log('redraw', this.attr('class'), this.get(0));
|
|
|
|
|
2012-03-06 20:17:42 +01:00
|
|
|
$('.cms-tree')
|
2012-05-01 14:10:09 +02:00
|
|
|
.toggleClass('draggable', this.val() == 'draggable')
|
|
|
|
.toggleClass('multiple', this.val() == 'multiselect');
|
2012-03-06 20:17:42 +01:00
|
|
|
}
|
|
|
|
});
|
2011-04-22 13:34:01 +02:00
|
|
|
});
|
2012-03-24 04:38:57 +01:00
|
|
|
}(jQuery));
|