mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-01-06 06:52:20 +08:00
2 lines
9.7 KiB
JavaScript
2 lines
9.7 KiB
JavaScript
/*! Inputosaurus Text v0.1.6 (c) 2013 Dan Kielp <dan@sproutsocial.com>; modified by RainLoop Team | MIT */
|
|
!function(e){var t={version:"0.1.6",fakeSpan:e('<span class="inputosaurus-fake-span"></span>'),eventprefix:"inputosaurus",options:{inputDelimiters:[",",";"],outputDelimiter:",",allowDuplicates:!1,allowDragAndDrop:!0,focusCallback:null,parseOnBlur:!1,wrapperElement:null,width:null,autoCompleteSource:"",activateFinalResult:!1,parseHook:null,elementHook:null,placeholder:null},_create:function(){var t=this,i={},n=t.options,s=n.placeholder||this.element.attr("placeholder")||null;this._chosenValues=[],i.ul=e('<ul class="inputosaurus-container"></ul>'),this.options.allowDragAndDrop&&i.ul.droppable({drop:function(e,n){n.draggable.addClass("inputosaurus-dropped"),i.input.val(n.draggable.data("inputosaurus-value")),n.draggable.__widget&&n.draggable.__widget._removeDraggedTag(n.draggable),t.parseInput()}}),i.input=e('<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />'),i.inputCont=e('<li class="inputosaurus-input inputosaurus-required"></li>'),i.origInputCont=e('<li class="inputosaurus-input-hidden inputosaurus-required"></li>'),i.lastEdit="",i.input.on("focus",function(){t._focusTrigger(!0)}).on("blur",function(){t._focusTrigger(!1)}),s&&(n.placeholder=s,i.input.attr("placeholder",n.placeholder),n.width&&i.input.css("min-width",n.width-50)),n.wrapperElement&&n.wrapperElement.append(i.ul),this.element.replaceWith(n.wrapperElement||i.ul),i.origInputCont.append(this.element).hide(),i.inputCont.append(i.input),i.ul.append(i.inputCont),i.ul.append(i.origInputCont),n.width&&i.ul.css("width",n.width),this.elements=i,t._attachEvents(),e.trim(this.element.val())&&(i.input.val(this.element.val()),this.parseInput()),this._instAutocomplete()},_focusTriggerTimer:0,_focusTrigger:function(e){var t=this;window.clearTimeout(this._focusTriggerTimer),this._focusTriggerTimer=window.setTimeout(function(){t.elements.ul[e?"addClass":"removeClass"]("inputosaurus-focused"),t.options.focusCallback&&t.options.focusCallback(e)},10)},_instAutocomplete:function(){if(this.options.autoCompleteSource){var t=this;this.elements.input.autocomplete({position:{of:this.elements.ul},source:this.options.autoCompleteSource,minLength:1,autoFocus:!0,select:function(e,i){e.preventDefault(),t.elements.input.val(i.item.value),t.parseInput()},open:function(){var i,n=e(this).data("ui-autocomplete").menu;n.element.width(t.elements.ul.outerWidth()-6),t.options.activateFinalResult&&(i=n.element.find("li"),1===i.size()&&n[n.activate?"activate":"focus"](e.Event("click"),i))},focus:function(){return!1}})}},_autoCompleteMenuPosition:function(){var e;this.options.autoCompleteSource&&(e=this.elements.input.data("ui-autocomplete"),e&&e.menu.element.position({of:this.elements.ul,my:"left top",at:"left bottom",collision:"none"}))},parseInput:function(t){var i,n=t&&t.data.widget||this,s=!1,u=[];i=n.elements.input.val(),i&&(s=n._containsDelimiter(i)),s!==!1?u=i.split(s):t&&(t.which!==e.ui.keyCode.ENTER||e(".ui-menu-item .ui-state-focus").size()||e("#ui-active-menuitem").size())?"blur"!==t.type||e("#ui-active-menuitem").size()||u.push(i):(u.push(i),t&&t.preventDefault()),e.isFunction(n.options.parseHook)&&(u=n.options.parseHook(u)),u.length&&(n._setChosen(u),n.elements.input.val(""),n._resizeInput()),n._resetPlaceholder()},_inputFocus:function(e){var t=e.data.widget||this;t.elements.input.value||t.options.autoCompleteSource.length&&t.elements.input.autocomplete("search","")},_inputKeypress:function(t){var i=t.data.widget||this;switch("keyup"===t.type&&i._trigger("keyup",t,i),t.which){case e.ui.keyCode.BACKSPACE:"keydown"===t.type&&i._inputBackspace(t);break;case e.ui.keyCode.LEFT:"keydown"===t.type&&i._inputBackspace(t);break;default:i.parseInput(t),i._resizeInput(t)}i.options.autoCompleteSource&&setTimeout(function(){i._autoCompleteMenuPosition.call(i)},200)},resizeInput:function(){this._resizeInput()},_resizeInput:function(e){var i=e&&e.data.widget||this;t.fakeSpan.text(i.elements.input.val());var n=25+t.fakeSpan.width();n=n>50?n:50,n=500>n?n:500,i.elements.input.width(n)},_resetPlaceholder:function(){var e=this.options.placeholder,t=this.elements.input,i=this.options.width||"inherit";e&&0===this.element.val().length?t.attr("placeholder",e).css("min-width",i-50):t.attr("placeholder","").css("min-width","inherit")},_inputBackspace:function(t){var i=t&&t.data.widget||this,n=i.elements.ul.find("li:not(.inputosaurus-required):last");t.stopPropagation(),(!e(t.currentTarget).val()||"selectionStart"in t.currentTarget&&0===t.currentTarget.selectionStart&&0===t.currentTarget.selectionEnd)&&n.size()&&(t.preventDefault(),n.find("a").focus())},_editTag:function(t){var i=t&&t.data.widget||this,n="",s=e(t.currentTarget).closest("li"),u=s.data("inputosaurus");if(!u)return!0;t.preventDefault();var a=null,o=!1;e.each(i._chosenValues,function(e,t){t.key===u?(n=t.value,o=!0):o&&!a&&(a=t)}),a&&(i.elements.lastEdit=a.value),s.after(i.elements.inputCont),i.elements.input.val(n),window.setTimeout(function(){i.elements.input.select()},100),i._removeTag(t),i._resizeInput(t)},_tagKeypress:function(t){var i=t.data.widget;switch(t.which){case e.ui.keyCode.DELETE:case e.ui.keyCode.BACKSPACE:t&&t.preventDefault(),t&&t.stopPropagation(),e(t.currentTarget).trigger("click");break;case 69:case e.ui.keyCode.ENTER:i._editTag(t);break;case e.ui.keyCode.LEFT:"keydown"===t.type&&i._prevTag(t);break;case e.ui.keyCode.RIGHT:"keydown"===t.type&&i._nextTag(t);break;case e.ui.keyCode.DOWN:"keydown"===t.type&&i._focus(t)}},_prevTag:function(t){var i=t&&t.data.widget||this,n=e(t.currentTarget).closest("li"),s=n.prev();s.is("li")?s.find("a").focus():i._focus()},_nextTag:function(t){var i=t&&t.data.widget||this,n=e(t.currentTarget).closest("li"),s=n.next();s.is("li:not(.inputosaurus-input)")?s.find("a").focus():i._focus()},_containsDelimiter:function(t){var i=!1;return e.each(this.options.inputDelimiters,function(e,n){-1!==t.indexOf(n)&&(i=n)}),i},_setChosen:function(t){var i=this;return e.isArray(t)?(e.each(t,function(t,n){var s="",u=!1,a=-1,o={key:"",obj:null,value:""};s=e.trim(n[0]),e.each(i._chosenValues,function(e,t){t.value===i.elements.lastEdit&&(a=e),t.value===s&&(u=!0)}),""===s||u&&!i.options.allowDuplicates||(o.key="mi_"+Math.random().toString(16).slice(2,10),o.value=s,o.obj=n[1],a>-1?i._chosenValues.splice(a,0,o):i._chosenValues.push(o),i.elements.lastEdit="",i._renderTags())}),1===t.length&&""===t[0]&&""!==i.elements.lastEdit&&(i.elements.lastEdit="",i._renderTags()),void i._setValue(i._buildValue())):!1},_buildValue:function(){var t=this,i="";return e.each(this._chosenValues,function(e,n){i+=i.length?t.options.outputDelimiter+n.value:n.value}),i},_setValue:function(e){var t=this.element.val();t!==e&&(this.element.val(e),this._trigger("change"))},_createTag:function(t,i,n){if(void 0!==t&&n){var s=this,u=e('<li data-inputosaurus="'+i+'" title="'+n.toLine(!1,!1,!0)+'"><a href="javascript:void(0);" class="ficon">✖</a><span>'+n.toLine(!0,!1,!0)+"</span></li>");return u.data("inputosaurus-value",n.toLine(!1,!1,!1)),this.options.allowDragAndDrop&&u.draggable({revert:"invalid",revertDuration:200,start:function(e,t){t.helper.__widget=s}}),e.isFunction(this.options.elementHook)&&this.options.elementHook(u,n),u}},_renderTags:function(){var t=this;this.elements.ul.find("li:not(.inputosaurus-required)").remove(),e.each(this._chosenValues,function(e,i){var n=t._createTag(i.value,i.key,i.obj);t.elements.ul.find("li.inputosaurus-input").before(n)})},_removeTag:function(t){var i=e(t.currentTarget).closest("li").data("inputosaurus"),n=!1,s=t&&t.data.widget||this;e.each(s._chosenValues,function(e,t){i===t.key&&(n=e)}),n!==!1&&s._chosenValues.splice(n,1),s._setValue(s._buildValue()),e(t.currentTarget).closest("li").remove(),window.setTimeout(function(){s.elements.input.focus()},100)},_removeDraggedTag:function(t){var i=t.data("inputosaurus"),n=this,s=!1;e.each(n._chosenValues,function(e,t){i===t.key&&(s=e)}),!1!==s&&(n._chosenValues.splice(s,1),n._setValue(n._buildValue())),t.remove()},focus:function(){this.elements.input.focus()},blur:function(){this.elements.input.blur()},_focus:function(t){var i=t&&t.data.widget||this,n=t&&t.target?e(t.target).closest("li"):null;n&&n.is("li")&&n.find("a").focus(),t&&e(t.target).closest("li").data("inputosaurus")||i.elements.input.focus()},_tagFocus:function(t){e(t.currentTarget).parent()["focusout"===t.type?"removeClass":"addClass"]("inputosaurus-selected")},refresh:function(){var t=this.options.outputDelimiter,i=this.element.val(),n=[];n.push(i),t&&(n=i.split(t)),n.length&&(this._chosenValues=[],e.isFunction(this.options.parseHook)&&(n=this.options.parseHook(n)),this._setChosen(n),this._renderTags(),this.elements.input.val(""),this._resizeInput())},_attachEvents:function(){var e=this;this.elements.input.on("keyup.inputosaurus",{widget:e},this._inputKeypress),this.elements.input.on("keydown.inputosaurus",{widget:e},this._inputKeypress),this.elements.input.on("change.inputosaurus",{widget:e},this._inputKeypress),this.elements.input.on("focus.inputosaurus",{widget:e},this._inputFocus),this.options.parseOnBlur&&this.elements.input.on("blur.inputosaurus",{widget:e},this.parseInput),this.elements.ul.on("click.inputosaurus",{widget:e},this._focus),this.elements.ul.on("click.inputosaurus","a",{widget:e},this._removeTag),this.elements.ul.on("dblclick.inputosaurus","li",{widget:e},this._editTag),this.elements.ul.on("doubletap.inputosaurus","li",{widget:e},this._editTag),this.elements.ul.on("focus.inputosaurus","a",{widget:e},this._tagFocus),this.elements.ul.on("blur.inputosaurus","a",{widget:e},this._tagFocus),this.elements.ul.on("keydown.inputosaurus","a",{widget:e},this._tagKeypress)},_destroy:function(){this.elements.input.unbind(".inputosaurus"),this.elements.ul.replaceWith(this.element)}};e("body").append(t.fakeSpan),e.widget("ui.inputosaurus",t)}(jQuery);
|