1 if(typeof SiteTreeHandlers == 'undefined') SiteTreeHandlers = {};
2 SiteTreeHandlers.parentChanged_url = 'admin/ajaxupdateparent';
3 SiteTreeHandlers.orderChanged_url = 'admin/ajaxupdatesort';
4 SiteTreeHandlers.loadPage_url = 'admin/getitem';
5 SiteTreeHandlers.loadTree_url = 'admin/getsubtree';
7 _NEW_PAGES = new Array();
12 addpage = Class.create();
13 addpage.applyTo('#addpage');
15 initialize: function () {
16 Observable.applyTo($(_HANDLER_FORMS[this.id]));
17 this.getElementsByTagName('button')[0].onclick = returnFalse;
18 $(_HANDLER_FORMS[this.id]).onsubmit = this.form_submit;
21 onclick : function() {
22 if(treeactions.toggleSelection(this)) {
23 var selectedNode = $('sitetree').firstSelected();
25 while(selectedNode.parentTreeNode && !selectedNode.hints.defaultChild) {
26 $('sitetree').changeCurrentTo(selectedNode.parentTreeNode);
27 selectedNode = selectedNode.parentTreeNode;
30 if( selectedNode.hints && selectedNode.hints.defaultChild )
31 $(_HANDLER_FORMS.addpage).elements.PageType.value = selectedNode.hints.defaultChild;
34 this.o1 = $('sitetree').observeMethod('SelectionChanged', this.treeSelectionChanged.bind(this));
35 this.o2 = $(_HANDLER_FORMS[this.id]).observeMethod('Close', this.popupClosed.bind(this));
37 $(_HANDLER_FORMS[this.id]).elements.PageType.onchange = this.typeDropdown_change;
42 treeSelectionChanged : function(selectedNode) {
43 $(_HANDLER_FORMS.addpage).elements.PageType.value = selectedNode.hints.defaultChild;
46 popupClosed : function() {
47 $('sitetree').stopObserving(this.o1);
48 $(_HANDLER_FORMS.addpage).stopObserving(this.o2);
51 typeDropdown_change : function() {
52 // Don't do anything if we're already on an appropriate node
53 var sel = $('sitetree').firstSelected();
54 if(sel && sel.hints && sel.hints.allowedChildren) {
55 var allowed = sel.hints.allowedChildren;
56 for(i=0;i<allowed.length;i++) if(allowed[i] == this.value) return;
59 if( typeof siteTreeHints == 'undefined' )
62 // Otherwise move to the default parent for that.
63 if( siteTreeHints && siteTreeHints[this.value] ) {
64 var newNode = $('sitetree').getTreeNodeByIdx(siteTreeHints[this.value].defaultParent);
65 if(newNode) $('sitetree').changeCurrentTo(newNode);
69 form_submit : function() {
70 var st = $('sitetree');
71 var parentID = st.getIdxOf(st.firstSelected());
72 if(parentID && parentID.substr(0,3) == 'new') {
73 alert("You have to save a page before adding children underneath it");
75 } else if( Element.hasClassName( st.firstSelected(), "nochildren" ) ) {
76 alert("You can't add children to the selected node" );
78 $(_HANDLER_FORMS.addpage).elements.ParentID.value = parentID ? parentID : 0;
80 if( !_NEW_PAGES[parentID] )
81 _NEW_PAGES[parentID] = 1;
83 var suffix = _NEW_PAGES[parentID]++;
84 Ajax.SubmitForm(_HANDLER_FORMS.addpage, "action_addpage", {
85 onSuccess : Ajax.Evaluator,
86 onFailure : this.showAddPageError,
87 extraData: '&Suffix=' + suffix
94 showAddPageError: function(response) {
95 errorMessage('Error adding page', response);
103 button_onclick : function() {
104 if(treeactions.toggleSelection(this)) {
105 deletepage.o1 = $('sitetree').observeMethod('SelectionChanged', deletepage.treeSelectionChanged);
106 deletepage.o2 = $(_HANDLER_FORMS.deletepage).observeMethod('Close', deletepage.popupClosed);
107 addClass($('sitetree'),'multiselect');
109 deletepage.selectedNodes = { };
111 var sel = $('sitetree').firstSelected();
112 if(sel && sel.className.indexOf('nodelete') == -1) {
113 var selIdx = $('sitetree').getIdxOf(sel);
114 deletepage.selectedNodes[selIdx] = true;
115 sel.removeNodeClass('current');
116 sel.addNodeClass('selected');
122 treeSelectionChanged : function(selectedNode) {
123 var idx = $('sitetree').getIdxOf(selectedNode);
125 if(selectedNode.className.indexOf('nodelete') == -1) {
126 if(selectedNode.selected) {
127 selectedNode.removeNodeClass('selected');
128 selectedNode.selected = false;
129 deletepage.selectedNodes[idx] = false;
132 selectedNode.addNodeClass('selected');
133 selectedNode.selected = true;
134 deletepage.selectedNodes[idx] = true;
141 popupClosed : function() {
142 removeClass($('sitetree'),'multiselect');
143 $('sitetree').stopObserving(deletepage.o1);
144 $(_HANDLER_FORMS.deletepage).stopObserving(deletepage.o2);
146 for(var idx in deletepage.selectedNodes) {
147 if(deletepage.selectedNodes[idx]) {
148 node = $('sitetree').getTreeNodeByIdx(idx);
150 node.removeNodeClass('selected');
151 node.selected = false;
157 form_submit : function() {
158 var csvIDs = "", count = 0;
159 var st = $('sitetree');
160 var newNodes = new Array();
162 for(var idx in deletepage.selectedNodes) {
163 if(deletepage.selectedNodes[idx]) {
166 if( idx.match(/^new-[a-z0-9A-Z\-]+$/) ) {
167 newNodes.push( idx );
169 var i, item, childrenToDelete = st.getTreeNodeByIdx(idx).getElementsByTagName('li');
170 for(i=0;item=childrenToDelete[i];i++) {
171 csvIDs += (csvIDs ? "," : "") + st.getIdxOf(childrenToDelete[i]);
174 csvIDs += (csvIDs ? "," : "") + idx;
180 if(csvIDs || newNodes.length > 0) {
181 count += newNodes.length;
183 if(confirm("Do you really want to delete the " + count + " marked pages?")) {
184 $(_HANDLER_FORMS.deletepage).elements.csvIDs.value = csvIDs;
186 statusMessage('deleting pages');
188 for( var idx = 0; idx < newNodes.length; idx++ ) {
189 var newNode = $('sitetree').getTreeNodeByIdx( newNodes[idx] );
191 if( newNode.parentTreeNode )
192 newNode.parentTreeNode.removeTreeNode( newNode );
194 alert( newNode.id + ' has no parent node');
196 $('Form_EditForm').reloadIfSetTo(idx);
199 newNodes = new Array();
201 Ajax.SubmitForm(_HANDLER_FORMS.deletepage, null, {
202 onSuccess : deletepage.submit_success,
203 onFailure : function(response) {
204 errorMessage('Error deleting pages', response);
208 $('deletepage').getElementsByTagName('button')[0].onclick();
212 alert("Please select at least 1 Newsletter item.");
217 submit_success: function(response) {
218 deletepage.selectedNodes = {};
220 Ajax.Evaluator(response);
221 treeactions.closeSelection($('deletepage'));
226 * Initialisation function to set everything up
228 appendLoader(function () {
229 // Set up deleet page
230 if( !$('deletepage') )
233 Observable.applyTo($(_HANDLER_FORMS.deletepage));
234 $('deletepage').onclick = deletepage.button_onclick;
235 $('deletepage').getElementsByTagName('button')[0].onclick = function() { return false; };
236 $(_HANDLER_FORMS.deletepage).onsubmit = deletepage.form_submit;
243 'Edit this page' : function(treeNode) {
244 treeNode.selectTreeNode();
246 'Duplicate this page' : function(treeNode) {
247 // First save the page silently (without confirmation) and then duplicate the page.
248 autoSave(false, treeNode.duplicatePage.bind(treeNode));
250 'Sort sub-pages' : function(treeNode) {
251 var children = treeNode.treeNodeHolder().childTreeNodes();
252 var sortedChildren = children.sort(function(a, b) {
253 var titleA = a.aTag.innerHTML.replace(/<[^>]*>/g,'');
254 var titleB = b.aTag.innerHTML.replace(/<[^>]*>/g,'');
255 return titleA < titleB ? -1 : (titleA > titleB ? 1 : 0);
259 for(i=0;child=sortedChildren[i];i++) {
260 treeNode.appendTreeNode(child);
263 treeNode.onOrderChanged(sortedChildren);