2016-01-26 05:28:36 +01:00
|
|
|
(function (global, factory) {
|
|
|
|
if (typeof define === "function" && define.amd) {
|
2016-01-31 21:39:10 +01:00
|
|
|
define('ss.CMSMain.AddForm', ['jQuery'], factory);
|
2016-01-26 05:28:36 +01:00
|
|
|
} else if (typeof exports !== "undefined") {
|
2016-01-31 21:39:10 +01:00
|
|
|
factory(require('jQuery'));
|
2016-01-26 05:28:36 +01:00
|
|
|
} else {
|
|
|
|
var mod = {
|
|
|
|
exports: {}
|
|
|
|
};
|
|
|
|
factory(global.jQuery);
|
|
|
|
global.ssCMSMainAddForm = 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', function ($) {
|
2016-03-04 02:58:17 +01:00
|
|
|
/**
|
|
|
|
* Reset the parent node selection if the type is
|
|
|
|
* set back to "toplevel page", to avoid submitting inconsistent state.
|
|
|
|
*/
|
2016-01-26 05:28:36 +01:00
|
|
|
$(".cms-add-form .parent-mode :input").entwine({
|
|
|
|
onclick: function onclick(e) {
|
|
|
|
if (this.val() == 'top') {
|
|
|
|
var parentField = this.closest('form').find('#Form_AddForm_ParentID_Holder .TreeDropdownField');
|
|
|
|
parentField.setValue('');
|
|
|
|
parentField.setTitle('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$(".cms-add-form").entwine({
|
2016-03-04 02:58:17 +01:00
|
|
|
ParentID: 0, // Last selected parentID
|
|
|
|
ParentCache: {}, // Cache allowed children for each selected page
|
2016-01-26 05:28:36 +01:00
|
|
|
onadd: function onadd() {
|
|
|
|
var self = this;
|
|
|
|
this.find('#Form_AddForm_ParentID_Holder .TreeDropdownField').bind('change', function () {
|
|
|
|
self.updateTypeList();
|
|
|
|
});
|
|
|
|
this.find(".SelectionGroup.parent-mode").bind('change', function () {
|
|
|
|
self.updateTypeList();
|
|
|
|
});
|
|
|
|
this.updateTypeList();
|
|
|
|
},
|
|
|
|
loadCachedChildren: function loadCachedChildren(parentID) {
|
|
|
|
var cache = this.getParentCache();
|
|
|
|
if (typeof cache[parentID] !== 'undefined') return cache[parentID];else return null;
|
|
|
|
},
|
|
|
|
saveCachedChildren: function saveCachedChildren(parentID, children) {
|
|
|
|
var cache = this.getParentCache();
|
|
|
|
cache[parentID] = children;
|
|
|
|
this.setParentCache(cache);
|
|
|
|
},
|
2016-03-04 02:58:17 +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.
|
|
|
|
* Similar implementation to LeftAndMain.Tree.js.
|
|
|
|
*/
|
2016-01-26 05:28:36 +01:00
|
|
|
updateTypeList: function updateTypeList() {
|
|
|
|
var hints = this.data('hints'),
|
|
|
|
parentTree = this.find('#Form_AddForm_ParentID_Holder .TreeDropdownField'),
|
|
|
|
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) {
|
2016-03-04 02:58:17 +01:00
|
|
|
// Prevent interface operations
|
2016-01-26 05:28:36 +01:00
|
|
|
if (this.hasClass('loading')) return;
|
|
|
|
this.addClass('loading');
|
2016-03-04 02:58:17 +01:00
|
|
|
|
|
|
|
// Enable last parent ID to be re-selected from memory
|
2016-01-26 05:28:36 +01:00
|
|
|
this.setParentID(id);
|
|
|
|
if (!parentTree.getValue()) parentTree.setValue(id);
|
|
|
|
|
2016-03-04 02:58:17 +01:00
|
|
|
// Use cached data if available
|
|
|
|
disallowedChildren = this.loadCachedChildren(id);
|
2016-01-26 05:28:36 +01:00
|
|
|
if (disallowedChildren !== null) {
|
|
|
|
this.updateSelectionFilter(disallowedChildren, defaultChildClass);
|
|
|
|
this.removeClass('loading');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
url: self.data('childfilter'),
|
2016-03-04 02:58:17 +01:00
|
|
|
data: { 'ParentID': id },
|
2016-01-26 05:28:36 +01:00
|
|
|
success: function success(data) {
|
2016-03-04 02:58:17 +01:00
|
|
|
// reload current form and tree
|
2016-01-26 05:28:36 +01:00
|
|
|
self.saveCachedChildren(id, data);
|
|
|
|
self.updateSelectionFilter(data, defaultChildClass);
|
|
|
|
},
|
|
|
|
complete: function complete() {
|
|
|
|
self.removeClass('loading');
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
disallowedChildren = hint && typeof hint.disallowedChildren !== 'undefined' ? hint.disallowedChildren : [], this.updateSelectionFilter(disallowedChildren, defaultChildClass);
|
|
|
|
}
|
|
|
|
},
|
2016-03-04 02:58:17 +01:00
|
|
|
/**
|
|
|
|
* Update the selection filter with the given blacklist and default selection
|
|
|
|
*
|
|
|
|
* @param array disallowedChildren
|
|
|
|
* @param string defaultChildClass
|
|
|
|
*/
|
2016-01-26 05:28:36 +01:00
|
|
|
updateSelectionFilter: function updateSelectionFilter(disallowedChildren, defaultChildClass) {
|
2016-03-04 02:58:17 +01:00
|
|
|
// Limit selection
|
|
|
|
var allAllowed = null; // troolian
|
2016-01-26 05:28:36 +01:00
|
|
|
this.find('#Form_AddForm_PageType li').each(function () {
|
|
|
|
var className = $(this).find('input').val(),
|
|
|
|
isAllowed = $.inArray(className, disallowedChildren) === -1;
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$(this).setEnabled(isAllowed);
|
|
|
|
if (!isAllowed) $(this).setSelected(false);
|
|
|
|
if (allAllowed === null) allAllowed = isAllowed;else allAllowed = allAllowed && isAllowed;
|
|
|
|
});
|
|
|
|
|
2016-03-04 02:58:17 +01:00
|
|
|
// Set default child selection, or fall back to first available option
|
2016-01-26 05:28:36 +01:00
|
|
|
if (defaultChildClass) {
|
|
|
|
var selectedEl = this.find('#Form_AddForm_PageType li input[value=' + defaultChildClass + ']').parents('li:first');
|
|
|
|
} else {
|
|
|
|
var selectedEl = this.find('#Form_AddForm_PageType li:not(.disabled):first');
|
|
|
|
}
|
|
|
|
selectedEl.setSelected(true);
|
|
|
|
selectedEl.siblings().setSelected(false);
|
2016-03-04 02:58:17 +01:00
|
|
|
|
|
|
|
// Disable the "Create" button if none of the pagetypes are available
|
2016-01-26 05:28:36 +01:00
|
|
|
var buttonState = this.find('#Form_AddForm_PageType li:not(.disabled)').length ? 'enable' : 'disable';
|
|
|
|
this.find('button[name=action_doAdd]').button(buttonState);
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
this.find('.message-restricted')[allAllowed ? 'hide' : 'show']();
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$(".cms-add-form #Form_AddForm_PageType li").entwine({
|
|
|
|
onclick: function onclick(e) {
|
|
|
|
this.setSelected(true);
|
|
|
|
},
|
|
|
|
setSelected: function setSelected(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 setEnabled(bool) {
|
|
|
|
$(this).toggleClass('disabled', !bool);
|
|
|
|
if (!bool) $(this).find('input').attr('disabled', 'disabled').removeAttr('checked');else $(this).find('input').removeAttr('disabled');
|
|
|
|
}
|
|
|
|
});
|
2016-03-04 02:58:17 +01:00
|
|
|
|
2016-01-26 05:28:36 +01:00
|
|
|
$(".cms-page-add-button").entwine({
|
|
|
|
onclick: function onclick(e) {
|
|
|
|
var tree = $('.cms-tree'),
|
|
|
|
list = $('.cms-list'),
|
|
|
|
parentId = 0;
|
|
|
|
|
2016-03-04 02:58:17 +01:00
|
|
|
// Choose parent ID either from tree or list view, depending which is visible
|
2016-01-26 05:28:36 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-04 02:58:17 +01:00
|
|
|
var data = { selector: this.data('targetPanel'), pjax: this.data('pjax') },
|
2016-01-26 05:28:36 +01:00
|
|
|
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();
|
2016-03-04 02:58:17 +01:00
|
|
|
|
|
|
|
// Remove focussed state from button
|
2016-01-26 05:28:36 +01:00
|
|
|
this.blur();
|
2016-03-04 02:58:17 +01:00
|
|
|
|
|
|
|
// $('.cms-page-add-form-dialog').dialog('open');
|
|
|
|
// e.preventDefault();
|
2016-01-26 05:28:36 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|