// 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('
' + pathOfTerm + 'x
') } }, function (x, args) { console.log("Call failed. Error: " + args.get_message()); } ); } else { $(this).siblings("div[id^=SELECTED_" + fieldName + "]").empty(); } }); var taxonomyValidator = function () { taxonomyValidator.prototype.Validate = function (value) { var isError = false; var errorMessage = "" if (value === "|" || value === "" || value == undefined) { isError = true; errorMessage = "You can't leave this blank."; } return new SPClientForms.ClientValidation.ValidationResult(isError, errorMessage); }; }; var jslinkTemplates = window.jslinkTemplates || {}; jslinkTemplates.Taxonomy = function () { var currentlySelectedTerms; var internalTermCounter; function _editMode(ctx) { // register our "get value callback" method var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx); formCtx.registerGetValueCallback(formCtx.fieldName, jslinkTemplates.Taxonomy.getValue.bind(null, formCtx.fieldName)); var validators = new SPClientForms.ClientValidation.ValidatorSet(); validators.RegisterValidator(new taxonomyValidator()); var multivalue = ctx.CurrentFieldSchema.AllowMultipleValues; formCtx.registerValidationErrorCallback(formCtx.fieldName, function (errorResult) { if (multivalue == true) { SPFormControl_AppendValidationErrorMessage("TAXVALUEALL_" + formCtx.fieldName, errorResult); } else { SPFormControl_AppendValidationErrorMessage("TAX_" + formCtx.fieldName, errorResult); } } ); formCtx.registerClientValidator(formCtx.fieldName, validators); _loadField(multivalue, formCtx.fieldName, ctx.CurrentFieldSchema.SspId, ctx.CurrentFieldSchema.TermSetId, ctx.CurrentFieldValue); if (multivalue == true) { return "
" + "
+
" + "" + "" + ""; } else { return "" + ""; } }; function _loadField(multivalue, fieldName, sspId, termSetId, currentValue) { //jQuery(document).ready(function () { // jQuery('h3#'+fieldName+' nobr').append(""); //}); // reset the current terms array currentlySelectedTerms = []; // wait for the page to render jQuery(document).ready(function () { var context = new SP.ClientContext.get_current(); var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); var termStore = taxonomySession.get_termStores().getById(sspId); var termSet = termStore.getTermSet(termSetId); var clonedArray = []; if (currentValue && currentValue.length > 0) { if (multivalue) { $("#TAXVALUEALL_" + fieldName).val(currentValue).trigger('change'); _renderDropDowns(fieldName, sspId, termSetId, clonedArray); } else { // pull the Id from the current value var currentTermId = currentValue.split("|")[1]; var currentTerm = termSet.getTerm(currentTermId); context.load(currentTerm); context.executeQueryAsync( function () { // retrieve the full path .. this will include all of the parent terms too var fullPath = currentTerm.get_pathOfTerm(); // they are semi-colon delimited, so if we perform a split we end up // with an array containing all of the currently selected values currentlySelectedTerms = fullPath.split(";"); clonedArray = currentlySelectedTerms.slice(); // now we can proceed with rendering... _renderDropDowns(fieldName, sspId, termSetId, clonedArray); }, function (x, args) { console.log("Call failed. Error: " + args.get_message()); }); } } else { // there is no current value, so just go _renderDropDowns(fieldName, sspId, termSetId, clonedArray); } }); }; function _renderDropDowns(fieldName, sspId, termSetId, clonedArray) { //jQuery("#"+fieldName+"_loading").show(); var elementId = "#TAX_" + fieldName; var context = new SP.ClientContext.get_current(); var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); var termStore = taxonomySession.get_termStores().getById(sspId); var termSet = termStore.getTermSet(termSetId); var terms = termSet.get_terms(); context.load(terms); context.executeQueryAsync( function () { jQuery(elementId).empty(); // clear out the existing options var html = ""; var termEnumerator = terms.getEnumerator(); // Unterschiedliche html erstellen, um die Sortierung anzupassen var termSetHtml = ""; var termHtml = ""; var isNewForm = false; if (document.location.pathname.substr(document.location.pathname.length - 12).toLowerCase() == 'newform.aspx') { isNewForm = true } while (termEnumerator.moveNext()) { var currentHtml = ""; var currentTerm = termEnumerator.get_current(); // Filter deprecated Terms bei Neuanlage (NewForm.aspx) if (!isNewForm || !currentTerm.get_isDeprecated()) { currentHtml += ""; termSetHtml += currentHtml; } else { currentHtml += ">" + termName + ""; termHtml += currentHtml; } } } html += termSetHtml + termHtml; // add the options jQuery(elementId).append(html).change(function () { var $dropOption = $(this).find("option:selected"); $dropOption.parent().nextAll("input:first").val($dropOption.text() + "|" + $dropOption.val()).trigger('change'); }); //jQuery("#"+fieldName+"_loading").hide(); // check if we selected one of them var selectedOption = jQuery(elementId + " option[selected='selected']"); if (selectedOption.length > 0) { // simulate the same "sender.target" property used in a click event var newSender = { 'target': selectedOption[0], 'fieldName': fieldName }; // recursively render the child drop downs _selectTerm(newSender, clonedArray); } jQuery(elementId + " option").click(jslinkTemplates.Taxonomy.selectTerm); var $dropOption = jQuery(elementId + " option:selected") $dropOption.parent().nextAll("input:first").val($dropOption.text() + "|" + $dropOption.val()).trigger('change'); }, function (x, args) { alert("Call failed. Error: " + args.get_message()); } ); }; function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { return true; } } return false; } function _selectTerm(sender, clonedArray) { var termId = jQuery(sender.target).val(); var termLabel = jQuery(sender.target).text(); // kill off any existing child-drop-downs (recursively) // before we re-render them below var selectElement = jQuery(sender.target).parent()[0]; removeChildDropDowns(selectElement); var termIndex = 0; var selParent = jQuery(sender.target).parent("[id^=TERM_]"); if (selParent.length > 0) { termIndex = parseInt(selParent.attr("termindex")) + 1; } if (termId == "") { jQuery(sender.target).parent().siblings("input").val("") return; } // update the hidden input field with the currently selected value // this should be in the value LABEL|GUID //jQuery(sender.target).parent().siblings("input").val(termLabel + "|" + termId); var context = new SP.ClientContext.get_current(); var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); var term = taxonomySession.getTerm(termId); var childTerms = term.get_terms(); context.load(term); context.load(childTerms); context.executeQueryAsync( function () { // only execute if it has child terms if (term.get_termsCount() > 0) { // start off with a select containing a blank option //jQuery("#"+sender.fieldName+"_loading").show(); var html = "