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

Last change on this file since 31055 was 31055, checked in by kjdon, 7 years ago

newRow is a jquery object which is not the same as the DOM element. the remove link code is expecting dom elements. get(0) apparently returns the underlying DOM element, so we can pass the tr to the function rather than the object

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