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

Last change on this file since 29077 was 29077, checked in by kjdon, 10 years ago

javascript escape function was deprecated in Java 1.5 Use encodeURI instead. This fixes pei jones issue of macrons disappearing from edited metadata.

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