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
|
|
|
|
2011-03-10 06:30:41 +01:00
|
|
|
var tree = $('#sitetree_ul');
|
2009-11-21 04:19:25 +01:00
|
|
|
this.setTree(tree);
|
2010-04-14 03:32:05 +02:00
|
|
|
|
|
|
|
// Event bindings
|
2011-03-10 06:30:41 +01:00
|
|
|
$(tree).bind('select_node.jstree', function(e, data) {self.refresh(data.rslt.obj);});
|
2010-04-14 03:32:05 +02:00
|
|
|
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) {
|
2011-03-10 06:30:41 +01:00
|
|
|
var newPages = this.getNewPages(), tree = this.getTree(), node = $(tree).jstree('get_selected');
|
|
|
|
var parentID = (node.length) ? node.data('id') : 0;
|
2009-11-21 04:19:25 +01:00
|
|
|
|
|
|
|
// TODO: Remove 'new-' code http://open.silverstripe.com/ticket/875
|
2011-03-10 06:30:41 +01:00
|
|
|
// if(parentID && parentID.substr(0,3) == 'new') {
|
|
|
|
// alert(ss.i18n._t('CMSMAIN.WARNINGSAVEPAGESBEFOREADDING'));
|
|
|
|
// }
|
|
|
|
|
|
|
|
if(node && node.hasClass("nochildren")) {
|
2009-11-21 04:19:25 +01:00
|
|
|
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
|
2011-03-10 06:30:41 +01:00
|
|
|
var button = this.find(':submit:first');
|
2009-11-21 04:19:25 +01:00
|
|
|
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
|
2011-03-10 06:30:41 +01:00
|
|
|
var data = this.serializeArray();
|
2009-11-21 04:19:25 +01:00
|
|
|
data.push({name:'Suffix',value:newPages[parentID]++});
|
|
|
|
data.push({name:button.attr('name'),value:button.val()});
|
2011-03-10 06:30:41 +01:00
|
|
|
|
2009-11-21 04:19:25 +01:00
|
|
|
// TODO Should be set by hiddenfield already
|
2010-04-13 07:55:56 +02:00
|
|
|
jQuery('#Form_EditForm').entwine('ss').loadForm(
|
2011-03-10 06:30:41 +01:00
|
|
|
this.attr('action'),
|
2009-11-21 04:19:25 +01:00
|
|
|
function() {
|
2011-03-10 06:30:41 +01:00
|
|
|
// Tree updates are triggered by Form_EditForm load events
|
2009-11-21 04:19:25 +01:00
|
|
|
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
|
2011-03-10 06:30:41 +01:00
|
|
|
var tree = this.getTree(),
|
|
|
|
selectedNode = selectedNode || $(tree).jstree('get_selected')
|
2010-04-14 03:32:05 +02:00
|
|
|
origOptions = this.getOrigOptions(),
|
2011-02-24 05:10:01 +01:00
|
|
|
dropdown = this.find('select[name=PageType]');
|
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)
|
2011-03-10 06:30:41 +01:00
|
|
|
this.find(':input[name=ParentID]').val(selectedNode ? selectedNode.data('id') : 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));
|