Views.AutoComplete = Views.Text.extend({ classType: "Views.AutoComplete", tagName: "div", listenEvents: function() { this.on("onrender", this.onRender); }, triggerEventsOn: function() { }, triggerAddModelToCollectionOn: function() { var that = this; this.on("onautocompleteitemselected", function(event) { that.trigger("onaddmodeltocollection"); }); }, hide: function(callback) { //this.$el.animate({height:'0px'}, 100, callback); var that = this; this.$el.slideUp(100, function() { if (callback) { callback(); } that.trigger("onhide"); }); this.options.isShowed = false; }, show: function(callback) { //this.$el.animate({height:'0px'}, 100, callback); var that = this; this.$el.slideDown(100, function() { if (callback) { callback(); } that.trigger("onshow"); }); this.options.isShowed = true; }, onRender: function() { this.excludeValues = []; if (this.options.excludeValues) this.excludeValues = JSON.parse(this.options.excludeValues); this.$el.removeClass("textInput").addClass("autoCompleteInput"); //manage autocomplete if (this.options.autocomplete) { if (this.options.autocomplete.objname && this.options.autocomplete.searchtype) { this.options.autocomplete.isvalid = true; this.on("onmodelchange", function(){ this.checkError(); }); this.$el.on("keyup", {view: this}, function(event) { event.data.view.checkError(); event.data.view.keyupAutoComplete(event); }); this.$el.on("click", {view: this}, function(event) { var autoCompleteEl = $("#autocomplete_" + event.data.view.id); if (autoCompleteEl.length > 0) { if (autoCompleteEl.attr("isopened") == "true") { event.data.view.closeAutoComplete(); } else { event.data.view.openAutoComplete(); } } }); this.on("onblur", function() { if (this.$el.val() != "" && this.model.get("value") == "") { this.validateCurrentAutoCompleteItem(); /*if (this.model.get("value") == "") { this.$el.removeAttr("val"); this.model.set("value", ""); this.$el.val(""); }*/ } else { this.$el.removeAttr("val"); } this.checkError(); var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { if (autoCompleteEl.attr("ismouseovered") != "true") { this.closeAutoComplete(); } } }); this.on("onfocus", function() { this.checkError(); }); } else { this.options.autocomplete.isvalid = false; } } else { this.options.autocomplete = {}; this.options.autocomplete.isvalid = false; } }, resetAddEl: function() { $("#autocomplete_" + this.id).remove(); this.$el.val(""); this.$el.attr("val", ""); }, initSearchField: function(search) { this.$el.val(search); this.$el.attr("val", search); }, resetEl: function() { /*this.$el.val(""); this.$el.attr("val", "");*/ }, checkError: function() { //if input is different than model.value, then reset the model.value var currentFilter = this.$el.val(); var currentSelectedLabel = this.model.get("label"); if (!currentSelectedLabel) { currentSelectedLabel = ""; } if (currentFilter.toLowerCase() != currentSelectedLabel.toLowerCase()) { this.model.set("value", ""); this.model.set("label", ""); this.$el.attr("val", ""); } if (!this.model.get("value")) { this.$el.addClass("error"); return; } else { if (this.model.get("value") == "") { this.$el.addClass("error"); return; } } this.$el.removeClass("error"); }, keyupAutoComplete: function(event) { if (event.keyCode == 27) { //ECHAP this.closeAutoComplete(); } else if (event.keyCode == 40) { //DOWN ARROW this.openAutoComplete(); this.selectNextAutoCompleteItem(); } else if (event.keyCode == 38) { //UP ARROW this.openAutoComplete(); this.selectPreviousAutoCompleteItem(); } else if (event.keyCode == 13) { //ENTER this.validateCurrentAutoCompleteItem(); this.$el.focus(); this.closeAutoComplete(); } else { this.lauchAutoComplete(event); this.openAutoComplete(); } }, lauchAutoComplete: function (event) { if (!this.options.autocomplete.minimumCharacters) { this.options.autocomplete.minimumCharacters = 3; } var tosearch = event.currentTarget.value; if (tosearch.length >= this.options.autocomplete.minimumCharacters) { if (!this.options.autocomplete.url) { this.options.autocomplete.url = "autoComplete.fl"; } //Immediately call the web service in order to fill the model var url = edApplication().path + this.options.autocomplete.url; var data = { search: tosearch }; data.objname = this.options.autocomplete.objname; data.searchtype = this.options.autocomplete.searchtype; if (this.options.autocomplete.query) { data.query = ""; if($.type(this.options.autocomplete.query) == "string") { data.query = this.options.autocomplete.query; }else { for (var i=0; i 0) { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length == 0) { autoCompleteEl = $(""); autoCompleteEl.width(this.$el.outerWidth()); if (this.options.autocompletepanelclass) { autoCompleteEl.addClass(this.options.autocompletepanelclass); } $("body").append(autoCompleteEl); autoCompleteEl.position({ my: "left top", at: "left bottom", of: this.$el }); } else { autoCompleteEl.html(""); } autoCompleteEl.on("mouseenter", function() { $(this).attr("ismouseovered", "true"); }); autoCompleteEl.on("mouseleave", function() { $(this).attr("ismouseovered", "false"); }); autoCompleteEl.append(""); var resultUl = $("ul", autoCompleteEl); var anItem; for (var i=0; i" + jQuery.trim(results[i].label) + ""); anItem.on("click", {view: this}, function(event){ event.data.view.selectAnAutoCompleteItem($(this)); event.data.view.validateCurrentAutoCompleteItem(); event.data.view.$el.focus(); event.data.view.closeAutoComplete(); }); resultUl.append(anItem); } } if (!this.options.actions.add.authorizedouble) { if (this.options.parentview) { if (this.options.parentview.options.ismultivalued) { this.options.parentview.model.each(function(modelItem) { $("li[val='" + modelItem.get("value") + "']", autoCompleteEl).remove(); }); } } } var liList = $("li", autoCompleteEl); if (liList.length == 1) { //if there is only one possible value left, preselect it this.selectAnAutoCompleteItem($(liList[0])); } if ($("li", autoCompleteEl).length > 0) { //this.openAutoComplete(); } else { this.resetAutoComplete(); } } else { this.resetAutoComplete(); } } } }, resetAutoComplete: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { autoCompleteEl.remove(); } }, closeAutoComplete: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { autoCompleteEl.attr("isopened", "false"); autoCompleteEl.slideUp(100); } }, openAutoComplete: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { autoCompleteEl.attr("isopened", "true"); autoCompleteEl.slideDown(100); } }, selectNextAutoCompleteItem: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { var currentSelectedItem = $("li[selected]", autoCompleteEl); if (currentSelectedItem.length > 0) { if (currentSelectedItem.next().length > 0) { currentSelectedItem.next().attr("selected", ""); currentSelectedItem.removeAttr("selected"); } else { currentSelectedItem.removeAttr("selected"); $("li", autoCompleteEl).first().attr("selected", ""); } } else { $("li", autoCompleteEl).first().attr("selected", ""); } if (($("li[selected]", autoCompleteEl).position().top + 20) > $("ul", autoCompleteEl).height() - $("ul", autoCompleteEl).scrollTop()) { $("ul", autoCompleteEl).scrollTop($("li[selected]", autoCompleteEl).position().top); } else { $("ul", autoCompleteEl).scrollTop(0); } } }, selectPreviousAutoCompleteItem: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { var currentSelectedItem = $("li[selected]", autoCompleteEl); if (currentSelectedItem.length > 0) { if (currentSelectedItem.prev().length > 0) { currentSelectedItem.prev().attr("selected", ""); currentSelectedItem.removeAttr("selected"); }else { currentSelectedItem.removeAttr("selected"); $("li", autoCompleteEl).last().attr("selected", ""); } } else { $("li", autoCompleteEl).last().attr("selected", ""); } if (($("li[selected]", autoCompleteEl).position().top + 20) > $("ul", autoCompleteEl).height() - $("ul", autoCompleteEl).scrollTop()) { $("ul", autoCompleteEl).scrollTop($("li[selected]", autoCompleteEl).position().top); } else { if ($("li[selected]", autoCompleteEl).position().top < 0) { $("ul", autoCompleteEl).scrollTop($("ul", autoCompleteEl).scrollTop() - $("ul", autoCompleteEl).height()); } } } }, validateCurrentAutoCompleteItem: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { var currentSelectedItem = $("li[selected]", autoCompleteEl); if (currentSelectedItem.length > 0) { this.$el.attr("val", currentSelectedItem.attr("val")); this.$el.val(currentSelectedItem.html()); this.$el.trigger("change"); this.trigger("onautocompleteitemselected"); } } }, selectAnAutoCompleteItem: function(el) { this.unselectAllAutoCompleteItems(); var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { el.attr("selected", true); } }, unselectAllAutoCompleteItems: function() { var autoCompleteEl = $("#autocomplete_" + this.id); if (autoCompleteEl.length > 0) { $("li[selected]", autoCompleteEl).removeAttr("selected"); } }, updateModelFromInput: function(event) { //console.log("updateModelFromInput", this.$el.val(), this.$el.attr("val")); if (this.$el.attr("val") != "") { this.model.set("label", this.$el.val()); this.model.set("value", this.$el.attr("val")); this.trigger("onmodelchange"); } }, setExcludeValues: function (_excludeValues) { if ($.type(_excludeValues)) this.excludeValues = _excludeValues; else this.excludeValues = [_excludeValues]; } }); Views.AutoCompleteList = Views.TextList.extend({ classType: "Views.AutoCompleteList", listenEvents: function() { //See Views.Base listenDefaultEvents() this.on("oninitialize", this.onInitialize); this.on("onremovemodelfromcollection ", function() { this.resetEl(); }); this.on("onrenderlist", function() { this.resetEl(); }); }, onInitialize: function() { if (!this.options.actions) { this.options.actions = {}; } if (!this.options.actions.add) { this.options.actions.add = {}; } if (!this.options.actions.remove) { this.options.actions.remove = {}; } if (!this.options.actions.remove.button) { this.options.actions.remove.button = {}; } this.options.actions.add.active = true; //this.options.actions.add.authorizedouble = true; this.options.actions.remove.active = true; this.options.actions.remove.button.position = { my: "right center", at: "right-15 center", collision: "none" }; if (!this.options.actions.sort) { this.options.actions.sort = {}; this.options.actions.sort.active = false; } if (!this.options.actions.sort.button) { this.options.actions.sort.button = {}; } this.options.actions.sort.button.position = { my: "left center", at: "left center", collision: "none" }; }, resetEl: function() { var that = this; var addAction = this.options.actions.add; var authorizedouble = addAction.authorizedouble; if (addAction.view) { addAction.view.resetAddEl(); } this.model.each(function(item) { var view = item.view; if (view) { if (!authorizedouble) { view.resetEl(); } view.$el.on("change", function(event) { that.resetEl(); }); } }); } });