cy
2022-06-22 425675051e544cf29b2132615cfbf7a93dc5e51f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(function($){
    var bigAutocomplete = new function(){
        this.currentInputText = null;//Ŀǰ»ñµÃ¹â±êµÄÊäÈë¿ò£¨½â¾öÒ»¸öÒ³Ãæ¶à¸öÊäÈë¿ò°ó¶¨×Ô¶¯²¹È«¹¦ÄÜ£©
        this.functionalKeyArray = [9,20,13,16,17,18,91,92,93,45,36,33,34,35,37,39,112,113,114,115,116,117,118,119,120,121,122,123,144,19,145,40,38,27];//¼üÅÌÉϹ¦Äܼü¼üÖµÊý×é
        this.holdText = null;//ÊäÈë¿òÖÐԭʼÊäÈëµÄÄÚÈÝ
        
        //³õʼ»¯²åÈë×Ô¶¯²¹È«div£¬²¢ÔÚdocument×¢²ámousedown£¬µã»÷·ÇdivÇøÓòÒþ²Ødiv
        this.init = function(){
            $("body").append("<div id='bigAutocompleteContent' class='bigautocomplete-layout'></div>");
            $(document).bind('mousedown',function(event){
                var $target = $(event.target);
                if((!($target.parents().andSelf().is('#bigAutocompleteContent'))) && (!$target.is(bigAutocomplete.currentInputText))){
                    bigAutocomplete.hideAutocomplete();
                }
            })
            
            //Êó±êÐüͣʱѡÖе±Ç°ÐÐ
            $("#bigAutocompleteContent").delegate("tr", "mouseover", function() {
                $("#bigAutocompleteContent tr").removeClass("ct");
                $(this).addClass("ct");
            }).delegate("tr", "mouseout", function() {
                $("#bigAutocompleteContent tr").removeClass("ct");
            });        
            
            
            //µ¥»÷Ñ¡ÖÐÐкó£¬Ñ¡ÖÐÐÐÄÚÈÝÉèÖõ½ÊäÈë¿òÖУ¬²¢Ö´ÐÐcallbackº¯Êý
            $("#bigAutocompleteContent").delegate("tr", "click", function() {
                bigAutocomplete.currentInputText.val( $(this).find("div:last").html());
                var callback_ = bigAutocomplete.currentInputText.data("config").callback;
                if($("#bigAutocompleteContent").css("display") != "none" && callback_ && $.isFunction(callback_)){
                    callback_($(this).data("jsonData"));
                    
                }                
                bigAutocomplete.hideAutocomplete();
            })            
            
        }
        
        this.autocomplete = function(param){
            
            if($("body").length > 0 && $("#bigAutocompleteContent").length <= 0){
                bigAutocomplete.init();//³õʼ»¯ÐÅÏ¢
            }            
            
            var $this = $(this);//Ϊ°ó¶¨×Ô¶¯²¹È«¹¦ÄܵÄÊäÈë¿òjquery¶ÔÏó
            
            var $bigAutocompleteContent = $("#bigAutocompleteContent");
            
            this.config = {
                           //width:ÏÂÀ­¿òµÄ¿í¶È£¬Ä¬ÈÏʹÓÃÊäÈë¿ò¿í¶È
                           width:$this.outerWidth() - 2,
                           //url£º¸ñʽurl:""ÓÃÀ´ajaxºǫ́»ñÈ¡Êý¾Ý£¬·µ»ØµÄÊý¾Ý¸ñʽΪdata²ÎÊýÒ»Ñù
                           url:null,
                           /*data£º¸ñʽ{data:[{title:null,result:{}},{title:null,result:{}}]}
                           urlºÍdata²ÎÊýÖ»ÓÐÒ»¸öÉúЧ£¬dataÓÅÏÈ*/
                           data:null,
                           //callback£ºÑ¡ÖÐÐк󰴻سµ»òµ¥»÷ʱ»Øµ÷µÄº¯Êý
                           callback:null};
            $.extend(this.config,param);
            
            $this.data("config",this.config);
            
            //ÊäÈë¿òkeydownʼþ
            $this.keydown(function(event) {
                switch (event.keyCode) {
                case 40://Ïòϼü
                    
                    if($bigAutocompleteContent.css("display") == "none")return;
                    
                    var $nextSiblingTr = $bigAutocompleteContent.find(".ct");
                    if($nextSiblingTr.length <= 0){//ûÓÐÑ¡ÖÐÐÐʱ£¬Ñ¡ÖеÚÒ»ÐÐ
                        $nextSiblingTr = $bigAutocompleteContent.find("tr:first");
                    }else{
                        $nextSiblingTr = $nextSiblingTr.next();
                    }
                    $bigAutocompleteContent.find("tr").removeClass("ct");
                    
                    if($nextSiblingTr.length > 0){//ÓÐÏÂÒ»ÐÐʱ£¨²»ÊÇ×îºóÒ»ÐУ©
                        $nextSiblingTr.addClass("ct");//Ñ¡ÖеÄÐмӱ³¾°
                        $this.val($nextSiblingTr.find("div:last").html());//Ñ¡ÖÐÐÐÄÚÈÝÉèÖõ½ÊäÈë¿òÖÐ
                        
                        //div¹ö¶¯µ½Ñ¡ÖеÄÐÐ,jquery-1.6.1 $nextSiblingTr.offset().top ÓÐbug£¬ÊýÖµÓÐÎÊÌâ
                        $bigAutocompleteContent.scrollTop($nextSiblingTr[0].offsetTop - $bigAutocompleteContent.height() + $nextSiblingTr.height() );
                        
                    }else{
                        $this.val(bigAutocomplete.holdText);//ÊäÈë¿òÏÔʾÓû§Ô­Ê¼ÊäÈëµÄÖµ
                    }
                    
                    
                    break;
                case 38://ÏòÉϼü
                    if($bigAutocompleteContent.css("display") == "none")return;
                    
                    var $previousSiblingTr = $bigAutocompleteContent.find(".ct");
                    if($previousSiblingTr.length <= 0){//ûÓÐÑ¡ÖÐÐÐʱ£¬Ñ¡ÖÐ×îºóÒ»ÐÐÐÐ
                        $previousSiblingTr = $bigAutocompleteContent.find("tr:last");
                    }else{
                        $previousSiblingTr = $previousSiblingTr.prev();
                    }
                    $bigAutocompleteContent.find("tr").removeClass("ct");
                    
                    if($previousSiblingTr.length > 0){//ÓÐÉÏÒ»ÐÐʱ£¨²»ÊǵÚÒ»ÐУ©
                        $previousSiblingTr.addClass("ct");//Ñ¡ÖеÄÐмӱ³¾°
                        $this.val($previousSiblingTr.find("div:last").html());//Ñ¡ÖÐÐÐÄÚÈÝÉèÖõ½ÊäÈë¿òÖÐ
                        
                        //div¹ö¶¯µ½Ñ¡ÖеÄÐÐ,jquery-1.6.1 $$previousSiblingTr.offset().top ÓÐbug£¬ÊýÖµÓÐÎÊÌâ
                        $bigAutocompleteContent.scrollTop($previousSiblingTr[0].offsetTop - $bigAutocompleteContent.height() + $previousSiblingTr.height());
                    }else{
                        $this.val(bigAutocomplete.holdText);//ÊäÈë¿òÏÔʾÓû§Ô­Ê¼ÊäÈëµÄÖµ
                    }
                    
                    break;
                case 27://ESC¼üÒþ²ØÏÂÀ­¿ò
                    
                    bigAutocomplete.hideAutocomplete();
                    break;
                }
            });        
            
            //ÊäÈë¿òkeyupʼþ
            $this.keyup(function(event) {
                var k = event.keyCode;
                var ctrl = event.ctrlKey;
                var isFunctionalKey = false;//°´ÏµļüÊÇ·ñÊǹ¦Äܼü
                for(var i=0;i<bigAutocomplete.functionalKeyArray.length;i++){
                    if(k == bigAutocomplete.functionalKeyArray[i]){
                        isFunctionalKey = true;
                        break;
                    }
                }
                //k¼üÖµ²»Êǹ¦Äܼü»òÊÇctrl+c¡¢ctrl+xʱ²Å´¥·¢×Ô¶¯²¹È«¹¦ÄÜ
                if(!isFunctionalKey && (!ctrl || (ctrl && k == 67) || (ctrl && k == 88)) ){
                    var config = $this.data("config");
                    
                    var offset = $this.offset();
                    $bigAutocompleteContent.width(config.width);
                    var h = $this.outerHeight() - 1;
                    $bigAutocompleteContent.css({"top":offset.top + h,"left":offset.left});
                    
                    var data = config.data;
                    var url = config.url;
                    var keyword_ = $.trim($this.val());
                    if(keyword_ == null || keyword_ == ""){
                        bigAutocomplete.hideAutocomplete();
                        return;
                    }                    
                    if(data != null && $.isArray(data) ){
                        var data_ = new Array();
                        for(var i=0;i<data.length;i++){
                            if(data[i].title.indexOf(keyword_) > -1){
                                data_.push(data[i]);
                            }
                        }
                        
                        makeContAndShow(data_);
                    }else if(url != null && url != ""){//ajaxÇëÇóÊý¾Ý
                        $.post(url,{keyword:keyword_},function(result){
                            makeContAndShow(result.data)
                        },"json")
                    }
 
                    
                    bigAutocomplete.holdText = $this.val();
                }
                //»Ø³µ¼ü
                if(k == 13){
                    var callback_ = $this.data("config").callback;
                    if($bigAutocompleteContent.css("display") != "none"){
                        if(callback_ && $.isFunction(callback_)){
                            callback_($bigAutocompleteContent.find(".ct").data("jsonData"));
                        }
                        $bigAutocompleteContent.hide();                        
                    }
                }
                
            });    
            
                    
            //×é×°ÏÂÀ­¿òhtmlÄÚÈݲ¢ÏÔʾ
            function makeContAndShow(data_){
                if(data_ == null || data_.length <=0 ){
                    return;
                }
                
                var cont = "<table><tbody>";
                for(var i=0;i<data_.length;i++){
                    cont += "<tr><td><div>" + data_[i].title + "</div></td></tr>"
                }
                cont += "</tbody></table>";
                $bigAutocompleteContent.html(cont);
                $bigAutocompleteContent.show();
                
                //ÿÐÐtr°ó¶¨Êý¾Ý£¬·µ»Ø¸ø»Øµ÷º¯Êý
                $bigAutocompleteContent.find("tr").each(function(index){
                    $(this).data("jsonData",data_[index]);
                })
            }            
                    
            
            //ÊäÈë¿òfocusʼþ
            $this.focus(function(){
                bigAutocomplete.currentInputText = $this;
            });
            
        }
        //Òþ²ØÏÂÀ­¿ò
        this.hideAutocomplete = function(){
            var $bigAutocompleteContent = $("#bigAutocompleteContent");
            if($bigAutocompleteContent.css("display") != "none"){
                $bigAutocompleteContent.find("tr").removeClass("ct");
                $bigAutocompleteContent.hide();
            }            
        }
        
    };
    
    
    $.fn.bigAutocomplete = bigAutocomplete.autocomplete;
    
})(jQuery)