Move to sane directory structure. Don't make 'cms' the top level of the silverstripe...
[silverstripe-elijah.git] / silverstripe-gsoc / cms / javascript / NewsletterAdmin_left.js
blobffb9d54e314521723f1172cf3db17d7274c9feb2
1 if(typeof SiteTreeHandlers == 'undefined') SiteTreeHandlers = {};
2 SiteTreeHandlers.showNLDraft_url = 'admin/newsletter/shownewsletter/';
3 SiteTreeHandlers.showNLType_url = 'admin/newsletter/showtype/';
5 SiteTree.prototype = {
6         castAsTreeNode: function(li) {
7                 behaveAs(li, SiteTreeNode, this.options);
8         },
9         
10         getIdxOf : function(treeNode) {
11                 if(treeNode && treeNode.id)
12                         return treeNode.id;
13         },
14         
15         getTreeNodeByIdx : function(idx) {
16                 if(!idx) idx = "0";
17                 return document.getElementById(idx);
18         },
19         
20         initialise: function() {
21                 this.observeMethod('SelectionChanged', this.changeCurrentTo);   
22         },
23         
24         createTreeNode : function(idx, title, pageType, secondIdx) {
25                 var i;
26                 var node = document.createElement('li');
27                 node.className = pageType;
28                 
29                 var aTag = document.createElement('a');
30                 
31                 node.id = idx;
32                 
33                 if( pageType == 'Draft' ) {
34                         aTag.href = SiteTreeHandlers.showNLDraft_url + idx;
35                 } else {
36                         aTag.href = SiteTreeHandlers.showNLType_url + idx;      
37                 }
38                 
39                 aTag.innerHTML = title;
40                 node.appendChild(aTag);
42                 SiteTreeNode.create(node, this.options);
43                 
44                 return node;
45         },
46         
47         addDraftNode: function( title, parentID, draftID ) {
48                 var draftNode = this.createTreeNode( 'draft_' + parentID + '_' + draftID, title, 'Draft', parentID );
49                 this.getTreeNodeByIdx('drafts_' + parentID).appendTreeNode( draftNode );
50                 this.changeCurrentTo( draftNode );
51         },
52         
53         addTypeNode: function( title, parentID ) {
54                 var typeNode = this.createTreeNode( 'mailtype_' + parentID, title, 'MailType' );
55                 var draftsNode = this.createTreeNode( 'drafts_' + parentID, 'Drafts', 'DraftFolder' );
56                 var sentItemsNode = this.createTreeNode( 'sent_' + parentID, 'Sent Items', 'SentFolder' );
57                 var mailingListNode = this.createTreeNode( 'recipients_' + parentID, 'Mailing List', 'Recipients' );
58                 typeNode.appendTreeNode( draftsNode );
59                 Element.addClassName( draftsNode, 'nodelete');
60                 typeNode.appendTreeNode( sentItemsNode );
61                 Element.addClassName(sentItemsNode,'nodelete');
62                 typeNode.appendTreeNode( mailingListNode );
63                 Element.addClassName(mailingListNode,'nodelete');
64                 this.appendTreeNode( typeNode );        
65                 this.changeCurrentTo( typeNode );
66         }
70 /**
71  * Delete pages action
72  */
73 deletedraft = {
74         button_onclick : function() {
75                 if(treeactions.toggleSelection(this)) {
76                         deletedraft.o1 = $('sitetree').observeMethod('SelectionChanged', deletedraft.treeSelectionChanged);
77                         deletedraft.o2 = $('deletedrafts_options').observeMethod('Close', deletedraft.popupClosed);
78                         addClass($('sitetree'),'multiselect');
80                         var sel = $('sitetree').firstSelected();
81                         
82                         if(sel) {
83                                 var selIdx = $('sitetree').getIdxOf(sel);
84                                 deletedraft.selectedNodes = { };
85                                 deletedraft.selectedNodes[selIdx] = true;
86                                 sel.removeNodeClass('current');
87                                 sel.addNodeClass('selected');   
88                         }       
89                 }
90                 return false;
91         },
93         treeSelectionChanged : function(selectedNode) {
94                 var idx = $('sitetree').getIdxOf(selectedNode);
96                 if(selectedNode.selected) {
97                         selectedNode.removeNodeClass('selected');
98                         selectedNode.selected = false;
99                         deletedraft.selectedNodes[idx] = false;
101                 } else {
102                         selectedNode.addNodeClass('selected');
103                         selectedNode.selected = true;
104                         deletedraft.selectedNodes[idx] = true;
105                 }
106                 
107                 return false;
108         },
109         
110         popupClosed : function() {
111                 removeClass($('sitetree'),'multiselect');
112                 $('sitetree').stopObserving(deletedraft.o1);
113                 $('deletedrafts_options').stopObserving(deletedraft.o2);
115                 for(var idx in deletedraft.selectedNodes) {
116                         if(deletedraft.selectedNodes[idx]) {
117                                 node = $('sitetree').getTreeNodeByIdx(idx);
118                                 if(node) {
119                                         node.removeNodeClass('selected');
120                                         node.selected = false;
121                                 }
122                         }
123                 }
124         },
126         form_submit : function() {
127                 var csvIDs = "";
128                 for(var idx in deletedraft.selectedNodes) {
129                         if(deletedraft.selectedNodes[idx]) csvIDs += (csvIDs ? "," : "") + idx;
130                 }
131                 
132                 if(csvIDs) {
133                         if(confirm("Do you really want to these Newsletter items?")) {
134                                 $('deletedrafts_options').elements.csvIDs.value = csvIDs;
135         
136                                 Ajax.SubmitForm('deletedrafts_options', null, {
137                                         onSuccess : function(response) {
138                                                 Ajax.Evaluator(response);
139         
140                                                 var sel;
141                                                 if((sel = $('sitetree').selected) && sel.parentNode) sel.addNodeClass('current');
142         //                                      else $('Form_EditForm').innerHTML = "";
143         
144                                                 treeactions.closeSelection($('deletedrafts'));
145                                         },
146                                         onFailure : function(response) {
147                                                 errorMessage('Error deleting drafts', response);
148                                         }
149                                 });
150         
151                                 $('deletedrafts').getElementsByTagName('button')[0].onclick();
152                         }
153                 } else {
154                         alert("Please select at least 1 Newsletter item.");
155                 }
157                 return false;
158         },
159         
160         treeSelectionChanged : function(selectedNode) {
161                 var idx = $('sitetree').getIdxOf(selectedNode);
163                 if( !deletedraft.selectedNodes )
164                         deletedraft.selectedNodes = {};
166                 if(selectedNode.className.indexOf('nodelete') == -1) {
167                         if(selectedNode.selected) {
168                                 selectedNode.removeNodeClass('selected');
169                                 selectedNode.selected = false;
170                                 deletedraft.selectedNodes[idx] = false;
171         
172                         } else {
173                                 selectedNode.addNodeClass('selected');
174                                 selectedNode.selected = true;
175                                 deletedraft.selectedNodes[idx] = true;
176                         }
177                 }
178                 
179                 return false;
180         }
183 SiteTreeNode.prototype.onselect = function() {
184         $('sitetree').changeCurrentTo(this);
185         if($('sitetree').notify('SelectionChanged', this)) {
186                 autoSave(true, this.getPageFromServer.bind(this));
187         }
188         return false; 
191 SiteTreeNode.prototype.getPageFromServer = function() {
193     var match = this.id.match(/(mailtype|drafts|draft|sent|recipients)_([\d]+)$/);
194     var openTabName = null;
195     var currentPageID = null;
196         
197         if( $('Form_EditForm_ID') )
198                 currentPageID = $('Form_EditForm_ID').value;
199         
200     var newPageID = null;
201     var otherID = null;
202     var type = null;
203     
204                 if( match ) {
205       
206       newPageID = match[2];
207       type = match[1];
208                 } else if(this.id.match(/(mailtype|drafts|draft|sent|recipients)_([\d]+)_([\d]+)$/)) {
209                         newPageID = RegExp.$2;
210                         type = RegExp.$1;
211                         otherID = RegExp.$3;
212                 }
213                 $('Form_EditForm').getPageFromServer(newPageID, type, otherID, openTabName);
216 function draft_sent_ok( newsletterID, draftID, numEmails ) {
217     var draftsListNode = $('drafts_' + newsletterID);
218     var sentListNode = $('sent_' + newsletterID);
219     var draftNode = $('draft_' + newsletterID + '_' + draftID );
220     
221     draftsListNode.removeTreeNode( draftNode );
222     draftNode.id = 'sent_' + newsletterID + '_' + draftID;
223     sentListNode.appendTreeNode( draftNode, null );
224     statusMessage('Sent newsletter to mailing list. Sent ' + numEmails + ' emails successfully.', 'good'); 
227 function resent_ok( newsletterID, sentID, numEmails ) {
228     statusMessage('Resent newsletter to mailing list. Sent ' + numEmails + ' emails successfully.', 'good'); 
231 function reloadSiteTree() {
232         
233         new Ajax.Request( 'admin/newsletter/getsitetree', {
234                 method: get,
235                 onSuccess: function( response ) {
236                         $('sitetree_holder').innerHTML = response.responseText;
237                 },
238                 onFailure: function( response ) {
239                                 
240                 }       
241         });
242                 
245 _HANDLER_FORMS['addtype'] = 'addtype_options';
246 _HANDLER_FORMS['deletedrafts'] = 'deletedrafts_options';
248 AddForm = Class.extend('addpage');
249 AddForm.applyTo('#addtype');
250 AddForm.prototype = {
251   initialize: function () {
252                 Observable.applyTo($(_HANDLER_FORMS[this.id]));
253                 this.getElementsByTagName('button')[0].onclick = returnFalse;
254                 $(_HANDLER_FORMS[this.id]).onsubmit = this.form_submit;
255         },
256     
257         form_submit : function() {
258                 var st = $('sitetree');
259                 if (st) {
260                         if( st.selected && st.selected.length ) {
261                                 selectedNode = st.selected[0];
262                         } else {
263                                 selectedNode = st.selected;
264                         }
265                 } else {
266                         var selectedNode = null;
267                 }
268                 var parentID = null;
269     
270                 while( selectedNode && !parentID ) {
271                         if( selectedNode && selectedNode.id && selectedNode.id.match(/mailtype_([0-9a-z\-]+)$/) )
272                                 parentID = RegExp.$1;
273                         else
274                                 selectedNode = selectedNode.parentNode;
275                 }
276         
277                 if(parentID && parentID.substr(0,3) == 'new') {
278                         alert("You have to save a page before adding children underneath it");
279                         
280                 } else {
281                         if (this.elements) {
282                                 this.elements.ParentID.value = parentID;
283                         }
285                         var type = 'draft';
286                         var selectIDPrefix = 'draft_' + parentID + '_';
287                         
288                         if( $('add_type').value == 'type' ) {
289                                 type = 'type';
290                                 selectIDPrefix = 'mailtype_';
291                         }
292                         // Call either addtype or adddraft
293                         var request = new Ajax.Request( this.action + type + '?ajax=1' + '&ParentID=' + parentID, {
294                                 method: 'get',
295                                 asynchronous: true,
296                                 onSuccess : function( response ) {
297                                         
298                                         $('Form_EditForm').loadNewPage(response.responseText);
299                                         
300                                         // create a new node and add it to the site tree
301                                         if( type == 'draft' ) {
302                                                 $('sitetree').addDraftNode('New draft newsletter', parentID, $('Form_EditForm_ID').value );
303                                         } else {
304                                                 $('sitetree').addTypeNode('New newsletter type', $('Form_EditForm_ID').value ); 
305                                         }
306                                         // Save it so that change detection will work
307                                         $('Form_EditForm').save();
308                                         
309                                         statusMessage('Added new ' + type);
310                                 },
311         
312                                 onFailure : function(response) {
313                                         alert(response.responseText);
314                                         statusMessage('Could not add new ' + type );
315                                 }
316                         });
317                 }
318                 
319                 return false;
320         },
321     
322   reloadSiteTree: function( response ) {
323     statusMessage('Added new newsletter type', 'good' );
324     $('sitetree_holder').innerHTML = response.responseText;
325     Behaviour.apply( $('sitetree_holder') );
326   },
327     
328   button_onclick : function() {
329                 if(treeactions.toggleSelection(this)) {
330                         var selectedNode = $('sitetree').firstSelected();
331                         if(selectedNode) {
332                                 while(selectedNode.parentTreeNode && !selectedNode.hints.defaultChild) {
333                                         $('sitetree').changeCurrentTo(selectedNode.parentTreeNode);
334                                         selectedNode = selectedNode.parentTreeNode;
335                                 }
336                         }
337                                                 
338                         this.o1 = $('sitetree').observeMethod('SelectionChanged', this.treeSelectionChanged.bind(this));
339                         this.o2 = $(_HANDLER_FORMS[this.id]).observeMethod('Close', this.popupClosed.bind(this));
340                 }
341                 return false;
342         },
344   treeSelectionChanged: function( treeNode ) {
345     this.selected = treeNode;
346   },
347   
348   popupClosed: function() {
349         $('sitetree').stopObserving(this.o1);
350         $(_HANDLER_FORMS.addtype).stopObserving(this.o2);
351   }
354 function removeTreeNodeByIdx( tree, nodeID ) {
355         var node = tree.getTreeNodeByIdx( nodeID );
356         if( node )
357                 if( node.parentTreeNode )
358                         node.parentTreeNode.removeTreeNode( node );
359                 else
360                         $('sitetree').removeTreeNode( node );
363 /** 
364  * Initialisation function to set everything up
365  */
366 Behaviour.addLoader(function () {
367         // Set up add draft
368         Observable.applyTo($('addtype_options'));
369     
370     if( $('addtype') ) {
371         if( AddForm.button_click )
372             $('addtype').getElementsByTagName('a')[0].onclick = function() {return false;};
373             if( AddForm.button_click )
374                 $('addtype_options').onsubmit = AddForm.form_submit;
375         }
376         // Set up delete drafts
377         Observable.applyTo($('deletedrafts_options'));
378     
379     var deleteDrafts = $('deletedrafts');
380     
381     if( deleteDrafts ) {
382             deleteDrafts.onclick = deletedraft.button_onclick;
383             deleteDrafts.getElementsByTagName('button')[0].onclick = function() {return false;};
384             $('deletedrafts_options').onsubmit = deletedraft.form_submit;
385     }