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

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

new function addMetaSetTolist so that other places can call this - eg when making a fixed list of sets rather than a dynamic one

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