// v1.2 var taxonomyHelper = window.taxonomyHelper || {}; // Only for multivalue taxonomyfield // Takes the value from the dropdown and writes it in the multivalue result field taxonomyHelper.getTaxValue = function (fieldName) { // retrieve value from the relevant hidden input var hiddenValue = jQuery("#TAXVALUE_" + fieldName).val(); if (hiddenValue == "|" || hiddenValue == "" || hiddenValue == undefined) { return; } if ($("#TAXVALUEALL_" + fieldName).val() == "") { $("#TAXVALUEALL_" + fieldName).val(hiddenValue).trigger('change'); } else { if ($("#TAXVALUEALL_" + fieldName).val().indexOf(hiddenValue) == -1) { $("#TAXVALUEALL_" + fieldName).val($("#TAXVALUEALL_" + fieldName).val() + ";" + hiddenValue).trigger('change'); } } }; // Only for multivalue taxonomyfield // Removes a taxonomyvalue with the spezified id from the multivalue result field taxonomyHelper.removeTaxValue = function (fieldName, id) { var newValue = ""; var taxValueArray = $("#TAXVALUEALL_" + fieldName).val().split(";").filter(Boolean); for (var i = 0; i < taxValueArray.length; i++) { if (taxValueArray[i].split("|")[1] != id) { if (newValue != "") { newValue = newValue + ";" + taxValueArray[i]; } else { newValue = taxValueArray[i]; } } } $("#TAXVALUEALL_" + fieldName).val(newValue).trigger('change'); }; taxonomyHelper.openDialog = function (dialogTitel) { $('#' + dialogTitel + "_ModalDialog").show(); $('#' + dialogTitel + "_ModalOverlay").show(); }; taxonomyHelper.closeDialog = function (dialogTitel) { $('#' + dialogTitel + "_ModalDialog").hide(); $('#' + dialogTitel + "_ModalOverlay").hide(); }; jQuery(document).on("change", "select[id^=TERM_]", function () { var $dropOption = $(this).find("option:selected"); $dropOption.parent().nextAll("input:first").val($dropOption.text() + "|" + $dropOption.val()).trigger('change'); }); // Only for multivalue taxonomyfield // On multivalue taxonomiefield result change jQuery(document).on("change", "input[id^=TAXVALUEALL_]", function () { var that = this; var fieldName = $(this).attr("id").replace("TAXVALUEALL_", ""); var sspId = $(this).attr("data-sspId"); var termSetId = $(this).attr("data-termSetId"); var value = $(this).val(); if (value != "") { var context = new SP.ClientContext.get_current(); var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); // Read out TermSet to get the correct path (Reused term) var termStore = taxonomySession.get_termStores().getById(sspId); var termSet = termStore.getTermSet(termSetId); var terms = []; // Check if new taxonomyvalue else ignore (prevent double add) var valueArr = value.split(";").filter(Boolean); valueArr.forEach(function (termItem) { var guid = termItem.split("|")[1]; if ($("#" + guid).length == 0) { var term = termSet.getTerm(guid); terms.push(term); context.load(term); } }); // Check if an taxonomyfield was removed // If yes remove the corresponding taxonomyfieldvalue $(this).siblings("div[id^=SELECTED_" + fieldName + "]").children().each(function () { var contains = false; var item = $(this); valueArr.forEach(function (termItem) { var guid = termItem.split("|")[1]; if (item.attr("id") == guid) { contains = true; } }); if (!contains) { item.remove(); } }) // Add missing taxonomyfieldvalues context.executeQueryAsync( function () { for (var i = 0; i < terms.length; i++) { var pathOfTerm = terms[i].get_pathOfTerm().split(";").join(" > "); var termId = terms[i].get_id(); $(that).siblings("div[id^=SELECTED_" + fieldName + "]").append('