source: main/trunk/greenstone3/web/interfaces/default/js/documentedit_scripts_util.js@ 31909

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

implementing 'add all metadata' button. only add if enable_add_all_metadata_button is true, and if our metadata selector is a fixed list or autocomplete. will add a new row to the table for each metadata field in hte list. TODO, don't add if they are already in the list??

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