2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* File: LeftAndMain.AddForm.js
|
|
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
$.entwine('ss', function($){
|
|
|
|
/**
|
2011-03-31 10:52:29 +02:00
|
|
|
* Class: .add-form
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
|
|
|
* Simple form with a page type dropdown
|
2011-03-31 10:52:29 +02:00
|
|
|
* which creates a new page through .cms-edit-form and adds a new tree node.
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
|
|
|
* Requires:
|
|
|
|
* ss.i18n
|
2011-03-31 10:52:29 +02:00
|
|
|
* .cms-edit-form
|
2011-03-23 10:51:00 +01:00
|
|
|
*/
|
2012-04-05 22:10:25 +02:00
|
|
|
$('.cms-edit-form.cms-add-form').entwine({
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Variable: Tree
|
|
|
|
* (DOMElement)
|
|
|
|
*/
|
|
|
|
Tree: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable: OrigOptions
|
|
|
|
* (Array) Map of <option> values to an object of "title" and "value"
|
|
|
|
*/
|
|
|
|
OrigOptions: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variable: NewPages
|
|
|
|
* (Array) Internal counter to create unique page identifiers prior to ajax saving
|
|
|
|
*/
|
|
|
|
NewPages: [],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor: onmatch
|
|
|
|
*/
|
|
|
|
onmatch: function() {
|
|
|
|
var self = this, typeDropdown = this.find(':input[name=PageType]');
|
|
|
|
|
2011-04-17 09:55:23 +02:00
|
|
|
var tree = $('.cms-tree');
|
2011-03-23 10:51:00 +01:00
|
|
|
this.setTree(tree);
|
|
|
|
|
|
|
|
// Event bindings
|
|
|
|
$(tree).bind('select_node.jstree', function(e, data) {self.refresh(data.rslt.obj);});
|
|
|
|
typeDropdown.bind('change', function(e) {self.refresh();});
|
|
|
|
// TODO Bind on tree initialization to set dropdown for selected node
|
|
|
|
|
|
|
|
// Store original page type options (they might get filtered to "allowed_children") later on
|
|
|
|
// TODO Better DOM element serialization (jQuery 1.4?)
|
|
|
|
var opts = {};
|
|
|
|
typeDropdown.find('option').each(function(el) {
|
|
|
|
opts[$(this).val()] = {html:$(this).html(), value: $(this).val()};
|
|
|
|
});
|
|
|
|
this.setOrigOptions(opts);
|
|
|
|
|
|
|
|
this._super();
|
|
|
|
},
|
2012-05-22 01:28:02 +02:00
|
|
|
onunmatch: function() {
|
|
|
|
this._super();
|
2012-05-22 04:57:30 +02:00
|
|
|
},
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Function: onsubmit
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Event) e
|
|
|
|
*/
|
|
|
|
onsubmit: function(e) {
|
|
|
|
var newPages = this.getNewPages(), tree = this.getTree(), node = $(tree).jstree('get_selected');
|
|
|
|
var parentID = (node.length) ? node.data('id') : 0;
|
|
|
|
|
|
|
|
// TODO: Remove 'new-' code http://open.silverstripe.com/ticket/875
|
|
|
|
// if(parentID && parentID.substr(0,3) == 'new') {
|
|
|
|
// alert(ss.i18n._t('CMSMAIN.WARNINGSAVEPAGESBEFOREADDING'));
|
|
|
|
// }
|
|
|
|
|
|
|
|
if(node && node.hasClass("nochildren")) {
|
|
|
|
alert(ss.i18n._t('CMSMAIN.CANTADDCHILDREN') );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally initalize the new pages tracker
|
|
|
|
if(!newPages[parentID] ) newPages[parentID] = 1;
|
|
|
|
|
|
|
|
// default to first button
|
|
|
|
var button = this.find(':submit:first');
|
|
|
|
button.addClass('loading');
|
|
|
|
|
|
|
|
// collect data and submit the form
|
|
|
|
var data = this.serializeArray();
|
|
|
|
data.push({name:'Suffix',value:newPages[parentID]++});
|
|
|
|
data.push({name:button.attr('name'),value:button.val()});
|
2012-03-24 03:19:02 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// TODO Should be set by hiddenfield already
|
2012-05-30 11:57:49 +02:00
|
|
|
$('.cms-container').submitForm(
|
2012-05-22 05:37:31 +02:00
|
|
|
this,
|
|
|
|
button,
|
2011-03-23 10:51:00 +01:00
|
|
|
function() {
|
|
|
|
// Tree updates are triggered by Form_EditForm load events
|
|
|
|
button.removeClass('loading');
|
|
|
|
},
|
2012-03-24 03:19:02 +01:00
|
|
|
{
|
2012-05-22 05:37:31 +02:00
|
|
|
type: 'POST',
|
|
|
|
data: data,
|
2012-03-24 03:19:02 +01:00
|
|
|
// Refresh the whole area to avoid reloading just the form, without the tree around it
|
|
|
|
headers: {'X-Pjax': 'Content'}
|
|
|
|
}
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.setNewPages(newPages);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function: refresh
|
2011-05-08 06:02:28 +02:00
|
|
|
* This is called after each change event of PageType dropdown
|
2011-03-23 10:51:00 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (DOMElement) selectedNode
|
|
|
|
*/
|
|
|
|
refresh: function(selectedNode) {
|
2011-05-08 06:02:28 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
var tree = this.getTree(),
|
|
|
|
selectedNode = selectedNode || $(tree).jstree('get_selected')
|
|
|
|
origOptions = this.getOrigOptions(),
|
2011-05-08 06:02:28 +02:00
|
|
|
dropdown = this.find('select[name=PageType]'),
|
|
|
|
disallowed = [],
|
2012-05-22 01:28:02 +02:00
|
|
|
className = (selectedNode.length>0) ? selectedNode.entwine('ss.tree').getClassname() : null,
|
2011-05-08 06:02:28 +02:00
|
|
|
siteTreeHints = $.parseJSON($('#sitetree_ul').attr('data-hints')),
|
|
|
|
disableDropDown = true,
|
|
|
|
selectedOption = dropdown.val();
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// Clear all existing <option> elements
|
|
|
|
// (IE doesn't allow setting display:none on these elements)
|
|
|
|
dropdown.find('option').remove();
|
|
|
|
|
2011-05-08 06:02:28 +02:00
|
|
|
//Use tree hints to find allowed children for this node
|
2011-06-08 01:35:56 +02:00
|
|
|
if (className && siteTreeHints) {
|
2011-05-08 06:02:28 +02:00
|
|
|
disallowed = siteTreeHints[className].disallowedChildren;
|
2011-03-23 10:51:00 +01:00
|
|
|
}
|
|
|
|
|
2011-05-08 06:02:28 +02:00
|
|
|
$.each(origOptions, function(i, optProps) {
|
|
|
|
if ($.inArray(i, disallowed) === -1 && optProps) {
|
|
|
|
dropdown.append($('<option value="' + optProps.value + '">' + optProps.html + '</option>'));
|
|
|
|
disableDropDown = false;
|
|
|
|
}
|
|
|
|
});
|
2011-03-23 10:51:00 +01:00
|
|
|
|
|
|
|
// Disable dropdown if no elements are selectable
|
2011-05-08 06:02:28 +02:00
|
|
|
if (!disableDropDown) dropdown.removeAttr('disabled');
|
2011-03-23 10:51:00 +01:00
|
|
|
else dropdown.attr('disabled', 'disabled');
|
2011-05-08 06:02:28 +02:00
|
|
|
|
|
|
|
//Re-select the currently selected element
|
|
|
|
if (selectedOption) dropdown.val(selectedOption);
|
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// Set default child (optional)
|
|
|
|
if(selectedNode.hints && selectedNode.hints.defaultChild) {
|
|
|
|
dropdown.val(selectedNode.hints.defaultChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set parent node (fallback to root)
|
|
|
|
this.find(':input[name=ParentID]').val(selectedNode ? selectedNode.data('id') : 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2012-05-11 05:31:12 +02:00
|
|
|
}(jQuery));
|