diff --git a/citesphere/src/main/webapp/WEB-INF/views/auth/authorities/create.html b/citesphere/src/main/webapp/WEB-INF/views/auth/authorities/create.html index c1162764f..9fefd6a7c 100644 --- a/citesphere/src/main/webapp/WEB-INF/views/auth/authorities/create.html +++ b/citesphere/src/main/webapp/WEB-INF/views/auth/authorities/create.html @@ -2,368 +2,23 @@ + diff --git a/citesphere/src/main/webapp/WEB-INF/views/auth/group/editItem.html b/citesphere/src/main/webapp/WEB-INF/views/auth/group/editItem.html index a2fd307c0..d72ea5c73 100644 --- a/citesphere/src/main/webapp/WEB-INF/views/auth/group/editItem.html +++ b/citesphere/src/main/webapp/WEB-INF/views/auth/group/editItem.html @@ -1,6 +1,7 @@ + + +
-

+

[[${author.lastName}]], [[${author.firstName}]] ; @@ -1351,6 +72,7 @@

[[(${citation.dateFreetext})]]

+

New Item

-
- + - + - - + + @@ -1848,10 +570,6 @@
  • Authorities from VIAF -
  • -
  • - Authorities from IsisCB
  • @@ -1914,19 +632,6 @@
    Collection
    Item Key[[${citation.key}]]

    - -
    - - -
    - - - - -
    NameURIDescription
    -
    diff --git a/citesphere/src/main/webapp/resources/authorities/authorities.legacy.js b/citesphere/src/main/webapp/resources/authorities/authorities.legacy.js new file mode 100644 index 000000000..46bd7ec3a --- /dev/null +++ b/citesphere/src/main/webapp/resources/authorities/authorities.legacy.js @@ -0,0 +1,963 @@ + + +var affCount = 0; +var affiliationTemplate = $($.parseHTML('
    ' + + '
    ' + + '
    ' + + '' + + '' + + '' + + '
    ' + + '' + + '
    ' + + '' + + '' + + '
    ' + + '
    ') +); + +$(document).ready(function() { + + $("#uriLoadingSpinnerAuthor").hide(); + $("#uriLoadingFailureAuthor").hide(); + $("#uriLoadingFoundAuthor").hide(); + $("#searchAuthorSpinner").hide(); + + $("#uriLoadingFoundAuthor").popover(); + $("#uriLoadingFailureAuthor").popover(); + + $("#uriLoadingSpinnerEditor").hide(); + $("#uriLoadingFailureEditor").hide(); + $("#uriLoadingFoundEditor").hide(); + $("#searchEditorSpinner").hide(); + + $("#uriLoadingFoundEditor").popover(); + $("#uriLoadingFailureEditor").popover(); + + $("#uriLoadingSpinnerCreator").hide(); + $("#uriLoadingFailureCreator").hide(); + $("#uriLoadingFoundCreator").hide(); + $("#searchCreatorSpinner").hide(); + + $("#uriLoadingFoundCreator").popover(); + $("#uriLoadingFailureCreator").popover(); + + $("#submitForm").click(function(e) { + constructPersonArray("author", "author", 0); + constructPersonArray("editor", "editor", 0); + var creatorSubmitCount = 0; + $(".creator-row").each(function(idx, elem){ + var ele = $(elem).children().first(); + if(!(ele.attr("id")=="editor" || ele.attr("id")=="author")) { + var roleCount = constructPersonArray("creator", ele.attr("id"), creatorSubmitCount); + creatorSubmitCount = creatorSubmitCount + roleCount; + } + }); + + createConceptTags(); + }); + + /* Handle Author events */ + $("#addAuthorButton").click(function() { + savePersonDetails("Author", "Author"); + }); + + $("#addAuthorModalCancel").click(function() { + $("#authorModal").modal('hide'); + resetPersonCreationModal("Author"); + }); + + /* Disable search authority button when first name and last name fields are empty*/ + $("#firstNameAuthor").keyup(function(e){ + allowSearchAndAdd("Author"); + }); + + $("#lastNameAuthor").keyup(function(e){ + allowSearchAndAdd("Author"); + }); + + $("#firstNameEditor").keyup(function(e){ + allowSearchAndAdd("Editor"); + }); + + $("#lastNameEditor").keyup(function(e){ + allowSearchAndAdd("Editor"); + }); + + $("#firstNameCreator").keyup(function(e){ + allowSearchAndAdd("Creator"); + }); + + $("#lastNameCreator").keyup(function(e){ + allowSearchAndAdd("Creator"); + }); + + $(document).on("click", ".remove-aff", function() { + $(this).closest('.aff-entry').remove(); + }); + + $("#searchAuthor").click(function() { + searchAuthorities('Author', 'Author'); + }); + + $("#searchEditor").click(function() { + searchAuthorities('Editor', 'Editor'); + }); + + $("#searchCreator").click(function() { + searchAuthorities('Creator', 'Creator'); + }); + + $(".edit-author").click(function(){ + var authorItem = $(this).parent(); + editPerson('Author', authorItem[0]); + }); + + $(".edit-author").css('cursor', 'pointer'); + + $(".remove-author").click(removePerson); + $(".remove-author").css('cursor', 'pointer'); + $(".remove-concept").click(removeConcept); + $(".remove-concept").css('cursor', 'pointer'); + + $("#addAuthorAffiliation").click(function() { + addAffiliation("Author", affCount + 1); + }); + + $("#addEditorAffiliation").click(function() { + addAffiliation("Editor", affCount + 1); + }); + + $("#addCreatorAffiliation").click(function() { + addAffiliation("Creator", affCount + 1); + }); + + $("#authorIconContainer").on('click', ".popover #authorCreateAuthority", function() { + var uri = $("#uriLoadingFoundAuthor").attr('data-authority-uri'); + importAuthorityURL = "/auth/authority/import?uri="+uri; + $.ajax({ + dataType: "json", + type: 'POST', + url: importAuthorityURL, + data: {csrfParameterName: csrfToken}, + async: false, + success: function(data) { + $("#authorCreateAuthority").hide(); + $("#uriAuthorLocalId").val(data['id']); + $("#authorAuthorityUsed").html("Created new authority entry " + data['name'] + "."); + $("#authorAuthorityCreationFeedback").html('
    Authority entry has been created!
    '); + showPersonNameInModal(data['name'], "Author"); + $("#uriLoadingFoundAuthor").popover('hide'); + $("#addAuthorButton").prop("disabled", false); + } + }); + }); + + $("#authorIconContainer").on('click', ".popover .foundAuthorities li a", function(event) { + var authId = $(this).attr('data-authority-id'); + $("#uriAuthorLocalId").val(authId); + $("#authorAuthorityUsed").html("Using stored authority entry " + $(this).attr('data-authority-name') + "."); + showPersonNameInModal($(this).attr('data-authority-name'), "Author"); + $("#uriLoadingFoundAuthor").popover('hide'); + $("#addAuthorButton").prop("disabled", false); + event.preventDefault(); + }); + + var timer = null; + $("#uriAuthor").change(function() { + resetPersonAuthorityCreation("Author"); + $("#uriLoadingSpinnerAuthor").show(); + var uri = $("#uriAuthor").val(); + clearTimeout(timer); + timer = setTimeout(function() { + getPersonAuthority(uri, "Author"); + }, 1000); + }); + + $("#closeAuthoritySearchResult").click(function() { + $("#selectAuthorityModel").modal('hide'); + $('#selectAuthorityModel a:first').tab('show'); + }); + + /* Handle editor events */ + $(".edit-editor").click(function(){ + var editorItem = $(this).parent(); + editPerson('Editor', editorItem[0]); + }); + + $(".edit-editor").css('cursor', 'pointer'); + + $("#addEditorButton").click(function() { + savePersonDetails('Editor', 'Editor'); + }); + + $("#addEditorModalCancel").click(function() { + $("#editorModal").modal('hide'); + resetPersonCreationModal("Editor"); + }); + + $(".remove-editor").click(removePerson); + $(".remove-editor").css('cursor', 'pointer'); + + $("#uriEditor").change(function() { + resetPersonAuthorityCreation("Editor"); + $("#uriLoadingSpinnerEditor").show(); + var uri = $("#uriEditor").val(); + clearTimeout(timer); + timer = setTimeout(function() { + getPersonAuthority(uri, "Editor"); + }, 1000); + }); + + $("#editorIconContainer").on('click', ".popover #editorCreateAuthority", function() { + var uri = $("#uriLoadingFoundEditor").attr('data-authority-uri'); + importAuthorityURL = "/auth/authority/import?uri="+uri; + $.ajax({ + dataType: "json", + type: 'POST', + url: importAuthorityURL, + data: {csrfParameterName:csrfToken}, + async: false, + success: function(data) { + $("#editorCreateAuthority").hide(); + $("#uriEditorLocalId").val(data['id']); + $("#editorAuthorityUsed").html("Created new authority entry " + data['name'] + "."); + $("#editorAuthorityCreationFeedback").html('
    Authority entry has been created!
    '); + showPersonNameInModal(data['name'], "Editor"); + $("#uriLoadingFoundEditor").popover('hide'); + $("#addEditorButton").prop("disabled", false); + } + }); + }); + + $("#editorIconContainer").on('click', ".popover .foundAuthorities li a", function(event) { + var authId = $(this).attr('data-authority-id'); + $("#uriEditorLocalId").val(authId); + $("#editorAuthorityUsed").html("Using stored authority entry " + $(this).attr('data-authority-name') + "."); + showPersonNameInModal($(this).attr('data-authority-name'), "Editor"); + $("#uriLoadingFoundEditor").popover('hide'); + $("#addEditorButton").prop("disabled", false); + event.preventDefault(); + }); + + /* Handle Other Creators events */ + $("#addCreatorButton").click(function(e) { + var target = $(e.target); + if(target.attr("data-creator-type") != null) { + savePersonDetails(target.attr("data-creator-type"), "Creator"); + } else { + savePersonDetails("", "Creator"); + } + }); + + $(".creatorModalLink").click(function(e) { + var target = $(e.target); + var creatorType = target.attr("data-creator-type").charAt(0).toUpperCase() + target.attr("data-creator-type").slice(1); + $("#creatorLabel").text("Enter "+creatorType+" Information"); + $("#addCreatorButton").text("Add "+creatorType); + $("#addCreatorButton").attr("data-creator-type", target.attr("data-creator-type")); + }); + + $("#addCreatorModalCancel").click(function() { + $("#creatorModal").modal('hide'); + resetPersonCreationModal("Creator"); + }); + + $(".edit-creator").click(function(){ + var creatorItem = $(this).parent(); + editPerson('Creator', creatorItem[0]); + }); + + $(".edit-creator").css('cursor', 'pointer'); + + $(".remove-creator").click(removePerson); + $(".remove-creator").css('cursor', 'pointer'); + + $("#uriCreator").change(function() { + resetPersonAuthorityCreation("Creator"); + $("#uriLoadingSpinnerCreator").show(); + var uri = $("#uriCreator").val(); + clearTimeout(timer); + timer = setTimeout(function() { + getPersonAuthority(uri, "Creator"); + }, 1000); + }); + + $("#creatorIconContainer").on('click', ".popover #creatorCreateAuthority", function() { + var uri = $("#uriLoadingFoundCreator").attr('data-authority-uri'); + importAuthorityURL = "/auth/authority/import?uri="+uri; + $.ajax({ + dataType: "json", + type: 'POST', + url: importAuthorityURL, + data: {csrfParameterName:csrfToken}, + async: false, + success: function(data) { + $("#creatorCreateAuthority").hide(); + $("#uriCreatorLocalId").val(data['id']); + $("#creatorAuthorityUsed").html("Created new authority entry " + data['name'] + "."); + $("#creatorAuthorityCreationFeedback").html('
    Authority entry has been created!
    '); + showPersonNameInModal(data['name'], "Creator"); + $("#uriLoadingFoundCreator").popover('hide'); + $("#addCreatorButton").prop("disabled", false); + } + }); + }); + + $("#creatorIconContainer").on('click', ".popover .foundAuthorities li a", function(event) { + var authId = $(this).attr('data-authority-id'); + $("#uriCreatorLocalId").val(authId); + $("#creatorAuthorityUsed").html("Using stored authority entry " + $(this).attr('data-authority-name') + "."); + showPersonNameInModal($(this).attr('data-authority-name'), "Creator"); + $("#uriLoadingFoundCreator").popover('hide'); + $("#addCreatorButton").prop("disabled", false); + event.preventDefault(); + }); + + /* adding concepts */ + $("#addConceptButton").click(function(e) { + e.preventDefault(); + + var conceptId = $("#addConceptConceptSelect"); + var conceptType = $("#addConceptTypeSelect"); + + var conceptSpan = $(''); + conceptSpan.attr("data-concept-uri", conceptId.val()); + conceptSpan.attr("data-type-uri", conceptType.val()); + + var text = $("#addConceptConceptSelect option:selected").text(); + var typeName = $("#addConceptTypeSelect option:selected").text(); + conceptSpan.text(text + " | " + typeName + " "); + var deleteIcon = $(''); + deleteIcon.click(removeConcept); + conceptSpan.append(deleteIcon); + $("#conceptTags").append(conceptSpan); + + $("#addConceptModal").modal('hide'); + }); +}); + + +/*Clones the affiliation template, modifies it appropriately and appends it to the given modal type*/ +function addAffiliation(modalType, counter) { + modalTypeL = modalType.toLowerCase(); + var affiliationCopy = affiliationTemplate.clone(); + affiliationCopy.attr('id', modalTypeL+'AffiliationTemplate'+counter); + affiliationCopy.addClass('aff-info'); + + affiliationCopy.find('label[for="affiliation"]').attr('for', 'firstName'+modalType+'Affiliation'+counter); + affiliationCopy.find('label[for="uri"]').attr('for', 'uri'+modalType+'Affiliation'+counter); + + var affInput = affiliationCopy.find('#firstName'); + affInput.addClass('firstName'+modalType+'Affiliation'); + affInput.attr('id', 'firstName'+modalType+'Affiliation'+counter); + affInput.attr('data-client-id', affInput.attr('id')); + + var searchButton = affiliationCopy.find('button'); + searchButton.attr('id', 'search'+modalType+'Affiliation'+counter); + searchButton.attr('data-client-id', searchButton.attr('id')); + searchButton.find('#searchSpinner').attr('id', 'search'+modalType+'Affiliation'+counter+'Spinner'); + searchButton.prop('disabled', true); + + var affUri = affiliationCopy.find('.affUri'); + affUri.attr('id', 'uri'+modalType+'Affiliation'+counter); + affUri.attr('data-client-id', affUri.attr('id')); + + $("#"+modalTypeL+"Affiliations").append(affiliationCopy); + + var affiliationCounter = modalType + 'Affiliation' + counter; + $('#firstName'+affiliationCounter).keyup(function(e) { + allowSearchAndAdd(affiliationCounter); + }); + + $("#search"+affiliationCounter).click(function() { + searchAuthorities(affiliationCounter, affiliationCounter); + }); + + affCount++; +} + +function searchAuthorities(modalType, personType) { + $("#selectAuthorityModel").on('hidden.bs.modal', function(e) { + $('#selectAuthorityModel a:first').tab('show'); + }) + $("#search"+modalType+"Spinner").show(); + $('#userAuthority-pagination-top').twbsPagination('destroy'); + $('#groupAuthority-pagination-top').twbsPagination('destroy'); + $('#conceptpowerAuthority-pagination-top').twbsPagination('destroy'); + $('#viafAuthority-pagination-top').twbsPagination('destroy'); + + getAuthorities(modalType, personType, 0, "user") + getAuthorities(modalType, personType, 0, "group") + getAuthorities(modalType, personType, 0, "viaf") + getAuthorities(modalType, personType, 0, "conceptpower") + $("#search"+modalType+"Spinner").hide(); +} + +function allowSearchAndAdd(element){ + if($("#firstName"+element).val() == "" && $("#lastName"+element).val()==""){ + $("#search"+element).prop("disabled", true); + $("#add"+element+"Button").prop("disabled", true); + } + else{ + $("#search"+element).prop("disabled", false); + $("#add"+element+"Button").prop("disabled", false); + } +} + +function showAffiliationNameInModal(name, uri, personType){ + $("#firstName"+personType).val(name); + $("#uri"+personType).val(uri); +} + +/* Function to populate name in modal fetched from uri */ +function showPersonNameInModal(name, personType){ + var personName = name; + + /* Name containing brackets + example: Dempsey, Hugh A. (Hugh Aylmer), 1929- */ + if(name.includes("(")) { + personName = name.substring(0, name.indexOf("(")); + } + + /* Name containing title/year + example: Iqbāl, Muḥammad, Sir, 1877-1938 */ + if(personName.split(",").length > 2) { + personName = personName.substring(0, personName.indexOf(',', personName.indexOf(",")+1)); + } + + /* Name containing span + example: Dempsey, Patrick, 1966- */ + if(personName.includes("-")) { + personName = personName.trim(); + personName = personName.substring(0, personName.lastIndexOf(' ')); + } + + /* Name separated by comma + example: Dempsey, Paul Stephen */ + if(personName.indexOf(",") != -1) { + $("#firstName"+personType).val(personName.substring(personName.indexOf(',')+1).trim()); + $("#lastName"+personType).val(personName.substring(0, personName.lastIndexOf(', '))); + } else { + $("#lastName"+personType).val(personName.substring(personName.lastIndexOf(' ')+1).trim()); + $("#firstName"+personType).val(personName.substring(0, personName.lastIndexOf(' '))); + } +} + +/* Function to populate modal on edit */ +function editPerson(modalName, item){ + var personItem = $(item); + var modalNameLCase = modalName.toLowerCase(); + $("#firstName"+modalName).val(personItem.attr("data-"+modalNameLCase+"-firstname")); + $("#lastName"+modalName).val(personItem.attr("data-"+modalNameLCase+"-lastname")); + $("#uri"+modalName).val(personItem.attr("data-"+modalNameLCase+"-uri")); + $("#id"+modalName).attr("data-"+modalNameLCase+"-id", personItem.attr("id")); + + var counter = 0; + personItem.children(".affiliation-class").each(function(idx, elem) { + var affiliationCopy = affiliationTemplate.clone(); + affiliationCopy.attr('id', modalNameLCase+'AffiliationTemplate'+counter); + affiliationCopy.addClass('aff-info'); + + affiliationCopy.find('label[for="affiliation"]').attr('for', 'firstName'+modalName+'Affiliation'+counter); + affiliationCopy.find('label[for="uri"]').attr('for', 'uri'+modalName+'Affiliation'+counter); + + var affInput = affiliationCopy.find('#firstName'); + affInput.addClass('firstName'+modalName+'Affiliation'); + affInput.attr('id', 'firstName'+modalName+'Affiliation'+counter); + affInput.attr('data-client-id', affInput.attr('id')); + affInput.attr("data-affiliation-name", $(elem).data("affiliationName")); + affInput.attr("data-affiliation-id", $(elem).data("affiliationId")); + affInput.val($(elem).data("affiliationName")); + + var searchButton = affiliationCopy.find('button'); + searchButton.attr('id', 'search'+modalName+'Affiliation'+counter); + searchButton.attr('data-client-id', searchButton.attr('id')); + searchButton.find('#searchSpinner').attr('id', 'search'+modalName+'Affiliation'+counter+'Spinner'); + searchButton.prop('disabled', false); + + var affUri = affiliationCopy.find('.affUri'); + affUri.attr('id', 'uri'+modalName+'Affiliation'+counter); + affUri.attr('data-client-id', affUri.attr('id')); + affUri.attr("data-affiliation-uri", $(elem).data("affiliationUri")); + affUri.val($(elem).data("affiliationUri")); + + $("#"+modalNameLCase+"Affiliations").append(affiliationCopy); + + var affiliationCounter = modalName + 'Affiliation' + counter; + $('#firstName'+affiliationCounter).keyup(function(e) { + allowSearchAndAdd(affiliationCounter); + }); + + $("#search"+affiliationCounter).click(function() { + searchAuthorities(affiliationCounter, affiliationCounter); + }); + + counter++; + }); + /*Need to fast forward the affiliation counter which is used while adding a new affiliation entry*/ + affCount = counter; + $("#addCreatorButton").attr("data-"+modalNameLCase+"-type", personItem.attr("data-"+modalNameLCase+"type")); + $("#add"+modalName+"Button").text("Update "+modalName); + $("#add"+modalName+"Button").prop("disabled", false); + + $("#"+modalNameLCase+"Modal").modal('show'); + if($("#firstName"+modalName).val() != "" || $("#lastName"+modalName).val() != ""){ + $("#search"+modalName).prop("disabled", false); + } +} + +/* Function to save information on closing modal */ +function savePersonDetails(personType, modalName){ + var modalNameLCase = modalName.toLowerCase(); + var personSpan; + var personTypeLCase = personType.toLowerCase(); + if($("#id"+modalName).attr("data-"+modalNameLCase+"-id") != null && $("#id"+modalName).attr("data-"+modalNameLCase+"-id").length > 0) { + personSpan = $('#'+$("#id"+modalName).attr("data-"+modalNameLCase+"-id")); + personTypeLCase = personSpan.attr("data-creator-type").toLowerCase(); + } else { + var id = personTypeLCase + $("."+personTypeLCase+"-item").length; + personSpan = $(''); + } + + personSpan.attr("class", "label label-warning "+personTypeLCase +"-item"); + personSpan.html(""); + + var firstname = $("#firstName"+modalName).val(); + var lastname = $("#lastName"+modalName).val(); + + var uri = $("#uri"+modalName).val(); + var localAuthority = $("#uri"+modalName+"LocalId").val(); + personSpan.attr("data-"+modalNameLCase+"-firstname", firstname); + personSpan.attr("data-"+modalNameLCase+"-lastname", lastname); + personSpan.attr("data-"+modalNameLCase+"-uri", uri); + personSpan.attr("data-"+modalNameLCase+"-authority-id", localAuthority); + personSpan.attr("data-creator-type", personTypeLCase); + + var affiliationsList = []; + var affSpan = $(""); + $("#"+modalNameLCase+"Affiliations").children().each(function(idx, elem) { + var affName = $(elem).find(".firstName"+modalName+"Affiliation"); + var affUri = $(elem).find(".affUri"); + if(affName.val().length!=0){ + var affSpan = $(""); + affSpan.addClass("affiliation-class"); + affSpan.attr("data-affiliation-name", affName.val()); + affSpan.attr("data-affiliation-uri", affUri.val()); + affiliationsList.push(affName.val()); + personSpan.append(affSpan); + } + }); + + var affiliationString = ""; + if (affiliationsList.length != 0) { + affiliationString = " (" + $.grep(affiliationsList, Boolean).join(", ") + ")"; + } + + personSpan.append(lastname + ', ' + firstname + affiliationString + '   '); + var editIcon = $('') + var deleteIcon = $(''); + editIcon.click(function(){ + var personItem = $(this).parent(); + editPerson(modalName, personItem[0]); + }); + deleteIcon.click(removePerson); + personSpan.append(editIcon); + personSpan.append(deleteIcon); + $("#"+personTypeLCase+"List").append(personSpan); + $("#"+personTypeLCase+"List").append("   "); + $("#"+modalNameLCase+"Modal").modal('hide'); + resetPersonCreationModal(modalName); +} + + +/* Function to append final information for form submission */ +function constructPersonArray(arrayName, role, iter){ + var creator, otherCreatorCount = 0; + var roleLC = role.toLowerCase(); + if(arrayName == "creator"){ + creator = "otherCreator"; + } else { + creator = arrayName; + } + $('.'+roleLC+'-item').each(function(idx, person) { + var creatorSubmitCount = idx + iter; + var personIdField = $(""); + personIdField.attr("type", "hidden"); + personIdField.attr("id", creator+"s" + creatorSubmitCount + ".id"); + personIdField.attr("name", creator+"s[" + creatorSubmitCount + "].id"); + personIdField.attr("value", $(person).attr("data-"+arrayName+"-id")); + $("#editForm").append(personIdField); + + var personFirstNameField = $(""); + personFirstNameField.attr("type", "hidden"); + personFirstNameField.attr("id", creator+"s" + creatorSubmitCount + ".firstName"); + personFirstNameField.attr("name", creator+"s[" + creatorSubmitCount + "].firstName"); + personFirstNameField.attr("value", $(person).attr("data-"+arrayName+"-firstname")); + $("#editForm").append(personFirstNameField); + + var personLastNameField = $(""); + personLastNameField.attr("type", "hidden"); + personLastNameField.attr("id", creator+"s" + creatorSubmitCount + ".lastName"); + personLastNameField.attr("name", creator+"s[" + creatorSubmitCount + "].lastName"); + personLastNameField.attr("value", $(person).attr("data-"+arrayName+"-lastname")); + $("#editForm").append(personLastNameField); + + var personRoleField = $(""); + personRoleField.attr("type", "hidden"); + personRoleField.attr("id", creator+"s" + creatorSubmitCount + ".role"); + personRoleField.attr("name", creator+"s[" + creatorSubmitCount + "].role"); + personRoleField.attr("value", role); + $("#editForm").append(personRoleField); + + var personUriField = $(""); + personUriField.attr("type", "hidden"); + personUriField.attr("id", creator+"s" + creatorSubmitCount + ".uri"); + personUriField.attr("name", creator+"s[" + creatorSubmitCount + "].uri"); + personUriField.attr("value", $(person).attr("data-"+arrayName+"-uri")); + $("#editForm").append(personUriField); + + var personAuthorityField = $(""); + personAuthorityField.attr("type", "hidden"); + personAuthorityField.attr("id", creator+"s" + creatorSubmitCount + ".localAuthorityId"); + personAuthorityField.attr("name", creator+"s[" + creatorSubmitCount + "].localAuthorityId"); + + personAuthorityField.attr("value", $(person).attr("data-"+arrayName+"-authority-id")); + $("#editForm").append(personAuthorityField); + + $(person).children(".affiliation-class").each(function(idx2, affiliation) { + var affiliationField = $(""); + affiliationField.attr("type", "hidden"); + affiliationField.attr("id", creator+"s" + creatorSubmitCount + ".affiliations" + idx2 + ".name"); + affiliationField.attr("name", creator+"s[" + creatorSubmitCount + "].affiliations[" + idx2 + "].name"); + affiliationField.attr("value", $(affiliation).attr("data-affiliation-name")); + $("#editForm").append(affiliationField); + + var affiliationIdField = $(""); + affiliationIdField.attr("type", "hidden"); + affiliationIdField.attr("id", creator+"s" + creatorSubmitCount + ".affiliations" + idx2 + ".id"); + affiliationIdField.attr("name", creator+"s[" + creatorSubmitCount + "].affiliations[" + idx2 + "].id"); + affiliationIdField.attr("value", $(affiliation).attr("data-affiliation-id")); + $("#editForm").append(affiliationIdField); + + var affiliationUriField = $(""); + affiliationUriField.attr("type", "hidden"); + affiliationUriField.attr("id", creator+"s" + creatorSubmitCount + ".affiliations" + idx2 + ".uri"); + affiliationUriField.attr("name", creator+"s[" + creatorSubmitCount + "].affiliations[" + idx2 + "].uri"); + affiliationUriField.attr("value", $(affiliation).attr("data-affiliation-uri")); + $("#editForm").append(affiliationUriField); + }); + otherCreatorCount += 1; + }); + return otherCreatorCount; +} + +function createConceptTags() { + $("#conceptTags").children("span").each(function (idx, tag) { + var conceptTagInput = $(""); + conceptTagInput.attr("type", "hidden"); + conceptTagInput.attr("id", "conceptTags" + idx + ".conceptId"); + conceptTagInput.attr("name", "conceptTags[" + idx + "].conceptId"); + conceptTagInput.attr("value", $(tag).attr("data-concept-id")); + $("#editForm").append(conceptTagInput); + + var conceptTagTypeInput = $(""); + conceptTagTypeInput.attr("type", "hidden"); + conceptTagTypeInput.attr("id", "conceptTags" + idx + ".conceptTypeId"); + conceptTagTypeInput.attr("name", "conceptTags[" + idx + "].conceptTypeId"); + conceptTagTypeInput.attr("value", $(tag).attr("data-concept-type-id")); + $("#editForm").append(conceptTagTypeInput); + + var conceptTagUri = $(""); + conceptTagUri.attr("type", "hidden"); + conceptTagUri.attr("id", "conceptTags" + idx + ".conceptUri"); + conceptTagUri.attr("name", "conceptTags[" + idx + "].conceptUri"); + conceptTagUri.attr("value", $(tag).attr("data-concept-uri")); + $("#editForm").append(conceptTagUri); + + var conceptTagName = $(""); + conceptTagName.attr("type", "hidden"); + conceptTagName.attr("id", "conceptTags" + idx + ".conceptName"); + conceptTagName.attr("name", "conceptTags[" + idx + "].conceptName"); + conceptTagName.attr("value", $(tag).attr("data-concept-name")); + $("#editForm").append(conceptTagName); + + var conceptTagType = $(""); + conceptTagType.attr("type", "hidden"); + conceptTagType.attr("id", "conceptTags" + idx + ".conceptTypeName"); + conceptTagType.attr("name", "conceptTags[" + idx + "].conceptTypeName"); + conceptTagType.attr("value", $(tag).attr("data-type-name")); + $("#editForm").append(conceptTagType); + + var conceptTagTypeUri = $(""); + conceptTagTypeUri.attr("type", "hidden"); + conceptTagTypeUri.attr("id", "conceptTags" + idx + ".conceptTypeUri"); + conceptTagTypeUri.attr("name", "conceptTags[" + idx + "].conceptTypeUri"); + conceptTagTypeUri.attr("value", $(tag).attr("data-type-uri")); + $("#editForm").append(conceptTagTypeUri); + }); +} + +function resetPersonCreationModal(modalType) { + var modalNameLCase = modalType.toLowerCase(); + $("#firstName"+modalType).val(""); + $("#lastName"+modalType).val(""); + $("#id"+modalType).attr("data-"+modalType+"-id", ""); + $("#"+modalNameLCase+"Affiliations").children().remove(); + $("#uri"+modalType).val(""); + if(modalType == "Creator") { + $("#addCreatorButton").attr("data-creator-type", ""); + $("#creatorLabel").text("Enter Creator Information"); + $("#addCreatorButton").text("Add Creator"); + } + + $("#add"+modalType+"Button").text("Add "+modalType); + $("#add"+modalType+"Button").prop("disabled",true); + resetPersonAuthorityCreation(modalType); + affCount = 0; +} + +function resetPersonAuthorityCreation(personType) { + $("#uriLoadingFound"+personType).hide(); + $("#uriLoadingFailure"+personType).hide(); + $("#uriLoadingSpinner"+personType).hide(); + $("#uriLoadingFound"+personType).popover('hide'); + $("#uriLoadingFailure"+personType).popover('hide'); + $("#"+personType.toLowerCase()+"AuthorityUsed").html(""); +} + +function getPersonAuthority(uri, personType) { + + personType_lowerCase = personType.toLowerCase(); + $.get("/auth/authority/get?uri=" + uri + '&zoteroGroupId=' + zoteroGroupId, function(data) { + $("#uriLoadingFound"+personType).attr("data-authority-uri", data['uri']); + var content = "Authority " + uri + ""; + if (data['userAuthorityEntries'] != null && data['userAuthorityEntries'].length > 0) { + content += "

    This authority entry has already been imported by you:"; + content += '
      '; + data['userAuthorityEntries'].forEach(function(elem) { + content += '
    • ' + elem['name']; + content += ' [Use this one]'; + content += '
    • '; + }); + content += "
    "; + } + if (data['datasetAuthorityEntries'] != null && data['datasetAuthorityEntries'].length > 0) { + content += "

    This authority entry has already been imported by someone else for this dataset:"; + content += '
      '; + data['datasetAuthorityEntries'].forEach(function(elem) { + content += '
    • ' + elem['name']; + content += ' [Use this one]'; + content += '
    • '; + }); + content += "
    "; + } + + if((data['userAuthorityEntries'] == null || data['userAuthorityEntries'].length == 0) + && (data['datasetAuthorityEntries'] == null || data['datasetAuthorityEntries'].length == 0)) { + content += "

    No authorities found for the given URI
    "; + } + + $("#uriLoadingFound"+personType).attr("data-content", content); + $("#uriLoadingFound"+personType).attr("data-authority-uri", uri); + $("#uriLoadingFound"+personType).show(); + $("#uriLoadingFound"+personType).popover('show'); + }) + .fail(function() { + $("#uriLoadingFailure"+personType).show(); + }) + .always(function() { + $("#uriLoadingSpinner"+personType).hide(); + }); + +} + +function getAuthorities(modalType, personType, page, userType) { + var firstName = $("#firstName"+personType).val(); + var lastName = $("#lastName"+personType).val(); + if (lastName === undefined) { + lastName = ""; + } + personType_lowerCase = personType.toLowerCase(); + url = "/citesphere/auth/authority/"+zoteroGroupId+ "/find/authorities/" + userType + '?firstName='+ firstName + '&lastName=' + lastName +'&page='+page; + $.ajax({ + dataType: "json", + type: 'GET', + url: url , + async: false, + success: function(data) { + $("#" + userType + "AuthoritySearchResult").empty(); + var content = ''; + + if (data['foundAuthorities'] != null && data['foundAuthorities'].length > 0) { + data['foundAuthorities'].forEach(function(elem) { + content += ' ' + elem['name'] + ' ' + elem['uri'] + ' ' ; + if(elem['description']==null){ + content += ' - '; + } + else{ + content +=elem['description'] + ''; + } + content += ''; + }); + + $('#' + userType + 'Authority-pagination-top').twbsPagination({ + totalPages: data['totalPages'], + startPage: data['currentPage'], + prev: "«", + next: "»", + visiblePages: 5, + initiateStartPageClick: false, + onPageClick:function(event, page) { + getAuthorities(modalType, personType, page-1, userType) + } + }); + } + + + $("#" + userType + "AuthoritySearchResult").append(content); + $("." + userType + "-authority-entry").click(function() { + name = $(this).closest("tr").find(".name").text(); + uri = $(this).closest("tr").find(".uri").text(); + + if (modalType.includes("Affiliation")) { + showAffiliationNameInModal(name, uri, personType); + } else { + showPersonNameInModal(name, personType) + $("#uri"+modalType).val( uri); + } + + $("#uri"+modalType).val( uri); + $("#"+personType_lowerCase+"AuthorityUsed").html("Using stored authority entry " + name + "."); + $("#selectAuthorityModel").modal('hide'); + }); + + }, + error: function(data){ + $('#' + userType + 'AuthoritySearchResult').parents('table').hide() + $("#" + userType + "AuthoritiesError").show(); + } + }); +} + +let removePerson = function removePerson(e) { + var deleteIcon = e.currentTarget; + var person = $(deleteIcon).parent(); + person.remove(); +} + +let removeConcept = function removeConcept(e) { + var deleteIcon = e.currentTarget; + var concept = $(deleteIcon).parent(); + concept.remove(); +} + +function loadFields() { + var itemType = $('#items option:selected').val() + $("#displayMessage").html("" + + " Loading form fields"); + $("#messageModal").modal('show'); + $.ajax({ + url : "/citesphere/auth/items/" + itemType + '/fields', + type : 'GET', + success: function(changedFields){ + $('form input').each(function(idx, elem) { + $(elem).parent().closest('tr').hide(); + }); + for(i=0;i" + + "Error loading the form fields. Try again later."); + $('#messageModal').modal('show'); + setTimeout(function() { + $('#messageModal').modal('hide'); + }, 3000); + + } + }); + $.ajax({ + url : "/citesphere/auth/items/" + itemType + '/creators', + + type : 'GET', + success: function(creators){ + $('.creator-row').each(function(idx, elem) { + $(elem).hide(); + }); + for(i=0;i"); + creatorRow.css("display", "table-row"); + creatorRow.addClass("creator-row"); + var creatorLabel = $(""); + creatorLabel.addClass("creator"); + creatorLabel.css("text-transform", "capitalize"); + creatorLabel.attr("id", creators[i]); + creatorLabel.append(creators[i]); + creatorRow.append(creatorLabel); + var creatorData = $(""); + var creatorList = $(""); + creatorList.attr("id",creators[i]+"List"); + creatorList.css("font-size", "18px"); + creatorData.append(creatorList); + var addIconDiv = $("
    "); + addIconDiv.addClass("pull-right"); + var iconLink = $(""); + + iconLink.attr("data-toggle","modal"); + iconLink.attr("data-creator-type", creators[i]); + iconLink.attr("data-target","#creatorModal"); + var iconImg = $(""); + iconImg.addClass("icon-circle-add"); + iconLink.append(iconImg); + iconLink.append("Add "+creators[i]); + addIconDiv.append(iconLink); + creatorData.append(addIconDiv); + creatorRow.append(creatorData); + creatorRow.insertAfter($('.creator').last().parent()); + $("#creatorLabel").css("text-transform", "capitalize"); + $("#creatorLabel").text("Enter "+creators[i]+" Information"); + $("#addCreatorButton").css("text-transform", "capitalize"); + $("#addCreatorButton").text("Add "+creators[i]); + $("#addCreatorButton").attr("data-creator-type", creators[i]); + iconLink.click(function(e) { + creatorLinkHandler($(e.target)); + }); + } + + } + }, + error: function(){ + $("#displayMessage").html("" + + "Error loading the creators. Try again later."); + $('#messageModal').modal('show'); + setTimeout(function() { + $('#messageModal').modal('hide'); + }, 3000); + + } + }); +} +function creatorLinkHandler(target) { + var creatorType = target.attr("data-creator-type").charAt(0).toUpperCase() + target.attr("data-creator-type").slice(1); + $("#creatorLabel").text("Enter "+creatorType+" Information"); + $("#addCreatorButton").text("Add "+creatorType); + $("#addCreatorButton").attr("data-creator-type", target.attr("data-creator-type")); +} + + + diff --git a/citesphere/src/main/webapp/resources/authorities/authority-search.js b/citesphere/src/main/webapp/resources/authorities/authority-search.js new file mode 100644 index 000000000..86dd5f4dd --- /dev/null +++ b/citesphere/src/main/webapp/resources/authorities/authority-search.js @@ -0,0 +1,1428 @@ +var CitesphereAuthority = CitesphereAuthority || {}; + +CitesphereAuthority.Config = (function() { + var config = { + baseUrl: '', + zoteroGroupId: null, + csrfToken: null, + csrfParameterName: null, + endpoints: {}, + importerNames: {} + }; + + return { + setBaseUrl: function(url) { config.baseUrl = url; }, + getBaseUrl: function() { return config.baseUrl; }, + setZoteroGroupId: function(groupId) { config.zoteroGroupId = groupId; }, + getZoteroGroupId: function() { return config.zoteroGroupId; }, + setCsrfToken: function(token) { config.csrfToken = token; }, + getCsrfToken: function() { return config.csrfToken; }, + setCsrfParameterName: function(name) { config.csrfParameterName = name; }, + getCsrfParameterName: function() { return config.csrfParameterName; }, + setEndpoint: function(key, url) { config.endpoints[key] = url; }, + getEndpoint: function(key) { + var endpoint = config.endpoints[key] || ''; + return config.baseUrl + endpoint; + }, + setImporterName: function(key, name) { config.importerNames[key] = name; }, + getImporterName: function(key) { return config.importerNames[key] || ''; }, + setImporterId: function(key, id) { + config.importerIds = config.importerIds || {}; + config.importerIds[key] = id; + }, + getImporterId: function(key) { return (config.importerIds && config.importerIds[key]) || ''; } + }; +})(); + +CitesphereAuthority.BaseConnector = function(options) { + this.source = options.source; + this.endpointKey = options.endpointKey || options.source; + this.importerId = options.importerId || null; + this.importerName = options.importerName || null; + this.useGroupId = options.useGroupId || false; +}; + +CitesphereAuthority.BaseConnector.prototype = { + buildUrl: function(params) { + var url = CitesphereAuthority.Config.getEndpoint(this.endpointKey); + + if (this.useGroupId) { + var groupId = params.groupId || CitesphereAuthority.Config.getZoteroGroupId(); + url = url.replace('{groupId}', groupId); + } + + var queryParams = { + firstName: params.firstName || '', + lastName: params.lastName || '', + page: params.page || 0 + }; + + var queryString = $.param(queryParams); + return url + (url.indexOf('?') === -1 ? '?' : '&') + queryString; + }, + + search: function(params, callbacks) { + var self = this; + var url = this.buildUrl(params); + + $.ajax({ + dataType: "json", + type: 'GET', + url: url, + success: function(data) { + var results = self.processResults(data); + if (callbacks.onSuccess) { + callbacks.onSuccess(results); + } + }, + error: function(xhr, status, error) { + if (callbacks.onError) { + callbacks.onError({xhr: xhr, status: status, error: error}); + } + } + }); + }, + + processResults: function(data) { + return { + authorities: data.foundAuthorities || [], + totalPages: data.totalPages || 0, + currentPage: data.currentPage || 1, + source: this.source, + importerId: this.getImporterId(), + importerName: this.getImporterName() + }; + }, + + getImporterId: function() { + return this.importerId || CitesphereAuthority.Config.getImporterId(this.source); + }, + + getImporterName: function() { + return this.importerName || CitesphereAuthority.Config.getImporterName(this.source); + } +}; + +CitesphereAuthority.ConceptpowerConnector = function(options) { + options = options || {}; + options.source = 'conceptpower'; + options.importerId = options.importerId || 'authority.importer.conceptpower'; + CitesphereAuthority.BaseConnector.call(this, options); +}; +CitesphereAuthority.ConceptpowerConnector.prototype = Object.create(CitesphereAuthority.BaseConnector.prototype); +CitesphereAuthority.ConceptpowerConnector.prototype.constructor = CitesphereAuthority.ConceptpowerConnector; + +CitesphereAuthority.ViafConnector = function(options) { + options = options || {}; + options.source = 'viaf'; + options.importerId = options.importerId || 'authority.importer.viaf'; + CitesphereAuthority.BaseConnector.call(this, options); +}; +CitesphereAuthority.ViafConnector.prototype = Object.create(CitesphereAuthority.BaseConnector.prototype); +CitesphereAuthority.ViafConnector.prototype.constructor = CitesphereAuthority.ViafConnector; + +CitesphereAuthority.IsiscbConnector = function(options) { + options = options || {}; + options.source = 'isiscb'; + CitesphereAuthority.BaseConnector.call(this, options); +}; +CitesphereAuthority.IsiscbConnector.prototype = Object.create(CitesphereAuthority.BaseConnector.prototype); +CitesphereAuthority.IsiscbConnector.prototype.constructor = CitesphereAuthority.IsiscbConnector; + +CitesphereAuthority.UserConnector = function(options) { + options = options || {}; + options.source = 'user'; + options.useGroupId = true; + CitesphereAuthority.BaseConnector.call(this, options); +}; +CitesphereAuthority.UserConnector.prototype = Object.create(CitesphereAuthority.BaseConnector.prototype); +CitesphereAuthority.UserConnector.prototype.constructor = CitesphereAuthority.UserConnector; + +CitesphereAuthority.GroupConnector = function(options) { + options = options || {}; + options.source = 'group'; + options.useGroupId = true; + CitesphereAuthority.BaseConnector.call(this, options); +}; +CitesphereAuthority.GroupConnector.prototype = Object.create(CitesphereAuthority.BaseConnector.prototype); +CitesphereAuthority.GroupConnector.prototype.constructor = CitesphereAuthority.GroupConnector; + +CitesphereAuthority.SearchResults = function(options) { + this.containerId = options.containerId; + this.source = options.source; + this.onSelect = options.onSelect || function() {}; + this.showImporterInfo = options.showImporterInfo !== false; +}; + +CitesphereAuthority.SearchResults.prototype = { + render: function(results) { + var $container = $('#' + this.containerId); + $container.empty(); + + if (!results.authorities || results.authorities.length === 0) { + return; + } + + var self = this; + var content = ''; + + results.authorities.forEach(function(authority) { + content += self.renderRow(authority, results); + }); + + $container.append(content); + this.bindEvents(results); + }, + + renderRow: function(authority, results) { + var content = ''; + content += '' + (authority.name || '') + ''; + content += '' + (authority.uri || '') + ''; + content += '' + (authority.description || ' - ') + ''; + + if (this.showImporterInfo && results.importerName) { + content += '' + results.importerName + ''; + } + if (this.showImporterInfo && results.importerId) { + content += '' + results.importerId + ''; + } + + content += ''; + content += ' 0) { + $container.twbsPagination({ + totalPages: results.totalPages, + startPage: results.currentPage, + prev: "«", + next: "»", + visiblePages: 5, + initiateStartPageClick: false, + onPageClick: function(event, page) { + self.onPageChange(page - 1); + } + }); + } + }, + + destroy: function() { + var $container = $('#' + this.containerId); + try { + $container.twbsPagination('destroy'); + } catch (e) { + // Ignore if not initialized + } + } +}; + +CitesphereAuthority.AuthorityService = function() { + this.connectors = {}; +}; + +CitesphereAuthority.AuthorityService.prototype = { + registerConnector: function(name, connector) { + this.connectors[name] = connector; + }, + + getConnector: function(name) { + return this.connectors[name]; + }, + + search: function(source, params, callbacks) { + var connector = this.connectors[source]; + if (connector) { + connector.search(params, callbacks); + } + }, + + searchAll: function(sources, params, callbacks) { + var self = this; + + sources.forEach(function(source) { + self.search(source, params, { + onSuccess: function(results) { + if (callbacks.onSourceSuccess) { + callbacks.onSourceSuccess(source, results); + } + }, + onError: function(error) { + if (callbacks.onSourceError) { + callbacks.onSourceError(source, error); + } + } + }); + }); + } +}; + +CitesphereAuthority.CreateHandler = function(options) { + this.options = options || {}; + this.authorityService = new CitesphereAuthority.AuthorityService(); + this.searchResults = {}; + this.pagination = {}; + this.sources = options.sources || ['conceptpower', 'viaf', 'isiscb']; + + this._initConnectors(); + this._initComponents(); +}; + +CitesphereAuthority.CreateHandler.prototype = { + _initConnectors: function() { + var self = this; + + this.sources.forEach(function(source) { + var ConnectorClass; + switch (source) { + case 'conceptpower': + ConnectorClass = CitesphereAuthority.ConceptpowerConnector; + break; + case 'viaf': + ConnectorClass = CitesphereAuthority.ViafConnector; + break; + case 'isiscb': + ConnectorClass = CitesphereAuthority.IsiscbConnector; + break; + default: + return; + } + self.authorityService.registerConnector(source, new ConnectorClass()); + }); + }, + + _initComponents: function() { + var self = this; + + this.sources.forEach(function(source) { + self.searchResults[source] = new CitesphereAuthority.SearchResults({ + containerId: source + 'AuthoritySearchResult', + source: source, + onSelect: function(authority) { + self._onAuthoritySelected(authority); + } + }); + + self.pagination[source] = new CitesphereAuthority.Pagination({ + containerId: source + 'Authority-pagination-top', + onPageChange: function(page) { + self._onPageChange(source, page); + } + }); + }); + }, + + init: function() { + var self = this; + + this._hideLoadingIndicators(); + + $('#firstNameAuthor, #lastNameAuthor').on('keyup', function() { + self._updateSearchButtonState(); + }); + + $('#searchAuthor').on('click', function() { + self._onSearchClick(); + }); + + $('#closeAuthoritySearchResult').on('click', function() { + $('#selectAuthorityModel').modal('hide'); + $('#selectAuthorityModel a:first').tab('show'); + }); + + $('#addAuthorModalCancel').on('click', function() { + $('#authorModal').modal('hide'); + self._resetModal(); + }); + + $('#clear').on('click', function() { + $('#tr-uriAuthor').hide(); + $('#tr-importerId').hide(); + $('#tr-importerName').hide(); + }); + + var uriTimer = null; + $('#uriAuthor').on('change', function() { + self._resetAuthorityCreation(); + $('#uriLoadingSpinnerAuthor').show(); + var uri = $(this).val(); + clearTimeout(uriTimer); + uriTimer = setTimeout(function() { + self._getPersonAuthority(uri); + }, 1000); + }); + + $('#authorIconContainer').on('click', '.popover #authorCreateAuthority', function() { + var name = $(this).closest('div').find('.name').text(); + var uri = $(this).closest('div').find('.uri').text(); + var importerId = $(this).closest('div').find('.importerId').text(); + var importerName = $(this).closest('div').find('.importerName').text(); + + self._showPerson(name, null, uri, importerId, importerName); + $('#authorCreateAuthority').hide(); + $('#uriLoadingFoundAuthor').popover('hide'); + $('#selectAuthorityModel').modal('hide'); + $('#authorModal').modal('hide'); + self._resetModal(); + }); + }, + + _hideLoadingIndicators: function() { + $('#uriLoadingSpinnerAuthor').hide(); + $('#uriLoadingFailureAuthor').hide(); + $('#uriLoadingFoundAuthor').hide(); + $('#searchAuthorSpinner').hide(); + $('#tr-uriAuthor').hide(); + $('#tr-importerId').hide(); + $('#tr-importerName').hide(); + $('#importerId').hide(); + $('#importerName').hide(); + }, + + _updateSearchButtonState: function() { + var firstName = $('#firstNameAuthor').val(); + var lastName = $('#lastNameAuthor').val(); + var disabled = (firstName === '' && lastName === ''); + $('#searchAuthor').prop('disabled', disabled); + }, + + _onSearchClick: function() { + var self = this; + + $('#selectAuthorityModel').on('hidden.bs.modal', function() { + $('#selectAuthorityModel a:first').tab('show'); + }); + + $('#searchAuthorSpinner').show(); + + this.sources.forEach(function(source) { + self.pagination[source].destroy(); + }); + + var params = { + firstName: $('#firstNameAuthor').val(), + lastName: $('#lastNameAuthor').val(), + page: 0 + }; + + this._searchAllSources(params); + + $('#searchAuthorSpinner').hide(); + }, + + _searchAllSources: function(params) { + var self = this; + this.sources.forEach(function(source) { + self._searchSource(source, params); + }); + }, + + _searchSource: function(source, params) { + var self = this; + + this.authorityService.search(source, params, { + onSuccess: function(results) { + self.searchResults[source].render(results); + self.pagination[source].render(results); + }, + onError: function(error) { + self.searchResults[source].showError(); + } + }); + }, + + _onPageChange: function(source, page) { + var params = { + firstName: $('#firstNameAuthor').val(), + lastName: $('#lastNameAuthor').val(), + page: page + }; + this._searchSource(source, params); + }, + + _onAuthoritySelected: function(authority) { + this._showPerson( + authority.name, + authority.description, + authority.uri, + authority.importerId, + authority.importerName + ); + $('#selectAuthorityModel').modal('hide'); + $('#authorModal').modal('hide'); + this._resetModal(); + }, + + _showPerson: function(name, description, uri, importerId, importerName) { + $('#name').val(name); + + if (description !== undefined && description !== ' - ' && description !== null) { + $('#description').val(description); + } + + if (uri !== undefined && uri !== '') { + $('#uri').val(uri); + $('#tr-uriAuthor').show(); + } + + if (importerId !== undefined) { + $('#importerId').val(importerId); + } + + if (importerName !== undefined) { + $('#importerName').val(importerName); + $('#importerName').show(); + $('#tr-importerName').show(); + } + }, + + _getPersonAuthority: function(uri) { + var self = this; + var url = CitesphereAuthority.Config.getEndpoint('getAuthority'); + + $.get(url + '?uri=' + encodeURIComponent(uri), function(data) { + $('#uriLoadingFoundAuthor').attr('data-authority-uri', data.uri); + var content = 'Authority ' + uri + ''; + + if (data.userAuthorityEntries && data.userAuthorityEntries.length > 0) { + content += '

    This authority entry has already been imported by you:'; + content += '
      '; + data.userAuthorityEntries.forEach(function(elem) { + content += '
    • ' + elem.name + '
    • '; + }); + content += '
    '; + } else if (data.importedAuthority) { + content += '

    Following authority can be imported:
    ' + data.importedAuthority.name; + content += ''; + } else { + content += '

    No authorities found for the given URI
    '; + } + + $('#uriLoadingFoundAuthor').attr('data-content', content); + $('#uriLoadingFoundAuthor').attr('data-authority-uri', uri); + $('#uriLoadingFoundAuthor').show(); + $('#uriLoadingFoundAuthor').popover('show'); + }) + .fail(function() { + $('#uriLoadingFailureAuthor').show(); + }) + .always(function() { + $('#uriLoadingSpinnerAuthor').hide(); + }); + }, + + _resetModal: function() { + $('#firstNameAuthor').val(''); + $('#lastNameAuthor').val(''); + $('#idAuthor').attr('data-Author-id', ''); + $('#uriAuthor').val(''); + this._resetAuthorityCreation(); + }, + + _resetAuthorityCreation: function() { + $('#uriLoadingFoundAuthor').hide(); + $('#uriLoadingFailureAuthor').hide(); + $('#uriLoadingSpinnerAuthor').hide(); + $('#uriLoadingFoundAuthor').popover('hide'); + $('#uriLoadingFailureAuthor').popover('hide'); + } +}; + +CitesphereAuthority.EditItemHandler = function(options) { + this.options = options || {}; + this.authorityService = new CitesphereAuthority.AuthorityService(); + this.searchResults = {}; + this.pagination = {}; + this.sources = options.sources || ['user', 'group', 'viaf', 'conceptpower']; + this.affCount = 0; + this._currentModalType = null; + this._currentPersonType = null; + + this._initConnectors(); + this._initComponents(); + this._createAffiliationTemplate(); +}; + +CitesphereAuthority.EditItemHandler.prototype = { + _initConnectors: function() { + var self = this; + + this.sources.forEach(function(source) { + var ConnectorClass; + switch (source) { + case 'conceptpower': + ConnectorClass = CitesphereAuthority.ConceptpowerConnector; + break; + case 'viaf': + ConnectorClass = CitesphereAuthority.ViafConnector; + break; + case 'isiscb': + ConnectorClass = CitesphereAuthority.IsiscbConnector; + break; + case 'user': + ConnectorClass = CitesphereAuthority.UserConnector; + break; + case 'group': + ConnectorClass = CitesphereAuthority.GroupConnector; + break; + default: + return; + } + self.authorityService.registerConnector(source, new ConnectorClass()); + }); + }, + + _initComponents: function() { + var self = this; + + this.sources.forEach(function(source) { + self.searchResults[source] = new CitesphereAuthority.SearchResults({ + containerId: source + 'AuthoritySearchResult', + source: source, + showImporterInfo: false, + onSelect: function(authority) { + self._onAuthoritySelected(authority); + } + }); + + self.pagination[source] = new CitesphereAuthority.Pagination({ + containerId: source + 'Authority-pagination-top', + onPageChange: function(page) { + self._onPageChange(source, page); + } + }); + }); + }, + + _createAffiliationTemplate: function() { + this.affiliationTemplate = $($.parseHTML( + '
    ' + + '
    ' + + '
    ' + + '' + + '' + + '' + + '
    ' + + '' + + '
    ' + + '' + + '' + + '
    ' + + '
    ' + )); + }, + + init: function() { + var self = this; + + this._hideLoadingIndicators(); + + $('#uriLoadingFoundAuthor').popover(); + $('#uriLoadingFailureAuthor').popover(); + $('#uriLoadingFoundEditor').popover(); + $('#uriLoadingFailureEditor').popover(); + $('#uriLoadingFoundCreator').popover(); + $('#uriLoadingFailureCreator').popover(); + + $('#submitForm').on('click', function(e) { + self._constructPersonArray('author', 'author', 0); + self._constructPersonArray('editor', 'editor', 0); + var creatorSubmitCount = 0; + $('.creator-row').each(function(idx, elem) { + var ele = $(elem).children().first(); + if (!(ele.attr('id') === 'editor' || ele.attr('id') === 'author')) { + var roleCount = self._constructPersonArray('creator', ele.attr('id'), creatorSubmitCount); + creatorSubmitCount = creatorSubmitCount + roleCount; + } + }); + self._createConceptTags(); + }); + + this._initPersonEvents('Author'); + this._initPersonEvents('Editor'); + this._initPersonEvents('Creator'); + + $(document).on('click', '.remove-aff', function() { + $(this).closest('.aff-entry').remove(); + }); + + $('#closeAuthoritySearchResult').on('click', function() { + $('#selectAuthorityModel').modal('hide'); + $('#selectAuthorityModel a:first').tab('show'); + }); + + this._bindExistingPersonHandlers(); + + $('#addConceptButton').on('click', function(e) { + e.preventDefault(); + self._addConcept(); + }); + }, + + _initPersonEvents: function(personType) { + var self = this; + var personTypeLower = personType.toLowerCase(); + + $('#firstName' + personType + ', #lastName' + personType).on('keyup', function() { + self._allowSearchAndAdd(personType); + }); + + $('#add' + personType + 'Button').on('click', function(e) { + if (personType === 'Creator') { + var target = $(e.target); + if (target.attr('data-creator-type') != null) { + self._savePersonDetails(target.attr('data-creator-type'), 'Creator'); + } else { + self._savePersonDetails('', 'Creator'); + } + } else { + self._savePersonDetails(personType, personType); + } + }); + + $('#add' + personType + 'ModalCancel').on('click', function() { + $('#' + personTypeLower + 'Modal').modal('hide'); + self._resetPersonCreationModal(personType); + }); + + $('#search' + personType).on('click', function() { + self._searchAuthorities(personType, personType); + }); + + $('#add' + personType + 'Affiliation').on('click', function() { + self._addAffiliation(personType, self.affCount + 1); + }); + + var timer = null; + $('#uri' + personType).on('change', function() { + self._resetPersonAuthorityCreation(personType); + $('#uriLoadingSpinner' + personType).show(); + var uri = $(this).val(); + clearTimeout(timer); + timer = setTimeout(function() { + self._getPersonAuthority(uri, personType); + }, 1000); + }); + + $('#' + personTypeLower + 'IconContainer').on('click', '.popover #' + personTypeLower + 'CreateAuthority', function() { + var uri = $('#uriLoadingFound' + personType).attr('data-authority-uri'); + var importAuthorityURL = '/auth/authority/import?uri=' + uri; + + $.ajax({ + dataType: 'json', + type: 'POST', + url: importAuthorityURL, + data: {csrfParameterName: CitesphereAuthority.Config.getCsrfToken()}, + success: function(data) { + $('#' + personTypeLower + 'CreateAuthority').hide(); + $('#uri' + personType + 'LocalId').val(data.id); + $('#' + personTypeLower + 'AuthorityUsed').html('Created new authority entry ' + data.name + '.'); + $('#' + personTypeLower + 'AuthorityCreationFeedback').html('
    Authority entry has been created!
    '); + self._showPersonNameInModal(data.name, personType); + $('#uriLoadingFound' + personType).popover('hide'); + $('#add' + personType + 'Button').prop('disabled', false); + } + }); + }); + + $('#' + personTypeLower + 'IconContainer').on('click', '.popover .foundAuthorities li a', function(event) { + var authId = $(this).attr('data-authority-id'); + $('#uri' + personType + 'LocalId').val(authId); + $('#' + personTypeLower + 'AuthorityUsed').html('Using stored authority entry ' + $(this).attr('data-authority-name') + '.'); + self._showPersonNameInModal($(this).attr('data-authority-name'), personType); + $('#uriLoadingFound' + personType).popover('hide'); + $('#add' + personType + 'Button').prop('disabled', false); + event.preventDefault(); + }); + }, + + _hideLoadingIndicators: function() { + var types = ['Author', 'Editor', 'Creator']; + types.forEach(function(type) { + $('#uriLoadingSpinner' + type).hide(); + $('#uriLoadingFailure' + type).hide(); + $('#uriLoadingFound' + type).hide(); + $('#search' + type + 'Spinner').hide(); + }); + }, + + _bindExistingPersonHandlers: function() { + var self = this; + var types = ['author', 'editor', 'creator']; + + types.forEach(function(type) { + var Type = type.charAt(0).toUpperCase() + type.slice(1); + + $('.edit-' + type).on('click', function() { + var item = $(this).parent(); + self._editPerson(Type, item[0]); + }); + $('.edit-' + type).css('cursor', 'pointer'); + + $('.remove-' + type).on('click', self._removePerson); + $('.remove-' + type).css('cursor', 'pointer'); + }); + + $('.remove-concept').on('click', this._removeConcept); + $('.remove-concept').css('cursor', 'pointer'); + + $('.creatorModalLink').on('click', function(e) { + var target = $(e.target); + var creatorType = target.attr('data-creator-type'); + creatorType = creatorType.charAt(0).toUpperCase() + creatorType.slice(1); + $('#creatorLabel').text('Enter ' + creatorType + ' Information'); + $('#addCreatorButton').text('Add ' + creatorType); + $('#addCreatorButton').attr('data-creator-type', target.attr('data-creator-type')); + }); + }, + + _allowSearchAndAdd: function(element) { + if ($('#firstName' + element).val() === '' && $('#lastName' + element).val() === '') { + $('#search' + element).prop('disabled', true); + $('#add' + element + 'Button').prop('disabled', true); + } else { + $('#search' + element).prop('disabled', false); + $('#add' + element + 'Button').prop('disabled', false); + } + }, + + _searchAuthorities: function(modalType, personType) { + var self = this; + + this._currentModalType = modalType; + this._currentPersonType = personType; + + $('#selectAuthorityModel').on('hidden.bs.modal', function() { + $('#selectAuthorityModel a:first').tab('show'); + }); + + $('#search' + modalType + 'Spinner').show(); + + this.sources.forEach(function(source) { + self.pagination[source].destroy(); + }); + + var params = { + firstName: $('#firstName' + personType).val(), + lastName: $('#lastName' + personType).val() || '', + page: 0 + }; + + this.sources.forEach(function(source) { + self.searchResults[source].onSelect = function(authority) { + self._onAuthoritySelectedForPerson(authority, modalType, personType); + }; + }); + + this._searchAllSources(params); + + $('#search' + modalType + 'Spinner').hide(); + }, + + _searchAllSources: function(params) { + var self = this; + this.sources.forEach(function(source) { + self._searchSource(source, params); + }); + }, + + _searchSource: function(source, params) { + var self = this; + + this.authorityService.search(source, params, { + onSuccess: function(results) { + self.searchResults[source].render(results); + self.pagination[source].render(results); + }, + onError: function(error) { + self.searchResults[source].showError(); + } + }); + }, + + _onPageChange: function(source, page) { + var params = { + firstName: $('#firstName' + this._currentPersonType).val(), + lastName: $('#lastName' + this._currentPersonType).val() || '', + page: page + }; + this._searchSource(source, params); + }, + + _onAuthoritySelected: function(authority) { + this._onAuthoritySelectedForPerson(authority, this._currentModalType, this._currentPersonType); + }, + + _onAuthoritySelectedForPerson: function(authority, modalType, personType) { + var personTypeLower = personType.toLowerCase(); + + if (modalType.includes('Affiliation')) { + this._showAffiliationNameInModal(authority.name, authority.uri, personType); + } else { + this._showPersonNameInModal(authority.name, personType); + $('#uri' + modalType).val(authority.uri); + } + + $('#uri' + modalType).val(authority.uri); + $('#' + personTypeLower + 'AuthorityUsed').html('Using stored authority entry ' + authority.name + '.'); + $('#selectAuthorityModel').modal('hide'); + }, + + _showAffiliationNameInModal: function(name, uri, personType) { + $('#firstName' + personType).val(name); + $('#uri' + personType).val(uri); + }, + + // Parses authority name formats and extracts first/last name + // Handles: "Last, First (Alternate), Year-", "Last, First, Title, Year", "Last, First" + _showPersonNameInModal: function(name, personType) { + var personName = name; + + // Strip bracketed alternate names: "Dempsey, Hugh A. (Hugh Aylmer), 1929-" -> "Dempsey, Hugh A. , 1929-" + if (name.includes('(')) { + personName = name.substring(0, name.indexOf('(')); + } + + // Strip extra comma-separated parts (title/year): "Iqbal, Muhammad, Sir, 1877-1938" -> "Iqbal, Muhammad" + if (personName.split(',').length > 2) { + personName = personName.substring(0, personName.indexOf(',', personName.indexOf(',') + 1)); + } + + // Strip trailing year span: "Dempsey, Patrick, 1966-" -> "Dempsey, Patrick" + if (personName.includes('-')) { + personName = personName.trim(); + personName = personName.substring(0, personName.lastIndexOf(' ')); + } + + // Split into first/last name + if (personName.indexOf(',') !== -1) { + $('#firstName' + personType).val(personName.substring(personName.indexOf(',') + 1).trim()); + $('#lastName' + personType).val(personName.substring(0, personName.lastIndexOf(', '))); + } else { + $('#lastName' + personType).val(personName.substring(personName.lastIndexOf(' ') + 1).trim()); + $('#firstName' + personType).val(personName.substring(0, personName.lastIndexOf(' '))); + } + }, + + _addAffiliation: function(modalType, counter) { + var self = this; + var modalTypeLower = modalType.toLowerCase(); + var affiliationCopy = this.affiliationTemplate.clone(); + + affiliationCopy.attr('id', modalTypeLower + 'AffiliationTemplate' + counter); + affiliationCopy.addClass('aff-info'); + + affiliationCopy.find('label[for="affiliation"]').attr('for', 'firstName' + modalType + 'Affiliation' + counter); + affiliationCopy.find('label[for="uri"]').attr('for', 'uri' + modalType + 'Affiliation' + counter); + + var affInput = affiliationCopy.find('#firstName'); + affInput.addClass('firstName' + modalType + 'Affiliation'); + affInput.attr('id', 'firstName' + modalType + 'Affiliation' + counter); + affInput.attr('data-client-id', affInput.attr('id')); + + var searchButton = affiliationCopy.find('button'); + searchButton.attr('id', 'search' + modalType + 'Affiliation' + counter); + searchButton.attr('data-client-id', searchButton.attr('id')); + searchButton.find('#searchSpinner').attr('id', 'search' + modalType + 'Affiliation' + counter + 'Spinner'); + searchButton.prop('disabled', true); + + var affUri = affiliationCopy.find('.affUri'); + affUri.attr('id', 'uri' + modalType + 'Affiliation' + counter); + affUri.attr('data-client-id', affUri.attr('id')); + + $('#' + modalTypeLower + 'Affiliations').append(affiliationCopy); + + var affiliationCounter = modalType + 'Affiliation' + counter; + $('#firstName' + affiliationCounter).on('keyup', function() { + self._allowSearchAndAdd(affiliationCounter); + }); + + $('#search' + affiliationCounter).on('click', function() { + self._searchAuthorities(affiliationCounter, affiliationCounter); + }); + + this.affCount++; + }, + + _editPerson: function(modalName, item) { + var self = this; + var personItem = $(item); + var modalNameLower = modalName.toLowerCase(); + + $('#firstName' + modalName).val(personItem.attr('data-' + modalNameLower + '-firstname')); + $('#lastName' + modalName).val(personItem.attr('data-' + modalNameLower + '-lastname')); + $('#uri' + modalName).val(personItem.attr('data-' + modalNameLower + '-uri')); + $('#id' + modalName).attr('data-' + modalNameLower + '-id', personItem.attr('id')); + + var counter = 0; + personItem.children('.affiliation-class').each(function(idx, elem) { + var affiliationCopy = self.affiliationTemplate.clone(); + affiliationCopy.attr('id', modalNameLower + 'AffiliationTemplate' + counter); + affiliationCopy.addClass('aff-info'); + + affiliationCopy.find('label[for="affiliation"]').attr('for', 'firstName' + modalName + 'Affiliation' + counter); + affiliationCopy.find('label[for="uri"]').attr('for', 'uri' + modalName + 'Affiliation' + counter); + + var affInput = affiliationCopy.find('#firstName'); + affInput.addClass('firstName' + modalName + 'Affiliation'); + affInput.attr('id', 'firstName' + modalName + 'Affiliation' + counter); + affInput.attr('data-client-id', affInput.attr('id')); + affInput.attr('data-affiliation-name', $(elem).data('affiliationName')); + affInput.attr('data-affiliation-id', $(elem).data('affiliationId')); + affInput.val($(elem).data('affiliationName')); + + var searchButton = affiliationCopy.find('button'); + searchButton.attr('id', 'search' + modalName + 'Affiliation' + counter); + searchButton.attr('data-client-id', searchButton.attr('id')); + searchButton.find('#searchSpinner').attr('id', 'search' + modalName + 'Affiliation' + counter + 'Spinner'); + searchButton.prop('disabled', false); + + var affUri = affiliationCopy.find('.affUri'); + affUri.attr('id', 'uri' + modalName + 'Affiliation' + counter); + affUri.attr('data-client-id', affUri.attr('id')); + affUri.attr('data-affiliation-uri', $(elem).data('affiliationUri')); + affUri.val($(elem).data('affiliationUri')); + + $('#' + modalNameLower + 'Affiliations').append(affiliationCopy); + + var affiliationCounter = modalName + 'Affiliation' + counter; + $('#firstName' + affiliationCounter).on('keyup', function() { + self._allowSearchAndAdd(affiliationCounter); + }); + + $('#search' + affiliationCounter).on('click', function() { + self._searchAuthorities(affiliationCounter, affiliationCounter); + }); + + counter++; + }); + + this.affCount = counter; + $('#addCreatorButton').attr('data-' + modalNameLower + '-type', personItem.attr('data-' + modalNameLower + 'type')); + $('#add' + modalName + 'Button').text('Update ' + modalName); + $('#add' + modalName + 'Button').prop('disabled', false); + + $('#' + modalNameLower + 'Modal').modal('show'); + if ($('#firstName' + modalName).val() !== '' || $('#lastName' + modalName).val() !== '') { + $('#search' + modalName).prop('disabled', false); + } + }, + + _savePersonDetails: function(personType, modalName) { + var self = this; + var modalNameLower = modalName.toLowerCase(); + var personSpan; + var personTypeLower = personType.toLowerCase(); + + if ($('#id' + modalName).attr('data-' + modalNameLower + '-id') != null && + $('#id' + modalName).attr('data-' + modalNameLower + '-id').length > 0) { + personSpan = $('#' + $('#id' + modalName).attr('data-' + modalNameLower + '-id')); + personTypeLower = personSpan.attr('data-creator-type').toLowerCase(); + } else { + var id = personTypeLower + $('.' + personTypeLower + '-item').length; + personSpan = $(''); + } + + personSpan.attr('class', 'label label-warning ' + personTypeLower + '-item'); + personSpan.html(''); + + var firstname = $('#firstName' + modalName).val(); + var lastname = $('#lastName' + modalName).val(); + var uri = $('#uri' + modalName).val(); + var localAuthority = $('#uri' + modalName + 'LocalId').val(); + + personSpan.attr('data-' + modalNameLower + '-firstname', firstname); + personSpan.attr('data-' + modalNameLower + '-lastname', lastname); + personSpan.attr('data-' + modalNameLower + '-uri', uri); + personSpan.attr('data-' + modalNameLower + '-authority-id', localAuthority); + personSpan.attr('data-creator-type', personTypeLower); + + var affiliationsList = []; + $('#' + modalNameLower + 'Affiliations').children().each(function(idx, elem) { + var affName = $(elem).find('.firstName' + modalName + 'Affiliation'); + var affUri = $(elem).find('.affUri'); + if (affName.val().length !== 0) { + var affSpan = $(''); + affSpan.addClass('affiliation-class'); + affSpan.attr('data-affiliation-name', affName.val()); + affSpan.attr('data-affiliation-uri', affUri.val()); + affiliationsList.push(affName.val()); + personSpan.append(affSpan); + } + }); + + var affiliationString = ''; + if (affiliationsList.length !== 0) { + affiliationString = ' (' + $.grep(affiliationsList, Boolean).join(', ') + ')'; + } + + personSpan.append(lastname + ', ' + firstname + affiliationString + '   '); + + var editIcon = $(''); + var deleteIcon = $(''); + + editIcon.on('click', function() { + var personItem = $(this).parent(); + self._editPerson(modalName, personItem[0]); + }); + deleteIcon.on('click', this._removePerson); + + personSpan.append(editIcon); + personSpan.append(deleteIcon); + $('#' + personTypeLower + 'List').append(personSpan); + $('#' + personTypeLower + 'List').append('   '); + $('#' + modalNameLower + 'Modal').modal('hide'); + this._resetPersonCreationModal(modalName); + }, + + // Builds hidden form fields for person data on form submission + _constructPersonArray: function(arrayName, role, iter) { + var self = this; + var creator, otherCreatorCount = 0; + var roleLower = role.toLowerCase(); + + if (arrayName === 'creator') { + creator = 'otherCreator'; + } else { + creator = arrayName; + } + + $('.' + roleLower + '-item').each(function(idx, person) { + var creatorSubmitCount = idx + iter; + + var fields = [ + {suffix: '.id', attr: 'data-' + arrayName + '-id'}, + {suffix: '.firstName', attr: 'data-' + arrayName + '-firstname'}, + {suffix: '.lastName', attr: 'data-' + arrayName + '-lastname'}, + {suffix: '.role', value: role}, + {suffix: '.uri', attr: 'data-' + arrayName + '-uri'}, + {suffix: '.localAuthorityId', attr: 'data-' + arrayName + '-authority-id'} + ]; + + fields.forEach(function(field) { + var input = $(''); + input.attr('type', 'hidden'); + input.attr('id', creator + 's' + creatorSubmitCount + field.suffix); + input.attr('name', creator + 's[' + creatorSubmitCount + ']' + field.suffix); + input.attr('value', field.value !== undefined ? field.value : $(person).attr(field.attr)); + $('#editForm').append(input); + }); + + $(person).children('.affiliation-class').each(function(idx2, affiliation) { + var affFields = [ + {suffix: '.name', attr: 'data-affiliation-name'}, + {suffix: '.id', attr: 'data-affiliation-id'}, + {suffix: '.uri', attr: 'data-affiliation-uri'} + ]; + + affFields.forEach(function(field) { + var input = $(''); + input.attr('type', 'hidden'); + input.attr('id', creator + 's' + creatorSubmitCount + '.affiliations' + idx2 + field.suffix); + input.attr('name', creator + 's[' + creatorSubmitCount + '].affiliations[' + idx2 + ']' + field.suffix); + input.attr('value', $(affiliation).attr(field.attr)); + $('#editForm').append(input); + }); + }); + + otherCreatorCount += 1; + }); + + return otherCreatorCount; + }, + + // Builds hidden form fields for concept tags on form submission + _createConceptTags: function() { + $('#conceptTags').children('span').each(function(idx, tag) { + var fields = [ + {suffix: '.conceptId', attr: 'data-concept-id'}, + {suffix: '.conceptTypeId', attr: 'data-concept-type-id'}, + {suffix: '.conceptUri', attr: 'data-concept-uri'}, + {suffix: '.conceptName', attr: 'data-concept-name'}, + {suffix: '.conceptTypeName', attr: 'data-type-name'}, + {suffix: '.conceptTypeUri', attr: 'data-type-uri'} + ]; + + fields.forEach(function(field) { + var input = $(''); + input.attr('type', 'hidden'); + input.attr('id', 'conceptTags' + idx + field.suffix); + input.attr('name', 'conceptTags[' + idx + ']' + field.suffix); + input.attr('value', $(tag).attr(field.attr)); + $('#editForm').append(input); + }); + }); + }, + + _addConcept: function() { + var conceptId = $('#addConceptConceptSelect'); + var conceptType = $('#addConceptTypeSelect'); + + var conceptSpan = $(''); + conceptSpan.attr('data-concept-uri', conceptId.val()); + conceptSpan.attr('data-type-uri', conceptType.val()); + + var text = $('#addConceptConceptSelect option:selected').text(); + var typeName = $('#addConceptTypeSelect option:selected').text(); + conceptSpan.text(text + ' | ' + typeName + ' '); + + var deleteIcon = $(''); + deleteIcon.on('click', this._removeConcept); + conceptSpan.append(deleteIcon); + $('#conceptTags').append(conceptSpan); + + $('#addConceptModal').modal('hide'); + }, + + _getPersonAuthority: function(uri, personType) { + var self = this; + var personTypeLower = personType.toLowerCase(); + var zoteroGroupId = CitesphereAuthority.Config.getZoteroGroupId(); + + $.get('/auth/authority/get?uri=' + encodeURIComponent(uri) + '&zoteroGroupId=' + zoteroGroupId, function(data) { + $('#uriLoadingFound' + personType).attr('data-authority-uri', data.uri); + var content = 'Authority ' + uri + ''; + + if (data.userAuthorityEntries && data.userAuthorityEntries.length > 0) { + content += '

    This authority entry has already been imported by you:'; + content += '
      '; + data.userAuthorityEntries.forEach(function(elem) { + content += '
    • ' + elem.name; + content += ' [Use this one]'; + content += '
    • '; + }); + content += '
    '; + } + + if (data.datasetAuthorityEntries && data.datasetAuthorityEntries.length > 0) { + content += '

    This authority entry has already been imported by someone else for this dataset:'; + content += '
      '; + data.datasetAuthorityEntries.forEach(function(elem) { + content += '
    • ' + elem.name; + content += ' [Use this one]'; + content += '
    • '; + }); + content += '
    '; + } + + if ((!data.userAuthorityEntries || data.userAuthorityEntries.length === 0) && + (!data.datasetAuthorityEntries || data.datasetAuthorityEntries.length === 0)) { + content += '

    No authorities found for the given URI
    '; + } + + $('#uriLoadingFound' + personType).attr('data-content', content); + $('#uriLoadingFound' + personType).attr('data-authority-uri', uri); + $('#uriLoadingFound' + personType).show(); + $('#uriLoadingFound' + personType).popover('show'); + }) + .fail(function() { + $('#uriLoadingFailure' + personType).show(); + }) + .always(function() { + $('#uriLoadingSpinner' + personType).hide(); + }); + }, + + _resetPersonCreationModal: function(modalType) { + var modalNameLower = modalType.toLowerCase(); + $('#firstName' + modalType).val(''); + $('#lastName' + modalType).val(''); + $('#id' + modalType).attr('data-' + modalType + '-id', ''); + $('#' + modalNameLower + 'Affiliations').children().remove(); + $('#uri' + modalType).val(''); + + if (modalType === 'Creator') { + $('#addCreatorButton').attr('data-creator-type', ''); + $('#creatorLabel').text('Enter Creator Information'); + $('#addCreatorButton').text('Add Creator'); + } + + $('#add' + modalType + 'Button').text('Add ' + modalType); + $('#add' + modalType + 'Button').prop('disabled', true); + this._resetPersonAuthorityCreation(modalType); + this.affCount = 0; + }, + + _resetPersonAuthorityCreation: function(personType) { + $('#uriLoadingFound' + personType).hide(); + $('#uriLoadingFailure' + personType).hide(); + $('#uriLoadingSpinner' + personType).hide(); + $('#uriLoadingFound' + personType).popover('hide'); + $('#uriLoadingFailure' + personType).popover('hide'); + $('#' + personType.toLowerCase() + 'AuthorityUsed').html(''); + }, + + _removePerson: function(e) { + var deleteIcon = e.currentTarget; + var person = $(deleteIcon).parent(); + person.remove(); + }, + + _removeConcept: function(e) { + var deleteIcon = e.currentTarget; + var concept = $(deleteIcon).parent(); + concept.remove(); + }, + + loadFields: function() { + var self = this; + var itemType = $('#items option:selected').val(); + + $('#displayMessage').html(" Loading form fields"); + $('#messageModal').modal('show'); + + $.ajax({ + url: '/citesphere/auth/items/' + itemType + '/fields', + type: 'GET', + success: function(changedFields) { + $('form input').each(function(idx, elem) { + $(elem).parent().closest('tr').hide(); + }); + for (var i = 0; i < changedFields.length; i++) { + var fieldId = changedFields[i]; + if (fieldId === 'date') { + fieldId = 'dateFreetext'; + } + $('form input#' + fieldId).parent().closest('tr').show(); + } + $('#messageModal').modal('hide'); + }, + error: function() { + $('#displayMessage').html(" Error loading the form fields. Try again later."); + $('#messageModal').modal('show'); + setTimeout(function() { + $('#messageModal').modal('hide'); + }, 3000); + } + }); + + $.ajax({ + url: '/citesphere/auth/items/' + itemType + '/creators', + type: 'GET', + success: function(creators) { + $('.creator-row').each(function(idx, elem) { + $(elem).hide(); + }); + for (var i = 0; i < creators.length; i++) { + if ($('[id=' + creators[i]).length > 0) { + $('[id=' + creators[i]).parent().closest('tr').addClass('creator-row'); + $('[id=' + creators[i]).parent().closest('tr').show(); + } else if (creators[i] !== 'editor' && creators[i] !== 'author') { + self._createCreatorRow(creators[i]); + } + } + }, + error: function() { + $('#displayMessage').html(" Error loading the creators. Try again later."); + $('#messageModal').modal('show'); + setTimeout(function() { + $('#messageModal').modal('hide'); + }, 3000); + } + }); + }, + + _createCreatorRow: function(creatorType) { + var self = this; + + var creatorRow = $(''); + creatorRow.css('display', 'table-row'); + creatorRow.addClass('creator-row'); + + var creatorLabel = $(''); + creatorLabel.addClass('creator'); + creatorLabel.css('text-transform', 'capitalize'); + creatorLabel.attr('id', creatorType); + creatorLabel.append(creatorType); + creatorRow.append(creatorLabel); + + var creatorData = $(''); + var creatorList = $(''); + creatorList.attr('id', creatorType + 'List'); + creatorList.css('font-size', '18px'); + creatorData.append(creatorList); + + var addIconDiv = $('
    '); + addIconDiv.addClass('pull-right'); + + var iconLink = $(''); + iconLink.attr('data-toggle', 'modal'); + iconLink.attr('data-creator-type', creatorType); + iconLink.attr('data-target', '#creatorModal'); + + var iconImg = $(''); + iconImg.addClass('icon-circle-add'); + iconLink.append(iconImg); + iconLink.append('Add ' + creatorType); + addIconDiv.append(iconLink); + creatorData.append(addIconDiv); + creatorRow.append(creatorData); + creatorRow.insertAfter($('.creator').last().parent()); + + $('#creatorLabel').css('text-transform', 'capitalize'); + $('#creatorLabel').text('Enter ' + creatorType + ' Information'); + $('#addCreatorButton').css('text-transform', 'capitalize'); + $('#addCreatorButton').text('Add ' + creatorType); + $('#addCreatorButton').attr('data-creator-type', creatorType); + + iconLink.on('click', function(e) { + self._creatorLinkHandler($(e.target)); + }); + }, + + _creatorLinkHandler: function(target) { + var creatorType = target.attr('data-creator-type'); + creatorType = creatorType.charAt(0).toUpperCase() + creatorType.slice(1); + $('#creatorLabel').text('Enter ' + creatorType + ' Information'); + $('#addCreatorButton').text('Add ' + creatorType); + $('#addCreatorButton').attr('data-creator-type', target.attr('data-creator-type')); + } +};