//Some "constants" to match the server constants var SUCCESS = 1; var ACCEPTED = 2; var ERROR = 3; var CONTINUING = 10; var COMPLETED = 11; var HALTED = 12; function encodeDelimiters(meta_value) { var new_value = meta_value.replace(/;/g, "%253B"); return new_value.replace(/&/g, "%2526"); } function getElementsByClassName(cl, parent) { var elemArray = []; var classRegEx = new RegExp("\\b" + cl + "\\b"); var elems; if(parent) { elems = parent.getElementsByTagName("*"); } else { elems = document.getElementsByTagName("*"); } for (var i = 0; i < elems.length; i++) { var classeName = elems[i].className; if (classRegEx.test(classeName)) elemArray.push(elems[i]); } return elemArray; }; function getNextSiblingOfType(elem, type) { if(elem == null) { return null; } var current = elem.nextSibling; while(current != null) { if(current.nodeName.toLowerCase() == type) { return current; } current = current.nextSibling; } return null; } function getPrevSiblingOfType(elem, type) { if(elem == null) { return null; } var current = elem.previousSibling; while(current != null) { if(current.nodeName.toLowerCase() == type) { return current; } current = current.previousSibling; } return null; } function saveTransaction(transaction) { console.log(transaction); _transactions.push(transaction); } function undo() { if(_undoOperations.length == 0) { return; } var undoOp = _undoOperations.pop(); //Create/Duplicate undo if(undoOp.op == "del") { if(undoOp.srcElem.childList) { removeFromParent(undoOp.srcElem.childList); } if(undoOp.srcElem.parentItem) { undoOp.srcElem.parentItem.menu.newSectionLink.style.display = "inline"; undoOp.srcElem.parentItem.childList = null; } removeFromParent(undoOp.srcElem); } if(undoOp.op == "delMeta") { if(undoOp.srcElem.childList) { removeFromParent(undoOp.srcElem.childList); } if(undoOp.srcElem.parentItem) { undoOp.srcElem.parentItem.menu.newSectionLink.style.display = "inline"; undoOp.srcElem.parentItem.childList = null; } de.doc.unregisterEditSection(undoOp.srcElem); removeFromParent(undoOp.srcElem); } //Move undo (mva is move after, mvb is move before, mvi is move into) else if(undoOp.op == "mva" || undoOp.op == "mvb" || undoOp.op == "mvi") { if(undoOp.op == "mvb") { undoOp.refElem.parentNode.insertBefore(undoOp.srcElem, undoOp.refElem); } else if(undoOp.op == "mva") { insertAfter(undoOp.srcElem, undoOp.refElem); } else { undoOp.refElem.removeChild(undoOp.refElem.firstChild); undoOp.refElem.appendChild(undoOp.srcElem); } if(undoOp.srcElem.textDiv) { insertAfter(undoOp.srcElem.textDiv, undoOp.srcElem); } if(undoOp.srcElem.childList) { insertAfter(undoOp.srcElem.childList, undoOp.srcElem.textDiv); } if(undoOp.srcElem.onmouseout) { //Uncolour the section if it coloured undoOp.srcElem.onmouseout(); } updateFromTop(); } else if(undoOp.op == "display") { undoOp.srcElem.style.display = undoOp.subOp; } if(undoOp.removeDeletedMetadata) { _deletedMetadata.pop(); } if(undoOp.removeTransaction) { _transactions.pop(); } } function enableSaveButtons(enabled) { if (enabled) { $("#saveButton, #quickSaveButton").html(gs.text.de.save_changes); $("#saveButton, #quickSaveButton").removeAttr("disabled"); } else { $("#saveButton, #quickSaveButton").html(gs.text.de.saving + "..."); $("#saveButton, #quickSaveButton").attr("disabled", "disabled"); } } function addCollectionToBuild(collection) { for(var i = 0; i < _collectionsToBuild.length; i++) { if(collection == _collectionsToBuild[i]) { return; } } _collectionsToBuild.push(collection); } function save() { saveAndRebuild(false); } function rebuildCurrentCollection() { console.log(gs.text.de.rebuilding_collection); enableSaveButtons(false); var collection = gs.cgiParams.c; var collectionsArray = new Array(); collectionsArray.push(collection); buildCollections(collectionsArray); } function saveAndRebuild(rebuild) { //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 var collection; if(gs.cgiParams.c && gs.cgiParams.c != "") { collection = gs.cgiParams.c; } else { collection = gs.cgiParams.p_c; } var sendBuildRequest = function() { var request = "["; for(var i = 0; i < _transactions.length; i++) { request += _transactions[i]; if(i != _transactions.length - 1) { request += ","; } } request += "]"; var statusID; var ajax = new gs.functions.ajaxRequest(); ajax.open("POST", gs.xsltParams.library_name, true); ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); ajax.onreadystatechange = function() { if(ajax.readyState == 4 && ajax.status == 200) { var text = ajax.responseText; var xml = validateXML(text); var errorElems; if(!xml || checkForErrors(xml)) { alert(gs.text.dse.error_saving); enableSaveButtons(true); if(_statusBar) { _statusBar.removeStatus(statusID); } return; } if(_statusBar) { _statusBar.removeStatus(statusID); } if (rebuild) { buildCollections(_collectionsToBuild); } else { // reset the save button here enableSaveButtons(true); } } } if(_collectionsToBuild.length > 0) { enableSaveButtons(false); if(_statusBar) { statusID = _statusBar.addStatus(gs.text.dse.modifying_archives + "..."); } ajax.send("a=g&rt=r&s=DocumentExecuteTransaction&s1.transactions=" + request); } } // end sendBuildRequest definition var metadataChanges = new Array(); if (_deletedMetadata.length > 0) { addCollectionToBuild(collection); for(var i = 0; i < _deletedMetadata.length; i++) { var currentRow = _deletedMetadata[i]; //Get document ID var currentElem = currentRow; while((currentElem = currentElem.parentNode).tagName != "TABLE"); var docID = currentElem.getAttribute("id").substring(4); //Get metadata name var cells = currentRow.getElementsByTagName("TD"); var nameCell = cells[0]; var name = nameCell.innerHTML; var valueCell = cells[1]; var value = valueCell.getElementsByTagName("TEXTAREA")[0].value; metadataChanges.push({type:'delete', docID:docID, name:name, value:value}); removeFromParent(currentRow); } } var changes = changesToUpdate(); //Clean changes editableInitStates = editableLastStates; for(var i = 0; i < changes.length; i++) { var changedElem = changes[i]; //Save metadata if(gs.functions.hasClass(changedElem, "metaTableCellArea")) { //Get document ID var currentElem = changedElem; while((currentElem = currentElem.parentNode).tagName != "TABLE"); var docID = currentElem.getAttribute("id").substring(4); //Get metadata name var row = changedElem.parentNode.parentNode; var cells = row.getElementsByTagName("TD"); var nameCell = cells[0]; var name = nameCell.innerHTML; var value = changedElem.value; value = value.replace(/ /g, " "); var orig = changedElem.originalValue; if (orig) { orig = orig.replace(/ /g, " "); } metadataChanges.push({collection:collection, docID:docID, name:name, value:value, orig:orig}); changedElem.originalValue = changedElem.value; addCollectionToBuild(collection); } //Save content else if(gs.functions.hasClass(changedElem, "renderedText")) { var section = changedElem.parentDiv.parentItem; saveTransaction('{"operation":"setText", "text":"' + CKEDITOR.instances[changedElem.getAttribute("id")].getData().replace(/%/g, "%25").replace(/"/g, "\\\"").replace(/&/g, "%26") + '", "collection":"' + section.collection + '", "oid":"' + section.nodeID + '"}'); //' addCollectionToBuild(section.collection); } else if(gs.functions.hasClass(changedElem, "sectionText")) { var id = changedElem.getAttribute("id"); var sectionID = id.substring(4); saveTransaction('{"operation":"setText", "text":"' + CKEDITOR.instances[changedElem.getAttribute("id")].getData().replace(/%/g, "%25").replace(/"/g, "\\\"").replace(/&/g, "%26") + '", "collection":"' + gs.cgiParams.c + '", "oid":"' + sectionID + '"}'); //' addCollectionToBuild(gs.cgiParams.c); } } var processChangesLoop = function(index) { var change = metadataChanges[index]; var callbackFunction; if(index + 1 == metadataChanges.length) { callbackFunction = sendBuildRequest; } else { callbackFunction = function(){processChangesLoop(index + 1)}; } if (change.type == "delete") { gs.functions.removeArchivesMetadata(collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), function(){callbackFunction();}); } else { if(change.orig) { gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), encodeDelimiters(change.orig), "override", function(){callbackFunction();}); } else { gs.functions.setArchivesMetadata(change.collection, gs.xsltParams.site_name, change.docID, change.name, null, encodeDelimiters(change.value), null, "accumulate", function(){callbackFunction();}); } } } if (metadataChanges.length>0) { // this will process each change one by one, and then send the build request processChangesLoop(0); } else if(_collectionsToBuild.length > 0) { // if there are no metadata changes, but some other changes eg text have happened, then we need to send the build request. sendBuildRequest(); } /* need to clear the changes from the page so that we don't process them again next time */ while (_deletedMetadata.length>0) { _deletedMetadata.pop(); } } function buildCollections(collections, documents, callback) { if(!collections || collections.length == 0) { console.log(gs.text.dse.empty_collection_list); enableSaveButtons(true); return; } var docs = ""; var buildOperation = ""; if(documents) { buildOperation = "ImportCollection"; docs += "&s1.documents="; for(var i = 0; i < documents.length; i++) { docs += documents[i]; if(i < documents.length - 1) { docs += ","; } } } else { buildOperation = "BuildAndActivateCollection"; } var counter = 0; var statusID = 0; var buildFunction = function() { var ajax = new gs.functions.ajaxRequest(); ajax.open("GET", gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=" + buildOperation + "&s1.incremental=true&s1.collection=" + collections[counter] + docs); ajax.onreadystatechange = function() { if(ajax.readyState == 4 && ajax.status == 200) { var text = ajax.responseText; var xml = validateXML(text); if(!xml || checkForErrors(xml)) { alert(gs.text.dse.could_not_build_p1 + " " + collections[counter] + gs.text.dse.could_not_build_p2); if(_statusBar) { _statusBar.removeStatus(statusID); } enableSaveButtons(true); return; } var status = xml.getElementsByTagName("status")[0]; var pid = status.getAttribute("pid"); startCheckLoop(pid, buildOperation, statusID, function() { /* var localAjax = new gs.functions.ajaxRequest(); localAjax.open("GET", gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ActivateCollection&s1.collection=" + collections[counter], true); localAjax.onreadystatechange = function() { if(localAjax.readyState == 4 && localAjax.status == 200) { var localText = localAjax.responseText; var localXML = validateXML(localText); if(!xml || checkForErrors(xml)) { alert(gs.text.dse.could_not_activate_p1 + " " + collections[counter] + gs.text.dse.could_not_activate_p2); if(_statusBar) { _statusBar.removeStatus(statusID); } enableSaveButtons(true); return; } var localStatus = localXML.getElementsByTagName("status")[0]; if(localStatus) { var localPID = localStatus.getAttribute("pid"); startCheckLoop(localPID, "ActivateCollection", statusID, function() { */ if(counter == collections.length - 1) { removeCollectionsFromBuildList(collections); if(callback) { callback(); } } else { counter++; buildFunction(); } _transactions = new Array(); if(_statusBar) { _statusBar.removeStatus(statusID); } enableSaveButtons(true); /* }); } } } if(_statusBar) { _statusBar.changeStatus(statusID, gs.text.dse.activating + " " + collections[counter] + "..."); } localAjax.send(); */ }); } } if(_statusBar) { statusID = _statusBar.addStatus(gs.text.dse.building + " " + collections[counter] + "..."); } ajax.send(); } buildFunction(); } function startCheckLoop(pid, serverFunction, statusID, callbackFunction) { var ajaxFunction = function() { var ajax = new gs.functions.ajaxRequest(); ajax.open("GET", gs.xsltParams.library_name + "?a=g&rt=s&ro=1&s=" + serverFunction + "&s1.pid=" + pid, true); ajax.onreadystatechange = function() { if(ajax.readyState == 4 && ajax.status == 200) { var text = ajax.responseText; var xml = validateXML(text); if(!xml || checkForErrors(xml)) { alert(gs.text.dse.could_not_check_status_p1 + " " + serverFunction + gs.text.dse.could_not_check_status_p2a); if(_statusBar) { _statusBar.removeStatus(statusID); } enableSaveButtons(true); return; } var status = xml.getElementsByTagName("status")[0]; var code = status.getAttribute("code"); if (code == COMPLETED || code == SUCCESS) { callbackFunction(); } else if (code == HALTED || code == ERROR) { alert(gs.text.dse.could_not_check_status_p1 + " " + serverFunction + gs.text.dse.could_not_check_status_p2b); if(_statusBar) { _statusBar.removeStatus(statusID); } enableSaveButtons(true); } else { setTimeout(ajaxFunction, 1000); } } } ajax.send(); } ajaxFunction(); } function removeCollectionsFromBuildList(collections) { var tempArray = new Array(); for(var i = 0; i < _collectionsToBuild.length; i++) { var found = false; for(var j = 0; j < collections.length; j++) { if(collections[j] == _collectionsToBuild[i]) { found = true; break; } } if(!found) { tempArray.push(_collectionsToBuild[i]); } } _collectionsToBuild = tempArray; } function checkForErrors(xml) { var errorElems = xml.getElementsByTagName("error"); if(errorElems && errorElems.length > 0) { var errorString = gs.text.dse.error_saving_changes + ": "; for(var i = 0; i < errorElems.length; i++) { errorString += " " + errorElems.item(i).firstChild.nodeValue; } alert(errorString); return true; } return false; //No errors } function validateXML(txt) { // code for IE if (window.ActiveXObject) { var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = "false"; xmlDoc.loadXML(document.all(txt).value); if(xmlDoc.parseError.errorCode!=0) { txt = dse.error_code + ": " + xmlDoc.parseError.errorCode + "\n"; txt = txt + dse.error_reason + ": " + xmlDoc.parseError.reason; txt = txt + dse.error_line + ": " + xmlDoc.parseError.line; console.log(txt); return null; } return xmlDoc; } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation.createDocument) { var parser = new DOMParser(); var xmlDoc = parser.parseFromString(txt,"text/xml"); if (xmlDoc.getElementsByTagName("parsererror").length > 0) { console.log(gs.text.dse.xml_error); return null; } return xmlDoc; } else { console.log(gs.text.dse.browse_cannot_validate_xml); } return null; } function onVisibleMetadataSetChange() { var metadataList = document.getElementById("metadataSetList"); var index = metadataList.selectedIndex; var options = metadataList.getElementsByTagName("OPTION"); var selectedOption = options[index]; var selectedSet = selectedOption.value; changeVisibleMetadata(selectedSet); } function changeVisibleMetadata(metadataSetName) { var metaSetList = metadataSetName.split(","); var tables = document.getElementsByTagName("TABLE"); for(var i = 0; i < tables.length; i++) { var id = tables[i].getAttribute("id"); if(id && id.search(/^meta/) != -1) { var rows = tables[i].getElementsByTagName("TR"); for(var j = 0; j < rows.length; j++) { if(metadataSetName == "All") { rows[j].style.display = "table-row"; } else { var cells = rows[j].getElementsByTagName("TD"); var cellName = cells[0].innerHTML; if(cellName.indexOf(".") == -1) { rows[j].style.display = "none"; } else { var setName = cellName.substring(0, cellName.lastIndexOf(".")); if (metaSetList.indexOf(setName)!= -1) { rows[j].style.display = "table-row"; } else { rows[j].style.display = "none"; } } } } } } } function asyncRegisterEditSection(cell) { //This registering can cause a sizeable delay so we'll thread it (effectively) so the browser is not paused cell.originalValue = cell.value; setTimeout(function(){addEditableState(cell, editableInitStates)}, 0); } function addOptionToList(list, optionvalue, optiontext, selected) { var newOption = $("