1 if(typeof SiteTreeHandlers == 'undefined') SiteTreeHandlers = {};
2 SiteTreeHandlers.showNLDraft_url = 'admin/newsletter/shownewsletter/';
3 SiteTreeHandlers.showNLType_url = 'admin/newsletter/showtype/';
6 castAsTreeNode: function(li) {
7 behaveAs(li, SiteTreeNode, this.options);
10 getIdxOf : function(treeNode) {
11 if(treeNode && treeNode.id)
15 getTreeNodeByIdx : function(idx) {
17 return document.getElementById(idx);
20 initialise: function() {
21 this.observeMethod('SelectionChanged', this.changeCurrentTo);
24 createTreeNode : function(idx, title, pageType, secondIdx) {
26 var node = document.createElement('li');
27 node.className = pageType;
29 var aTag = document.createElement('a');
33 if( pageType == 'Draft' ) {
34 aTag.href = SiteTreeHandlers.showNLDraft_url + idx;
36 aTag.href = SiteTreeHandlers.showNLType_url + idx;
39 aTag.innerHTML = title;
40 node.appendChild(aTag);
42 SiteTreeNode.create(node, this.options);
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 );
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 );
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();
83 var selIdx = $('sitetree').getIdxOf(sel);
84 deletedraft.selectedNodes = { };
85 deletedraft.selectedNodes[selIdx] = true;
86 sel.removeNodeClass('current');
87 sel.addNodeClass('selected');
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;
102 selectedNode.addNodeClass('selected');
103 selectedNode.selected = true;
104 deletedraft.selectedNodes[idx] = true;
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);
119 node.removeNodeClass('selected');
120 node.selected = false;
126 form_submit : function() {
128 for(var idx in deletedraft.selectedNodes) {
129 if(deletedraft.selectedNodes[idx]) csvIDs += (csvIDs ? "," : "") + idx;
133 if(confirm("Do you really want to these Newsletter items?")) {
134 $('deletedrafts_options').elements.csvIDs.value = csvIDs;
136 Ajax.SubmitForm('deletedrafts_options', null, {
137 onSuccess : function(response) {
138 Ajax.Evaluator(response);
141 if((sel = $('sitetree').selected) && sel.parentNode) sel.addNodeClass('current');
142 // else $('Form_EditForm').innerHTML = "";
144 treeactions.closeSelection($('deletedrafts'));
146 onFailure : function(response) {
147 errorMessage('Error deleting drafts', response);
151 $('deletedrafts').getElementsByTagName('button')[0].onclick();
154 alert("Please select at least 1 Newsletter item.");
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;
173 selectedNode.addNodeClass('selected');
174 selectedNode.selected = true;
175 deletedraft.selectedNodes[idx] = true;
183 SiteTreeNode.prototype.onselect = function() {
184 $('sitetree').changeCurrentTo(this);
185 if($('sitetree').notify('SelectionChanged', this)) {
186 autoSave(true, this.getPageFromServer.bind(this));
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;
197 if( $('Form_EditForm_ID') )
198 currentPageID = $('Form_EditForm_ID').value;
200 var newPageID = null;
206 newPageID = match[2];
208 } else if(this.id.match(/(mailtype|drafts|draft|sent|recipients)_([\d]+)_([\d]+)$/)) {
209 newPageID = RegExp.$2;
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 );
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() {
233 new Ajax.Request( 'admin/newsletter/getsitetree', {
235 onSuccess: function( response ) {
236 $('sitetree_holder').innerHTML = response.responseText;
238 onFailure: function( response ) {
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;
257 form_submit : function() {
258 var st = $('sitetree');
260 if( st.selected && st.selected.length ) {
261 selectedNode = st.selected[0];
263 selectedNode = st.selected;
266 var selectedNode = null;
270 while( selectedNode && !parentID ) {
271 if( selectedNode && selectedNode.id && selectedNode.id.match(/mailtype_([0-9a-z\-]+)$/) )
272 parentID = RegExp.$1;
274 selectedNode = selectedNode.parentNode;
277 if(parentID && parentID.substr(0,3) == 'new') {
278 alert("You have to save a page before adding children underneath it");
282 this.elements.ParentID.value = parentID;
286 var selectIDPrefix = 'draft_' + parentID + '_';
288 if( $('add_type').value == 'type' ) {
290 selectIDPrefix = 'mailtype_';
292 // Call either addtype or adddraft
293 var request = new Ajax.Request( this.action + type + '?ajax=1' + '&ParentID=' + parentID, {
296 onSuccess : function( response ) {
298 $('Form_EditForm').loadNewPage(response.responseText);
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 );
304 $('sitetree').addTypeNode('New newsletter type', $('Form_EditForm_ID').value );
306 // Save it so that change detection will work
307 $('Form_EditForm').save();
309 statusMessage('Added new ' + type);
312 onFailure : function(response) {
313 alert(response.responseText);
314 statusMessage('Could not add new ' + type );
322 reloadSiteTree: function( response ) {
323 statusMessage('Added new newsletter type', 'good' );
324 $('sitetree_holder').innerHTML = response.responseText;
325 Behaviour.apply( $('sitetree_holder') );
328 button_onclick : function() {
329 if(treeactions.toggleSelection(this)) {
330 var selectedNode = $('sitetree').firstSelected();
332 while(selectedNode.parentTreeNode && !selectedNode.hints.defaultChild) {
333 $('sitetree').changeCurrentTo(selectedNode.parentTreeNode);
334 selectedNode = selectedNode.parentTreeNode;
338 this.o1 = $('sitetree').observeMethod('SelectionChanged', this.treeSelectionChanged.bind(this));
339 this.o2 = $(_HANDLER_FORMS[this.id]).observeMethod('Close', this.popupClosed.bind(this));
344 treeSelectionChanged: function( treeNode ) {
345 this.selected = treeNode;
348 popupClosed: function() {
349 $('sitetree').stopObserving(this.o1);
350 $(_HANDLER_FORMS.addtype).stopObserving(this.o2);
354 function removeTreeNodeByIdx( tree, nodeID ) {
355 var node = tree.getTreeNodeByIdx( nodeID );
357 if( node.parentTreeNode )
358 node.parentTreeNode.removeTreeNode( node );
360 $('sitetree').removeTreeNode( node );
364 * Initialisation function to set everything up
366 Behaviour.addLoader(function () {
368 Observable.applyTo($('addtype_options'));
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;
376 // Set up delete drafts
377 Observable.applyTo($('deletedrafts_options'));
379 var deleteDrafts = $('deletedrafts');
382 deleteDrafts.onclick = deletedraft.button_onclick;
383 deleteDrafts.getElementsByTagName('button')[0].onclick = function() {return false;};
384 $('deletedrafts_options').onsubmit = deletedraft.form_submit;