2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* File: LeftAndMain.AddForm.js
|
|
|
|
*/
|
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
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Class: #Form_AddForm
|
|
|
|
*
|
|
|
|
* Simple form with a page type dropdown
|
2009-11-21 04:19:59 +01:00
|
|
|
* which creates a new page through #Form_EditForm and adds a new tree node.
|
2010-05-08 04:45:05 +02:00
|
|
|
*
|
|
|
|
* Requires:
|
|
|
|
* ss.i18n
|
|
|
|
* #Form_EditForm
|
2009-11-21 04:19:59 +01:00
|
|
|
*/
|
2010-05-08 04:45:05 +02:00
|
|
|
$('#Form_AddForm').entwine({
|
2009-11-21 04:19:25 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Variable: Tree
|
|
|
|
* (DOMElement)
|
2009-11-21 04:19:25 +01:00
|
|
|
*/
|
|
|
|
Tree: null,
|
2010-04-14 03:32:05 +02:00
|
|
|
|
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Variable: OrigOptions
|
|
|
|
* (Array) Map of <option> values to an object of "title" and "value"
|
2010-04-14 03:32:05 +02:00
|
|
|
*/
|
|
|
|
OrigOptions: null,
|
2009-11-21 04:19:59 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
/**
|
2010-05-08 04:45:05 +02:00
|
|
|
* Variable: NewPages
|
|
|
|
* (Array) Internal counter to create unique page identifiers prior to ajax saving
|
2009-11-21 04:19:25 +01:00
|
|
|
*/
|
2009-11-21 04:19:59 +01:00
|
|
|
NewPages: [],
|
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Constructor: onmatch
|
|
|
|
*/
|
2009-11-21 04:19:25 +01:00
|
|
|
onmatch: function() {
|
2010-04-14 03:32:05 +02:00
|
|
|
var self = this, typeDropdown = this.find(':input[name=PageType]');
|
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);
|
2010-04-14 03:32:05 +02:00
|
|
|
|
|
|
|
// Event bindings
|
|
|
|
jQuery(tree).bind('selectionchanged', function(e, data) {self.refresh(data.node);});
|
|
|
|
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);
|
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
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Function: onsubmit
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Event) e
|
|
|
|
*/
|
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;
|
|
|
|
},
|
2010-04-14 03:32:05 +02:00
|
|
|
|
2010-05-08 04:45:05 +02:00
|
|
|
/**
|
|
|
|
* Function: refresh
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (DOMElement) selectedNode
|
|
|
|
*/
|
2010-04-14 03:32:05 +02:00
|
|
|
refresh: function(selectedNode) {
|
|
|
|
// Note: Uses siteTreeHints global
|
|
|
|
var tree = this.getTree(),
|
|
|
|
origOptions = this.getOrigOptions(),
|
2011-02-24 05:10:01 +01:00
|
|
|
dropdown = this.find('select[name=PageType]');
|
2010-04-14 03:32:05 +02:00
|
|
|
if(!selectedNode) selectedNode = tree.firstSelected();
|
2009-11-21 04:19:25 +01:00
|
|
|
|
2010-04-14 03:32:05 +02:00
|
|
|
// Clear all existing <option> elements
|
|
|
|
// (IE doesn't allow setting display:none on these elements)
|
|
|
|
dropdown.find('option').remove();
|
|
|
|
|
|
|
|
// Find allowed children through preferences on node or globally
|
|
|
|
var allowed = [];
|
|
|
|
if(selectedNode) {
|
|
|
|
if(selectedNode.hints && selectedNode.hints.allowedChildren) {
|
|
|
|
allowed = selectedNode.hints.allowedChildren;
|
|
|
|
} else {
|
|
|
|
// Fallback to globals
|
2010-08-03 06:32:09 +02:00
|
|
|
allowed = (typeof siteTreeHints !== 'undefined') ? siteTreeHints['Root'].allowedChildren : [];
|
2010-04-14 03:32:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-add all allowed <option> to the dropdown
|
2009-11-21 04:19:25 +01:00
|
|
|
for(i=0;i<allowed.length;i++) {
|
2010-04-14 03:32:05 +02:00
|
|
|
var optProps = origOptions[allowed[i]];
|
|
|
|
if(optProps) dropdown.append($('<option value="' + optProps.value + '">' + optProps.html + '</option>'));
|
2009-11-21 04:19:25 +01:00
|
|
|
}
|
2010-04-14 03:32:05 +02:00
|
|
|
} else {
|
|
|
|
// No tree node selected, reset to original elements
|
|
|
|
$.each(origOptions, function(i, optProps) {
|
|
|
|
if(optProps) dropdown.append($('<option value="' + optProps.value + '">' + optProps.html + '</option>'));
|
|
|
|
});
|
2009-11-21 04:19:25 +01:00
|
|
|
}
|
2010-04-14 03:32:05 +02:00
|
|
|
|
|
|
|
// TODO Re-select the currently selected element
|
|
|
|
|
|
|
|
// Disable dropdown if no elements are selectable
|
|
|
|
if(allowed) dropdown.removeAttr('disabled');
|
|
|
|
else dropdown.attr('disabled', 'disabled');
|
|
|
|
|
|
|
|
// Set default child (optional)
|
|
|
|
if(selectedNode.hints && selectedNode.hints.defaultChild) {
|
|
|
|
dropdown.val(selectedNode.hints.defaultChild);
|
2009-11-21 04:19:25 +01:00
|
|
|
}
|
2010-04-14 03:32:05 +02:00
|
|
|
|
|
|
|
// Set parent node (fallback to root)
|
|
|
|
var parentID = tree.getIdxOf(selectedNode);
|
|
|
|
this.find(':input[name=ParentID]').val(parentID ? parentID : 0);
|
2009-11-21 04:19:25 +01:00
|
|
|
}
|
2009-11-21 04:19:59 +01:00
|
|
|
});
|
2009-11-21 04:19:25 +01:00
|
|
|
});
|
|
|
|
}(jQuery));
|