(function ($) { $.Behaviours.AutoPopulateBusinessFieldsBehaviour = function (element, options) { /********************************************************** * Inherit from base Behaviour ***********************************************************/ $.Behaviours.InputBehaviour.call(this, element, options); /********************************************************** * Variables ***********************************************************/ var me = this; /********************************************************** * Properties ***********************************************************/ this.mappingsConfig = this.$element.data("mappings"); this.mappings = []; this.$parent = $(this.$element.parents("td:first")); this.$ajaxLoader = $(".ajax-loader", this.$parent); this.$noResults = $(".no-results", this.$parent); this.$establishmentsList = $("select.establishments", this.$parent); /********************************************************** * Public Methods ***********************************************************/ /********************************************************** * Private Methods ***********************************************************/ var populateFields = function (subDossierSelected) { var dossierNumber = this.$element.val(); var subDossierNumber = this.$establishmentsList.val(); var url = "/Form/kvkModule/Service/GetDossier/"; var params = { dossierNumber: dossierNumber, subDossierNumber: subDossierNumber }; // Reset UI me.$noResults.hide(); subDossierSelected || me.$establishmentsList.hide(); // Check if the field has been emptied if (dossierNumber.length == 0) { // Clear & Enable all targets for (var i = 0; i < this.mappings.length; i++) { var mapping = me.mappings[i]; var targetInputItemName = mapping.target; var $target = findInputItem(targetInputItemName); $target.val(""); $target.removeAttr("disabled"); } return; } // Query KvK service this.$ajaxLoader.show(); $.getJSON(url, params).then(function (data) { switch (data.Count) { case 0: // No results were found me.$noResults.show(); break; case 1: // 1 result was found for (var i = 0; i < me.mappings.length; i++) { var mapping = me.mappings[i]; var targetInputItemName = mapping.target; var sourceFieldName = mapping.source; var $target = findInputItem(targetInputItemName); if ($target.length > 0) { var value = data[sourceFieldName]; $target.val(value); $target.attr("disabled", true); }; } break; default: // multiple results were found for (var i = 0; i < data.BusinessReferences.length; i++) { var establishment = data.BusinessReferences[i]; var $option = $(""); me.$establishmentsList.append($option); } me.$establishmentsList.show(); break; } me.$ajaxLoader.hide(); }); }; var parseMappingsConfig = function () { var items = this.mappingsConfig.split(";"); var mapping = []; for (var i = 0; i < items.length; i++) { var parts = items[i].split("->"); var sourceFieldName = parts[0]; var targetInputItemName = parts[1]; mapping.push({ source: sourceFieldName, target: targetInputItemName }); } return mapping; }; var findInputItem = function (inputItemName) { var triggerAttrSelector = "[data-input-name='" + inputItemName + "']"; return $("input" + triggerAttrSelector + ", select" + triggerAttrSelector); }; /********************************************************** * EventHandlers ***********************************************************/ this.$element.change(function (e) { populateFields.call(me); }); this.$establishmentsList.change(function (e) { populateFields.call(me, true); }); /********************************************************** * Initialization ***********************************************************/ this.mappings = parseMappingsConfig.call(this); }; /********************************************************** * Plugin Registration ***********************************************************/ $.fn.autoPopulateBusinessFieldsBehaviour = function (options) { $.registerBehaviour.call(this, "autoPopulateBusinessFieldsBehaviour", $.Behaviours.AutoPopulateBusinessFieldsBehaviour, options); }; })(jQuery); /********************************************************** * Attach behaviour ***********************************************************/ $(function () { $.BehaviourInitializers.push(function (context) { $(".auto-populate-business-fields-behaviour", context).autoPopulateBusinessFieldsBehaviour(); }); });