forked from jstayton/jquery-manifest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.manifest.min.js
73 lines (71 loc) · 20.1 KB
/
jquery.manifest.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*!
* jQuery UI Widget 1.8.14
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Widget
*/
(function(b,j){if(b.cleanData){var k=b.cleanData;b.cleanData=function(a){for(var c=0,d;(d=a[c])!=null;c++)b(d).triggerHandler("remove");k(a)}}else{var l=b.fn.remove;b.fn.remove=function(a,c){return this.each(function(){if(!c)if(!a||b.filter(a,[this]).length)b("*",this).add([this]).each(function(){b(this).triggerHandler("remove")});return l.call(b(this),a,c)})}}b.widget=function(a,c,d){var e=a.split(".")[0],f;a=a.split(".")[1];f=e+"-"+a;if(!d){d=c;c=b.Widget}b.expr[":"][f]=function(h){return!!b.data(h,
a)};b[e]=b[e]||{};b[e][a]=function(h,g){arguments.length&&this._createWidget(h,g)};c=new c;c.options=b.extend(true,{},c.options);b[e][a].prototype=b.extend(true,c,{namespace:e,widgetName:a,widgetEventPrefix:b[e][a].prototype.widgetEventPrefix||a,widgetBaseClass:f},d);b.widget.bridge(a,b[e][a])};b.widget.bridge=function(a,c){b.fn[a]=function(d){var e=typeof d==="string",f=Array.prototype.slice.call(arguments,1),h=this;d=!e&&f.length?b.extend.apply(null,[true,d].concat(f)):d;if(e&&d.charAt(0)==="_")return h;
e?this.each(function(){var g=b.data(this,a),i=g&&b.isFunction(g[d])?g[d].apply(g,f):g;if(i!==g&&i!==j){h=i;return false}}):this.each(function(){var g=b.data(this,a);g?g.option(d||{})._init():b.data(this,a,new c(d,this))});return h}};b.Widget=function(a,c){arguments.length&&this._createWidget(a,c)};b.Widget.prototype={widgetName:"widget",widgetEventPrefix:"",options:{disabled:false},_createWidget:function(a,c){b.data(c,this.widgetName,this);this.element=b(c);this.options=b.extend(true,{},this.options,
this._getCreateOptions(),a);var d=this;this.element.bind("remove."+this.widgetName,function(){d.destroy()});this._create();this._trigger("create");this._init()},_getCreateOptions:function(){return b.metadata&&b.metadata.get(this.element[0])[this.widgetName]},_create:function(){},_init:function(){},destroy:function(){this.element.unbind("."+this.widgetName).removeData(this.widgetName);this.widget().unbind("."+this.widgetName).removeAttr("aria-disabled").removeClass(this.widgetBaseClass+"-disabled ui-state-disabled")},
widget:function(){return this.element},option:function(a,c){var d=a;if(arguments.length===0)return b.extend({},this.options);if(typeof a==="string"){if(c===j)return this.options[a];d={};d[a]=c}this._setOptions(d);return this},_setOptions:function(a){var c=this;b.each(a,function(d,e){c._setOption(d,e)});return this},_setOption:function(a,c){this.options[a]=c;if(a==="disabled")this.widget()[c?"addClass":"removeClass"](this.widgetBaseClass+"-disabled ui-state-disabled").attr("aria-disabled",c);return this},
enable:function(){return this._setOption("disabled",false)},disable:function(){return this._setOption("disabled",true)},_trigger:function(a,c,d){var e=this.options[a];c=b.Event(c);c.type=(a===this.widgetEventPrefix?a:this.widgetEventPrefix+a).toLowerCase();d=d||{};if(c.originalEvent){a=b.event.props.length;for(var f;a;){f=b.event.props[--a];c[f]=c.originalEvent[f]}}this.element.trigger(c,d);return!(b.isFunction(e)&&e.call(this.element[0],c,d)===false||c.isDefaultPrevented())}}})(jQuery);
/**
* Marco Polo v1.5.0
*
* A jQuery autocomplete plugin for the discerning developer.
*
* https://github.com/jstayton/jquery-marcopolo
*
* Copyright 2011 by Justin Stayton
* Released under the MIT License
* http://en.wikipedia.org/wiki/MIT_License
*/
(function(f){"function"===typeof define&&define.amd?define(["jquery"],f):f(jQuery)})(function(f,m){var j={};f.widget("mp.marcoPolo",{options:{cache:!0,compare:!1,data:{},delay:250,formatData:null,formatError:function(){return"<em>Your search could not be completed at this time.</em>"},formatItem:function(a){return a.title||a.name},formatMinChars:function(a){return"<em>Your search must be at least <strong>"+a+"</strong> characters.</em>"},formatNoResults:function(a){return"<em>No results for <strong>"+
a+"</strong>.</em>"},hideOnSelect:!0,label:null,minChars:1,onBlur:null,onChange:null,onError:null,onFocus:null,onMinChars:null,onNoResults:null,onRequestBefore:null,onRequestAfter:null,onResults:null,onSelect:function(a){this.val(a.title||a.name)},param:"q",required:!1,selectable:"*",selected:null,url:null},keys:{DOWN:40,ENTER:13,ESC:27,UP:38},_create:function(){this.$input=this.element.addClass("mp_input");this.$list=f('<ol class="mp_list" />').hide().insertAfter(this.$input);this.autocomplete=this.$input.attr("autocomplete");
this.$input.attr("autocomplete","off");this.ajax=null;this.ajaxAborted=!1;this.documentMouseup=null;this.mousedown=this.focusReal=this.focusPseudo=!1;this.selected=null;this.selectedMouseup=!1;this.timer=null;this.value=this.$input.val();this._bindInput()._bindList()._bindDocument();this._initOptions()._initSelected()},_setOption:function(a,b){f.Widget.prototype._setOption.apply(this,arguments);this._initOptions(a,b)},_initOptions:function(a,b){var c=this,d=a===m,e={};d?e=c.options:e[a]=b;f.each(e,
function(a,b){switch(a){case "label":c.options.label=f(b).addClass("mp_label");c._toggleLabel();break;case "selected":d&&b&&c.select(b,null);break;case "url":if(!b)c.options.url=c.$input.closest("form").attr("action")}});return c},change:function(a){a!==this.value&&(this.$input.val(a),this._change(a),this.focusPseudo?this._cancelPendingRequest()._hideAndEmptyList():this._toggleLabel())},search:function(a){var b=this.$input;a!==m&&b.val(a);b.focus()},select:function(a,b){var c=this.$input,d=this.options.hideOnSelect;
this.selected=a;d&&this._hideList();a?this._trigger("select",[a,b]):c.val("");if(c.val()!==this.value)this.value=c.val(),this.focusPseudo||this._toggleLabel(),this._hideAndEmptyList()},_initSelected:function(){var a=this.$input,b=a.data("selected"),a=a.val();b?this.select(b,null):a&&this.select(a,null);return this},destroy:function(){var a=this.options;this.$list.remove();"off"!==this.autocomplete&&this.$input.removeAttr("autocomplete");this.$input.removeClass("mp_input");a.label&&a.label.removeClass("mp_label");
f(document).unbind("mouseup.marcoPolo",this.documentMouseup);f.Widget.prototype.destroy.apply(this,arguments)},list:function(){return this.$list},_bindInput:function(){var a=this,b=a.$input,c=a.$list;b.bind("focus.marcoPolo",function(){if(!a.focusReal)a.focusPseudo=!0,a.focusReal=!0,a._toggleLabel(),a.selectedMouseup?a.selectedMouseup=!1:(a._trigger("focus"),a._request(b.val()))}).bind("keydown.marcoPolo",function(b){var e=f();switch(b.which){case a.keys.UP:b.preventDefault();a._showList()._highlightPrev();
break;case a.keys.DOWN:b.preventDefault();a._showList()._highlightNext();break;case a.keys.ENTER:b.preventDefault();if(!c.is(":visible"))break;e=a._highlighted();e.length&&a.select(e.data("marcoPolo"),e);break;case a.keys.ESC:a._cancelPendingRequest()._hideList()}}).bind("keyup.marcoPolo",function(){b.val()!==a.value&&a._request(b.val())}).bind("blur.marcoPolo",function(){a.focusReal=!1;setTimeout(function(){a.mousedown||a._dismiss()},1)});return a},_bindList:function(){var a=this;a.$list.bind("mousedown.marcoPolo",
function(){a.mousedown=!0}).delegate("li.mp_selectable","mouseover",function(){a._addHighlight(f(this))}).delegate("li.mp_selectable","mouseout",function(){a._removeHighlight(f(this))}).delegate("li.mp_selectable","mouseup",function(){var b=f(this);a.select(b.data("marcoPolo"),b);a.selectedMouseup=!0;a.$input.focus()});return a},_bindDocument:function(){var a=this;f(document).bind("mouseup.marcoPolo",a.documentMouseup=function(){a.mousedown=!1;!a.focusReal&&a.$list.is(":visible")&&a._dismiss()});
return a},_toggleLabel:function(){var a=this.options.label;a.length&&(this.focusPseudo||this.$input.val()?a.hide():a.show());return this},_firstSelectableItem:function(){return this.$list.children("li.mp_selectable:visible:first")},_lastSelectableItem:function(){return this.$list.children("li.mp_selectable:visible:last")},_highlighted:function(){return this.$list.children("li.mp_highlighted")},_removeHighlight:function(a){a.removeClass("mp_highlighted");return this},_addHighlight:function(a){this._removeHighlight(this._highlighted());
a.addClass("mp_highlighted");return this},_highlightFirst:function(){this._addHighlight(this._firstSelectableItem());return this},_highlightPrev:function(){var a=this._highlighted().prevAll("li.mp_selectable:visible:first");a.length||(a=this._lastSelectableItem());this._addHighlight(a);return this},_highlightNext:function(){var a=this._highlighted().nextAll("li.mp_selectable:visible:first");a.length||(a=this._firstSelectableItem());this._addHighlight(a);return this},_showList:function(){var a=this.$list;
a.children().length&&a.show();return this},_hideList:function(){this.$list.hide();return this},_hideAndEmptyList:function(){this.$list.hide().empty();return this},_buildNoResultsList:function(a){var b=this.$input,c=this.$list,d=this.options,e=f('<li class="mp_no_results" />');(b=d.formatNoResults&&d.formatNoResults.call(b,a,e))&&e.html(b);this._trigger("noResults",[a,e]);b?(e.appendTo(c),this._showList()):this._hideList();return this},_buildResultsList:function(a,b){for(var c=this.$input,d=this.$list,
e=this.options,h=this.selected,g=e.compare&&h,i,k,n=!1,l=f(),j=0,m=b.length;j<m;j++)i=b[j],l=f('<li class="mp_item" />'),k=e.formatItem.call(c,i,l),l.data("marcoPolo",i),l.html(k).appendTo(d),g&&(!0===e.compare?k=h:(i=i[e.compare],k=h[e.compare]),i===k&&(this._addHighlight(l),g=!1,n=!0));d.children(e.selectable).addClass("mp_selectable");this._trigger("results",[b]);this._showList();n||this._highlightFirst();return this},_buildSuccessList:function(a,b){var c=this.$input,d=this.options;this.$list.empty();
d.formatData&&(b=d.formatData.call(c,b));f.isEmptyObject(b)?this._buildNoResultsList(a):this._buildResultsList(a,b);return this},_buildErrorList:function(a,b,c){var d=this.$input,e=this.$list,h=this.options,g=f('<li class="mp_error" />');e.empty();(d=h.formatError&&h.formatError.call(d,g,a,b,c))&&g.html(d);this._trigger("error",[g,a,b,c]);d?(g.appendTo(e),this._showList()):this._hideList();return this},_buildMinCharsList:function(a){var b=this.$input,c=this.$list,d=this.options,e=f('<li class="mp_min_chars" />');
if(!a.length)return this._hideAndEmptyList(),this;c.empty();(a=d.formatMinChars&&d.formatMinChars.call(b,d.minChars,e))&&e.html(a);this._trigger("minChars",[d.minChars,e]);a?(e.appendTo(c),this._showList()):this._hideList();return this},_cancelPendingRequest:function(){this.ajax?(this.ajaxAborted=!0,this.ajax.abort()):this.ajaxAborted=!1;clearTimeout(this.timer);return this},_change:function(a){this.selected=null;this.value=a;this._trigger("change",[a]);return this},_request:function(a){var b=this,
c=b.$input,d=b.options;b._cancelPendingRequest();a!==b.value&&b._change(a);b.timer=setTimeout(function(){var e={},h={},g,i=f();if(a.length<d.minChars)return b._buildMinCharsList(a),b;e[d.param]=a;h=f.extend({},d.data,e);g=d.url+(-1===d.url.indexOf("?")?"?":"&")+f.param(h);d.cache&&j[g]?b._buildSuccessList(a,j[g]):(b._trigger("requestBefore"),i=c.parent().addClass("mp_busy"),b.ajax=f.ajax({url:d.url,dataType:"json",data:h,success:function(c){b._buildSuccessList(a,c);d.cache&&(j[g]=c)},error:function(a,
c,d){b.ajaxAborted||b._buildErrorList(a,c,d)},complete:function(a,c){b.ajax=null;b.ajaxAborted=!1;i.removeClass("mp_busy");b._trigger("requestAfter",[a,c])}}))},d.delay);return b},_dismiss:function(){var a=this.$input,b=this.options;this.focusPseudo=!1;this._cancelPendingRequest()._hideAndEmptyList();b.required&&!this.selected&&(a.val(""),this._change(""));this._toggleLabel()._trigger("blur");return this},_trigger:function(a,b){var c="on"+a.charAt(0).toUpperCase()+a.slice(1),d=this.widgetEventPrefix.toLowerCase()+
a.toLowerCase(),c=this.options[c];this.element.trigger(d,b);return c&&c.apply(this.element,b)}})});
/**
* Manifest v1.2.1
*
* A jQuery plugin that adds delight to selecting multiple values for an input.
*
* https://github.com/jstayton/jquery-manifest
*
* Copyright 2011 by Justin Stayton
* Released under the MIT License
* http://en.wikipedia.org/wiki/MIT_License
*/
(function(c){"function"===typeof define&&define.amd?define(["jquery"],c):c(jQuery)})(function(c,m){c.widget("mf.manifest",{options:{formatDisplay:function(a,b,c){return c?c.html():a},formatRemove:function(){return"X"},formatValue:function(a,b,c,e){return e?e.text():a},marcoPolo:!1,onAdd:null,onChange:null,onHighlight:null,onHighlightRemove:null,onRemove:null,onSelect:null,onSelectRemove:null,required:!1,separator:",",values:null,valuesName:null},_marcoPoloOptions:function(){var a=this,b=a.options;
return{onFocus:function(){b.marcoPolo.onFocus&&b.marcoPolo.onFocus.call(this);a._resizeInput()},onRequestAfter:function(){a.$container.parent().removeClass("mf_busy");b.marcoPolo.onRequestAfter&&b.marcoPolo.onRequestAfter.call(this)},onRequestBefore:function(){a.$container.parent().addClass("mf_busy");b.marcoPolo.onRequestBefore&&b.marcoPolo.onRequestBefore.call(this)},onSelect:function(c,e){b.marcoPolo.onSelect&&b.marcoPolo.onSelect.call(this,c,e);a.add(c,e,!0,!0)},required:b.required}},keys:{BACKSPACE:8,
DELETE:46,ENTER:13,LEFT:37,RIGHT:39},_create:function(){var a=this.options,b;this.$input=this.element.addClass("mf_input");this.$container=c('<div class="mf_container" />');this.$list=c('<ol class="mf_list" />');this.$measure=c('<span class="mf_measure" />');this.mpMousedown=this.mousedown=!1;this.originalWidth=this.$input.css("width");a.marcoPolo&&(b=this.$input.val(),this.$input.val(""),this._bindMarcoPolo(),this.$input.val(b));this._bindInput()._bindList()._bindContainer()._bindDocument();this.$input.wrap(this.$container).before(this.$list).after(this.$measure);
this.$container=this.$input.parent();a.values&&this.add(a.values,null,!1,!1);this._addInputValues()._styleMeasure()._resizeInput()},_setOption:function(a,b){var d=this.$input;switch(a){case "marcoPolo":this.options.marcoPolo?b?d.marcoPolo("option",c.extend({},b,this._marcoPoloOptions())):d.marcoPolo("destroy"):b&&(this._bindMarcoPolo(c.extend({},b,this._marcoPoloOptions())),d.marcoPolo("list").insertAfter(d.parent()));break;case "required":this.options.marcoPolo&&d.marcoPolo("option","required",b);
break;case "values":this.add(b,null,!1,!0);break;case "valuesName":this.$list.find("input:hidden.mf_value").attr("name",b+"[]")}c.Widget.prototype._setOption.apply(this,arguments)},_bindMarcoPolo:function(a){var b=this,d=b.$input,e=b.options;a===m&&(a=c.extend({},e.marcoPolo,b._marcoPoloOptions()));d.marcoPolo(a);d.marcoPolo("list").bind("mousedown.manifest",function(){b.mpMousedown=!0});return b},_bindInput:function(){var a=this,b=a.$input,c=a.options;b.bind("keydown.manifest",function(c){a._resizeInput();
c.which===a.keys.ENTER&&c.preventDefault();if(!b.val())switch(c.which){case a.keys.BACKSPACE:case a.keys.DELETE:c=a._selected();c.length?a.remove(c):a._selectPrev();break;case a.keys.LEFT:a._selectPrev();break;case a.keys.RIGHT:a._selectNext();break;default:a._removeSelected()}}).bind("keypress.manifest",function(e){!c.required&&a._isSeparator(e.which)&&(e.preventDefault(),b.val()&&a.add(b.val(),null,!0,!0))}).bind("keyup.manifest",function(){a._resizeInput()}).bind("blur.manifest",function(){setTimeout(function(){a.mousedown||
a._removeSelected();a.mpMousedown||(c.marcoPolo&&c.required?a._resizeInput():b.val()&&a.add(b.val(),null,!0,!0))},1)});return a},_bindList:function(){var a=this;a.$list.delegate("li","mouseover",function(){a._addHighlight(c(this))}).delegate("li","mouseout",function(){a._removeHighlight(c(this))}).delegate("li","mousedown",function(){a.mousedown=!0}).delegate("li","click",function(){a._toggleSelect(c(this))}).delegate("a.mf_remove","click",function(b){a.remove(c(this).closest("li"));b.preventDefault()});
return a},_bindContainer:function(){var a=this;a.$container.bind("click.manifest",function(){a.$input.focus()});return a},_bindDocument:function(){var a=this,b=a.$input;c(document).bind("mouseup.manifest",function(d){if(a.mousedown)a.mousedown=!1,c(d.target).is("li.mf_item, li.mf_item *")||a._removeSelected();if(a.mpMousedown)a.mpMousedown=!1,a.options.required?a._resizeInput():b.val()&&a.add(b.val(),null,!0,!0)});return a},container:function(){return this.$container},list:function(){return this.$list},
add:function(a,b,d,e){for(var k=this.$input,h=this.options,a=c.isArray(a)?a:[a],f,g=c(),i=c(),j=c(),i=!0,l=0,n=a.length;l<n;l++)if(f=a[l],"string"===typeof f&&(f=c.trim(f)),f&&(!c.isPlainObject(f)||!c.isEmptyObject(f)))g=c('<li class="mf_item" />'),i=c('<a href="#" class="mf_remove" title="Remove" />'),j=c('<input type="hidden" class="mf_value" />'),g.data("manifest",f),g.html(h.formatDisplay.call(k,f,g,b)),i.html(h.formatRemove.call(k,i,g)),h.valuesName?j.attr("name",h.valuesName+"[]"):j.attr("name",
k.attr("name")+"_values[]"),j.val(h.formatValue.call(k,f,j,g,b)),g.append(i,j),i=this._trigger("add",[f,g]),!1!==i&&(g.appendTo(this.$list),(e||e===m)&&this._trigger("change",["add",f,g]));d&&this._clearInputValue()},_addInputValues:function(){var a=this.$input,b=a.data("values"),a=a.val(),d=[];b?d=c.isArray(b)?b:[b]:a&&(d=a.split(this._separator()),d=c.map(d,c.trim));d.length&&this.add(d,null,!0,!1);return this},remove:function(a){var b=this,d=c(),d=a instanceof jQuery?a:b.$list.children(a);d.each(function(){var a=
c(this),d=a.data("manifest"),h=!0,h=b._trigger("remove",[d,a]);!1!==h&&(a.remove(),b._trigger("change",["remove",d,a]))})},values:function(){var a=[];this.$list.find("input:hidden.mf_value").each(function(){a.push(c(this).val())});return a},destroy:function(){var a=this.values().join(this._separator()+" ");this.options.marcoPolo&&this.$input.marcoPolo("destroy");this.$list.remove();this.$measure.remove();this.$input.unwrap().removeClass("mf_input").width(this.originalWidth).val(a);c(document).unbind(".manifest");
c.Widget.prototype.destroy.apply(this,arguments)},_styleMeasure:function(){var a=this.$input;this.$measure.css({fontFamily:a.css("font-family"),fontSize:a.css("font-size"),fontStyle:a.css("font-style"),fontVariant:a.css("font-variant"),fontWeight:a.css("font-weight"),left:-9999,letterSpacing:a.css("letter-spacing"),position:"absolute",textTransform:a.css("text-transform"),top:-9999,whiteSpace:"nowrap",width:"auto",wordSpacing:a.css("word-spacing")});return this},_measureText:function(a){var b=this.$measure,
a=a.replace(/&/g,"&").replace(/\s/g," ").replace(/</g,"<").replace(/>/g,">");b.html(a);return b.width()},_maxInputWidth:function(){var a=this.$input;return this.$container.width()-(a.outerWidth(!0)-a.width())},_resizeInput:function(){var a=this.$input,b;b=this._measureText(a.val()+"---");a.width(Math.min(b,this._maxInputWidth()));return this},_clearInputValue:function(){var a=this.$input;this.options.marcoPolo?a.marcoPolo("change",null):a.val("");this._resizeInput();return this},_isSeparator:function(a){var b=
this.options.separator;return c.isArray(b)?-1!==c.inArray(String.fromCharCode(a),b):a===b.charCodeAt()},_separator:function(){var a=this.options.separator;return c.isArray(a)?a[0]:a},_highlighted:function(){return this.$list.children("li.mf_highlighted")},_addHighlight:function(a){if(!a.length)return this;this._removeHighlighted();a.addClass("mf_highlighted");this._trigger("highlight",[a.data("marcoPolo"),a]);return this},_removeHighlight:function(a){if(!a.length)return this;a.removeClass("mf_highlighted");
this._trigger("highlightRemove",[a.data("marcoPolo"),a]);return this},_removeHighlighted:function(){return this._removeHighlight(this._highlighted())},_selected:function(){return this.$list.children("li.mf_selected")},_addSelect:function(a){if(!a.length)return this;this._removeSelected();a.addClass("mf_selected");this._trigger("select",[a.data("marcoPolo"),a]);return this},_removeSelect:function(a){if(!a.length)return this;a.removeClass("mf_selected");this._trigger("selectRemove",[a.data("marcoPolo"),
a]);return this},_removeSelected:function(){return this._removeSelect(this._selected())},_toggleSelect:function(a){return a.hasClass("mf_selected")?this._removeSelect(a):this._addSelect(a)},_selectPrev:function(){var a=this._selected(),b=c(),b=a.length?a.prev():this.$list.children(":last");b.length&&this._addSelect(b);return this},_selectNext:function(){var a=this._selected(),b=a.next();return b.length?this._addSelect(b):this._removeSelect(a)},_trigger:function(a,b){var c="on"+a.charAt(0).toUpperCase()+
a.slice(1),e=this.widgetEventPrefix.toLowerCase()+a.toLowerCase(),c=this.options[c];this.element.trigger(e,b);return c&&c.apply(this.element,b)}})});