socialforge/public/assets/kindeditor/at/jquery.atwho.min.js

1 line
19 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("jquery")):b(jQuery)}(this,function(a){var b,c,d,e,f,g,h,i,j,k,l=[].slice,m=function(a,b){function c(){this.constructor=a}for(var d in b)n.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a},n={}.hasOwnProperty;b=a,d=function(){function a(a){this.currentFlag=null,this.controllers={},this.aliasMaps={},this.$inputor=b(a),this.setupRootElement(),this.listen()}return a.prototype.createContainer=function(a){var c;return null!=(c=this.$el)&&c.remove(),b(a.body).append(this.$el=b("<div class='atwho-container'></div>"))},a.prototype.setupRootElement=function(a,c){var d;if(null==c&&(c=!1),a)this.window=a.contentWindow,this.document=a.contentDocument||this.window.document,this.iframe=a;else{this.document=this.$inputor[0].ownerDocument,this.window=this.document.defaultView||this.document.parentWindow;try{this.iframe=this.window.frameElement}catch(e){if(d=e,this.iframe=null,b.fn.atwho.debug)throw new Error("iframe auto-discovery is failed.\nPlease use `setIframe` to set the target iframe manually.\n"+d)}}return this.createContainer((this.iframeAsRoot=c)?this.document:document)},a.prototype.controller=function(a){var b,c,d,e;if(this.aliasMaps[a])c=this.controllers[this.aliasMaps[a]];else{e=this.controllers;for(d in e)if(b=e[d],d===a){c=b;break}}return c?c:this.controllers[this.currentFlag]},a.prototype.setContextFor=function(a){return this.currentFlag=a,this},a.prototype.reg=function(a,b){var c,d;return d=(c=this.controllers)[a]||(c[a]=this.$inputor.is("[contentEditable]")?new g(this,a):new j(this,a)),b.alias&&(this.aliasMaps[b.alias]=a),d.init(b),this},a.prototype.listen=function(){return this.$inputor.on("compositionstart",function(a){return function(b){var c;return null!=(c=a.controller())&&c.view.hide(),a.isComposing=!0,null}}(this)).on("compositionend",function(a){return function(b){return a.isComposing=!1,null}}(this)).on("keyup.atwhoInner",function(a){return function(b){return a.onKeyup(b)}}(this)).on("keydown.atwhoInner",function(a){return function(b){return a.onKeydown(b)}}(this)).on("blur.atwhoInner",function(a){return function(b){var c;return(c=a.controller())?(c.expectedQueryCBId=null,c.view.hide(b,c.getOpt("displayTimeout"))):void 0}}(this)).on("click.atwhoInner",function(a){return function(b){return a.dispatch(b)}}(this)).on("scroll.atwhoInner",function(a){return function(){var b;return b=a.$inputor.scrollTop(),function(c){var d,e;return d=c.target.scrollTop,b!==d&&null!=(e=a.controller())&&e.view.hide(c),b=d,!0}}}(this)())},a.prototype.shutdown=function(){var a,b,c;c=this.controllers;for(a in c)b=c[a],b.destroy(),delete this.controllers[a];return this.$inputor.off(".atwhoInner"),this.$el.remove()},a.prototype.dispatch=function(a){var b,c,d,e;d=this.controllers,e=[];for(b in d)c=d[b],e.push(c.lookUp(a));return e},a.prototype.onKeyup=function(a){var c;switch(a.keyCode){case h.ESC:a.preventDefault(),null!=(c=this.controller())&&c.view.hide();break;case h.DOWN:case h.UP:case h.CTRL:case h.ENTER:b.noop();break;case h.P:case h.N:a.ctrlKey||this.dispatch(a);break;default:this.dispatch(a)}},a.prototype.onKeydown=function(a){var c,d;if(d=null!=(c=this.controller())?c.view:void 0,d&&d.visible())switch(a.keyCode){case h.ESC:a.preventDefault(),d.hide(a);break;case h.UP:a.preventDefault(),d.prev();break;case h.DOWN:a.preventDefault(),d.next();break;case h.P:if(!a.ctrlKey)return;a.preventDefault(),d.prev();break;case h.N:if(!a.ctrlKey)return;a.preventDefault(),d.next();break;case h.TAB:case h.ENTER:case h.SPACE:if(!d.visible())return;if(!this.controller().getOpt("spaceSelectsMatch")&&a.keyCode===h.SPACE)return;if(!this.controller().getOpt("tabSelectsMatch")&&a.keyCode===h.TAB)return;d.highlighted()?(a.preventDefault(),d.choose(a)):d.hide(a);break;default:b.noop()}},a}(),e=function(){function a(a,c){this.app=a,this.at=c,this.$inputor=this.app.$inputor,this.id=this.$inputor[0].id||this.uid(),this.expectedQueryCBId=null,this.setting=null,this.query=null,this.pos=0,this.range=null,0===(this.$el=b("#atwho-ground-"+this.id,this.app.$el)).length&&this.app.$el.append(this.$el=b("<div id='atwho-ground-"+this.id+"'></div>")),this.model=new i(this),this.view=new k(this)}return a.prototype.uid=function(){return(Math.random().toString(16)+"000000000").substr(2,8)+(new Date).getTime()},a.prototype.init=function(a){return this.setting=b.extend({},this.setting||b.fn.atwho["default"],a),this.view.init(),this.model.reload(this.setting.data)},a.prototype.destroy=function(){return this.trigger("beforeDestroy"),this.model.destroy(),this.view.destroy(),this.$el.remove()},a.prototype.callDefault=function(){var a,c,d;d=arguments[0],a=2<=arguments.length?l.call(arguments,1):[];try{return f[d].apply(this,a)}catch(e){return c=e,b.error(c+" Or maybe At.js doesn't have function "+d)}},a.prototype.trigger=function(a,b){var c,d;return null==b&&(b=[]),b.push(this),c=this.getOpt("alias"),d=c?a+"-"+c+".atwho":a+".atwho",this.$inputor.trigger(d,b)},a.prototype.callbacks=function(a){return this.getOpt("callbacks")[a]||f[a]},a.prototype.getOpt=function(a,b){var c;try{return this.setting[a]}catch(d){return c=d,null}},a.prototype.insertContentFor=function(a){var c,d;return d=this.getOpt("insertTpl"),c=b.extend({},a.data("item-data"),{"atwho-at":this.at}),this.callbacks("tplEval").call(this,d,c,"onInsert")},a.prototype.renderView=function(a){var b;return b=this.getOpt("searchKey"),a=this.callbacks("sorter").call(this,this.query.text,a.slice(0,1001),b),this.view.render(a.slice(0,this.getOpt("limit")))},a.arrayToDefaultHash=function(a){var c,d,e,f;if(!b.isArray(a))return a;for(f=[],c=0,e=a.length;e>c;c++)d=a[c],b.isPlainObject(d)?f.push(d):f.push({name:d});return f},a.prototype.lookUp=function(a){var b,c;if((!a||"click"!==a.type||this.getOpt("lookUpOnClick"))&&(!this.getOpt("suspendOnComposing")||!this.app.isComposing))return(b=this.catchQuery(a))?(this.app.setContextFor(this.at),(c=this.getOpt("delay"))?this._delayLookUp(b,c):this._lookUp(b),b):(this.expectedQueryCBId=null,b)},a.prototype._delayLookUp=function(a,b){var c,d;return c=Date.now?Date.now():(new Date).getTime(),this.previousCallTime||(this.previousCallTime=c),d=b-(c-this.previousCallTime),d>0&&b>d?(this.previousCallTime=c,this._stopDelayedCall(),this.delayedCallTimeout=setTimeout(function(b){return function(){return b.previousCallTime=0,b.delayedCallTimeout=null,b._lookUp(a)}}(this),b)):(this._stopDelayedCall(),this.previousCallTime!==c&&(this.previousCallTime=0),this._lookUp(a))},a.prototype._stopDelayedCall=function(){return this.delayedCallTimeout?(clearTimeout(this.delayedCallTimeout),this.delayedCallTimeout=null):void 0},a.prototype._generateQueryCBId=function(){return{}},a.prototype._lookUp=function(a){var c;return c=function(a,b){return a===this.expectedQueryCBId?b&&b.length>0?this.renderView(this.constructor.arrayToDefaultHash(b)):this.view.hide():void 0},this.expectedQueryCBId=this._generateQueryCBId(),this.model.query(a.text,b.proxy(c,this,this.expectedQueryCBId))},a}(),j=function(a){function c(){return c.__super__.constructor.apply(this,arguments)}return m(c,a),c.prototype.catchQuery=function(){var a,b,c,d,e,f,g;return b=this.$inputor.val(),a=this.$inputor.caret("pos",{iframe:this.app.iframe}),g=b.slice(0,a),e=this.callbacks("matcher").call(this,this.at,g,this.getOpt("startWithSpace")),d="string"==typeof e,d&&e.length<this.getOpt("minLen",0)?void 0:(d&&e.length<=this.getOpt("maxLen",20)?(f=a-e.length,c=f+e.length,this.pos=f,e={text:e,headPos:f,endPos:c},this.trigger("matched",[this.at,e.text])):(e=null,this.view.hide()),this.query=e)},c.prototype.rect=function(){var a,c,d;if(a=this.$inputor.caret("offset",this.pos-1,{iframe:this.app.iframe}))return this.app.iframe&&!this.app.iframeAsRoot&&(c=b(this.app.iframe).offset(),a.left+=c.left,a.top+=c.top),d=this.app.document.selection?0:2,{left:a.left,top:a.top,bottom:a.top+a.height+d}},c.prototype.insert=function(a,b){var c,d,e,f,g;return c=this.$inputor,d=c.val(),e=d.slice(0,Math.max(this.query.headPos-this.at.length,0)),f=""===(f=this.getOpt("suffix"))?f:f||" ",a+=f,g=""+e+a+d.slice(this.query.endPos||0),c.val(g),c.caret("pos",e.length+a.length,{iframe:this.app.iframe}),c.is(":focus")||c.focus(),c.change()},c}(e),g=function(a){function c(){return c.__super__.constructor.apply(this,arguments)}return m(c,a),c.prototype._getRange=function(){var a;return a=this.app.window.getSelection(),a.rangeCount>0?a.getRangeAt(0):void 0},c.prototype._setRange=function(a,c,d){return null==d&&(d=this._getRange()),d?(c=b(c)[0],"after"===a?(d.setEndAfter(c),d.setStartAfter(c)):(d.setEndBefore(c),d.setStartBefore(c)),d.collapse(!1),this._clearRange(d)):void 0},c.prototype._clearRange=function(a){var b;return null==a&&(a=this._getRange()),b=this.app.window.getSelection(),null==this.ctrl_a_pressed?(b.removeAllRanges(),b.addRange(a)):void 0},c.prototype._movingEvent=function(a){var b;return"click"===a.type||(b=a.which)===h.RIGHT||b===h.LEFT||b===h.UP||b===h.DOWN},c.prototype._unwrap=function(a){var c;return a=b(a).unwrap().get(0),(c=a.nextSibling)&&c.nodeValue&&(a.nodeValue+=c.nodeValue,b(c).remove()),a},c.prototype.catchQuery=function(a){var c,d,e,f,g,i,j,k,l,m,n,o;if((o=this._getRange())&&o.collapsed){if(a.which===h.ENTER)return(d=b(o.startContainer).closest(".atwho-query")).contents().unwrap(),d.is(":empty")&&d.remove(),(d=b(".atwho-query",this.app.document)).text(d.text()).contents().last().unwrap(),void this._clearRange();if(/firefox/i.test(navigator.userAgent)){if(b(o.startContainer).is(this.$inputor))return void this._clearRange();a.which===h.BACKSPACE&&o.startContainer.nodeType===document.ELEMENT_NODE&&(l=o.startOffset-1)>=0?(e=o.cloneRange(),e.setStart(o.startContainer,l),b(e.cloneContents()).contents().last().is(".atwho-inserted")&&(g=b(o.startContainer).contents().get(l),this._setRange("after",b(g).contents().last()))):a.which===h.LEFT&&o.startContainer.nodeType===document.TEXT_NODE&&(c=b(o.startContainer.previousSibling),c.is(".atwho-inserted")&&0===o.startOffset&&this._setRange("after",c.contents().last()))}if(b(o.startContainer).closest(".atwho-inserted").addClass("atwho-query").siblings().removeClass("atwho-query"),(d=b(".atwho-query",this.app.document)).length>0&&d.is(":empty")&&0===d.text().length&&d.remove(),this._movingEvent(a)||d.removeClass("atwho-inserted"),d.length>0)switch(a.which){case h.LEFT:return this._setRange("before",d.get(0),o),void d.removeClass("atwho-query");case h.RIGHT:return this._setRange("after",d.get(0).nextSibling,o),void d.removeClass("atwho-query")}if(d.length>0&&(n=d.attr("data-atwho-at-query"))&&(d.empty().html(n).attr("data-atwho-at-query",null),this._setRange("after",d.get(0),o)),e=o.cloneRange(),e.setStart(o.startContainer,0),k=this.callbacks("matcher").call(this,this.at,e.toString(),this.getOpt("startWithSpace")),i="string"==typeof k,0===d.length&&i&&(f=o.startOffset-this.at.length-k.length)>=0&&(o.setStart(o.startContainer,f),d=b("<span/>",this.app.document).attr(this.getOpt("editableAtwhoQueryAttrs")).addClass("atwho-query"),o.surroundContents(d.get(0)),j=d.contents().last().get(0),/firefox/i.test(navigator.userAgent)?(o.setStart(j,j.length),o.setEnd(j,j.length),this._clearRange(o)):this._setRange("after",j,o)),!(i&&k.length<this.getOpt("minLen",0)))return i&&k.length<=this.getOpt("maxLen",20)?(m={text:k,el:d},this.trigger("matched",[this.at,m.text]),this.query=m):(this.view.hide(),this.query={el:d},d.text().indexOf(this.at)>=0&&(this._movingEvent(a)&&d.hasClass("atwho-inserted")?d.removeClass("atwho-query"):!1!==this.callbacks("afterMatchFailed").call(this,this.at,d)&&this._setRange("after",this._unwrap(d.text(d.text()).contents().first()))),null)}},c.prototype.rect=function(){var a,c,d;return d=this.query.el.offset(),this.app.iframe&&!this.app.iframeAsRoot&&(c=(a=b(this.app.iframe)).offset(),d.left+=c.left-this.$inputor.scrollLeft(),d.top+=c.top-this.$inputor.scrollTop()),d.bottom=d.top+this.query.el.height(),d},c.prototype.insert=function(a,b){var c,d,e,f;return e=""===(e=this.getOpt("suffix"))?e:e||" ",c=b.data("item-data"),this.query.el.removeClass("atwho-query").addClass("atwho-inserted").html(a).attr("data-atwho-at-query",""+c["atwho-at"]+this.query.text),(d=this._getRange())&&(d.setEndAfter(this.query.el[0]),d.collapse(!1),d.insertNode(f=this.app.document.createTextNode(""+e)),this._setRange("after",f,d)),this.$inputor.is(":focus")||this.$inputor.focus(),this.$inputor.change()},c}(e),i=function(){function a(a){this.context=a,this.at=this.context.at,this.storage=this.context.$inputor}return a.prototype.destroy=function(){return this.storage.data(this.at,null)},a.prototype.saved=function(){return this.fetch()>0},a.prototype.query=function(a,b){var c,d,e;return d=this.fetch(),e=this.context.getOpt("searchKey"),d=this.context.callbacks("filter").call(this.context,a,d,e)||[],c=this.context.callbacks("remoteFilter"),d.length>0||!c&&0===d.length?b(d):c.call(this.context,a,b)},a.prototype.fetch=function(){return this.storage.data(this.at)||[]},a.prototype.save=function(a){return this.storage.data(this.at,this.context.callbacks("beforeSave").call(this.context,a||[]))},a.prototype.load=function(a){return!this.saved()&&a?this._load(a):void 0},a.prototype.reload=function(a){return this._load(a)},a.prototype._load=function(a){return"string"==typeof a?b.ajax(a,{dataType:"json"}).done(function(a){return function(b){return a.save(b)}}(this)):this.save(a)},a}(),k=function(){function a(a){this.context=a,this.$el=b("<div class='atwho-view'><ul class='atwho-view-ul'></ul></div>"),this.timeoutID=null,this.context.$el.append(this.$el),this.bindEvent()}return a.prototype.init=function(){var a;return a=this.context.getOpt("alias")||this.context.at.charCodeAt(0),this.$el.attr({id:"at-view-"+a})},a.prototype.destroy=function(){return this.$el.remove()},a.prototype.bindEvent=function(){var a;return a=this.$el.find("ul"),a.on("mouseenter.atwho-view","li",function(c){return a.find(".cur").removeClass("cur"),b(c.currentTarget).addClass("cur")}).on("click.atwho-view","li",function(c){return function(d){return a.find(".cur").removeClass("cur"),b(d.currentTarget).addClass("cur"),c.choose(d),d.preventDefault()}}(this))},a.prototype.visible=function(){return this.$el.is(":visible")},a.prototype.highlighted=function(){return this.$el.find(".cur").length>0},a.prototype.choose=function(a){var b,c;return(b=this.$el.find(".cur")).length&&(c=this.context.insertContentFor(b),this.context._stopDelayedCall(),this.context.insert(this.context.callbacks("beforeInsert").call(this.context,c,b),b),this.context.trigger("inserted",[b,a]),this.hide(a)),this.context.getOpt("hideWithoutSuffix")?this.stopShowing=!0:void 0},a.prototype.reposition=function(a){var c,d,e,f;return c=this.context.app.iframeAsRoot?this.context.app.window:window,a.bottom+this.$el.height()-b(c).scrollTop()>b(c).height()&&(a.bottom=a.top-this.$el.height()),a.left>(e=b(c).width()-this.$el.width()-5)&&(a.left=e),d={left:a.left,top:a.bottom},null!=(f=this.context.callbacks("beforeReposition"))&&f.call(this.context,d),this.$el.offset(d),this.context.trigger("reposition",[d])},a.prototype.next=function(){var a,b;return a=this.$el.find(".cur").removeClass("cur"),b=a.next(),b.length||(b=this.$el.find("li:first")),b.addClass("cur"),this.scrollTop(Math.max(0,a.innerHeight()*(b.index()+2)-this.$el.height()))},a.prototype.prev=function(){var a,b;return a=this.$el.find(".cur").removeClass("cur"),b=a.prev(),b.length||(b=this.$el.find("li:last")),b.addClass("cur"),this.scrollTop(Math.max(0,a.innerHeight()*(b.index()+2)-this.$el.height()))},a.prototype.scrollTop=function(a){var b;return b=this.context.getOpt("scrollDuration"),b?this.$el.animate({scrollTop:a},b):this.$el.scrollTop(a)},a.prototype.show=function(){var a;return this.stopShowing?void(this.stopShowing=!1):(this.visible()||(this.$el.show(),this.$el.scrollTop(0),this.context.trigger("shown")),(a=this.context.rect())?this.reposition(a):void 0)},a.prototype.hide=function(a,b){var c;if(this.visible())return isNaN(b)?(this.$el.hide(),this.context.trigger("hidden",[a])):(c=function(a){return function(){return a.hide()}}(this),clearTimeout(this.timeoutID),this.timeoutID=setTimeout(c,b))},a.prototype.render=function(a){var c,d,e,f,g,h,i;if(!(b.isArray(a)&&a.length>0))return void this.hide();for(this.$el.find("ul").empty(),d=this.$el.find("ul"),i=this.context.getOpt("displayTpl"),e=0,g=a.length;g>e;e++)f=a[e],f=b.extend({},f,{"atwho-at":this.context.at}),h=this.context.callbacks("tplEval").call(this.context,i,f,"onDisplay"),c=b(this.context.callbacks("highlighter").call(this.context,h,this.context.query.text)),c.data("item-data",f),d.append(c);return this.show(),this.context.getOpt("highlightFirst")?d.find("li:first").addClass("cur"):void 0},a}(),h={DOWN:40,UP:38,ESC:27,TAB:9,ENTER:13,CTRL:17,A:65,P:80,N:78,LEFT:37,UP:38,RIGHT:39,DOWN:40,BACKSPACE:8,SPACE:32},f={beforeSave:function(a){return e.arrayToDefaultHash(a)},matcher:function(a,b,c,d){var e,f,g,h,i;return a=a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),c&&(a="(?:^|\\s)"+a),e=decodeURI("%C3%80"),f=decodeURI("%C3%BF"),i=d?" ":"",h=new RegExp(a+"([A-Za-z"+e+"-"+f+"0-9_"+i+"'.+-]*)$|"+a+"([^\\x00-\\xff]*)$","gi"),g=h.exec(b),g?g[2]||g[1]:null},filter:function(a,b,c){var d,e,f,g;for(d=[],e=0,g=b.length;g>e;e++)f=b[e],~new String(f[c]).toLowerCase().indexOf(a.toLowerCase())&&d.push(f);return d},remoteFilter:null,sorter:function(a,b,c){var d,e,f,g;if(!a)return b;for(d=[],e=0,g=b.length;g>e;e++)f=b[e],f.atwho_order=new String(f[c]).toLowerCase().indexOf(a.toLowerCase()),f.atwho_order>-1&&d.push(f);return d.sort(function(a,b){return a.atwho_order-b.atwho_order})},tplEval:function(a,b){var c,d;d=a;try{return"string"!=typeof a&&(d=a(b)),d.replace(/\$\{([^\}]*)\}/g,function(a,c,d){return b[c]})}catch(e){return c=e,""}},highlighter:function(a,b){var c;return b?(c=new RegExp(">\\s*(\\w*?)("+b.replace("+","\\+")+")(\\w*)\\s*<","ig"),a.replace(c,function(a,b,c,d){return"> "+b+"<strong>"+c+"</strong>"+d+" <"})):a},beforeInsert:function(a,b){return a},beforeReposition:function(a){return a},afterMatchFailed:function(a,b){}},c={load:function(a,b){var c;return(c=this.controller(a))?c.model.load(b):void 0},isSelecting:function(){var a;return!!(null!=(a=this.controller())?a.view.visible():void 0)},hide:function(){var a;return null!=(a=this.controller())?a.view.hide():void 0},reposition:function(){var a;return(a=this.controller())?a.view.reposition(a.rect()):void 0},setIframe:function(a,b){return this.setupRootElement(a,b),null},run:function(){return this.dispatch()},destroy:function(){return this.shutdown(),this.$inputor.data("atwho",null)}},b.fn.atwho=function(a){var e,f;return e=arguments,f=null,this.filter('textarea, input, [contenteditable=""], [contenteditable=true]').each(function(){var g,h;return(h=(g=b(this)).data("atwho"))||g.data("atwho",h=new d(this)),"object"!=typeof a&&a?c[a]&&h?f=c[a].apply(h,Array.prototype.slice.call(e,1)):b.error("Method "+a+" does not exist on jQuery.atwho"):h.reg(a.at,a)}),null!=f?f:this},b.fn.atwho["default"]={at:void 0,alias:void 0,data:null,displayTpl:"<li>${name}</li>",insertTpl:"${atwho-at}${name}",callbacks:f,searchKey:"name",suffix:void 0,hideWithoutSuffix:!1,startWithSpace:!0,highlightFirst:!0,limit:5,maxLen:20,minLen:0,displayTimeout:300,delay:null,spaceSelectsMatch:!1,tabSelectsMatch:!0,editableAtwhoQueryAttrs:{},scrollDuration:150,suspendOnComposing:!0,lookUpOnClick:!0},b.fn.atwho.debug=!1});