1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/public/js/tinymce-mentions-plugin.js
2019-01-30 22:25:07 +11:00

1 line
6.6 KiB
JavaScript

!function(a,b){"use strict";var c=function(a,c){this.editor=a,this.options=b.extend({},{source:[],delay:500,queryBy:"name",items:10},c),this.matcher=this.options.matcher||this.matcher,this.renderDropdown=this.options.renderDropdown||this.renderDropdown,this.render=this.options.render||this.render,this.insert=this.options.insert||this.insert,this.highlighter=this.options.highlighter||this.highlighter,this.query="",this.hasFocus=!0,this.renderInput(),this.bindEvents()};c.prototype={constructor:c,renderInput:function(){var a='<span id="autocomplete"><span id="autocomplete-delimiter">'+this.options.delimiter+"</span>"+'<span id="autocomplete-searchtext"><span class="dummy">\ufeff</span></span>'+"</span>";this.editor.execCommand("mceInsertContent",!1,a),this.editor.focus(),this.editor.selection.select(this.editor.selection.dom.select("span#autocomplete-searchtext span")[0]),this.editor.selection.collapse(0)},bindEvents:function(){this.editor.on("keyup",this.editorKeyUpProxy=b.proxy(this.rteKeyUp,this)),this.editor.on("keydown",this.editorKeyDownProxy=b.proxy(this.rteKeyDown,this),!0),this.editor.on("click",this.editorClickProxy=b.proxy(this.rteClicked,this)),b("body").on("click",this.bodyClickProxy=b.proxy(this.rteLostFocus,this)),b(this.editor.getWin()).on("scroll",this.rteScroll=b.proxy(function(){this.cleanUp(!0)},this))},unbindEvents:function(){this.editor.off("keyup",this.editorKeyUpProxy),this.editor.off("keydown",this.editorKeyDownProxy),this.editor.off("click",this.editorClickProxy),b("body").off("click",this.bodyClickProxy),b(this.editor.getWin()).off("scroll",this.rteScroll)},rteKeyUp:function(a){switch(a.which||a.keyCode){case 40:case 38:case 16:case 17:case 18:break;case 8:""===this.query?this.cleanUp(!0):this.lookup();break;case 9:case 13:var b=void 0!==this.$dropdown?this.$dropdown.find("li.active"):[];b.length?(this.select(b.data()),this.cleanUp(!1)):this.cleanUp(!0);break;case 27:this.cleanUp(!0);break;default:this.lookup()}},rteKeyDown:function(a){switch(a.which||a.keyCode){case 9:case 13:case 27:a.preventDefault();break;case 38:a.preventDefault(),void 0!==this.$dropdown&&this.highlightPreviousResult();break;case 40:a.preventDefault(),void 0!==this.$dropdown&&this.highlightNextResult()}a.stopPropagation()},rteClicked:function(a){var c=b(a.target);this.hasFocus&&"autocomplete-searchtext"!==c.parent().attr("id")&&this.cleanUp(!0)},rteLostFocus:function(){this.hasFocus&&this.cleanUp(!0)},lookup:function(){this.query=b.trim(b(this.editor.getBody()).find("#autocomplete-searchtext").text()).replace("\ufeff",""),void 0===this.$dropdown&&this.show(),clearTimeout(this.searchTimeout),this.searchTimeout=setTimeout(b.proxy(function(){var a=b.isFunction(this.options.source)?this.options.source(this.query,b.proxy(this.process,this),this.options.delimiter):this.options.source;a&&this.process(a)},this),this.options.delay)},matcher:function(a){return~a[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())},sorter:function(a){for(var e,b=[],c=[],d=[];void 0!==(e=a.shift());)e[this.options.queryBy].toLowerCase().indexOf(this.query.toLowerCase())?~e[this.options.queryBy].indexOf(this.query)?c.push(e):d.push(e):b.push(e);return b.concat(c,d)},highlighter:function(a){return a.replace(new RegExp("("+this.query.replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")+")","ig"),function(a,b){return"<strong>"+b+"</strong>"})},show:function(){var a=this.editor.inline?this.offsetInline():this.offset();this.$dropdown=b(this.renderDropdown()).css({top:a.top,left:a.left}),b("body").append(this.$dropdown),this.$dropdown.on("click",b.proxy(this.autoCompleteClick,this))},process:function(a){if(this.hasFocus){var c=this,d=[],e=b.grep(a,function(a){return c.matcher(a)});e=c.sorter(e),e=e.slice(0,this.options.items),b.each(e,function(a,f){var g=b(c.render(f));g.html(g.html().replace(g.text(),c.highlighter(g.text()))),b.each(e[a],function(a,b){g.attr("data-"+a,b)}),d.push(g[0].outerHTML)}),d.length?this.$dropdown.html(d.join("")).show():this.$dropdown.hide()}},renderDropdown:function(){return'<ul class="rte-autocomplete dropdown-menu"><li class="loading"></li></ul>'},render:function(a){return'<li><a href="javascript:;"><span>'+a[this.options.queryBy]+"</span></a>"+"</li>"},autoCompleteClick:function(a){var c=b(a.target).closest("li").data();b.isEmptyObject(c)||(this.select(c),this.cleanUp(!1)),a.stopPropagation(),a.preventDefault()},highlightPreviousResult:function(){var a=this.$dropdown.find("li.active").index(),b=0===a?this.$dropdown.find("li").length-1:--a;this.$dropdown.find("li").removeClass("active").eq(b).addClass("active")},highlightNextResult:function(){var a=this.$dropdown.find("li.active").index(),b=a===this.$dropdown.find("li").length-1?0:++a;this.$dropdown.find("li").removeClass("active").eq(b).addClass("active")},select:function(a){this.editor.focus();var b=this.editor.dom.select("span#autocomplete")[0];this.editor.dom.remove(b),this.editor.execCommand("mceInsertContent",!1,this.insert(a)+"&nbsp;")},insert:function(a){return"<span>"+a[this.options.queryBy]+"</span>"},cleanUp:function(a){if(this.unbindEvents(),this.hasFocus=!1,void 0!==this.$dropdown&&(this.$dropdown.remove(),delete this.$dropdown),a){var c=this.query,d=b(this.editor.dom.select("span#autocomplete")),e=b("<p>"+this.options.delimiter+c+"</p>")[0].firstChild,f=b(this.editor.selection.getNode()).offset().top===d.offset().top+(d.outerHeight()-d.height())/2;this.editor.dom.replace(e,d[0]),f&&(this.editor.selection.select(e),this.editor.selection.collapse())}},offset:function(){var a=b(this.editor.getContainer()).offset(),c=b(this.editor.getContentAreaContainer()).position(),d=b(this.editor.dom.select("span#autocomplete")).position();return{top:a.top+c.top+d.top+b(this.editor.selection.getNode()).innerHeight()-b(this.editor.getDoc()).scrollTop()+5,left:a.left+c.left+d.left}},offsetInline:function(){var a=b(this.editor.dom.select("span#autocomplete")).offset();return{top:a.top+b(this.editor.selection.getNode()).innerHeight()+5,left:a.left}}},a.create("tinymce.plugins.Mention",{init:function(a){function g(){var c=a.selection.getRng(!0).startOffset,d=a.selection.getRng(!0).startContainer.data||"",e=d.substr(c-1,1);return b.trim(e).length?!1:!0}var e,f=a.getParam("mentions");f.delimiter=void 0!==f.delimiter?b.isArray(f.delimiter)?f.delimiter:[f.delimiter]:["@"],a.on("keypress",function(d){var h=b.inArray(String.fromCharCode(d.which||d.keyCode),f.delimiter);h>-1&&g()&&(void 0===e||void 0!==e.hasFocus&&!e.hasFocus)&&(d.preventDefault(),e=new c(a,b.extend({},f,{delimiter:f.delimiter[h]})))})},getInfo:function(){return{longname:"mention",author:"Steven Devooght",version:a.majorVersion+"."+a.minorVersion}}}),a.PluginManager.add("mention",a.plugins.Mention)}(tinymce,jQuery);