2009-11-21 04:19:25 +01:00
|
|
|
(function($) {
|
2010-04-13 07:55:56 +02:00
|
|
|
$.entwine('ss', function($){
|
2009-11-21 04:19:59 +01:00
|
|
|
/**
|
|
|
|
* @class Simple form with a page type dropdown
|
|
|
|
* which creates a new page through #Form_EditForm and adds a new tree node.
|
|
|
|
* @name ss.Form_AddForm
|
|
|
|
* @requires ss.i18n
|
|
|
|
* @requires ss.Form_EditForm
|
|
|
|
*/
|
2010-04-13 07:55:56 +02:00
|
|
|
$('#Form_AddForm').entwine(/** @lends ss.Form_AddForm */{
|
2009-11-21 04:19:25 +01:00
|
|
|
/**
|
|
|
|
* @type DOMElement
|
|
|
|
*/
|
|
|
|
Tree: null,
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
/**
|
|
|
|
* @type Array Internal counter to create unique page identifiers prior to ajax saving
|
|
|
|
*/
|
2009-11-21 04:19:59 +01:00
|
|
|
NewPages: [],
|
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
onmatch: function() {
|
|
|
|
var self = this;
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
Observable.applyTo(this[0]);
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
var tree = jQuery('#sitetree')[0];
|
|
|
|
this.setTree(tree);
|
|
|
|
jQuery(tree).bind('selectionchanged', function(e, data) {self.treeSelectionChanged(e, data);});
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
this.find(':input[name=PageType]').bind('change', this.typeDropdownChanged);
|
2009-11-21 04:19:59 +01:00
|
|
|
|
|
|
|
this._super();
|
2009-11-21 04:19:25 +01:00
|
|
|
},
|
2009-11-21 04:19:59 +01:00
|
|
|
|
|
|
|
onsubmit: function(e) {
|
|
|
|
var newPages = this.getNewPages();
|
|
|
|
var tree = this.getTree();
|
2009-11-21 04:19:25 +01:00
|
|
|
var parentID = (tree.firstSelected()) ? tree.getIdxOf(tree.firstSelected()) : 0;
|
|
|
|
|
|
|
|
// TODO: Remove 'new-' code http://open.silverstripe.com/ticket/875
|
|
|
|
if(parentID && parentID.substr(0,3) == 'new') {
|
|
|
|
alert(ss.i18n._t('CMSMAIN.WARNINGSAVEPAGESBEFOREADDING'));
|
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
if(tree.firstSelected() && jQuery(tree.firstSelected()).hasClass("nochildren")) {
|
|
|
|
alert(ss.i18n._t('CMSMAIN.CANTADDCHILDREN') );
|
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
// Optionally initalize the new pages tracker
|
|
|
|
if(!newPages[parentID] ) newPages[parentID] = 1;
|
|
|
|
|
|
|
|
// default to first button
|
|
|
|
var button = jQuery(this).find(':submit:first');
|
|
|
|
button.addClass('loading');
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
// collect data and submit the form
|
|
|
|
var data = jQuery(this).serializeArray();
|
|
|
|
data.push({name:'Suffix',value:newPages[parentID]++});
|
|
|
|
data.push({name:button.attr('name'),value:button.val()});
|
|
|
|
// TODO Should be set by hiddenfield already
|
2010-04-13 07:55:56 +02:00
|
|
|
jQuery('#Form_EditForm').entwine('ss').loadForm(
|
2009-11-21 04:19:25 +01:00
|
|
|
jQuery(this).attr('action'),
|
|
|
|
function() {
|
|
|
|
button.removeClass('loading');
|
|
|
|
},
|
|
|
|
{type: 'POST', data: data}
|
|
|
|
);
|
2009-11-21 04:19:59 +01:00
|
|
|
|
|
|
|
this.setNewPages(newPages);
|
2009-11-21 04:19:25 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
treeSelectionChanged : function(e, data) {
|
|
|
|
var selectedNode = data.node;
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
if(selectedNode.hints && selectedNode.hints.defaultChild) {
|
|
|
|
this.find(':input[name=PageType]').val(selectedNode.hints.defaultChild);
|
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
|
|
|
|
var parentID = this.getTree().getIdxOf(selectedNode);
|
2009-11-21 04:19:25 +01:00
|
|
|
this.find(':input[name=ParentID]').val(parentID ? parentID : 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
typeDropdownChanged : function() {
|
2009-11-21 04:19:59 +01:00
|
|
|
var tree = this.getTree();
|
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
// Don't do anything if we're already on an appropriate node
|
|
|
|
var sel = tree.firstSelected();
|
|
|
|
if(sel && sel.hints && sel.hints.allowedChildren) {
|
|
|
|
var allowed = sel.hints.allowedChildren;
|
|
|
|
for(i=0;i<allowed.length;i++) {
|
|
|
|
if(allowed[i] == this.value) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise move to the default parent for that.
|
|
|
|
if(siteTreeHints && siteTreeHints[this.value] ) {
|
|
|
|
var newNode = tree.getTreeNodeByIdx(siteTreeHints[this.value].defaultParent);
|
|
|
|
if(newNode) tree.changeCurrentTo(newNode);
|
|
|
|
}
|
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
});
|
2009-11-21 04:19:25 +01:00
|
|
|
});
|
|
|
|
}(jQuery));
|