source: main/trunk/greenstone3/web/interfaces/default_new/js/documentmaker_scripts_util.js@ 29852

Last change on this file since 29852 was 29852, checked in by Georgiy Litvinov, 9 years ago

Ckeditor integration commit

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