2011-04-24 01:05:01 +02:00
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($){
|
2012-05-07 14:30:08 +02:00
|
|
|
/**
|
|
|
|
* Reset the parent node selection if the type is
|
|
|
|
* set back to "toplevel page", to avoid submitting inconsistent state.
|
|
|
|
*/
|
|
|
|
$(".cms-add-form .parent-mode :input").entwine({
|
|
|
|
onclick: function(e) {
|
|
|
|
if(this.val() == 'top') {
|
2013-08-28 11:10:32 +02:00
|
|
|
var parentField = this.closest('form').find('#Form_AddForm_ParentID_Holder .TreeDropdownField')
|
2012-05-07 14:30:08 +02:00
|
|
|
parentField.setValue('');
|
2013-03-25 23:41:21 +01:00
|
|
|
parentField.setTitle('');
|
2012-05-07 14:30:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-05-08 13:36:55 +02:00
|
|
|
|
|
|
|
$(".cms-add-form").entwine({
|
2015-03-11 06:54:08 +01:00
|
|
|
ParentID: 0, // Last selected parentID
|
|
|
|
ParentCache: {}, // Cache allowed children for each selected page
|
2013-04-08 01:03:12 +02:00
|
|
|
onadd: function() {
|
2011-05-08 13:36:55 +02:00
|
|
|
var self = this;
|
2013-08-28 11:10:32 +02:00
|
|
|
this.find('#Form_AddForm_ParentID_Holder .TreeDropdownField').bind('change', function() {
|
2011-05-08 13:36:55 +02:00
|
|
|
self.updateTypeList();
|
|
|
|
});
|
2015-03-11 06:54:08 +01:00
|
|
|
this.find(".SelectionGroup.parent-mode").bind('change', function() {
|
|
|
|
self.updateTypeList();
|
|
|
|
});
|
2012-01-05 17:01:48 +01:00
|
|
|
this.updateTypeList();
|
2012-05-14 01:43:55 +02:00
|
|
|
},
|
2015-03-11 06:54:08 +01:00
|
|
|
loadCachedChildren: function(parentID) {
|
|
|
|
var cache = this.getParentCache();
|
|
|
|
if(typeof cache[parentID] !== 'undefined') return cache[parentID];
|
|
|
|
else return null;
|
|
|
|
},
|
|
|
|
saveCachedChildren: function(parentID, children) {
|
|
|
|
var cache = this.getParentCache();
|
|
|
|
cache[parentID] = children;
|
|
|
|
this.setParentCache(cache);
|
|
|
|
},
|
2011-05-08 13:36:55 +02:00
|
|
|
/**
|
2015-03-11 06:54:08 +01:00
|
|
|
* 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.
|
2011-05-08 13:36:55 +02:00
|
|
|
* Similar implementation to LeftAndMain.Tree.js.
|
|
|
|
*/
|
|
|
|
updateTypeList: function() {
|
2015-03-11 06:54:08 +01:00
|
|
|
var hints = this.data('hints'),
|
2015-03-31 08:54:43 +02:00
|
|
|
parentTree = this.find('#Form_AddForm_ParentID_Holder .TreeDropdownField'),
|
2015-03-11 06:54:08 +01:00
|
|
|
parentMode = this.find("input[name=ParentModeField]:checked").val(),
|
|
|
|
metadata = parentTree.data('metadata'),
|
|
|
|
id = (metadata && parentMode === 'child')
|
|
|
|
? (parentTree.getValue() || this.getParentID())
|
|
|
|
: null,
|
|
|
|
newClassName = metadata ? metadata.ClassName : null,
|
|
|
|
hintKey = (newClassName && parentMode === 'child')
|
|
|
|
? 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');
|
|
|
|
|
|
|
|
// Enable last parent ID to be re-selected from memory
|
|
|
|
this.setParentID(id);
|
|
|
|
if(!parentTree.getValue()) parentTree.setValue(id);
|
|
|
|
|
|
|
|
// 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) {
|
2011-05-08 13:36:55 +02:00
|
|
|
// Limit selection
|
2015-03-11 06:54:08 +01:00
|
|
|
var allAllowed = null; // troolian
|
2013-08-28 11:10:32 +02:00
|
|
|
this.find('#Form_AddForm_PageType_Holder li').each(function() {
|
2012-12-03 16:33:06 +01:00
|
|
|
var className = $(this).find('input').val(),
|
2015-03-11 06:54:08 +01:00
|
|
|
isAllowed = ($.inArray(className, disallowedChildren) === -1);
|
2012-12-03 16:33:06 +01:00
|
|
|
|
2011-05-08 13:36:55 +02:00
|
|
|
$(this).setEnabled(isAllowed);
|
2012-12-03 16:33:06 +01:00
|
|
|
if(!isAllowed) $(this).setSelected(false);
|
2015-03-11 06:54:08 +01:00
|
|
|
if(allAllowed === null) allAllowed = isAllowed;
|
|
|
|
else allAllowed = allAllowed && isAllowed;
|
2011-05-08 13:36:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Set default child selection, or fall back to first available option
|
|
|
|
if(defaultChildClass) {
|
2015-03-31 08:54:43 +02:00
|
|
|
var selectedEl = this
|
|
|
|
.find('#Form_AddForm_PageType_Holder li input[value=' + defaultChildClass + ']')
|
|
|
|
.parents('li:first');
|
2011-05-08 13:36:55 +02:00
|
|
|
} else {
|
2013-08-28 11:10:32 +02:00
|
|
|
var selectedEl = this.find('#Form_AddForm_PageType_Holder li:not(.disabled):first');
|
2011-05-08 13:36:55 +02:00
|
|
|
}
|
|
|
|
selectedEl.setSelected(true);
|
|
|
|
selectedEl.siblings().setSelected(false);
|
2012-09-11 04:56:13 +02:00
|
|
|
|
|
|
|
// Disable the "Create" button if none of the pagetypes are available
|
2015-03-31 08:54:43 +02:00
|
|
|
var buttonState = this.find('#Form_AddForm_PageType_Holder li:not(.disabled)').length
|
|
|
|
? 'enable'
|
|
|
|
: 'disable';
|
2012-09-11 04:56:13 +02:00
|
|
|
this.find('button[name=action_doAdd]').button(buttonState);
|
2012-12-03 16:33:06 +01:00
|
|
|
|
|
|
|
this.find('.message-restricted')[allAllowed ? 'hide' : 'show']();
|
2011-05-08 13:36:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-28 11:10:32 +02:00
|
|
|
$(".cms-add-form #Form_AddForm_PageType_Holder li").entwine({
|
2013-03-12 23:05:47 +01:00
|
|
|
onclick: function(e) {
|
2012-01-05 17:01:48 +01:00
|
|
|
this.setSelected(true);
|
|
|
|
},
|
2011-05-08 13:36:55 +02:00
|
|
|
setSelected: function(bool) {
|
2012-01-06 16:54:16 +01:00
|
|
|
var input = this.find('input');
|
|
|
|
if(bool && !input.is(':disabled')) {
|
2012-01-05 17:01:48 +01:00
|
|
|
this.siblings().setSelected(false);
|
2015-03-11 06:54:08 +01:00
|
|
|
this.toggleClass('selected', true);
|
|
|
|
input.prop('checked', true);
|
|
|
|
} else {
|
|
|
|
this.toggleClass('selected', false);
|
|
|
|
input.prop('checked', false);
|
2012-01-05 17:01:48 +01:00
|
|
|
}
|
2011-05-08 13:36:55 +02:00
|
|
|
},
|
|
|
|
setEnabled: function(bool) {
|
2012-01-05 17:01:48 +01:00
|
|
|
$(this).toggleClass('disabled', !bool);
|
|
|
|
if(!bool) $(this).find('input').attr('disabled', 'disabled').removeAttr('checked');
|
|
|
|
else $(this).find('input').removeAttr('disabled');
|
2011-04-24 01:05:01 +02:00
|
|
|
}
|
|
|
|
});
|
2013-11-11 13:31:32 +01:00
|
|
|
|
2011-04-24 01:05:01 +02:00
|
|
|
$(".cms-page-add-button").entwine({
|
|
|
|
onclick: function(e) {
|
2013-03-14 18:59:19 +01:00
|
|
|
var tree = $('.cms-tree'), list = $('.cms-list'), parentId = 0;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2013-04-30 16:54:18 +02:00
|
|
|
var data = {selector: this.data('targetPanel'),pjax: this.data('pjax')}, url;
|
2013-05-14 16:38:07 +02:00
|
|
|
if(parentId) {
|
|
|
|
extraParams = this.data('extraParams') ? this.data('extraParams') : '';
|
|
|
|
url = $.path.addSearchParams(ss.i18n.sprintf(this.data('urlAddpage'), parentId), extraParams);
|
2013-04-30 16:54:18 +02:00
|
|
|
} else {
|
|
|
|
url = this.attr('href');
|
|
|
|
}
|
2012-06-03 19:54:14 +02:00
|
|
|
|
|
|
|
$('.cms-container').loadPanel(url, null, data);
|
2011-04-24 01:05:01 +02:00
|
|
|
e.preventDefault();
|
2012-06-03 19:54:14 +02:00
|
|
|
|
2013-12-16 15:44:24 +01:00
|
|
|
// Remove focussed state from button
|
|
|
|
this.blur();
|
|
|
|
|
2012-06-03 19:54:14 +02:00
|
|
|
// $('.cms-page-add-form-dialog').dialog('open');
|
|
|
|
// e.preventDefault();
|
2011-04-24 01:05:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2012-05-11 07:37:31 +02:00
|
|
|
}(jQuery));
|