123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- /**
- * jQuery Plugin for creating AJAX auto-suggest textfield
- * @version 2.1
- * @requires jQuery 1.4 or later
- *
- * Copyright (c) 2011 Lucky
- * Licensed under the GPL license:
- * http://www.gnu.org/licenses/gpl.html
- */
- (function($) {
- function autosuggest(callBackUrl, textField){
- this.divId="suggestions_holder";
- this.hovered=false;
- this.arrData=null;
- this.textField=textField;
- this.callBackUrl=callBackUrl;
- var width=this.textField.width() + 3;
- var minChars=1;
- var currRow=0;
- var suggestRow="suggest_row";
- var suggestItem="suggest_item";
- this.idField=null;
- this.submitOnSelect=false;
- this.thumbnail=false;
- this.description=false;
- this.textField.after('<div class="suggestions" id="' + this.divId + '"></div>');
- this.textField.attr("autocomplete", "off");
- this.holder=this.textField.next("#" + this.divId);
- this.holder.hide();
- this.onSelected=null;
- var me=this;
- this.textField.keyup(
- function(e){
- if(e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40 && e.keyCode!=13){
- if($(this).val().length>=minChars){
- $.ajax({
- url:me.callBackUrl + encodeURI($(this).val()),
- success:function(data){
- try{
- me.arrData=$.parseJSON(data);
- var arr=me.arrData;
- var html="";
- currRow=0;
- if(arr==null){
- me.hide();
- }
- else{
- if(arr.length>0){
- for(i=0;i<arr.length;i++){
- cssClass=suggestItem;
- if(i==0){
- cssClass+=" first";
- }
- if(i==(arr.length-1)){
- cssClass+=" last";
- }
- var id_field='';
- if(me.idField!=null){
- id_field=' id_field="' + arr[i].id + '"';
- }
- var thumb="";
- if(me.thumbnail==true){
- var style="";
- if(arr[i].thumbnail!=undefined){
- style=' style="background-image:url(' + arr[i].thumbnail + ');"';
- }
- thumb='<div class="thumbnail"' + style + '></div>';
- }
- var desc="";
- if(me.description==true){
- if(arr[i].description!=undefined){
- desc='<div class="description">' + arr[i].description + '</div>';
- }
- }
- html+='<div id="' + suggestRow + (i+1) + '" class="' + cssClass + '"' + id_field + ' seq_id="' + i + '" >' + thumb + '<div class="suggestion_title">' + arr[i].data.replace(new RegExp('(' + me.textField.val() + ')', 'gi'), "<b>$1</b>") + '</div>' + desc + '</div>';
- }
- me.holder.html(html);
- for(i=1;i<=arr.length;i++){
- var target=me.holder.find("#" + suggestRow + i);
- target.mouseover(function(e){
- me.hovered=true;
- me.unSelectAll(this);
- $(this).addClass("selected");
- });
- target.mouseout(function(e){
- me.hovered=false;
- $(this).removeClass("selected");
- });
- target.click(function(e){
- me.textField.val($(this).find(".suggestion_title").text());
- if(me.idField!=null){
- me.idField.val($(this).attr("id_field"));
- }
- // Callback function
- if(me.onSelected!=null){
- me.onSelected.call(this, me.arrData[$(this).attr("seq_id")]);
- }
- if(me.submitOnSelect==true){
- $("form").has(me.textField).submit();
- }
- me.hide();
- });
- }
- me.show(me.holder.find("." + suggestItem).height() * arr.length);
- }
- else{
- me.hide();
- }
- }
- }
- catch(e){
- console.log('Sorry, an error has occured!');
- }
- },
- error: function(xhr, status, ex){
- console.log('Sorry, an error has occured!');
- }
- });
- }
- else{
- me.hide();
- }
- }
- else{
- if(me.holder.css("display")!="none"){
- checkKey(e);
- }
- else{
- // Callback function
- if(me.onSelected!=null){
- me.onSelected.call(this, null);
- }
- }
- }
- }
- );
- this.textField.bind(
- "blur",
- function(e){
- if(me.idField!=null){
- if(me.checkSelected(me.textField.val())==false){
- me.textField.val("");
- me.idField.val("");
- }
- }
- if(me.hovered==false){
- me.hide();
- }
- else{
- me.hovered=false;
- }
- }
- );
- this.show=function(height){
- this.holder.css({
- "position":"absolute",
- "left":this.textField.position().left + "px",
- "top":this.textField.position().top + this.textField.height() + 5 + "px",
- "z-index":1000,
- "height":height + "px"
- });
- this.holder.css({
- "width":width + "px"
- });
- this.holder.find("." + suggestItem).css({
- "width":width + "px",
- "overflow":"hidden"
- });
- this.holder.show();
- }
- this.hide=function(){
- this.holder.hide();
- }
- this.unSelectAll=function(div){
- var id=$(div).attr("id");
- var rows=this.holder.find("." + suggestItem).get().length;
- for(i=1;i<=rows;i++){
- this.holder.find("#" + suggestRow + i).removeClass("selected");
- }
- currRow=parseInt(id.replace(suggestRow, ""));
- var rgx=/^[0-9]+$/;
- if(!rgx.test(currRow)){
- currRow=0;
- }
- }
- this.setWidth=function(w){
- width=w;
- }
- this.setMinChars=function(c){
- minChars=c;
- }
- this.preventEnter=function(){
- this.textField.keypress(
- function(e){
- if(e.keyCode==13){
- return false;
- }
- return true;
- }
- );
- }
- this.checkSelected=function(data){
- if(this.arrData!=null){
- for(var i=0;i<this.arrData.length;i++){
- if(this.arrData[i].data==data){
- return true;
- }
- }
- }
- return false;
- }
- function checkKey(e){
- if(me.holder.css("display")!="none"){
- var rows=me.holder.find("." + suggestItem).get().length;
- if(e.keyCode==40){
- currRow++;
- if(currRow<=rows){
- if(currRow>0){
- me.holder.find("#" + suggestRow + (currRow-1)).removeClass("selected");
- }
- var target=me.holder.find("#" + suggestRow + currRow);
- target.addClass("selected");
- me.textField.val(target.find(".suggestion_title").text());
- if(me.idField!=null){
- me.idField.val(target.attr("id_field"));
- }
- }
- else{
- currRow=rows;
- }
- }
- else if(e.keyCode==38){
- currRow--;
- if(currRow>0){
- if(currRow<rows){
- me.holder.find("#" + suggestRow + (currRow+1)).removeClass("selected");
- }
- var target=me.holder.find("#" + suggestRow + currRow);
- target.addClass("selected");
- me.textField.val(target.find(".suggestion_title").text());
- if(me.idField!=null){
- me.idField.val(target.attr("id_field"));
- }
- }
- else{
- currRow=1;
- }
- }
- else if(e.keyCode==13){
- if(me.idField!=null){
- if(me.checkSelected(me.textField.val())==false){
- me.textField.val("");
- me.idField.val("");
- }
- }
- // Callback function
- if(me.onSelected!=null){
- if(currRow>0){
- me.onSelected.call(this, me.arrData[currRow-1]);
- }
- else{
- me.onSelected.call(this, null);
- }
- }
- me.hide();
- }
- }
- else{
- // Callback function
- if(me.onSelected!=null){
- me.onSelected.call(this, null);
- }
- }
- return true;
- }
- }
- $.fn.coolautosuggest = function(options) {
- var settings = {
- width: null,
- minChars: null,
- idField: null,
- submitOnSelect: false,
- showThumbnail : false,
- showDescription : false,
- onSelected : null
- };
- $.extend(settings, options);
- return this.each(function() {
- var obj = new autosuggest(settings.url, $(this));
- if(settings.width!=null){
- obj.setWidth(settings.width);
- }
- if(settings.minChars!=null){
- obj.setMinChars(settings.minChars);
- }
- if(settings.idField!=null){
- obj.idField=settings.idField;
- obj.preventEnter();
- }
- if(settings.submitOnSelect==true){
- obj.submitOnSelect=true;
- }
- else{
- obj.preventEnter();
- }
- if(settings.showThumbnail==true){
- obj.thumbnail=settings.showThumbnail;
- }
- if(settings.showDescription==true){
- obj.description=settings.showDescription;
- }
- /*if(obj.idField!=null){
- if(obj.checkSelected(obj.textField.val())==false){
- obj.textField.val("");
- obj.idField.val("");
- }
- }*/
- if($.isFunction(settings.onSelected)==true){
- obj.onSelected=settings.onSelected;
- }
- });
- }
- })(jQuery);
|