silverstripe-framework/admin/client/dist/js/LeftAndMain.Tree.js
Damian Mooyman f13e44a265 Merge 3 into master
# Conflicts:
#	admin/javascript/LeftAndMain.Tree.js
#	admin/javascript/lang/cs.js
#	admin/javascript/lang/de.js
#	admin/javascript/lang/en.js
#	admin/javascript/lang/eo.js
#	admin/javascript/lang/es.js
#	admin/javascript/lang/fa_IR.js
#	admin/javascript/lang/fi.js
#	admin/javascript/lang/fr.js
#	admin/javascript/lang/id.js
#	admin/javascript/lang/id_ID.js
#	admin/javascript/lang/it.js
#	admin/javascript/lang/ja.js
#	admin/javascript/lang/lt.js
#	admin/javascript/lang/mi.js
#	admin/javascript/lang/nb.js
#	admin/javascript/lang/nl.js
#	admin/javascript/lang/pl.js
#	admin/javascript/lang/ro.js
#	admin/javascript/lang/ru.js
#	admin/javascript/lang/sk.js
#	admin/javascript/lang/sl.js
#	admin/javascript/lang/sr.js
#	admin/javascript/lang/sr@latin.js
#	admin/javascript/lang/sr_RS.js
#	admin/javascript/lang/sr_RS@latin.js
#	admin/javascript/lang/src/cs.js
#	admin/javascript/lang/src/de.js
#	admin/javascript/lang/src/en.js
#	admin/javascript/lang/src/eo.js
#	admin/javascript/lang/src/es.js
#	admin/javascript/lang/src/fi.js
#	admin/javascript/lang/src/fr.js
#	admin/javascript/lang/src/id.js
#	admin/javascript/lang/src/id_ID.js
#	admin/javascript/lang/src/it.js
#	admin/javascript/lang/src/ja.js
#	admin/javascript/lang/src/lt.js
#	admin/javascript/lang/src/mi.js
#	admin/javascript/lang/src/nb.js
#	admin/javascript/lang/src/nl.js
#	admin/javascript/lang/src/pl.js
#	admin/javascript/lang/src/ro.js
#	admin/javascript/lang/src/ru.js
#	admin/javascript/lang/src/sk.js
#	admin/javascript/lang/src/sl.js
#	admin/javascript/lang/src/sr.js
#	admin/javascript/lang/src/sr@latin.js
#	admin/javascript/lang/src/sr_RS.js
#	admin/javascript/lang/src/sr_RS@latin.js
#	admin/javascript/lang/src/sv.js
#	admin/javascript/lang/src/zh.js
#	admin/javascript/lang/sv.js
#	admin/javascript/lang/zh.js
#	css/GridField.css
#	forms/gridfield/GridFieldExportButton.php
#	javascript/GridField.js
#	javascript/lang/ar.js
#	javascript/lang/cs.js
#	javascript/lang/de.js
#	javascript/lang/en.js
#	javascript/lang/en_GB.js
#	javascript/lang/eo.js
#	javascript/lang/es.js
#	javascript/lang/fi.js
#	javascript/lang/fr.js
#	javascript/lang/id.js
#	javascript/lang/id_ID.js
#	javascript/lang/it.js
#	javascript/lang/ja.js
#	javascript/lang/lt.js
#	javascript/lang/mi.js
#	javascript/lang/nb.js
#	javascript/lang/nl.js
#	javascript/lang/pl.js
#	javascript/lang/ru.js
#	javascript/lang/sk.js
#	javascript/lang/sl.js
#	javascript/lang/sr.js
#	javascript/lang/sr@latin.js
#	javascript/lang/sr_RS.js
#	javascript/lang/sr_RS@latin.js
#	javascript/lang/src/ar.js
#	javascript/lang/src/cs.js
#	javascript/lang/src/de.js
#	javascript/lang/src/en.js
#	javascript/lang/src/eo.js
#	javascript/lang/src/es.js
#	javascript/lang/src/fi.js
#	javascript/lang/src/fr.js
#	javascript/lang/src/id.js
#	javascript/lang/src/id_ID.js
#	javascript/lang/src/it.js
#	javascript/lang/src/ja.js
#	javascript/lang/src/lt.js
#	javascript/lang/src/mi.js
#	javascript/lang/src/nb.js
#	javascript/lang/src/nl.js
#	javascript/lang/src/pl.js
#	javascript/lang/src/ru.js
#	javascript/lang/src/sk.js
#	javascript/lang/src/sl.js
#	javascript/lang/src/sr.js
#	javascript/lang/src/sr@latin.js
#	javascript/lang/src/sr_RS.js
#	javascript/lang/src/sr_RS@latin.js
#	javascript/lang/src/sv.js
#	javascript/lang/src/zh.js
#	javascript/lang/sv.js
#	javascript/lang/zh.js
#	scss/GridField.scss
#	tests/model/ManyManyListTest.php
#	tests/model/SQLQueryTest.php
2016-05-11 15:19:35 +12:00

