1 line
12 KiB
JavaScript
1 line
12 KiB
JavaScript
angular.module("oi.select",[]),angular.module("oi.select").provider("oiSelect",function(){return{options:{debounce:500,searchFilter:"oiSelectCloseIcon",dropdownFilter:"oiSelectHighlight",listFilter:"oiSelectAscSort",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter tab blur"},version:{full:"0.2.18",major:0,minor:2,dot:18},$get:function(){return{options:this.options,version:this.version}}}}).factory("oiSelectEscape",function(){var e=/[-\/\\^$*+?.()|[\]{}]/g,t="\\$&";return function(n){return String(n).replace(e,t)}}).factory("oiSelectEditItem",function(){return function(e,t,n,r){return r?"":n(e)}}).factory("oiUtils",["$document","$timeout",function(e,t){function n(e,t,n){for(var r=t;r&&r.ownerDocument&&11!==r.nodeType;){if(n){if(r===e)return!1;if(r.className.indexOf(n)>=0)return!0}else if(r===e)return!0;r=r.parentNode}return!1}function r(r,o){function i(e){return e&&"INPUT"!==e.target.nodeName?void 0:(d=!1,c?void(d=!0):void t(function(){r.triggerHandler("blur")}))}function u(){a||(a=!0,t(function(){r.triggerHandler("focus")}))}function s(){c=!0}function l(e){c=!1;var u=e.target,s=n(r[0],u);d&&!s&&i(),s&&"INPUT"!==u.nodeName&&t(function(){o[0].focus()}),!s&&a&&(a=!1)}var a,c,d;return e[0].addEventListener("click",l,!0),r[0].addEventListener("mousedown",s,!0),r[0].addEventListener("blur",i,!0),o.on("focus",u),function(){e[0].removeEventListener("click",l),r[0].removeEventListener("mousedown",s,!0),r[0].removeEventListener("blur",i,!0),o.off("focus",u)}}function o(e,t){var n,r,o,i,s,a;t&&(r=e.offsetHeight,o=l(t,"height","margin"),i=e.scrollTop||0,n=u(t).top-u(e).top+i,s=n,a=n-r+o,n+o>r+i?e.scrollTop=a:i>n&&(e.scrollTop=s))}function i(e,t,n,r,o){function i(e){return parseFloat(o[e])}for(var u=n===(r?"border":"content")?4:"width"===t?1:0,s=0,l=["Top","Right","Bottom","Left"];4>u;u+=2)"margin"===n&&(s+=i(n+l[u])),r?("content"===n&&(s-=i("padding"+l[u])),"margin"!==n&&(s-=i("border"+l[u]+"Width"))):(s+=i("padding"+l[u]),"padding"!==n&&(s+=i("border"+l[u]+"Width")));return s}function u(e){var t,n,r=e.getBoundingClientRect(),o=e&&e.ownerDocument;if(o)return t=o.documentElement,n=s(o),{top:r.top+n.pageYOffset-t.clientTop,left:r.left+n.pageXOffset-t.clientLeft}}function s(e){return null!=e&&e===e.window?e:9===e.nodeType&&e.defaultView}function l(e,t,n){var r=!0,o="width"===t?e.offsetWidth:e.offsetHeight,u=window.getComputedStyle(e,null),s=!1;if(0>=o||null==o){if(o=u[t],(0>o||null==o)&&(o=e.style[t]),g.test(o))return o;o=parseFloat(o)||0}return o+i(e,t,n||(s?"border":"content"),r,u)}function a(e){for(var t in e)if(e.hasOwnProperty(t)&&e[t].length)return!1;return!0}function c(e){var t=[];return angular.forEach(e,function(e,n){"$"!==n.toString().charAt(0)&&t.push(e)}),t}function d(e,t,n,r,o){var i,u,s,l,a,c=o?[].concat(e):[];for(i=0,s=e.length;i<e.length;i++)for(l=n?n(e[i]):e[i],u=0;u<t.length;u++)if(a=r?r(t[u]):t[u],angular.equals(l,a,e,t,i,u)){o?c.splice(i+c.length-s,1):c.push(t[u]);break}return c}function f(e,t,n,r){var o={};return e.split(".").reduce(function(e,n,r,o){return e[n]=r<o.length-1?{}:t},o),r(n,o)}var p=/[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,g=new RegExp("^("+p+")(?!px)[a-z%]+$","i");return{contains:n,bindFocusBlur:r,scrollActiveOption:o,groupsIsEmpty:a,objToArr:c,getValue:f,intersection:d}}]),angular.module("oi.select").directive("oiSelect",["$document","$q","$timeout","$parse","$interpolate","$injector","$filter","$animate","oiUtils","oiSelect",function(e,t,n,r,o,i,u,s,l,a){var c=/^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?(?:\s+disable\s+when\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/,d=/([^\(\)\s\|\s]*)\s*(\(.*\))?\s*(\|?\s*.+)?/;return{restrict:"AE",templateUrl:"src/template.html",require:"ngModel",scope:{},compile:function(e,f){var p=f.oiOptions,g=p?p.match(c):["","i","","","","i","","",""];if(!g)throw new Error("Expected expression in form of '_select_ (as _label_)? for (_key_,)?_value_ in _collection_'");var m,h,v,$,w,b,y,S,I=/ as /.test(g[0])&&g[1],F=g[2]||g[1],k=g[5]||g[7],V=g[3]||"",E=g[4]||"",q=g[9]||F,L=g[8].match(d),P=L[1],C=P+(L[3]||""),A=P+(L[2]||""),H=I&&r(I),O=r(F),x=r(V),T=r(E),N=r(C),D=r(A),_=r(q),M=o(f.multiplePlaceholder||""),Q=o(f.placeholder||""),R=r(f.oiSelectOptions),U=angular.version.major<=1&&angular.version.minor<=3;return function(e,o,c,d){function f(){at&&o.removeClass("cleanMode"),at=!1}function p(e,t){t=t||150,o.addClass(e),n(function(){o.removeClass(e)},t)}function I(){e.listItemHide=!0,e.inputHide=!1}function F(){var t=G(d.$modelValue);e.listItemHide=!t,e.inputHide=t}function V(t){l.contains(o[0],t.target,"disabled")||e.output.length>=y&&l.contains(o[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!st.closeList||"INPUT"===t.target.nodeName&&e.query.length?J(e.query):(Z({query:st.editItem&&!at}),e.$evalAsync()))}function E(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function q(){e.isFocused=!1,b||F(),L("blur")||Z(),e.$evalAsync()}function L(r,o){o||(o=r,r=e.query);var i,u=st.saveTrigger.split(" ").indexOf(o)+1,s=st.newItem&&r,l="blur"!==o?e.order[e.selectorPosition]:null;return u&&(s||l)?(e.showLoader=!0,i=t.when(l||S(e.$parent,{$query:r})),i.then(function(r){if(void 0===r)return t.reject();e.addItem(r);var o=e.order.length-1;e.selectorPosition===o&&et(rt,0),st.newItemFn&&!l||n(angular.noop),Z()}).catch(function(){p("invalid-item"),e.showLoader=!1}),!0):void 0}function C(){var e=b&&G(d.$modelValue)?it:ot;nt.attr("placeholder",e)}function A(t){return l.getValue(k,t,e.$parent,_)}function j(t){return l.getValue(k,t,e.$parent,H)}function B(t){return l.getValue(k,t,e.$parent,O)}function W(t){return l.getValue(k,t,e.$parent,T)}function z(t){return l.getValue(k,t,e.$parent,x)||""}function X(t){return l.getValue(P,t,e.$parent,N)}function Y(e){return e=e instanceof Array?e:e?[e]:[],e.filter(function(e){return e&&(e instanceof Array&&e.length||H||B(e))})}function G(e){return!!Y(e).length}function J(r,o){return v&&ct&&n.cancel(v),v=n(function(){var i=D(e.$parent,{$query:r,$selectedAs:o})||"";return e.selectorPosition="prompt"===st.newItem?!1:0,r||o||(e.oldQuery=null),(i.$promise&&!i.$resolved||angular.isFunction(i.then))&&(ct=st.debounce),e.showLoader=!0,t.when(i.$promise||i).then(function(t){if(e.groups={},t&&!o){var n=b?e.output:[],i=vt(l.objToArr(t),r,B,$t(e.$parent)),u=l.intersection(i,n,A,A,!0),s=X(u);e.groups=tt(s)}return K(),t}).finally(function(){e.showLoader=!1,st.closeList&&!st.cleanModel&&n(function(){et(rt,0)})})},ct)}function K(){var t,n,r,o=[],i=0;e.order=[],e.groupPos={};for(n in e.groups)e.groups.hasOwnProperty(n)&&"$"!=n.charAt(0)&&o.push(n);for(U&&o.sort(),t=0;t<o.length;t++)n=o[t],r=e.groups[n],e.order=e.order.concat(r),e.groupPos[n]=i,i+=r.length}function Z(t){t=t||{},e.oldQuery=null,e.backspaceFocus=!1,e.groups={},e.order=[],e.showLoader=!1,e.isOpen=!1,ct=0,t.query||(e.query=""),v&&n.cancel(v)}function et(t,n){e.selectorPosition=n,l.scrollActiveOption(t[0],t.find("li")[n])}function tt(e){for(var t,n,r={"":[]},o=0;o<e.length;o++)t=z(e[o]),(n=r[t])||(n=r[t]=[]),n.push(e[o]);return r}d.$isEmpty=function(e){return!G(e)};var nt=o.find("input"),rt=angular.element(o[0].querySelector(".select-dropdown")),ot=Q(e),it=M(e),ut=R(e.$parent)||{},st=angular.extend({cleanModel:"prompt"===ut.newItem},a.options,ut),lt=st.editItem,at="correct"===lt,ct=0;(lt===!0||"correct"===lt)&&(lt="oiSelectEditItem");var dt=lt?i.get(lt):angular.noop,ft=r(st.removeItemFn);g=st.searchFilter.split(":");var pt=u(g[0]),gt=r(g[1]);g=st.dropdownFilter.split(":");var mt=u(g[0]),ht=r(g[1]);g=st.listFilter.split(":");var vt=u(g[0]),$t=r(g[1]);S=st.newItemFn?r(st.newItemFn):function(e,t){return(R(t)||{}).newItemModel||t.$query},!st.cleanModel||lt&&!at||o.addClass("cleanMode");var wt=l.bindFocusBlur(o,nt);angular.isDefined(c.autofocus)&&n(function(){nt[0].focus()}),angular.isDefined(c.readonly)&&nt.attr("readonly",!0),angular.isDefined(c.tabindex)&&(nt.attr("tabindex",c.tabindex),o[0].removeAttribute("tabindex")),c.$observe("disabled",function(t){nt.prop("disabled",t),b&&d.$modelValue&&d.$modelValue.length&&(e.inputHide=t)}),e.$on("$destroy",wt),e.$parent.$watch(c.multipleLimit,function(e){y=Number(e)||1/0}),e.$parent.$watch(c.multiple,function(e){b=void 0===e?angular.isDefined(c.multiple):e,o[b?"addClass":"removeClass"]("multiple")}),e.$parent.$watch(c.ngModel,function(n,r){var o=Y(n),i=t.when(o);C(),G(r)&&n!==r&&f(),b||F(),H&&G(n)&&(i=J(null,n).then(function(e){return l.intersection(o,e,null,j)}),v=null),b&&c.disabled&&!G(n)&&(e.inputHide=!1),i.then(function(t){e.output=t,t.length!==o.length&&e.removeItem(t.length)})}),e.$watch("query",function(t,n){L(t.slice(0,-1),t.slice(-1))||(t===n||e.oldQuery&&!t||h||(rt[0].scrollTop=0,t?(J(t),e.oldQuery=null):b&&(Z(),h=!0)),h=!1)}),e.$watch("groups",function(t){l.groupsIsEmpty(t)?e.isOpen=!1:e.isOpen||c.disabled||(e.isOpen=!0,e.isFocused=!0)}),e.$watch("isFocused",function(e){s[e?"addClass":"removeClass"](o,"focused",!U&&{tempClasses:"focused-animate"})}),e.$watch("isOpen",function(e){s[e?"addClass":"removeClass"](o,"open",!U&&{tempClasses:"open-animate"})}),e.$watch("showLoader",function(e){s[e?"addClass":"removeClass"](o,"loading",!U&&{tempClasses:"loading-animate"})}),e.addItem=function(t){if($=e.query,!b||!l.intersection(e.output,[t],A,A).length){if(e.output.length>=y)return void p("limited");var n=e.groups[z(t)]=e.groups[z(t)]||[],r=H?j(t):t;n.splice(n.indexOf(t),1),b?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(r):[r]):(d.$setViewValue(r),F()),l.groupsIsEmpty(e.groups)&&(e.groups={}),b||st.closeList||Z({query:!0}),f(),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(n){c.disabled||b&&0>n||(w=b?d.$modelValue[n]:d.$modelValue,t.when(ft(e.$parent,{$item:w})).then(function(){(b||e.inputHide)&&(b?(d.$modelValue.splice(n,1),d.$setViewValue([].concat(d.$modelValue))):(I(),st.cleanModel&&d.$setViewValue(void 0)),(b||!e.backspaceFocus)&&(e.query=dt(w,$,B,at)||""),b&&st.closeList&&Z({query:!0}))}))},e.setSelection=function(t){m||e.selectorPosition===t?m=!1:et(rt,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||b&&e.output.length||Z()}},e.keyDown=function(t){var n=0,r=e.order.length-1;switch(t.keyCode){case 38:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n,et(rt,e.selectorPosition===n?r:e.selectorPosition-1),m=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,et(rt,e.selectorPosition===r?n:e.selectorPosition+1),m=!0,e.query.length||e.isOpen||J(),e.inputHide&&I();break;case 37:case 39:break;case 9:L("tab");break;case 13:L("enter"),t.preventDefault();break;case 32:L("space");break;case 27:b||(F(),st.cleanModel&&d.$setViewValue(w)),Z();break;case 8:if(!e.query.length){if((!b||lt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output&&(!b||e.output.length)){e.removeItem(e.output.length-1),lt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&I(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=B(t);return pt(n,e.oldQuery||e.query,t,gt(e.$parent))},e.getDropdownLabel=function(t){var n=B(t);return mt(n,e.oldQuery||e.query,t,ht(e.$parent))},e.getDisableWhen=W,Z(),o[0].addEventListener("click",V,!0),o.on("focus",E),o.on("blur",q)}}}}]),angular.module("oi.select").filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='<span class="close select-search-list-item_selection-remove">×</span>';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce","oiSelectEscape",function(e,t){return function(n,r){var o;return r.length>0||angular.isNumber(r)?(n=n.toString(),r=t(r.toString()),o=n.replace(new RegExp(r,"gi"),"<strong>$&</strong>")):o=n,e.trustAsHtml(o)}}]).filter("oiSelectAscSort",["oiSelectEscape",function(e){function t(t,n,r,o){var i,u,s,l,a=[],c=[],d=[];if(n){for(n=e(String(n)),i=0,s=!1;i<t.length;i++){if(s=r(t[i]).match(new RegExp(n,"i")),!s&&o)for(u=0;u<o.length&&!s;u++)s=String(t[i][o[u]]).match(new RegExp(n,"i"));s&&a.push(t[i])}for(i=0;i<a.length;i++)r(a[i]).match(new RegExp("^"+n,"i"))?c.push(a[i]):d.push(a[i]);l=c.concat(d)}else l=[].concat(t);return l}return t}]).filter("none",function(){return function(e){return e}}); |