source: main/trunk/greenstone3/web/interfaces/default/js/documentmaker_scripts_util.js@ 28942

Last change on this file since 28942 was 28209, checked in by sjm84, 11 years ago

Added a clear method to StatusBar

  • Property svn:executable set to *
File size: 27.3 KB
Line 
1//Some "constants" to match the server constants
2var SUCCESS = 1;
3var ACCEPTED = 2;
4var ERROR = 3;
5var CONTINUING = 10;
6var COMPLETED = 11;
7var HALTED = 12;
8
9function getElementsByClassName(cl, parent)
10{
11 var elemArray = [];
12 var classRegEx = new RegExp("\\b" + cl + "\\b");
13 var elems;
14 if(parent)
15 {
16 elems = parent.getElementsByTagName("*");
17 }
18 else
19 {
20 elems = document.getElementsByTagName("*");
21 }
22
23 for (var i = 0; i < elems.length; i++)
24 {
25 var classeName = elems[i].className;
26 if (classRegEx.test(classeName)) elemArray.push(elems[i]);
27 }
28
29 return elemArray;
30};
31
32function getNextSiblingOfType(elem, type)
33{
34 if(elem == null)
35 {
36 return null;
37 }
38
39 var current = elem.nextSibling;
40 while(current != null)
41 {
42 if(current.nodeName.toLowerCase() == type)
43 {
44 return current;
45 }
46
47 current = current.nextSibling;
48 }
49 return null;
50}
51
52function getPrevSiblingOfType(elem, type)
53{
54 if(elem == null)
55 {
56 return null;
57 }
58
59 var current = elem.previousSibling;
60 while(current != null)
61 {
62 if(current.nodeName.toLowerCase() == type)
63 {
64 return current;
65 }
66
67 current = current.previousSibling;
68 }
69 return null;
70}
71
72function saveTransaction(transaction)
73{
74 console.log(transaction);
75 _transactions.push(transaction);
76}
77
78function undo()
79{
80 if(_undoOperations.length == 0)
81 {
82 return;
83 }
84
85 var undoOp = _undoOperations.pop();
86
87 //Create/Duplicate undo
88 if(undoOp.op == "del")
89 {
90 if(undoOp.srcElem.childList)
91 {
92 removeFromParent(undoOp.srcElem.childList);
93 }
94 if(undoOp.srcElem.parentItem)
95 {
96 undoOp.srcElem.parentItem.menu.newSectionLink.style.display = "inline";
97 undoOp.srcElem.parentItem.childList = null;
98 }
99 removeFromParent(undoOp.srcElem);
100 }
101
102 if(undoOp.op == "delMeta")
103 {
104 if(undoOp.srcElem.childList)
105 {
106 removeFromParent(undoOp.srcElem.childList);
107 }
108 if(undoOp.srcElem.parentItem)
109 {
110 undoOp.srcElem.parentItem.menu.newSectionLink.style.display = "inline";
111 undoOp.srcElem.parentItem.childList = null;
112 }
113 de.doc.unregisterEditSection(undoOp.srcElem);
114 removeFromParent(undoOp.srcElem);
115 }
116
117 //Move undo (mva is move after, mvb is move before, mvi is move into)
118 else if(undoOp.op == "mva" || undoOp.op == "mvb" || undoOp.op == "mvi")
119 {
120 if(undoOp.op == "mvb")
121 {
122 undoOp.refElem.parentNode.insertBefore(undoOp.srcElem, undoOp.refElem);
123 }
124 else if(undoOp.op == "mva")
125 {
126 insertAfter(undoOp.srcElem, undoOp.refElem);
127 }
128 else
129 {
130 undoOp.refElem.removeChild(undoOp.refElem.firstChild);
131 undoOp.refElem.appendChild(undoOp.srcElem);
132 }
133
134 if(undoOp.srcElem.textDiv)
135 {
136 insertAfter(undoOp.srcElem.textDiv, undoOp.srcElem);
137 }
138 if(undoOp.srcElem.childList)
139 {
140 insertAfter(undoOp.srcElem.childList, undoOp.srcElem.textDiv);
141 }
142
143 if(undoOp.srcElem.onmouseout)
144 {
145 //Uncolour the section if it coloured
146 undoOp.srcElem.onmouseout();
147 }
148 updateFromTop();
149 }
150 else if(undoOp.op == "display")
151 {
152 undoOp.srcElem.style.display = undoOp.subOp;
153 }
154
155 if(undoOp.removeDeletedMetadata)
156 {
157 _deletedMetadata.pop();
158 }
159
160 if(undoOp.removeTransaction)
161 {
162 _transactions.pop();
163 }
164}
165
166function addCollectionToBuild(collection)
167{
168 for(var i = 0; i < _collectionsToBuild.length; i++)
169 {
170 if(collection == _collectionsToBuild[i])
171 {
172 return;
173 }
174 }
175 _collectionsToBuild.push(collection);
176}
177
178function save()
179{
180 //This works in most cases but will not work when taking a doc from one collection to another, will need to be fixed at some point
181 var collection;
182 if(gs.cgiParams.c && gs.cgiParams.c != "")
183 {
184 collection = gs.cgiParams.c
185 }
186 else
187 {
188 collection = gs.cgiParams.p_c
189 }
190
191 for(var i = 0; i < _deletedMetadata.length; i++)
192 {
193 var currentRow = _deletedMetadata[i];
194
195 //Get document ID
196 var currentElem = currentRow;
197 while((currentElem = currentElem.parentNode).tagName != "TABLE");
198 var docID = currentElem.getAttribute("id").substring(4);
199
200 //Get metadata name
201 var cells = currentRow.getElementsByTagName("TD");
202 var nameCell = cells[0];
203 var name = nameCell.innerHTML;
204 var valueCell = cells[1];
205 var value = valueCell.innerHTML;
206
207 gs.functions.removeArchivesMetadata(collection, gs.xsltParams.site_name, docID, name, null, value, function(){console.log("REMOVED ARCHIVES");});
208 addCollectionToBuild(collection);
209
210 removeFromParent(currentRow);
211 }
212
213 var changes = de.Changes.getChangedEditableSections();
214 var metadataChanges = new Array();
215
216 for(var i = 0; i < changes.length; i++)
217 {
218 var changedElem = changes[i];
219
220 //Save metadata
221 if(gs.functions.hasClass(changedElem, "metaTableCell"))
222 {
223 //Get document ID
224 var currentElem = changedElem;
225 while((currentElem = currentElem.parentNode).tagName != "TABLE");
226 var docID = currentElem.getAttribute("id").substring(4);
227
228 //Get metadata name
229 var row = changedElem.parentNode;
230 var cells = row.getElementsByTagName("TD");
231 var nameCell = cells[0];
232 var name = nameCell.innerHTML;
233 var value = changedElem.innerHTML;
234 value = value.replace(/&nbsp;/g, " ");
235 value = escape(value);
236
237 changedElem.originalValue = changedElem.innerHTML;
238
239 metadataChanges.push({collection:collection, docID:docID, name:name, value:value, orig:changedElem.originalValue});
240 addCollectionToBuild(collection);
241 }
242 //Save content
243 else if(gs.functions.hasClass(changedElem, "renderedText"))
244 {
245 var section = changedElem.parentDiv.parentItem;
246 saveTransaction('{"operation":"setText", "text":"' + changedElem.innerHTML.replace(/"/g, "\\\"").replace(/&/g, "%26") + '", "collection":"' + section.collection + '", "oid":"' + section.nodeID + '"}');
247 addCollectionToBuild(section.collection);
248 }
249 else if(gs.functions.hasClass(changedElem, "sectionText"))
250 {
251 var id = changedElem.getAttribute("id");
252 var sectionID = id.substring(4);
253 saveTransaction('{"operation":"setText", "text":"' + changedElem.innerHTML.replace(/"/g, "\\\"").replace(/&/g, "%26") + '", "collection":"' + gs.cgiParams.c + '", "oid":"' + sectionID + '"}');
254 addCollectionToBuild(gs.cgiParams.c);
255 }
256 }
257
258 var sendBuildRequest = function()
259 {
260 var request = "[";
261 for(var i = 0; i < _transactions.length; i++)
262 {
263 request += _transactions[i];
264 if(i != _transactions.length - 1)
265 {
266 request += ",";
267 }
268 }
269 request += "]";
270
271 var statusID;
272 var ajax = new gs.functions.ajaxRequest();
273 ajax.open("POST", gs.xsltParams.library_name, true);
274 ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
275 ajax.onreadystatechange = function()
276 {
277 if(ajax.readyState == 4 && ajax.status == 200)
278 {
279 var text = ajax.responseText;
280 var xml = validateXML(text);
281
282 var errorElems;
283 if(!xml || checkForErrors(xml))
284 {
285 alert(gs.text.dse.error_saving);
286
287 $("#saveButton, #quickSaveButton").html(gs.text.dse.save_changes);
288 $("#saveButton, #quickSaveButton").removeAttr("disabled");
289
290 if(_statusBar)
291 {
292 _statusBar.removeStatus(statusID);
293 }
294 return;
295 }
296
297 if(_statusBar)
298 {
299 _statusBar.removeStatus(statusID);
300 }
301 buildCollections(_collectionsToBuild);
302 }
303 }
304
305 if(_collectionsToBuild.length > 0)
306 {
307 $("#saveButton, #quickSaveButton").html(gs.text.dse.saving + "...");
308 $("#saveButton, #quickSaveButton").attr("disabled", "disabled");
309
310 if(_statusBar)
311 {
312 statusID = _statusBar.addStatus(gs.text.dse.modifying_archives + "...");
313 }
314 ajax.send("a=g&rt=r&s=DocumentExecuteTransaction&s1.transactions=" + request);
315 }
316 }
317
318 var setMetadataLoop = function(index)
319 {
320 var change = metadataChanges[index];
321
322 var callbackFunction;
323 if(index + 1 == metadataChanges.length)
324 {
325 callbackFunction = sendBuildRequest;
326 }
327 else
328 {
329 callbackFunction = function(){setMetadataLoop(index + 1)};
330 }
331
332 if(change.orig)
333 {
334 gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, change.value, change.orig, "override", function(){callbackFunction();});
335 }
336 else
337 {
338 gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, change.value, null, "accumulate", function(){callbackFunction();});
339 }
340 }
341 setMetadataLoop(0);
342}
343
344function buildCollections(collections, documents, callback)
345{
346 if(!collections || collections.length == 0)
347 {
348 console.log(gs.text.dse.empty_collection_list);
349 $("#saveButton, #quickSaveButton").html(gs.text.save_changes);
350 $("#saveButton, #quickSaveButton").removeAttr("disabled");
351 return;
352 }
353
354 var docs = "";
355 var buildOperation = "";
356 if(documents)
357 {
358 buildOperation = "ImportCollection";
359 docs += "&s1.documents=";
360 for(var i = 0; i < documents.length; i++)
361 {
362 docs += documents[i];
363 if(i < documents.length - 1)
364 {
365 docs += ",";
366 }
367 }
368 }
369 else
370 {
371 buildOperation = "BuildAndActivateCollection";
372 }
373
374 var counter = 0;
375 var statusID = 0;
376 var buildFunction = function()
377 {
378 var ajax = new gs.functions.ajaxRequest();
379 ajax.open("GET", gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=" + buildOperation + "&s1.collection=" + collections[counter] + docs);
380 ajax.onreadystatechange = function()
381 {
382 if(ajax.readyState == 4 && ajax.status == 200)
383 {
384 var text = ajax.responseText;
385 var xml = validateXML(text);
386
387 if(!xml || checkForErrors(xml))
388 {
389 alert(gs.text.dse.could_not_build_p1 + " " + collections[counter] + gs.text.dse.could_not_build_p2);
390
391 if(_statusBar)
392 {
393 _statusBar.removeStatus(statusID);
394 }
395 $("#saveButton, #quickSaveButton").html(gs.text.dse.save_changes);
396 $("#saveButton, #quickSaveButton").removeAttr("disabled");
397
398 return;
399 }
400
401 var status = xml.getElementsByTagName("status")[0];
402 var pid = status.getAttribute("pid");
403
404 startCheckLoop(pid, buildOperation, statusID, function()
405 {
406 /*
407 var localAjax = new gs.functions.ajaxRequest();
408 localAjax.open("GET", gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ActivateCollection&s1.collection=" + collections[counter], true);
409 localAjax.onreadystatechange = function()
410 {
411 if(localAjax.readyState == 4 && localAjax.status == 200)
412 {
413 var localText = localAjax.responseText;
414 var localXML = validateXML(localText);
415
416 if(!xml || checkForErrors(xml))
417 {
418 alert(gs.text.dse.could_not_activate_p1 + " " + collections[counter] + gs.text.dse.could_not_activate_p2);
419
420 if(_statusBar)
421 {
422 _statusBar.removeStatus(statusID);
423 }
424 $("#saveButton, #quickSaveButton").html(gs.text.dse.save_changes);
425 $("#saveButton, #quickSaveButton").removeAttr("disabled");
426
427 return;
428 }
429
430 var localStatus = localXML.getElementsByTagName("status")[0];
431 if(localStatus)
432 {
433 var localPID = localStatus.getAttribute("pid");
434 startCheckLoop(localPID, "ActivateCollection", statusID, function()
435 {
436 */
437 if(counter == collections.length - 1)
438 {
439 removeCollectionsFromBuildList(collections);
440 if(callback)
441 {
442 callback();
443 }
444 }
445 else
446 {
447 counter++;
448 buildFunction();
449 }
450
451 _transactions = new Array();
452
453 if(_statusBar)
454 {
455 _statusBar.removeStatus(statusID);
456 }
457 $("#saveButton, #quickSaveButton").html(gs.text.dse.save_changes);
458 $("#saveButton, #quickSaveButton").removeAttr("disabled");
459 /*
460 });
461 }
462 }
463 }
464 if(_statusBar)
465 {
466 _statusBar.changeStatus(statusID, gs.text.dse.activating + " " + collections[counter] + "...");
467 }
468 localAjax.send();
469 */
470 });
471 }
472 }
473 if(_statusBar)
474 {
475 statusID = _statusBar.addStatus(gs.text.dse.building + " " + collections[counter] + "...");
476 }
477 ajax.send();
478 }
479 buildFunction();
480}
481
482function startCheckLoop(pid, serverFunction, statusID, callbackFunction)
483{
484 var ajaxFunction = function()
485 {
486 var ajax = new gs.functions.ajaxRequest();
487 ajax.open("GET", gs.xsltParams.library_name + "?a=g&rt=s&ro=1&s=" + serverFunction + "&s1.pid=" + pid, true);
488 ajax.onreadystatechange = function()
489 {
490 if(ajax.readyState == 4 && ajax.status == 200)
491 {
492 var text = ajax.responseText;
493 var xml = validateXML(text);
494
495 if(!xml || checkForErrors(xml))
496 {
497 alert(gs.text.dse.could_not_check_status_p1 + " " + serverFunction + gs.text.dse.could_not_check_status_p2a);
498
499 if(_statusBar)
500 {
501 _statusBar.removeStatus(statusID);
502 }
503 $("#saveButton, #quickSaveButton").html(gs.text.dse.save_changes);
504 $("#saveButton, #quickSaveButton").removeAttr("disabled");
505
506 return;
507 }
508
509 var status = xml.getElementsByTagName("status")[0];
510 var code = status.getAttribute("code");
511
512 if (code == COMPLETED || code == SUCCESS)
513 {
514 callbackFunction();
515 }
516 else if (code == HALTED || code == ERROR)
517 {
518 alert(gs.text.dse.could_not_check_status_p1 + " " + serverFunction + gs.text.dse.could_not_check_status_p2b);
519
520 if(_statusBar)
521 {
522 _statusBar.removeStatus(statusID);
523 }
524 $("#saveButton, #quickSaveButton").html(gs.text.dse.save_changes);
525 $("#saveButton, #quickSaveButton").removeAttr("disabled");
526 }
527 else
528 {
529 setTimeout(ajaxFunction, 1000);
530 }
531 }
532 }
533 ajax.send();
534 }
535 ajaxFunction();
536}
537
538function removeCollectionsFromBuildList(collections)
539{
540 var tempArray = new Array();
541 for(var i = 0; i < _collectionsToBuild.length; i++)
542 {
543 var found = false;
544 for(var j = 0; j < collections.length; j++)
545 {
546 if(collections[j] == _collectionsToBuild[i])
547 {
548 found = true;
549 break;
550 }
551 }
552
553 if(!found)
554 {
555 tempArray.push(_collectionsToBuild[i]);
556 }
557 }
558 _collectionsToBuild = tempArray;
559}
560
561function checkForErrors(xml)
562{
563 var errorElems = xml.getElementsByTagName("error");
564
565 if(errorElems && errorElems.length > 0)
566 {
567 var errorString = gs.text.dse.error_saving_changes + ": ";
568 for(var i = 0; i < errorElems.length; i++)
569 {
570 errorString += " " + errorElems.item(i).firstChild.nodeValue;
571 }
572 alert(errorString);
573 return true;
574 }
575 return false; //No errors
576}
577
578function validateXML(txt)
579{
580 // code for IE
581 if (window.ActiveXObject)
582 {
583 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
584 xmlDoc.async = "false";
585 xmlDoc.loadXML(document.all(txt).value);
586
587 if(xmlDoc.parseError.errorCode!=0)
588 {
589 txt = dse.error_code + ": " + xmlDoc.parseError.errorCode + "\n";
590 txt = txt + dse.error_reason + ": " + xmlDoc.parseError.reason;
591 txt = txt + dse.error_line + ": " + xmlDoc.parseError.line;
592 console.log(txt);
593 return null;
594 }
595
596 return xmlDoc;
597 }
598 // code for Mozilla, Firefox, Opera, etc.
599 else if (document.implementation.createDocument)
600 {
601 var parser = new DOMParser();
602 var xmlDoc = parser.parseFromString(txt,"text/xml");
603
604 if (xmlDoc.getElementsByTagName("parsererror").length > 0)
605 {
606 console.log(gs.text.dse.xml_error);
607 return null;
608 }
609
610 return xmlDoc;
611 }
612 else
613 {
614 console.log(gs.text.dse.browse_cannot_validate_xml);
615 }
616 return null;
617}
618
619function onVisibleMetadataSetChange()
620{
621 var metadataList = document.getElementById("metadataSetList");
622 var index = metadataList.selectedIndex;
623 var options = metadataList.getElementsByTagName("OPTION");
624 var selectedOption = options[index];
625
626 var selectedSet = selectedOption.innerHTML;
627 changeVisibleMetadata(selectedSet);
628}
629
630function changeVisibleMetadata(metadataSetName)
631{
632 var tables = document.getElementsByTagName("TABLE");
633 for(var i = 0; i < tables.length; i++)
634 {
635 var id = tables[i].getAttribute("id");
636 if(id && id.search(/^meta/) != -1)
637 {
638 var rows = tables[i].getElementsByTagName("TR");
639 for(var j = 0; j < rows.length; j++)
640 {
641 if(metadataSetName == "All")
642 {
643 rows[j].style.display = "table-row";
644 }
645 else
646 {
647 var cells = rows[j].getElementsByTagName("TD");
648 var cellName = cells[0].innerHTML;
649
650 if(cellName.indexOf(".") == -1)
651 {
652 rows[j].style.display = "none";
653 }
654 else
655 {
656 var setName = cellName.substring(0, cellName.lastIndexOf("."));
657 if(setName == metadataSetName)
658 {
659 rows[j].style.display = "table-row";
660 }
661 else
662 {
663 rows[j].style.display = "none";
664 }
665 }
666 }
667 }
668 }
669 }
670}
671
672function asyncRegisterEditSection(cell)
673{
674 //This registering can cause a sizeable delay so we'll thread it (effectively) so the browser is not paused
675 cell.originalValue = cell.innerHTML;
676 setTimeout(function(){de.doc.registerEditSection(cell)}, 0);
677}
678
679function addFunctionalityToTable(table)
680{
681 table.find("tr").each(function()
682 {
683 var cells = $(this).find("td");
684 var metadataName = $(cells[0]).html();
685
686 if(metadataName.indexOf(".") != -1)
687 {
688 var metadataSetName = metadataName.substring(0, metadataName.lastIndexOf("."));
689
690 var found = false;
691 for(var j = 0; j < _metadataSetList.length; j++)
692 {
693 if(metadataSetName == _metadataSetList[j])
694 {
695 found = true;
696 break;
697 }
698 }
699
700 if(!found)
701 {
702 _metadataSetList.push(metadataSetName);
703
704 var metadataSetList = $("#metadataSetList");
705 var newOption = $("<option>");
706 newOption.html(metadataSetName);
707 metadataSetList.append(newOption);
708 }
709 }
710
711 asyncRegisterEditSection(cells[1]);
712 addRemoveLinkToRow(this);
713 });
714
715
716 var metaNameField = $("<input>", {"type": "text","style":"margin: 5px; border: 1px solid #000;"});
717 table.after(metaNameField);
718 table.metaNameField = metaNameField;
719
720 var addRowButton = $("<button>",{"class": "ui-state-default ui-corner-all", "style": "margin: 5px;"});
721 addRowButton.html(gs.text.dse.add_new_metadata);
722 addRowButton.click(function()
723 {
724 var name = metaNameField.val();
725 if(!name || name == "")
726 {
727 console.log(gs.text.dse.no_value_given);
728 return;
729 }
730
731 var newRow = $("<tr>");
732 var nameCell = $("<td>" + name + "</td>");
733 nameCell.attr("class", "metaTableCellName");
734 var valueCell = $("<td>", {"class": "metaTableCell"});
735
736 newRow.append(nameCell);
737 newRow.append(valueCell);
738 addRemoveLinkToRow(newRow);
739 table.append(newRow);
740
741 var undo = new Array();
742 undo.op = "delMeta";
743 undo.srcElem = newRow;
744 undo.removeTransaction = false;
745 _undoOperations.push(undo);
746
747 //Threading this function here probably isn't necessary like the other times it is called
748 de.doc.registerEditSection(valueCell[0]);
749 });
750 table.addRowButton = addRowButton;
751 metaNameField.after(addRowButton);
752}
753
754function addRemoveLinkToRow(row)
755{
756 var newCell = $("<td>");
757 var removeLink = $("<a>remove</a>", {"href": "javascript:;"});
758 removeLink.click(function()
759 {
760 var undo = new Array();
761 undo.srcElem = row;
762 undo.op = "display";
763 undo.subOp = "table-row";
764 undo.removeDeletedMetadata = true;
765 _undoOperations.push(undo);
766 _deletedMetadata.push(row);
767 row.css("display", "none");
768 });
769 newCell.append(removeLink);
770 newCell.attr({"class": "metaTableCell", "style": "font-size:0.6em; padding-left: 3px; padding-right: 3px;"});
771 $(row).append(newCell);
772}
773
774function createTopMenuBar()
775{
776 //Create the top menu bar
777 var headerTable = document.createElement("TABLE");
778 var tableBody = document.createElement("TBODY");
779 var row = document.createElement("TR");
780 var newDocCell = document.createElement("TD");
781 var newSecCell = document.createElement("TD");
782 var saveCell = document.createElement("TD");
783 var undoCell = document.createElement("TD");
784 var metadataListCell = document.createElement("TD");
785
786 var metadataListLabel = document.createElement("SPAN");
787 metadataListLabel.innerHTML = "Visible metadata: ";
788 var metadataList = document.createElement("SELECT");
789 metadataList.setAttribute("id", "metadataSetList");
790 metadataList.onchange = onVisibleMetadataSetChange;
791 var allMetadataOption = document.createElement("OPTION");
792 metadataList.appendChild(allMetadataOption);
793 allMetadataOption.innerHTML = "All";
794 metadataListCell.appendChild(metadataListLabel);
795 metadataListCell.appendChild(metadataList);
796
797 metadataListCell.setAttribute("class", "headerTableTD");
798 newDocCell.setAttribute("class", "headerTableTD");
799 newSecCell.setAttribute("class", "headerTableTD");
800 undoCell.setAttribute("class", "headerTableTD");
801 saveCell.setAttribute("class", "headerTableTD");
802
803 headerTable.appendChild(tableBody);
804 tableBody.appendChild(row);
805 row.appendChild(saveCell);
806 row.appendChild(undoCell);
807 row.appendChild(newDocCell);
808 row.appendChild(newSecCell);
809 row.appendChild(metadataListCell);
810
811 //The "Save changes" button
812 var saveButton = document.createElement("BUTTON");
813 saveButton.innerHTML = gs.text.dse.save_changes;
814 saveButton.setAttribute("onclick", "save();");
815 saveButton.setAttribute("id", "saveButton");
816 saveCell.appendChild(saveButton);
817
818 //The "Undo" button
819 var undoButton = document.createElement("BUTTON");
820 undoButton.innerHTML = "Undo";
821 undoButton.setAttribute("onclick", "undo();");
822 undoCell.appendChild(undoButton);
823
824 //The "Create new document" button
825 var newDocButton = document.createElement("BUTTON");
826 newDocButton.innerHTML = gs.text.dse.create_new_document;
827 newDocButton.setAttribute("onclick", "createNewDocumentArea();");
828 newDocButton.setAttribute("id", "createNewDocumentButton");
829 newDocCell.appendChild(newDocButton);
830
831 //The "Insert new section" LI
832 var newSecLI = createDraggableNewSection(newSecCell);
833
834 return headerTable;
835}
836
837function getMetadataFromNode(node, name)
838{
839 var currentNode = node.firstChild;
840 while(currentNode != null)
841 {
842 if(currentNode.nodeName == "metadataList")
843 {
844 currentNode = currentNode.firstChild;
845 break;
846 }
847
848 currentNode = currentNode.nextSibling;
849 }
850
851 while(currentNode != null)
852 {
853 if(currentNode.nodeName == "metadata" && currentNode.getAttribute("name") == name)
854 {
855 return currentNode.firstChild.nodeValue;
856 }
857
858 currentNode = currentNode.nextSibling;
859 }
860 return "";
861}
862
863function storeMetadata(node, listItem)
864{
865 listItem.metadata = new Array();
866
867 var currentNode = node.firstChild;
868 while(currentNode != null)
869 {
870 if(currentNode.nodeName == "metadataList")
871 {
872 currentNode = currentNode.firstChild;
873 break;
874 }
875
876 currentNode = currentNode.nextSibling;
877 }
878
879 while(currentNode != null)
880 {
881 if(currentNode.nodeName == "metadata")
882 {
883 listItem.metadata[currentNode.getAttribute("name")] = currentNode.firstChild.nodeValue;
884 }
885
886 currentNode = currentNode.nextSibling;
887 }
888}
889
890function getNodeContent(node)
891{
892 var currentNode = node.firstChild;
893 while(currentNode != null)
894 {
895 if(currentNode.nodeName == "nodeContent")
896 {
897 return currentNode.firstChild;
898 }
899
900 currentNode = currentNode.nextSibling;
901 }
902 return null;
903}
904
905function containsDocumentNode(node)
906{
907 var currentNode = node.firstChild;
908 while(currentNode != null)
909 {
910 if(currentNode.nodeName == "documentNode")
911 {
912 return true;
913 }
914
915 currentNode = currentNode.nextSibling;
916 }
917 return false;
918}
919
920function isExpanded(textDiv)
921{
922 if(!textDiv.style.display || textDiv.style.display == "block")
923 {
924 return true;
925 }
926 return false;
927}
928
929function toggleTextDiv(section)
930{
931 var textDiv = section.textDiv;
932 if(textDiv)
933 {
934 if(isExpanded(textDiv))
935 {
936 textDiv.style.display = "none";
937 section.menu.editTextLink.innerHTML = gs.text.dse.edit;
938 }
939 else
940 {
941 textDiv.style.display = "block";
942 section.menu.editTextLink.innerHTML = gs.text.dse.hide;
943 }
944 }
945}
946
947function updateFromTop()
948{
949 updateRecursive(document.getElementById("dbDiv"), null, null, 0);
950}
951
952function insertAfter(elem, refElem)
953{
954 if(refElem.nextSibling)
955 {
956 refElem.parentNode.insertBefore(elem, refElem.nextSibling);
957 }
958 else
959 {
960 refElem.parentNode.appendChild(elem);
961 }
962}
963
964function removeFromParent(elem)
965{
966 elem.parentNode.removeChild(elem);
967}
968
969function createSectionTitle(text)
970{
971 var textSpan = document.createElement("SPAN");
972 if(text)
973 {
974 textSpan.appendChild(document.createTextNode(" " + text + " "));
975 }
976 else
977 {
978 textSpan.appendChild(document.createTextNode(" [" + gs.text.dse.untitled_section + "] "));
979 }
980 return textSpan;
981}
982
983function setMouseOverAndOutFunctions(section)
984{
985 //Colour the list item and display the menu on mouse over
986 section.onmouseover = function(e)
987 {
988 if(this.menu){this.menu.style.display = "inline";}
989 this.style.background = "rgb(255, 200, 0)";
990 };
991 //Uncolour the list item and hide the menu on mouse out
992 section.onmouseout = function(e)
993 {
994 if(this.menu){this.menu.style.display = "none";}
995 this.style.background = "none";
996 };
997}
998
999function createDraggableNewSection(parent)
1000{
1001 var newSecLI = document.createElement("LI");
1002 var newSpan = document.createElement("SPAN");
1003 newSpan.innerHTML = gs.text.dse.insert_new_section + " ";
1004
1005 newSecLI.sectionTitle = newSpan;
1006 newSecLI.appendChild(newSpan);
1007 newSecLI.setAttribute("class", "dragItem newSection");
1008 newSecLI.newSection = true;
1009 newSecLI.parent = parent;
1010 newSecLI.index = -1;
1011 new YAHOO.example.DDList(newSecLI);
1012 parent.appendChild(newSecLI);
1013}
1014
1015function closeAllOpenContents()
1016{
1017 for(var i = 0; i < _allContents.length; i++)
1018 {
1019 if(isExpanded(_allContents[i].textDiv))
1020 {
1021 toggleTextDiv(_allContents[i]);
1022 }
1023 }
1024 DDM.refreshCache();
1025}
1026
1027//Status Bar class (initialised with new StatusBar(elem);)
1028function StatusBar(mainElem)
1029{
1030 var _statusMap = new Array();
1031 var _statusIDCounter = 0;
1032 var _mainElem = mainElem;
1033 var _activeMessages = 0;
1034
1035 _mainElem.style.display = "none";
1036
1037 this.addStatus = function(newStatus)
1038 {
1039 _mainElem.style.display = "block";
1040 var newStatusDiv = document.createElement("DIV");
1041 var newStatusSpan = document.createElement("SPAN");
1042
1043 var workingImage = document.createElement("IMG");
1044 workingImage.setAttribute("src", gs.imageURLs.loading);
1045 workingImage.setAttribute("height", "16px");
1046 workingImage.setAttribute("width", "16px");
1047 newStatusDiv.appendChild(workingImage);
1048
1049 newStatusDiv.appendChild(newStatusSpan);
1050 newStatusSpan.innerHTML = " " + newStatus;
1051 newStatusDiv.setAttribute("class", "statusMessage");
1052 newStatusDiv.span = newStatusSpan;
1053
1054 _mainElem.appendChild(newStatusDiv);
1055 _statusMap["status" + _statusIDCounter] = newStatusDiv;
1056 _activeMessages++;
1057 return _statusIDCounter++;
1058 }
1059
1060 this.changeStatus = function(id, newStatus)
1061 {
1062 if(_statusMap["status" + id])
1063 {
1064 _statusMap["status" + id].span.innerHTML = " " + newStatus;
1065 }
1066 }
1067
1068 this.removeStatus = function(id)
1069 {
1070 if(_statusMap["status" + id])
1071 {
1072 removeFromParent(_statusMap["status" + id]);
1073
1074 if(--_activeMessages == 0)
1075 {
1076 _mainElem.style.display = "none";
1077 }
1078 }
1079 }
1080
1081 this.clear = function()
1082 {
1083 for(var p in _statusMap)
1084 {
1085 if(_statusMap.hasOwnProperty(p))
1086 {
1087 if(_statusMap[p] && _statusMap[p].parentNode)
1088 {
1089 removeFromParent(_statusMap[p]);
1090 }
1091
1092 if(--_activeMessages == 0)
1093 {
1094 _mainElem.style.display = "none";
1095 }
1096 }
1097 }
1098 }
1099}
1100
1101/*
1102function toggleEdit(e)
1103{
1104 var mousePos = de.events.getXYInWindowFromEvent(e);
1105 var cDesc = de.cursor.getCursorDescAtXY(mousePos.x, mousePos.y, de.events.getEventTarget(e));
1106 de.cursor.setCursor(cDesc);
1107}
1108*/
Note: See TracBrowser for help on using the repository browser.