Move to sane directory structure. Don't make 'cms' the top level of the silverstripe...
[silverstripe-elijah.git] / silverstripe-gsoc / cms / javascript / CMSMain_left.js
bloba84ed11c2c1d19fe08a79c634b3fabe26838fee4
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();
9 /**
10  * Add page action
11  */
12 addpage = Class.create();
13 addpage.applyTo('#addpage');
14 addpage.prototype = {
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;
19         },
20         
21         onclick : function() {
22                         if(treeactions.toggleSelection(this)) {
23                         var selectedNode = $('sitetree').firstSelected();
24                         if(selectedNode) {
25                                 while(selectedNode.parentTreeNode && !selectedNode.hints.defaultChild) {
26                                         $('sitetree').changeCurrentTo(selectedNode.parentTreeNode);
27                                         selectedNode = selectedNode.parentTreeNode;
28                                 }
29                                 
30                                 if( selectedNode.hints && selectedNode.hints.defaultChild )
31                                         $(_HANDLER_FORMS.addpage).elements.PageType.value = selectedNode.hints.defaultChild;
32                         }
33                                                 
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;
38                 }
39                 return false;
40         },
41         
42         treeSelectionChanged : function(selectedNode) {
43                 $(_HANDLER_FORMS.addpage).elements.PageType.value = selectedNode.hints.defaultChild;
44         },
45         
46         popupClosed : function() {
47                 $('sitetree').stopObserving(this.o1);
48                 $(_HANDLER_FORMS.addpage).stopObserving(this.o2);
49         },
50         
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;
57                 }
59                 if( typeof siteTreeHints == 'undefined' )
60                         return;
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);
66                 }
67         },
68         
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");
74                         
75                 } else if( Element.hasClassName( st.firstSelected(), "nochildren" ) ) {
76                         alert("You can't add children to the selected node" );
77                 } else {
78                         $(_HANDLER_FORMS.addpage).elements.ParentID.value = parentID ? parentID : 0;
79                 
80                         if( !_NEW_PAGES[parentID] )
81                                 _NEW_PAGES[parentID] = 1;
82                 
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
88                         });
89                 }
90                 
91                 return false;
92         },
93         
94         showAddPageError: function(response) {
95                 errorMessage('Error adding page', response);
96         }
99 /**
100  * Delete page action
101  */
102 deletepage = {
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');           
117                         }
118                 }
119                 return false;
120         },
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;
130         
131                         } else {
132                                 selectedNode.addNodeClass('selected');
133                                 selectedNode.selected = true;
134                                 deletepage.selectedNodes[idx] = true;
135                         }
136                 }
137                 
138                 return false;
139         },
140         
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);
149                                 if(node) {
150                                         node.removeNodeClass('selected');
151                                         node.selected = false;
152                                 }
153                         }
154                 }
155         },
157         form_submit : function() {
158                 var csvIDs = "", count = 0;
159                 var st = $('sitetree');
160                 var newNodes = new Array();
161                 
162                 for(var idx in deletepage.selectedNodes) {
163                         if(deletepage.selectedNodes[idx]) {
164                                 
165                                 // delete new nodes
166                                 if( idx.match(/^new-[a-z0-9A-Z\-]+$/) ) {
167                                         newNodes.push( idx );
168                                 } else {
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]);
172                                                 count++;
173                                         }
174                                         csvIDs += (csvIDs ? "," : "") + idx;
175                                         count++;
176                                 }
177                         }
178                 }
180                 if(csvIDs || newNodes.length > 0) {
181                         count += newNodes.length;
182                         
183                         if(confirm("Do you really want to delete the " + count + " marked pages?")) {
184                                 $(_HANDLER_FORMS.deletepage).elements.csvIDs.value = csvIDs;
185                                 
186                                 statusMessage('deleting pages');
187         
188                                 for( var idx = 0; idx < newNodes.length; idx++ ) {
189                                         var newNode = $('sitetree').getTreeNodeByIdx( newNodes[idx] );
190                                         
191                                         if( newNode.parentTreeNode )
192                                                 newNode.parentTreeNode.removeTreeNode( newNode );
193                                         else
194                                                 alert( newNode.id + ' has no parent node');
195                                                 
196                                         $('Form_EditForm').reloadIfSetTo(idx);
197                                 }
198                                 
199                                 newNodes = new Array();
200         
201                                 Ajax.SubmitForm(_HANDLER_FORMS.deletepage, null, {
202                                         onSuccess : deletepage.submit_success,
203                                         onFailure : function(response) {
204                                                 errorMessage('Error deleting pages', response);
205                                         }
206                                 });
207         
208                                 $('deletepage').getElementsByTagName('button')[0].onclick();
209                         }
210                         
211                 } else {
212                         alert("Please select at least 1 page.");
213                 }
215                 return false;
216         },
217         submit_success: function(response) {
218                 deletepage.selectedNodes = {};
219                 
220                 Ajax.Evaluator(response);
221                 treeactions.closeSelection($('deletepage'));
222         }
225 /** 
226  * Initialisation function to set everything up
227  */
228 appendLoader(function () {
229         // Set up deleet page
230     if( !$('deletepage') )
231         return;
232     
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;
240  * Tree context menu
241  */
242 TreeContextMenu = {
243         'Edit this page' : function(treeNode) {
244                 treeNode.selectTreeNode();
245         },
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)); 
249         },
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);
256                 });
257                 
258                 var i,child;
259                 for(i=0;child=sortedChildren[i];i++) {
260                         treeNode.appendTreeNode(child);
261                 }
262                 
263                 treeNode.onOrderChanged(sortedChildren);
264         }