$.use(["jQuery", "form", "doc", "util", "dropdown", "code"], function($, form, doc, util, dd, code) { var m_rd = "必选的", modelName = 'typeahead', placeholder = "placeholder", required = "required", typeahead = function() { this.ve = null; this.minLength = 1; this.maxShowCount = 10; this.keyPressInterval = 500; this.caption = ""; this.autoselect = true; this.ajaxCfg={mask:false}; }; typeahead.prototype = defCfg = {}; defCfg.source = function(query, hand) { // console.log("source:" + query) var self = this; this.lastQuerying = query; setTimeout(function() { if((query === self.lastQuerying) && (query !== self.lastQueryed)) { self.doQuery(query, hand); } }, this.keyPressInterval || 500); }; defCfg.doQuery = function(query, hand) { var self = this; this.lastQueryed = query; // console.log("doQuery:" + query); util.get(this.quri + encodeURIComponent(query), null, function(data) { if(self.lastQueryed === query) { if(self.transfer) { data = self.transfer(data); } // console.log("doHand:" + query); hand(data); } }, this.ajaxEH, this.ajaxCfg); }; defCfg.blur = function() { var self = this; setTimeout(function() { self.hide(); }, 150); }; defCfg.keydown = function(e) { this.suppressKeyPressRepeat = [40, 38, 9, 13, 27].indexOf(e.keyCode) >= 0; this.move(e); }; defCfg.move = function(e) { if(!this.shown) return; switch(e.keyCode) { case 9: // tab case 13: // enter case 27: // escape e.preventDefault(); break; case 38: // up arrow e.preventDefault(); this.prev(); break; case 40: // down arrow e.preventDefault(); this.next(); break; } e.stopPropagation(); }; defCfg.keypress = function(e) { if(this.suppressKeyPressRepeat) return; this.move(e); }; defCfg.keyup = function(e) { var self = this; switch(e.keyCode) { case 40: // down arrow case 38: // up arrow break; case 9: // tab case 13: // enter if(!self.shown) return; self.select(); break; case 27: // escape if(!self.shown) return; self.hide(); break; default: self.lookup(); } e.stopPropagation(); e.preventDefault(); }; defCfg.click = function(e) { e.stopPropagation(); e.preventDefault(); this.select(); }; defCfg.mouseenter = function(e) { this.ctn.find('.active').removeClass('active'); $(e.currentTarget).addClass('active'); }; defCfg.prev = function(e) { var active = this.ctn.find('.active').removeClass('active'); var prev = active.prev(); if(!prev.length) { prev = this.ctn.find('li').last(); } prev.addClass('active'); }; defCfg.next = function(e) { var active = this.ctn.find('.active').removeClass('active'); var next = active.next(); if(!next.length) { next = this.ctn.find('li').first(); } next.addClass('active'); }; defCfg.render = function(items) { var lis = []; items.forEach(function(item) { var pc = String(item.code || item); var pt = String(item.caption || item); lis.push({ tn: "li", attrs: [{ an: "code", av: pt }, { an: "caption", av: pt }, { an: "class", av: "select-item" }], chs: [pt] }); }); if(this.autoselect && lis.length) { lis[0].attrs[2].av = " active select-item"; } this.menu.empty(); util.appendChild(this.menu[0], lis); return this; }; defCfg.lookup = function(event) { var self = this; self.query = self.ve.val(); if(!self.query || self.query.length < self.minLength) { return self.shown ? self.hide() : self } if(self.source instanceof Function) { self.source(self.query, function() { self.process.apply(self, arguments); }); } else { self.process(self.source); } return self; }; defCfg.process = function(items) { if(items && items.length) { this.render(items.slice(0, this.maxShowCount)); this.show(); } }; defCfg.show = function() { this.shown = true; this.ctn.addClass("open"); }; defCfg.hide = function() { this.shown = false; this.ctn.removeClass("open"); var v = this.ve.val(); if(v) { if(v !== this.caption) { this.ve.val(this.caption); } } else { this.rv = ""; this.caption = ""; } this.lastQuerying = null; this.lastQueryed = null; }; defCfg.select = function() { if(this.shown) { var item = this.menu.find(".active"); this.rv = item.attr('code'); this.caption = item.attr("caption"); this.ve.val(this.caption); return this.hide(); } }; defCfg.init = function($e) { var self = this; this.ctn = $e; var ve = $e.children(".dd-drop"); if(ve.length && ve.children("ul").length === 1) { var rc = code.parseCode(ve); rc.empty(); this.render = function(items) { rc.val(items); self.menu=self.ctn.find(".dd-drop>ul"); } } else { $("