363 lines
10 KiB
JavaScript

(function (global, factory) {
if (typeof define === "function" && define.amd) {
define('ss.LeftAndMain.Tree', ['jQuery'], factory);
} else if (typeof exports !== "undefined") {
factory(require('jQuery'));
} else {
var mod = {
exports: {}
};
factory(global.jQuery);
global.ssLeftAndMainTree = mod.exports;
}
})(this, function (_jQuery) {
'use strict';
var _jQuery2 = _interopRequireDefault(_jQuery);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
_jQuery2.default.entwine('ss.tree', function ($) {
$('.cms-tree').entwine({
Hints: null,
IsUpdatingTree: false,
IsLoaded: false,
onadd: function onadd() {
this._super();
if ($.isNumeric(this.data('jstree_instance_id'))) return;
var hints = this.attr('data-hints');
if (hints) this.setHints($.parseJSON(hints));
var self = this;
this.jstree(this.getTreeConfig()).bind('loaded.jstree', function (e, data) {
self.setIsLoaded(true);
data.inst._set_settings({ 'html_data': { 'ajax': {
'url': self.data('urlTree'),
'data': function data(node) {
var params = self.data('searchparams') || [];
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;
}
} } });
self.updateFromEditForm();
self.css('visibility', 'visible');
data.inst.hide_checkboxes();
}).bind('before.jstree', function (e, data) {
if (data.func == 'start_drag') {
if (!self.hasClass('draggable') || self.hasClass('multiselect')) {
e.stopImmediatePropagation();
return false;
}
}
if ($.inArray(data.func, ['check_node', 'uncheck_node'])) {
var node = $(data.args[0]).parents('li:first');
var allowedChildren = node.find('li:not(.disabled)');
if (node.hasClass('disabled') && allowedChildren == 0) {
e.stopImmediatePropagation();
return false;
}
}
}).bind('move_node.jstree', function (e, data) {
if (self.getIsUpdatingTree()) return;
var movedNode = data.rslt.o,
newParentNode = data.rslt.np,
oldParentNode = data.inst._get_parent(movedNode),
newParentID = $(newParentNode).data('id') || 0,
nodeID = $(movedNode).data('id');
var siblingIDs = $.map($(movedNode).siblings().andSelf(), function (el) {
return $(el).data('id');
});
$.ajax({
'url': $.path.addSearchParams(self.data('urlSavetreenode'), self.data('extraParams')),
'type': 'POST',
'data': {
ID: nodeID,
ParentID: newParentID,
SiblingIDs: siblingIDs
},
success: function success() {
if ($('.cms-edit-form :input[name=ID]').val() == nodeID) {
$('.cms-edit-form :input[name=ParentID]').val(newParentID);
}
self.updateNodesFromServer([nodeID]);
},
statusCode: {
403: function _() {
$.jstree.rollback(data.rlbk);
}
}
});
}).bind('select_node.jstree check_node.jstree uncheck_node.jstree', function (e, data) {
$(document).triggerHandler(e, data);
});
},
onremove: function onremove() {
this.jstree('destroy');
this._super();
},
'from .cms-container': {
onafterstatechange: function onafterstatechange(e) {
this.updateFromEditForm();
}
},
'from .cms-container form': {
onaftersubmitform: function onaftersubmitform(e) {
var id = $('.cms-edit-form :input[name=ID]').val();
this.updateNodesFromServer([id]);
}
},
getTreeConfig: function getTreeConfig() {
var self = this;
return {
'core': {
'initially_open': ['record-0'],
'animation': 0,
'html_titles': true
},
'html_data': {},
'ui': {
"select_limit": 1,
'initially_select': [this.find('.current').attr('id')]
},
"crrm": {
'move': {
'check_move': function check_move(data) {
var movedNode = $(data.o),
newParent = $(data.np),
isMovedOntoContainer = data.ot.get_container()[0] == data.np[0],
movedNodeClass = movedNode.getClassname(),
newParentClass = newParent.getClassname(),
hints = self.getHints(),
disallowedChildren = [],
hintKey = newParentClass ? newParentClass : 'Root',
hint = hints && typeof hints[hintKey] != 'undefined' ? hints[hintKey] : null;
if (hint && movedNode.attr('class').match(/VirtualPage-([^\s]*)/)) movedNodeClass = RegExp.$1;
if (hint) disallowedChildren = typeof hint.disallowedChildren != 'undefined' ? hint.disallowedChildren : [];
var isAllowed = movedNode.data('id') !== 0 && !movedNode.hasClass('status-archived') && (!isMovedOntoContainer || data.p == 'inside') && !newParent.hasClass('nochildren') && (!disallowedChildren.length || $.inArray(movedNodeClass, disallowedChildren) == -1);
return isAllowed;
}
}
},
'dnd': {
"drop_target": false,
"drag_target": false
},
'checkbox': {
'two_state': true
},
'themes': {
'theme': 'apple',
'url': $('body').data('frameworkpath') + '/thirdparty/jstree/themes/apple/style.css'
},
'plugins': ['html_data', 'ui', 'dnd', 'crrm', 'themes', 'checkbox']
};
},
search: function search(params, callback) {
if (params) this.data('searchparams', params);else this.removeData('searchparams');
this.jstree('refresh', -1, callback);
},
getNodeByID: function getNodeByID(id) {
return this.find('*[data-id=' + id + ']');
},
createNode: function createNode(html, data, callback) {
var self = this,
parentNode = data.ParentID !== void 0 ? self.getNodeByID(data.ParentID) : false,
newNode = $(html);
var properties = { data: '' };
if (newNode.hasClass('jstree-open')) {
properties.state = 'open';
} else if (newNode.hasClass('jstree-closed')) {
properties.state = 'closed';
}
this.jstree('create_node', parentNode.length ? parentNode : -1, 'last', properties, function (node) {
var origClasses = node.attr('class');
for (var i = 0; i < newNode[0].attributes.length; i++) {
var attr = newNode[0].attributes[i];
node.attr(attr.name, attr.value);
}
node.addClass(origClasses).html(newNode.html());
callback(node);
});
},
updateNode: function updateNode(node, html, data) {
var self = this,
newNode = $(html),
origClasses = node.attr('class');
var nextNode = data.NextID ? this.getNodeByID(data.NextID) : false;
var prevNode = data.PrevID ? this.getNodeByID(data.PrevID) : false;
var parentNode = data.ParentID ? this.getNodeByID(data.ParentID) : false;
$.each(['id', 'style', 'class', 'data-pagetype'], function (i, attrName) {
node.attr(attrName, newNode.attr(attrName));
});
origClasses = origClasses.replace(/status-[^\s]*/, '');
var origChildren = node.children('ul').detach();
node.addClass(origClasses).html(newNode.html()).append(origChildren);
if (nextNode && nextNode.length) {
this.jstree('move_node', node, nextNode, 'before');
} else if (prevNode && prevNode.length) {
this.jstree('move_node', node, prevNode, 'after');
} else {
this.jstree('move_node', node, parentNode.length ? parentNode : -1);
}
},
updateFromEditForm: function updateFromEditForm() {
var node,
id = $('.cms-edit-form :input[name=ID]').val();
if (id) {
node = this.getNodeByID(id);
if (node.length) {
this.jstree('deselect_all');
this.jstree('select_node', node);
} else {
this.updateNodesFromServer([id]);
}
} else {
this.jstree('deselect_all');
}
},
updateNodesFromServer: function updateNodesFromServer(ids) {
if (this.getIsUpdatingTree() || !this.getIsLoaded()) return;
var self = this,
i,
includesNewNode = false;
this.setIsUpdatingTree(true);
self.jstree('save_selected');
var correctStateFn = function correctStateFn(node) {
self.getNodeByID(node.data('id')).not(node).remove();
self.jstree('deselect_all');
self.jstree('select_node', node);
};
self.jstree('open_node', this.getNodeByID(0));
self.jstree('save_opened');
self.jstree('save_selected');
$.ajax({
url: $.path.addSearchParams(this.data('urlUpdatetreenodes'), 'ids=' + ids.join(',')),
dataType: 'json',
success: function success(data, xhr) {
$.each(data, function (nodeId, nodeData) {
var node = self.getNodeByID(nodeId);
if (!nodeData) {
self.jstree('delete_node', node);
return;
}
if (node.length) {
self.updateNode(node, nodeData.html, nodeData);
setTimeout(function () {
correctStateFn(node);
}, 500);
} else {
includesNewNode = true;
if (nodeData.ParentID && !self.find('li[data-id=' + nodeData.ParentID + ']').length) {
self.jstree('load_node', -1, function () {
newNode = self.find('li[data-id=' + nodeId + ']');
correctStateFn(newNode);
});
} else {
self.createNode(nodeData.html, nodeData, function (newNode) {
correctStateFn(newNode);
});
}
}
});
if (!includesNewNode) {
self.jstree('deselect_all');
self.jstree('reselect');
self.jstree('reopen');
}
},
complete: function complete() {
self.setIsUpdatingTree(false);
}
});
}
});
$('.cms-tree.multiple').entwine({
onmatch: function onmatch() {
this._super();
this.jstree('show_checkboxes');
},
onunmatch: function onunmatch() {
this._super();
this.jstree('uncheck_all');
this.jstree('hide_checkboxes');
},
getSelectedIDs: function getSelectedIDs() {
return $(this).jstree('get_checked').not('.disabled').map(function () {
return $(this).data('id');
}).get();
}
});
$('.cms-tree li').entwine({
setEnabled: function setEnabled(bool) {
this.toggleClass('disabled', !bool);
},
getClassname: function getClassname() {
var matches = this.attr('class').match(/class-([^\s]*)/i);
return matches ? matches[1] : '';
},
getID: function getID() {
return this.data('id');
}
});
});
});