2017-05-29 07:24:48 +02:00
|
|
|
import $ from 'jquery';
|
2016-01-26 05:28:36 +01:00
|
|
|
|
|
|
|
$.entwine('ss', function($){
|
2017-08-30 12:40:56 +02:00
|
|
|
$('.TreeDropdownField').entwine({
|
|
|
|
OldValue: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#Form_AddForm_ParentID_Holder .treedropdownfield').entwine({
|
|
|
|
onmatch() {
|
|
|
|
this._super();
|
|
|
|
$('.cms-add-form').updateTypeList();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-25 07:25:22 +02:00
|
|
|
/**
|
2017-08-30 12:40:56 +02:00
|
|
|
* Reset the parent node selection if the type is set back to "toplevel page",
|
|
|
|
* to avoid submitting inconsistent state.
|
2016-07-25 07:25:22 +02:00
|
|
|
*/
|
|
|
|
$(".cms-add-form .parent-mode :input").entwine({
|
|
|
|
onclick: function(e) {
|
2017-08-30 12:40:56 +02:00
|
|
|
var parentField = this.closest('form').find('#Form_AddForm_ParentID_Holder .TreeDropdownField');
|
2016-07-25 07:25:22 +02:00
|
|
|
if(this.val() == 'top') {
|
2017-08-30 12:40:56 +02:00
|
|
|
parentField.setOldValue(parentField.getValue());
|
|
|
|
parentField.setValue(0);
|
|
|
|
} else {
|
|
|
|
parentField.setValue(parentField.getOldValue() || 0);
|
|
|
|
parentField.setOldValue(null);
|
2016-07-25 07:25:22 +02:00
|
|
|
}
|
2017-08-30 12:40:56 +02:00
|
|
|
parentField.refresh();
|
|
|
|
parentField.trigger('change');
|
2016-07-25 07:25:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".cms-add-form").entwine({
|
|
|
|
ParentCache: {}, // Cache allowed children for each selected page
|
|
|
|
onadd: function() {
|
|
|
|
var self = this;
|
2016-07-26 02:56:48 +02:00
|
|
|
// See SelectionGroup.js for logic which hides / shows ParentID
|
2022-11-21 05:11:42 +01:00
|
|
|
this.find('#Form_AddForm_ParentID_Holder .TreeDropdownField').bind('change', function() {
|
2016-07-25 07:25:22 +02:00
|
|
|
self.updateTypeList();
|
|
|
|
});
|
2022-11-21 05:11:42 +01:00
|
|
|
this.find(".SelectionGroup.parent-mode").bind('change', function() {
|
2016-07-25 07:25:22 +02:00
|
|
|
self.updateTypeList();
|
|
|
|
});
|
2017-08-30 12:40:56 +02:00
|
|
|
if ($(".cms-add-form .parent-mode :input").val() == 'top') {
|
|
|
|
this.updateTypeList();
|
|
|
|
}
|
2016-07-25 07:25:22 +02:00
|
|
|
},
|
|
|
|
loadCachedChildren: function(parentID) {
|
|
|
|
var cache = this.getParentCache();
|
2017-08-30 12:40:56 +02:00
|
|
|
if(typeof cache[parentID] !== 'undefined') {
|
|
|
|
return cache[parentID];
|
|
|
|
}
|
|
|
|
return null;
|
2016-07-25 07:25:22 +02:00
|
|
|
},
|
|
|
|
saveCachedChildren: function(parentID, children) {
|
|
|
|
var cache = this.getParentCache();
|
|
|
|
cache[parentID] = children;
|
|
|
|
this.setParentCache(cache);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Limit page type selection based on parent selection.
|
|
|
|
* Select of root classes is pre-computed, but selections with a given parent
|
|
|
|
* are updated on-demand.
|
|
|
|
* Similar implementation to LeftAndMain.Tree.js.
|
|
|
|
*/
|
|
|
|
updateTypeList: function() {
|
|
|
|
var hints = this.data('hints'),
|
2017-11-19 21:17:22 +01:00
|
|
|
parentTree = this.find('#Form_AddForm_ParentID'),
|
2016-07-25 07:25:22 +02:00
|
|
|
parentMode = this.find("input[name=ParentModeField]:checked").val(),
|
|
|
|
metadata = parentTree.data('metadata'),
|
2017-11-19 21:17:22 +01:00
|
|
|
id = (parentMode === 'child')
|
2017-08-30 12:40:56 +02:00
|
|
|
? parentTree.getValue()
|
2016-07-25 07:25:22 +02:00
|
|
|
: null,
|
|
|
|
newClassName = metadata ? metadata.ClassName : null,
|
2016-12-28 17:02:15 +01:00
|
|
|
hintKey = (newClassName && parentMode === 'child' && id)
|
2016-07-25 07:25:22 +02:00
|
|
|
? newClassName
|
|
|
|
: 'Root',
|
|
|
|
hint = (typeof hints[hintKey] !== 'undefined') ? hints[hintKey] : null,
|
|
|
|
self = this,
|
|
|
|
defaultChildClass = (hint && typeof hint.defaultChild !== 'undefined')
|
|
|
|
? hint.defaultChild
|
|
|
|
: null,
|
|
|
|
disallowedChildren = [];
|
|
|
|
|
|
|
|
if(id) {
|
|
|
|
// Prevent interface operations
|
|
|
|
if(this.hasClass('loading')) return;
|
|
|
|
this.addClass('loading');
|
|
|
|
|
|
|
|
// Use cached data if available
|
|
|
|
disallowedChildren = this.loadCachedChildren(id);
|
|
|
|
if(disallowedChildren !== null) {
|
|
|
|
this.updateSelectionFilter(disallowedChildren, defaultChildClass);
|
|
|
|
this.removeClass('loading');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
url: self.data('childfilter'),
|
|
|
|
data: {'ParentID': id},
|
|
|
|
success: function(data) {
|
|
|
|
// reload current form and tree
|
|
|
|
self.saveCachedChildren(id, data);
|
|
|
|
self.updateSelectionFilter(data, defaultChildClass);
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
self.removeClass('loading');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
disallowedChildren = (hint && typeof hint.disallowedChildren !== 'undefined')
|
|
|
|
? hint.disallowedChildren
|
|
|
|
: [];
|
|
|
|
this.updateSelectionFilter(disallowedChildren, defaultChildClass);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Update the selection filter with the given blacklist and default selection
|
|
|
|
*
|
|
|
|
* @param {Array} disallowedChildren
|
|
|
|
* @param {string} defaultChildClass
|
|
|
|
*/
|
|
|
|
updateSelectionFilter: function(disallowedChildren, defaultChildClass) {
|
2019-05-31 05:50:10 +02:00
|
|
|
var currentSelection = this.find('#Form_AddForm_PageType div.radio.selected')[0];
|
|
|
|
var keepSelection = false;
|
|
|
|
|
2016-07-25 07:25:22 +02:00
|
|
|
// Limit selection
|
|
|
|
var allAllowed = null; // troolian
|
2019-05-31 05:50:10 +02:00
|
|
|
this.find('#Form_AddForm_PageType div.radio').each(function (i, el) {
|
2016-07-25 07:25:22 +02:00
|
|
|
var className = $(this).find('input').val(),
|
|
|
|
isAllowed = ($.inArray(className, disallowedChildren) === -1);
|
|
|
|
|
2019-05-31 05:50:10 +02:00
|
|
|
// Avoid changing the selected pagetype if still allowed
|
|
|
|
if (el === currentSelection && isAllowed) {
|
|
|
|
keepSelection = true;
|
|
|
|
}
|
|
|
|
|
2016-07-25 07:25:22 +02:00
|
|
|
$(this).setEnabled(isAllowed);
|
|
|
|
if(!isAllowed) {
|
|
|
|
$(this).setSelected(false);
|
|
|
|
}
|
|
|
|
if(allAllowed === null) {
|
|
|
|
allAllowed = isAllowed;
|
|
|
|
} else {
|
|
|
|
allAllowed = allAllowed && isAllowed;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-31 05:50:10 +02:00
|
|
|
// Keep current selection if we can, or set default child selection, or fall back to first available option
|
|
|
|
if (keepSelection) {
|
|
|
|
var selectedEl = $(currentSelection).parents('li:first');
|
|
|
|
} else if (defaultChildClass) {
|
2016-07-25 07:25:22 +02:00
|
|
|
var selectedEl = this
|
|
|
|
.find('#Form_AddForm_PageType div.radio input[value=' + defaultChildClass + ']')
|
|
|
|
.parents('li:first');
|
|
|
|
} else {
|
|
|
|
var selectedEl = this.find('#Form_AddForm_PageType div.radio:not(.disabled):first');
|
|
|
|
}
|
|
|
|
selectedEl.setSelected(true);
|
|
|
|
selectedEl.siblings().setSelected(false);
|
|
|
|
|
|
|
|
// Disable the "Create" button if none of the pagetypes are available
|
2016-11-30 21:31:15 +01:00
|
|
|
if(this.find('#Form_AddForm_PageType div.radio:not(.disabled)').length) {
|
|
|
|
this.find('button[name=action_doAdd]').removeAttr('disabled');
|
|
|
|
} else {
|
2017-01-24 04:28:26 +01:00
|
|
|
this.find('button[name=action_doAdd]').attr('disabled', 'disabled');
|
2016-11-30 21:31:15 +01:00
|
|
|
}
|
2016-07-25 07:25:22 +02:00
|
|
|
|
|
|
|
this.find('.message-restricted')[allAllowed ? 'hide' : 'show']();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".cms-add-form #Form_AddForm_PageType div.radio").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
this.setSelected(true);
|
|
|
|
},
|
|
|
|
setSelected: function(bool) {
|
|
|
|
var input = this.find('input');
|
|
|
|
if(bool && !input.is(':disabled')) {
|
|
|
|
this.siblings().setSelected(false);
|
|
|
|
this.toggleClass('selected', true);
|
|
|
|
input.prop('checked', true);
|
|
|
|
} else {
|
|
|
|
this.toggleClass('selected', false);
|
|
|
|
input.prop('checked', false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setEnabled: function(bool) {
|
|
|
|
$(this).toggleClass('disabled', !bool);
|
|
|
|
if(!bool) {
|
|
|
|
$(this).find('input').attr('disabled', 'disabled').removeAttr('checked');
|
|
|
|
} else {
|
|
|
|
$(this).find('input').removeAttr('disabled');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".cms-content-addpage-button").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
var tree = $('.cms-tree'), list = $('.cms-list'), parentId = 0, extraParams;
|
|
|
|
|
|
|
|
// Choose parent ID either from tree or list view, depending which is visible
|
|
|
|
if(tree.is(':visible')) {
|
|
|
|
var selected = tree.jstree('get_selected');
|
|
|
|
parentId = selected ? $(selected[0]).data('id') : null;
|
|
|
|
} else {
|
|
|
|
var state = list.find('input[name="Page[GridState]"]').val();
|
|
|
|
if(state) {
|
|
|
|
parentId = parseInt(JSON.parse(state).ParentID, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var data = {selector: this.data('targetPanel'),pjax: this.data('pjax')}, url;
|
|
|
|
if(parentId) {
|
|
|
|
extraParams = this.data('extraParams') ? this.data('extraParams') : '';
|
|
|
|
url = $.path.addSearchParams(i18n.sprintf(this.data('urlAddpage'), parentId), extraParams);
|
|
|
|
} else {
|
|
|
|
url = this.attr('href');
|
|
|
|
}
|
|
|
|
$('.cms-container').loadPanel(url, null, data);
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Remove focussed state from button
|
|
|
|
this.blur();
|
|
|
|
|
|
|
|
// $('.cms-page-add-form-dialog').dialog('open');
|
|
|
|
// e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2016-01-26 05:28:36 +01:00
|
|
|
});
|