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

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

to allow metadata values to contain & and ; when doing online editing, we need to doubly encode them. unencoded & will cause an error later when doing unXMlEncode on the string, and ; is used as a delimiter by perl CGI, so the value gets split at that point if it is not encoded

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