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

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

style the remove link separately; metadata element input cna now be a select element, or an input element, which can optionally have autocomplete data. based on availableMetadataElements var, and new_metadata_field_input_type var. addMetaSetToList function changed name to addOptionTolist, as it can be used for adding any options. Added new param so can have different value and display text for the options.

  • Property svn:executable set to *
File size: 29.0 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.value;
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 addOptionToList(list, optionvalue, optiontext, selected) {
689 var newOption = $("<option>");
690 if (optiontext) {
691 newOption.html(optiontext);
692 newOption.attr("value", optionvalue);
693 } else {
694 newOption.html(optionvalue);
695 }
696 if (selected) {
697 newOption.attr("selected", true);
698 }
699 list.append(newOption);
700}
701
702/* returns either an input or a select element. Data based on
703 availableMetadataElements var. */
704function createMetadataElementSelector() {
705 var metaNameField;
706 if (new_metadata_field_input_type == "fixedlist") {
707 metaNameField = $("<select>", {"class": "ui-state-default"});
708 for(var i=0; i<availableMetadataElements.length; i++) {
709 addOptionToList(metaNameField, availableMetadataElements[i]);
710 }
711 return metaNameField;
712 }
713 metaNameField = $("<input>", {"type": "text","style":"margin: 5px; border: 1px solid #000;"});
714 if (new_metadata_field_input_type == "autocomplete") {
715 metaNameField.autocomplete({
716 minLength: 0,
717 source: availableMetadataElements
718 });
719 metaNameField.attr("title", "Enter a metadata name, or use the down arrow to select one, then click 'Add New Metadata'");
720 } else {
721metaNameField.attr("title", "Enter a metadata name, then click 'Add New Metadata'");
722 }
723
724 return metaNameField;
725}
726
727
728
729function addFunctionalityToTable(table)
730{
731 table.find("tr").each(function()
732 {
733 var cells = $(this).find("td");
734 var metadataName = $(cells[0]).html();
735
736 if(dynamic_metadata_set_list == true && metadataName.indexOf(".") != -1)
737 {
738 var metadataSetName = metadataName.substring(0, metadataName.lastIndexOf("."));
739
740 var found = false;
741 for(var j = 0; j < _metadataSetList.length; j++)
742 {
743 if(metadataSetName == _metadataSetList[j])
744 {
745 found = true;
746 break;
747 }
748 }
749
750 if(!found)
751 {
752 _metadataSetList.push(metadataSetName);
753 addOptionToList( $("#metadataSetList"), metadataSetName);
754 }
755 }
756
757 asyncRegisterEditSection(cells[1]);
758 addRemoveLinkToRow(this);
759 });
760
761 var metaNameField = createMetadataElementSelector();
762 table.after(metaNameField);
763 table.metaNameField = metaNameField;
764
765 var addRowButton = $("<button>",{"class": "ui-state-default ui-corner-all", "style": "margin: 5px;"});
766 addRowButton.html(gs.text.dse.add_new_metadata);
767 addRowButton.click(function()
768 {
769 var name = metaNameField.val();
770 if(!name || name == "")
771 {
772 console.log(gs.text.dse.no_value_given);
773 return;
774 }
775
776 var newRow = $("<tr>");
777 var nameCell = $("<td>" + name + "</td>");
778 nameCell.attr("class", "metaTableCellName");
779 var valueCell = $("<td>", {"class": "metaTableCell"});
780
781 newRow.append(nameCell);
782 newRow.append(valueCell);
783 addRemoveLinkToRow(newRow);
784 table.append(newRow);
785
786 var undo = new Array();
787 undo.op = "delMeta";
788 undo.srcElem = newRow;
789 undo.removeTransaction = false;
790 _undoOperations.push(undo);
791
792 //Threading this function here probably isn't necessary like the other times it is called
793 de.doc.registerEditSection(valueCell[0]);
794 });
795 table.addRowButton = addRowButton;
796 metaNameField.after(addRowButton);
797
798}
799
800function addRemoveLinkToRow(row)
801{
802 var newCell = $("<td>");
803 var removeLink = $("<a>remove</a>", {"href": "javascript:;"});
804 removeLink.click(function()
805 {
806 var undo = new Array();
807 undo.srcElem = row;
808 undo.op = "display";
809 undo.subOp = "table-row";
810 undo.removeDeletedMetadata = true;
811 _undoOperations.push(undo);
812 _deletedMetadata.push(row);
813 //row.css("display", "none");
814 $(row).hide();
815 });
816 newCell.append(removeLink);
817 newCell.attr({"class": "metaTableCellRemove", "style": "font-size:0.6em; padding-left: 3px; padding-right: 3px;"});
818 $(row).append(newCell);
819}
820
821/* This is for 'edit structure' menu bar */
822function createTopMenuBar()
823{
824 //Create the top menu bar
825 var headerTable = document.createElement("TABLE");
826 var tableBody = document.createElement("TBODY");
827 var row = document.createElement("TR");
828 var newDocCell = document.createElement("TD");
829 var newSecCell = document.createElement("TD");
830 var saveCell = document.createElement("TD");
831 var undoCell = document.createElement("TD");
832 var metadataListCell = document.createElement("TD");
833
834 var metadataListLabel = document.createElement("SPAN");
835 metadataListLabel.innerHTML = "Visible metadata: ";
836 var metadataList = document.createElement("SELECT");
837 metadataList.setAttribute("id", "metadataSetList");
838 metadataList.onchange = onVisibleMetadataSetChange;
839 var allMetadataOption = document.createElement("OPTION");
840 metadataList.appendChild(allMetadataOption);
841 allMetadataOption.innerHTML = "All";
842 metadataListCell.appendChild(metadataListLabel);
843 metadataListCell.appendChild(metadataList);
844
845 metadataListCell.setAttribute("class", "headerTableTD");
846 newDocCell.setAttribute("class", "headerTableTD");
847 newSecCell.setAttribute("class", "headerTableTD");
848 undoCell.setAttribute("class", "headerTableTD");
849 saveCell.setAttribute("class", "headerTableTD");
850
851 headerTable.appendChild(tableBody);
852 tableBody.appendChild(row);
853 row.appendChild(saveCell);
854 row.appendChild(undoCell);
855 row.appendChild(newDocCell);
856 row.appendChild(newSecCell);
857 row.appendChild(metadataListCell);
858
859 //The "Save changes" button
860 var saveButton = document.createElement("BUTTON");
861 saveButton.innerHTML = gs.text.dse.save_changes;
862 saveButton.setAttribute("onclick", "save();");
863 saveButton.setAttribute("id", "saveButton");
864 saveCell.appendChild(saveButton);
865
866 //The "Undo" button
867 var undoButton = document.createElement("BUTTON");
868 undoButton.innerHTML = "Undo";
869 undoButton.setAttribute("onclick", "undo();");
870 undoCell.appendChild(undoButton);
871
872 //The "Create new document" button
873 var newDocButton = document.createElement("BUTTON");
874 newDocButton.innerHTML = gs.text.dse.create_new_document;
875 newDocButton.setAttribute("onclick", "createNewDocumentArea();");
876 newDocButton.setAttribute("id", "createNewDocumentButton");
877 newDocCell.appendChild(newDocButton);
878
879 //The "Insert new section" LI
880 var newSecLI = createDraggableNewSection(newSecCell);
881
882 return headerTable;
883}
884
885function getMetadataFromNode(node, name)
886{
887 var currentNode = node.firstChild;
888 while(currentNode != null)
889 {
890 if(currentNode.nodeName == "metadataList")
891 {
892 currentNode = currentNode.firstChild;
893 break;
894 }
895
896 currentNode = currentNode.nextSibling;
897 }
898
899 while(currentNode != null)
900 {
901 if(currentNode.nodeName == "metadata" && currentNode.getAttribute("name") == name)
902 {
903 return currentNode.firstChild.nodeValue;
904 }
905
906 currentNode = currentNode.nextSibling;
907 }
908 return "";
909}
910
911function storeMetadata(node, listItem)
912{
913 listItem.metadata = new Array();
914
915 var currentNode = node.firstChild;
916 while(currentNode != null)
917 {
918 if(currentNode.nodeName == "metadataList")
919 {
920 currentNode = currentNode.firstChild;
921 break;
922 }
923
924 currentNode = currentNode.nextSibling;
925 }
926
927 while(currentNode != null)
928 {
929 if(currentNode.nodeName == "metadata")
930 {
931 listItem.metadata[currentNode.getAttribute("name")] = currentNode.firstChild.nodeValue;
932 }
933
934 currentNode = currentNode.nextSibling;
935 }
936}
937
938function getNodeContent(node)
939{
940 var currentNode = node.firstChild;
941 while(currentNode != null)
942 {
943 if(currentNode.nodeName == "nodeContent")
944 {
945 return currentNode.firstChild;
946 }
947
948 currentNode = currentNode.nextSibling;
949 }
950 return null;
951}
952
953function containsDocumentNode(node)
954{
955 var currentNode = node.firstChild;
956 while(currentNode != null)
957 {
958 if(currentNode.nodeName == "documentNode")
959 {
960 return true;
961 }
962
963 currentNode = currentNode.nextSibling;
964 }
965 return false;
966}
967
968function isExpanded(textDiv)
969{
970 if(!textDiv.style.display || textDiv.style.display == "block")
971 {
972 return true;
973 }
974 return false;
975}
976
977function toggleTextDiv(section)
978{
979 var textDiv = section.textDiv;
980 if(textDiv)
981 {
982 if(isExpanded(textDiv))
983 {
984 textDiv.style.display = "none";
985 section.menu.editTextLink.innerHTML = gs.text.dse.edit;
986 }
987 else
988 {
989 textDiv.style.display = "block";
990 section.menu.editTextLink.innerHTML = gs.text.dse.hide;
991 }
992 }
993}
994
995function updateFromTop()
996{
997 updateRecursive(document.getElementById("dbDiv"), null, null, 0);
998}
999
1000function insertAfter(elem, refElem)
1001{
1002 if(refElem.nextSibling)
1003 {
1004 refElem.parentNode.insertBefore(elem, refElem.nextSibling);
1005 }
1006 else
1007 {
1008 refElem.parentNode.appendChild(elem);
1009 }
1010}
1011
1012function removeFromParent(elem)
1013{
1014 elem.parentNode.removeChild(elem);
1015}
1016
1017function createSectionTitle(text)
1018{
1019 var textSpan = document.createElement("SPAN");
1020 if(text)
1021 {
1022 textSpan.appendChild(document.createTextNode(" " + text + " "));
1023 }
1024 else
1025 {
1026 textSpan.appendChild(document.createTextNode(" [" + gs.text.dse.untitled_section + "] "));
1027 }
1028 return textSpan;
1029}
1030
1031function setMouseOverAndOutFunctions(section)
1032{
1033 //Colour the list item and display the menu on mouse over
1034 section.onmouseover = function(e)
1035 {
1036 if(this.menu){this.menu.style.display = "inline";}
1037 this.style.background = "rgb(255, 200, 0)";
1038 };
1039 //Uncolour the list item and hide the menu on mouse out
1040 section.onmouseout = function(e)
1041 {
1042 if(this.menu){this.menu.style.display = "none";}
1043 this.style.background = "none";
1044 };
1045}
1046
1047function createDraggableNewSection(parent)
1048{
1049 var newSecLI = document.createElement("LI");
1050 var newSpan = document.createElement("SPAN");
1051 newSpan.innerHTML = gs.text.dse.insert_new_section + " ";
1052
1053 newSecLI.sectionTitle = newSpan;
1054 newSecLI.appendChild(newSpan);
1055 newSecLI.setAttribute("class", "dragItem newSection");
1056 newSecLI.newSection = true;
1057 newSecLI.parent = parent;
1058 newSecLI.index = -1;
1059 new YAHOO.example.DDList(newSecLI);
1060 parent.appendChild(newSecLI);
1061}
1062
1063function closeAllOpenContents()
1064{
1065 for(var i = 0; i < _allContents.length; i++)
1066 {
1067 if(isExpanded(_allContents[i].textDiv))
1068 {
1069 toggleTextDiv(_allContents[i]);
1070 }
1071 }
1072 DDM.refreshCache();
1073}
1074
1075//Status Bar class (initialised with new StatusBar(elem);)
1076function StatusBar(mainElem)
1077{
1078 var _statusMap = new Array();
1079 var _statusIDCounter = 0;
1080 var _mainElem = mainElem;
1081 var _activeMessages = 0;
1082
1083 _mainElem.style.display = "none";
1084
1085 this.addStatus = function(newStatus)
1086 {
1087 _mainElem.style.display = "block";
1088 var newStatusDiv = document.createElement("DIV");
1089 var newStatusSpan = document.createElement("SPAN");
1090
1091 var workingImage = document.createElement("IMG");
1092 workingImage.setAttribute("src", gs.imageURLs.loading);
1093 workingImage.setAttribute("height", "16px");
1094 workingImage.setAttribute("width", "16px");
1095 newStatusDiv.appendChild(workingImage);
1096
1097 newStatusDiv.appendChild(newStatusSpan);
1098 newStatusSpan.innerHTML = " " + newStatus;
1099 newStatusDiv.setAttribute("class", "statusMessage");
1100 newStatusDiv.span = newStatusSpan;
1101
1102 _mainElem.appendChild(newStatusDiv);
1103 _statusMap["status" + _statusIDCounter] = newStatusDiv;
1104 _activeMessages++;
1105 return _statusIDCounter++;
1106 }
1107
1108 this.changeStatus = function(id, newStatus)
1109 {
1110 if(_statusMap["status" + id])
1111 {
1112 _statusMap["status" + id].span.innerHTML = " " + newStatus;
1113 }
1114 }
1115
1116 this.removeStatus = function(id)
1117 {
1118 if(_statusMap["status" + id])
1119 {
1120 removeFromParent(_statusMap["status" + id]);
1121
1122 if(--_activeMessages == 0)
1123 {
1124 _mainElem.style.display = "none";
1125 }
1126 }
1127 }
1128
1129 this.clear = function()
1130 {
1131 for(var p in _statusMap)
1132 {
1133 if(_statusMap.hasOwnProperty(p))
1134 {
1135 if(_statusMap[p] && _statusMap[p].parentNode)
1136 {
1137 removeFromParent(_statusMap[p]);
1138 }
1139
1140 if(--_activeMessages == 0)
1141 {
1142 _mainElem.style.display = "none";
1143 }
1144 }
1145 }
1146 }
1147}
1148
1149/*
1150function toggleEdit(e)
1151{
1152 var mousePos = de.events.getXYInWindowFromEvent(e);
1153 var cDesc = de.cursor.getCursorDescAtXY(mousePos.x, mousePos.y, de.events.getEventTarget(e));
1154 de.cursor.setCursor(cDesc);
1155}
1156*/
Note: See TracBrowser for help on using the repository browser.