snappymail/vendors/inputosaurus/inputosaurus.min.js
RainLoop Team afb7ec8c4e Select2 replaced by inputosaurus + ui-autocomplete
+ a lot of small fixes.
2013-11-27 02:34:05 +04:00

2 lines
No EOL
8.1 KiB
JavaScript

/*! Inputosaurus Text v0.1.6 (c) 2013 Dan Kielp <dan@sproutsocial.com>; modified by RainLoop Team | MIT */
!function(a){var b={version:"0.1.6",eventprefix:"inputosaurus",options:{inputDelimiters:[",",";"],outputDelimiter:",",allowDuplicates:!1,parseOnBlur:!1,wrapperElement:null,width:null,autoCompleteSource:"",activateFinalResult:!1,parseHook:null,placeholder:null},_create:function(){var b=this,c={},d=b.options,e=d.placeholder||this.element.attr("placeholder")||null;this._chosenValues=[],c.ul=a('<ul class="inputosaurus-container"></ul>'),c.fakeSpan=a('<span class="inputosaurus-fake-span"></span>'),c.input=a('<input type="text" />'),c.inputCont=a('<li class="inputosaurus-input inputosaurus-required"></li>'),c.origInputCont=a('<li class="inputosaurus-input-hidden inputosaurus-required"></li>'),c.lastEdit="",e&&(d.placeholder=e,c.input.attr("placeholder",d.placeholder),d.width&&c.input.css("min-width",d.width-50)),d.wrapperElement&&d.wrapperElement.append(c.ul),this.element.replaceWith(d.wrapperElement||c.ul),c.origInputCont.append(this.element).hide(),c.inputCont.append(c.input),c.ul.append(c.inputCont),c.ul.append(c.origInputCont),c.ul.append(c.fakeSpan),d.width&&c.ul.css("width",d.width),this.elements=c,b._attachEvents(),a.trim(this.element.val())&&(c.input.val(this.element.val()),this.parseInput()),this._instAutocomplete()},_instAutocomplete:function(){if(this.options.autoCompleteSource){var b=this;this.elements.input.autocomplete({position:{of:this.elements.ul},source:this.options.autoCompleteSource,minLength:1,select:function(a,c){a.preventDefault(),b.elements.input.val(c.item.value),b.parseInput()},open:function(){var c,d=a(this).data("ui-autocomplete").menu;d.element.width(b.elements.ul.outerWidth()),b.options.activateFinalResult&&(c=d.element.find("li"),1===c.size()&&d[d.activate?"activate":"focus"](a.Event("click"),c))},focus:function(){return!1}})}},_autoCompleteMenuPosition:function(){var a;this.options.autoCompleteSource&&(a=this.elements.input.data("ui-autocomplete"),a&&a.menu.element.position({of:this.elements.ul,my:"left top",at:"left bottom",collision:"none"}))},parseInput:function(b){var c,d=b&&b.data.widget||this,e=!1,f=[];c=d.elements.input.val(),c&&(e=d._containsDelimiter(c)),e!==!1?f=c.split(e):b&&(b.which!==a.ui.keyCode.ENTER||a(".ui-menu-item .ui-state-focus").size()||a("#ui-active-menuitem").size())?"blur"!==b.type||a("#ui-active-menuitem").size()||f.push(c):(f.push(c),b&&b.preventDefault()),a.isFunction(d.options.parseHook)&&(f=d.options.parseHook(f)),f.length&&(d._setChosen(f),d.elements.input.val(""),d._resizeInput()),d._resetPlaceholder()},_inputFocus:function(a){var b=a.data.widget||this;b.elements.input.value||b.options.autoCompleteSource.length&&b.elements.input.autocomplete("search","")},_inputKeypress:function(b){var c=b.data.widget||this;switch("keyup"===b.type&&c._trigger("keyup",b,c),b.which){case a.ui.keyCode.BACKSPACE:"keydown"===b.type&&c._inputBackspace(b);break;case a.ui.keyCode.LEFT:"keydown"===b.type&&c._inputBackspace(b);break;default:c.parseInput(b),c._resizeInput(b)}c.options.autoCompleteSource&&setTimeout(function(){c._autoCompleteMenuPosition.call(c)},200)},_resizeInput:function(a){var b=50,c=400,d=a&&a.data.widget||this,e=0;d.elements.fakeSpan.text(d.elements.input.val()),e=20+d.elements.fakeSpan.width(),e=c>e?e:c,e=b>e?b:e,d.elements.input.width(e)},_resetPlaceholder:function(){var a=this.options.placeholder,b=this.elements.input,c=this.options.width||"inherit";a&&0===this.element.val().length?b.attr("placeholder",a).css("min-width",c-50):b.attr("placeholder","").css("min-width","inherit")},_inputBackspace:function(b){var c=b&&b.data.widget||this,d=c.elements.ul.find("li:not(.inputosaurus-required):last");b.stopPropagation(),(!a(b.currentTarget).val()||"selectionStart"in b.currentTarget&&0===b.currentTarget.selectionStart&&0===b.currentTarget.selectionEnd)&&d.size()&&(b.preventDefault(),d.find("a").focus())},_editTag:function(b){var c=b&&b.data.widget||this,d="",e=a(b.currentTarget).closest("li"),f=e.data("inputosaurus");if(!f)return!0;b.preventDefault();var g=null,h=!1;a.each(c._chosenValues,function(a,b){b.key===f?(d=b.value,h=!0):h&&!g&&(g=b)}),g&&(c.elements.lastEdit=g.value),e.after(c.elements.inputCont),c.elements.input.val(d),c.elements.input.select(),c._removeTag(b),c._resizeInput(b)},_tagKeypress:function(b){var c=b.data.widget;switch(b.which){case a.ui.keyCode.DELETE:case a.ui.keyCode.BACKSPACE:b&&b.preventDefault(),b&&b.stopPropagation(),a(b.currentTarget).trigger("click");break;case 69:c._editTag(b);break;case a.ui.keyCode.LEFT:"keydown"===b.type&&c._prevTag(b);break;case a.ui.keyCode.RIGHT:"keydown"===b.type&&c._nextTag(b);break;case a.ui.keyCode.DOWN:"keydown"===b.type&&c._focus(b)}},_prevTag:function(b){var c=b&&b.data.widget||this,d=a(b.currentTarget).closest("li"),e=d.prev();e.is("li")?e.find("a").focus():c._focus()},_nextTag:function(b){var c=b&&b.data.widget||this,d=a(b.currentTarget).closest("li"),e=d.next();e.is("li:not(.inputosaurus-input)")?e.find("a").focus():c._focus()},_containsDelimiter:function(b){var c=!1;return a.each(this.options.inputDelimiters,function(a,d){-1!==b.indexOf(d)&&(c=d)}),c},_setChosen:function(b){var c=this;return a.isArray(b)?(a.each(b,function(b,d){var e="",f=!1,g=-1,h={key:"",obj:null,value:""};e=a.trim(d[0]),a.each(c._chosenValues,function(a,b){b.value===c.elements.lastEdit&&(g=a),b.value===e&&(f=!0)}),""===e||f&&!c.options.allowDuplicates||(h.key="mi_"+Math.random().toString(16).slice(2,10),h.value=e,h.obj=d[1],g>-1?c._chosenValues.splice(g,0,h):c._chosenValues.push(h),c.elements.lastEdit="",c._renderTags())}),c._setValue(c._buildValue()),void 0):!1},_buildValue:function(){var b=this,c="";return a.each(this._chosenValues,function(a,d){c+=c.length?b.options.outputDelimiter+d.value:d.value}),c},_setValue:function(a){var b=this.element.val();b!==a&&(this.element.val(a),this._trigger("change"))},_createTag:function(b,c,d){return void 0!==b&&d?a('<li data-inputosaurus="'+c+'" title="'+d.toLine(!1,!1,!0)+'"><a href="javascript:void(0);" class="ficon">&#x2716;</a><span>'+d.toLine(!0,!1,!0)+"</span></li>"):void 0},_renderTags:function(){var b=this;this.elements.ul.find("li:not(.inputosaurus-required)").remove(),a.each(this._chosenValues,function(a,c){var d=b._createTag(c.value,c.key,c.obj);b.elements.ul.find("li.inputosaurus-input").before(d)})},_removeTag:function(b){var c=a(b.currentTarget).closest("li").data("inputosaurus"),d=!1,e=b&&b.data.widget||this;a.each(e._chosenValues,function(a,b){c===b.key&&(d=a)}),d!==!1&&e._chosenValues.splice(d,1),e._setValue(e._buildValue()),a(b.currentTarget).closest("li").remove(),e.elements.input.focus()},_focus:function(b){var c=b&&b.data.widget||this,d=b&&b.target?a(b.target).closest("li"):null;d&&d.is("li")&&d.find("a").focus(),b&&a(b.target).closest("li").data("inputosaurus")||c.elements.input.focus()},_tagFocus:function(b){a(b.currentTarget).parent()["focusout"===b.type?"removeClass":"addClass"]("inputosaurus-selected")},refresh:function(){var b=this.options.outputDelimiter,c=this.element.val(),d=[];d.push(c),b&&(d=c.split(b)),d.length&&(this._chosenValues=[],a.isFunction(this.options.parseHook)&&(d=this.options.parseHook(d)),this._setChosen(d),this._renderTags(),this.elements.input.val(""),this._resizeInput())},_attachEvents:function(){var a=this;this.elements.input.on("keyup.inputosaurus",{widget:a},this._inputKeypress),this.elements.input.on("keydown.inputosaurus",{widget:a},this._inputKeypress),this.elements.input.on("change.inputosaurus",{widget:a},this._inputKeypress),this.elements.input.on("focus.inputosaurus",{widget:a},this._inputFocus),this.options.parseOnBlur&&this.elements.input.on("blur.inputosaurus",{widget:a},this.parseInput),this.elements.ul.on("click.inputosaurus",{widget:a},this._focus),this.elements.ul.on("click.inputosaurus","a",{widget:a},this._removeTag),this.elements.ul.on("dblclick.inputosaurus","li",{widget:a},this._editTag),this.elements.ul.on("focus.inputosaurus","a",{widget:a},this._tagFocus),this.elements.ul.on("blur.inputosaurus","a",{widget:a},this._tagFocus),this.elements.ul.on("keydown.inputosaurus","a",{widget:a},this._tagKeypress)},_destroy:function(){this.elements.input.unbind(".inputosaurus"),this.elements.ul.replaceWith(this.element)}};a.widget("ui.inputosaurus",b)}(jQuery);