diff --git a/dist/select-tpls.js b/dist/select-tpls.js index 83981b7..08b2b3b 100644 --- a/dist/select-tpls.js +++ b/dist/select-tpls.js @@ -540,7 +540,7 @@ angular.module('oi.select') //length less then minlength if (String(inputValue).length < options.minlength) return; - + //We don't get matches if nothing added into matches list if (inputValue !== oldValue && (!scope.oldQuery || inputValue) && !matchesWereReset) { listElement[0].scrollTop = 0; @@ -777,10 +777,35 @@ angular.module('oi.select') resetMatches(); - element[0].addEventListener('click', click, true); //triggered before add or delete item event + // //triggered before add or delete item event + var eventTriger = ''; + + var focusEvent = function(e) { + eventTriger = eventTriger ? eventTriger : 'focus'; + click(e); + element[0].addEventListener('click', clickEvent, true); + element[0].addEventListener('blur', blurEvent, true); + } + + var clickEvent = function(e) { + eventTriger = eventTriger ? eventTriger : 'click'; + click(e); + } + + var blurEvent = function () { + eventTriger = '' + element[0].removeEventListener('click', clickEvent, true); + element[0].removeEventListener('blur', blurEvent, true); + } + + element[0].addEventListener('focus', focusEvent, true); //triggered before add or delete item event scope.$on('$destroy', function() { - element[0].removeEventListener('click', click, true); + element[0].removeEventListener('click', clickEvent, true); + element[0].removeEventListener('focus', focusEvent, true); + element[0].removeEventListener('blur', blurEvent, true); }); + + element.on('focus', focus); element.on('blur', blur); @@ -808,7 +833,7 @@ angular.module('oi.select') function click(event) { //query length less then minlength if (scope.query.length < options.minlength) return; - + //option is disabled if (oiUtils.contains(element[0], event.target, 'disabled')) return; @@ -820,8 +845,11 @@ angular.module('oi.select') } if (scope.isOpen && options.closeList && (event.target.nodeName !== 'INPUT' || !scope.query.length)) { //do not reset if you are editing the query - resetMatches({query: options.editItem && !editItemIsCorrected}); - scope.$evalAsync(); + if (event.type == eventTriger) { + resetMatches({query: options.editItem && !editItemIsCorrected}); + scope.$evalAsync(); + eventTriger = ''; + } } else { getMatches(scope.query); } diff --git a/dist/select-tpls.min.js b/dist/select-tpls.min.js index 0366da1..5ce7d4a 100644 --- a/dist/select-tpls.min.js +++ b/dist/select-tpls.min.js @@ -1 +1 @@ -angular.module("oi.select",[]),angular.module("oi.select").provider("oiSelect",function(){return{options:{debounce:500,searchFilter:"oiSelectCloseIcon",dropdownFilter:"oiSelectHighlight",listFilter:"oiSelectAscSort",groupFilter:"oiSelectGroup",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter tab blur",minlength:0},version:{full:"0.2.21",major:0,minor:2,dot:21},$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,o){return o?"":n(e)}}).factory("oiUtils",["$document","$timeout",function(e,t){function n(e,t,n){for(var o=t;o&&o.ownerDocument&&11!==o.nodeType;){if(n){if(o===e)return!1;if(o.className.indexOf(n)>=0)return!0}else if(o===e)return!0;o=o.parentNode}return!1}function o(o,r){function i(e){return e&&"INPUT"!==e.target.nodeName?void 0:(d=!1,c?void(d=!0):void t(function(){o.triggerHandler("blur")}))}function s(){a||(a=!0,t(function(){o.triggerHandler("focus")}))}function l(){c=!0}function u(e){c=!1;var s=e.target,l=n(o[0],s);d&&!l&&i(),l&&"INPUT"!==s.nodeName&&t(function(){r[0].focus()}),!l&&a&&(a=!1)}var a,c,d;return e[0].addEventListener("click",u,!0),o[0].addEventListener("mousedown",l,!0),o[0].addEventListener("blur",i,!0),r.on("focus",s),function(){e[0].removeEventListener("click",u,!0),o[0].removeEventListener("mousedown",l,!0),o[0].removeEventListener("blur",i,!0),r.off("focus",s)}}function r(e,t){var n,o,r,i,l,a;t&&(o=e.offsetHeight,r=u(t,"height","margin"),i=e.scrollTop||0,n=s(t).top-s(e).top+i,l=n,a=n-o+r,n+r>o+i?e.scrollTop=a:i>n&&(e.scrollTop=l))}function i(e,t,n,o,r){function i(e){return parseFloat(r[e])}for(var s=n===(o?"border":"content")?4:"width"===t?1:0,l=0,u=["Top","Right","Bottom","Left"];4>s;s+=2)"margin"===n&&(l+=i(n+u[s])),o?("content"===n&&(l-=i("padding"+u[s])),"margin"!==n&&(l-=i("border"+u[s]+"Width"))):(l+=i("padding"+u[s]),"padding"!==n&&(l+=i("border"+u[s]+"Width")));return l}function s(e){var t,n,o=e.getBoundingClientRect(),r=e&&e.ownerDocument;if(r)return t=r.documentElement,n=l(r),{top:o.top+n.pageYOffset-t.clientTop,left:o.left+n.pageXOffset-t.clientLeft}}function l(e){return null!=e&&e===e.window?e:9===e.nodeType&&e.defaultView}function u(e,t,n){var o=!0,r="width"===t?e.offsetWidth:e.offsetHeight,s=window.getComputedStyle(e,null),l=!1;if(0>=r||null==r){if(r=s[t],(0>r||null==r)&&(r=e.style[t]),f.test(r))return r;r=parseFloat(r)||0}return r+i(e,t,n||(l?"border":"content"),o,s)}function a(e){for(var t in e)if(e.hasOwnProperty(t)&&e[t].length)return!1;return!0}function c(e,t,n,o,r){var i,s,l,u,a,c=r?[].concat(e):[];for(i=0,l=e.length;i=P&&u.contains(r[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!pt.closeList||"INPUT"===t.target.nodeName&&e.query.length?tt(e.query):(ot({query:pt.editItem&&!gt}),e.$evalAsync()))}function b(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function y(){e.isFocused=!1,V||h(),L("blur")||ot(),e.$evalAsync()}function L(o,r){r||(r=o,o=e.query);var i,s=pt.saveTrigger.split(" ").indexOf(r)+1,l=pt.newItem&&o,u="blur"!==r?e.order[e.selectorPosition]:null;return s&&(l||u&&!Y(u))?(e.showLoader=!0,i=t.when(u||x(e.$parent,{$query:o})),i.then(function(o){if(void 0===o)return t.reject();e.addItem(o);var r=e.order.length-1;e.selectorPosition===r&&rt(lt,0),pt.newItemFn&&!u||n(angular.noop),ot()}).catch(function(){f("invalid-item"),e.showLoader=!1}),!0):void 0}function O(){var e=V&&et(d.$modelValue)?at:ut;st.attr("placeholder",e)}function A(t){return u.getValue(v,t,e.$parent,U)}function z(t){return u.getValue(v,t,e.$parent,D)}function X(t){return u.getValue(v,t,e.$parent,_)}function Y(t){return e.isEmptyList||u.getValue(v,t,e.$parent,N)}function J(t){return u.getValue(v,t,e.$parent,T)||""}function K(t){return u.getValue(H,t,e.$parent,M)}function Z(e){return e=e instanceof Array?e:e?[e]:[],e.filter(function(e){return void 0!==e&&(e instanceof Array&&e.length||D||X(e))})}function et(e){return!!Z(e).length}function tt(o,i){return e.isEmptyList=!1,F&&mt&&n.cancel(F),F=n(function(){var s=Q(e.$parent,{$query:o,$selectedAs:i})||"";return e.selectorPosition="prompt"===pt.newItem?!1:0,o||i||(e.oldQuery=null),(s.$promise&&!s.$resolved||angular.isFunction(s.then))&&(mt=pt.debounce),e.showLoader=!0,t.when(s.$promise||s).then(function(t){if(e.groups={},t&&$){var n=[];angular.forEach(t,function(e,t){if("$"!==t.toString().charAt(0)){var o={};o[S]=t,o[k]=e,n.push(o)}}),t=n}if(t&&!i){var s=V?e.output:[],l=Lt(t,o,X,kt(e.$parent),r),a=u.intersection(l,s,A,A,!0),c=K(a);if(!c.length&&(e.isEmptyList=!0,ct)){var d={};_.assign(d,ct),c=[d[v]]}e.groups=it(c)}return nt(),t}).finally(function(){e.showLoader=!1,pt.closeList&&!pt.cleanModel&&n(function(){rt(lt,0)})})},mt)}function nt(){var t,n,o,r=[],i=0;e.order=[],e.groupPos={};for(n in e.groups)e.groups.hasOwnProperty(n)&&"$"!=n.charAt(0)&&r.push(n);for(G&&r.sort(),t=0;t=P)return void f("limited");var n=e.groups[J(t)]=e.groups[J(t)]||[],o=D?z(t):t;n.splice(n.indexOf(t),1),V?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(o):[o]):(d.$setViewValue(o),h()),u.groupsIsEmpty(e.groups)&&(e.groups={}),V||pt.closeList||ot({query:!0}),p(),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(n){c.disabled||V&&0>n||(q=V?d.$modelValue[n]:d.$modelValue,t.when(vt(e.$parent,{$item:q})).then(function(){(V||e.inputHide)&&(V?(d.$modelValue.splice(n,1),d.$setViewValue([].concat(d.$modelValue))):(m(),pt.cleanModel&&d.$setViewValue(void 0)),(V||!e.backspaceFocus)&&(e.query=ht(q,C,X,gt,r)||""),V&&pt.closeList&&ot({query:!0}))}))},e.setSelection=function(t){I||e.selectorPosition===t?I=!1:rt(lt,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||V&&e.output.length||ot()}},e.keyDown=function(t){var n=0,o=e.order.length-1;switch(t.keyCode){case 38:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n,rt(lt,e.selectorPosition===n?o:e.selectorPosition-1),I=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,rt(lt,e.selectorPosition===o?n:e.selectorPosition+1),I=!0,e.query.length||e.isOpen||tt(),e.inputHide&&m();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:V||(h(),pt.cleanModel&&d.$setViewValue(q)),ot();break;case 8:if(!e.query.length){if((!V||ft)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output&&(!V||e.output.length)){e.removeItem(e.output.length-1),ft&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&m(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=X(t);return $t(n,e.oldQuery||e.query,t,wt(e.$parent),r)},e.getDropdownLabel=function(t){var n=X(t);return bt(n,e.oldQuery||e.query,t,yt(e.$parent),r)},e.getGroupLabel=function(t,n){return St(t,e.oldQuery||e.query,n,It(e.$parent),r)},e.getDisableWhen=Y,ot(),r[0].addEventListener("click",w,!0),e.$on("$destroy",function(){r[0].removeEventListener("click",w,!0)}),r.on("focus",b),r.on("blur",y)}}}}]),angular.module("oi.select").filter("oiSelectGroup",["$sce",function(e){return function(t){return e.trustAsHtml(t)}}]).filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce","oiSelectEscape",function(e,t){return function(n,o){var r;return o.length>0||angular.isNumber(o)?(n=n.toString(),o=t(o),r=n.replace(new RegExp(o,"gi"),"$&")):r=n,e.trustAsHtml(r)}}]).filter("oiSelectAscSort",["oiSelectEscape",function(e){function t(t,n,o,r){var i,s,l,u,a=[],c=[],d=[],p=[];if(n){for(n=e(n).toLocaleLowerCase(),i=0,l=!1;i
')}]); \ No newline at end of file +angular.module("oi.select",[]),angular.module("oi.select").provider("oiSelect",function(){return{options:{debounce:500,searchFilter:"oiSelectCloseIcon",dropdownFilter:"oiSelectHighlight",listFilter:"oiSelectAscSort",groupFilter:"oiSelectGroup",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter tab blur",minlength:0},version:{full:"0.2.21",major:0,minor:2,dot:21},$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,o){return o?"":n(e)}}).factory("oiUtils",["$document","$timeout",function(e,t){function n(e,t,n){for(var o=t;o&&o.ownerDocument&&11!==o.nodeType;){if(n){if(o===e)return!1;if(o.className.indexOf(n)>=0)return!0}else if(o===e)return!0;o=o.parentNode}return!1}function o(o,r){function i(e){return e&&"INPUT"!==e.target.nodeName?void 0:(d=!1,c?void(d=!0):void t(function(){o.triggerHandler("blur")}))}function s(){a||(a=!0,t(function(){o.triggerHandler("focus")}))}function l(){c=!0}function u(e){c=!1;var s=e.target,l=n(o[0],s);d&&!l&&i(),l&&"INPUT"!==s.nodeName&&t(function(){r[0].focus()}),!l&&a&&(a=!1)}var a,c,d;return e[0].addEventListener("click",u,!0),o[0].addEventListener("mousedown",l,!0),o[0].addEventListener("blur",i,!0),r.on("focus",s),function(){e[0].removeEventListener("click",u,!0),o[0].removeEventListener("mousedown",l,!0),o[0].removeEventListener("blur",i,!0),r.off("focus",s)}}function r(e,t){var n,o,r,i,l,a;t&&(o=e.offsetHeight,r=u(t,"height","margin"),i=e.scrollTop||0,n=s(t).top-s(e).top+i,l=n,a=n-o+r,n+r>o+i?e.scrollTop=a:i>n&&(e.scrollTop=l))}function i(e,t,n,o,r){function i(e){return parseFloat(r[e])}for(var s=n===(o?"border":"content")?4:"width"===t?1:0,l=0,u=["Top","Right","Bottom","Left"];4>s;s+=2)"margin"===n&&(l+=i(n+u[s])),o?("content"===n&&(l-=i("padding"+u[s])),"margin"!==n&&(l-=i("border"+u[s]+"Width"))):(l+=i("padding"+u[s]),"padding"!==n&&(l+=i("border"+u[s]+"Width")));return l}function s(e){var t,n,o=e.getBoundingClientRect(),r=e&&e.ownerDocument;if(r)return t=r.documentElement,n=l(r),{top:o.top+n.pageYOffset-t.clientTop,left:o.left+n.pageXOffset-t.clientLeft}}function l(e){return null!=e&&e===e.window?e:9===e.nodeType&&e.defaultView}function u(e,t,n){var o=!0,r="width"===t?e.offsetWidth:e.offsetHeight,s=window.getComputedStyle(e,null),l=!1;if(0>=r||null==r){if(r=s[t],(0>r||null==r)&&(r=e.style[t]),f.test(r))return r;r=parseFloat(r)||0}return r+i(e,t,n||(l?"border":"content"),o,s)}function a(e){for(var t in e)if(e.hasOwnProperty(t)&&e[t].length)return!1;return!0}function c(e,t,n,o,r){var i,s,l,u,a,c=r?[].concat(e):[];for(i=0,l=e.length;i=P&&u.contains(r[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!pt.closeList||"INPUT"===t.target.nodeName&&e.query.length?tt(e.query):t.type==Ft&&(ot({query:pt.editItem&&!gt}),e.$evalAsync(),Ft=""))}function b(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function y(){e.isFocused=!1,V||h(),L("blur")||ot(),e.$evalAsync()}function L(o,r){r||(r=o,o=e.query);var i,s=pt.saveTrigger.split(" ").indexOf(r)+1,l=pt.newItem&&o,u="blur"!==r?e.order[e.selectorPosition]:null;return s&&(l||u&&!Y(u))?(e.showLoader=!0,i=t.when(u||x(e.$parent,{$query:o})),i.then(function(o){if(void 0===o)return t.reject();e.addItem(o);var r=e.order.length-1;e.selectorPosition===r&&rt(lt,0),pt.newItemFn&&!u||n(angular.noop),ot()}).catch(function(){f("invalid-item"),e.showLoader=!1}),!0):void 0}function O(){var e=V&&et(d.$modelValue)?at:ut;st.attr("placeholder",e)}function A(t){return u.getValue(v,t,e.$parent,U)}function z(t){return u.getValue(v,t,e.$parent,D)}function X(t){return u.getValue(v,t,e.$parent,_)}function Y(t){return e.isEmptyList||u.getValue(v,t,e.$parent,N)}function J(t){return u.getValue(v,t,e.$parent,T)||""}function K(t){return u.getValue(H,t,e.$parent,M)}function Z(e){return e=e instanceof Array?e:e?[e]:[],e.filter(function(e){return void 0!==e&&(e instanceof Array&&e.length||D||X(e))})}function et(e){return!!Z(e).length}function tt(o,i){return e.isEmptyList=!1,F&&mt&&n.cancel(F),F=n(function(){var s=Q(e.$parent,{$query:o,$selectedAs:i})||"";return e.selectorPosition="prompt"===pt.newItem?!1:0,o||i||(e.oldQuery=null),(s.$promise&&!s.$resolved||angular.isFunction(s.then))&&(mt=pt.debounce),e.showLoader=!0,t.when(s.$promise||s).then(function(t){if(e.groups={},t&&$){var n=[];angular.forEach(t,function(e,t){if("$"!==t.toString().charAt(0)){var o={};o[E]=t,o[k]=e,n.push(o)}}),t=n}if(t&&!i){var s=V?e.output:[],l=Lt(t,o,X,kt(e.$parent),r),a=u.intersection(l,s,A,A,!0),c=K(a);if(!c.length&&(e.isEmptyList=!0,ct)){var d={};_.assign(d,ct),c=[d[v]]}e.groups=it(c)}return nt(),t}).finally(function(){e.showLoader=!1,pt.closeList&&!pt.cleanModel&&n(function(){rt(lt,0)})})},mt)}function nt(){var t,n,o,r=[],i=0;e.order=[],e.groupPos={};for(n in e.groups)e.groups.hasOwnProperty(n)&&"$"!=n.charAt(0)&&r.push(n);for(G&&r.sort(),t=0;t=P)return void f("limited");var n=e.groups[J(t)]=e.groups[J(t)]||[],o=D?z(t):t;n.splice(n.indexOf(t),1),V?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(o):[o]):(d.$setViewValue(o),h()),u.groupsIsEmpty(e.groups)&&(e.groups={}),V||pt.closeList||ot({query:!0}),p(),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(n){c.disabled||V&&0>n||(q=V?d.$modelValue[n]:d.$modelValue,t.when(vt(e.$parent,{$item:q})).then(function(){(V||e.inputHide)&&(V?(d.$modelValue.splice(n,1),d.$setViewValue([].concat(d.$modelValue))):(m(),pt.cleanModel&&d.$setViewValue(void 0)),(V||!e.backspaceFocus)&&(e.query=ht(q,C,X,gt,r)||""),V&&pt.closeList&&ot({query:!0}))}))},e.setSelection=function(t){S||e.selectorPosition===t?S=!1:rt(lt,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||V&&e.output.length||ot()}},e.keyDown=function(t){var n=0,o=e.order.length-1;switch(t.keyCode){case 38:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n,rt(lt,e.selectorPosition===n?o:e.selectorPosition-1),S=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,rt(lt,e.selectorPosition===o?n:e.selectorPosition+1),S=!0,e.query.length||e.isOpen||tt(),e.inputHide&&m();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:V||(h(),pt.cleanModel&&d.$setViewValue(q)),ot();break;case 8:if(!e.query.length){if((!V||ft)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output&&(!V||e.output.length)){e.removeItem(e.output.length-1),ft&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&m(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=X(t);return $t(n,e.oldQuery||e.query,t,wt(e.$parent),r)},e.getDropdownLabel=function(t){var n=X(t);return bt(n,e.oldQuery||e.query,t,yt(e.$parent),r)},e.getGroupLabel=function(t,n){return Et(t,e.oldQuery||e.query,n,St(e.$parent),r)},e.getDisableWhen=Y,ot();var Ft="",Ct=function(e){Ft=Ft?Ft:"focus",w(e),r[0].addEventListener("click",qt,!0),r[0].addEventListener("blur",Vt,!0)},qt=function(e){Ft=Ft?Ft:"click",w(e)},Vt=function(){Ft="",r[0].removeEventListener("click",qt,!0),r[0].removeEventListener("blur",Vt,!0)};r[0].addEventListener("focus",Ct,!0),e.$on("$destroy",function(){r[0].removeEventListener("click",qt,!0),r[0].removeEventListener("focus",Ct,!0),r[0].removeEventListener("blur",Vt,!0)}),r.on("focus",b),r.on("blur",y)}}}}]),angular.module("oi.select").filter("oiSelectGroup",["$sce",function(e){return function(t){return e.trustAsHtml(t)}}]).filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce","oiSelectEscape",function(e,t){return function(n,o){var r;return o.length>0||angular.isNumber(o)?(n=n.toString(),o=t(o),r=n.replace(new RegExp(o,"gi"),"$&")):r=n,e.trustAsHtml(r)}}]).filter("oiSelectAscSort",["oiSelectEscape",function(e){function t(t,n,o,r){var i,s,l,u,a=[],c=[],d=[],p=[];if(n){for(n=e(n).toLocaleLowerCase(),i=0,l=!1;i
')}]); \ No newline at end of file diff --git a/dist/select.js b/dist/select.js index 88b5537..977cd67 100644 --- a/dist/select.js +++ b/dist/select.js @@ -540,7 +540,7 @@ angular.module('oi.select') //length less then minlength if (String(inputValue).length < options.minlength) return; - + //We don't get matches if nothing added into matches list if (inputValue !== oldValue && (!scope.oldQuery || inputValue) && !matchesWereReset) { listElement[0].scrollTop = 0; @@ -777,10 +777,35 @@ angular.module('oi.select') resetMatches(); - element[0].addEventListener('click', click, true); //triggered before add or delete item event + // //triggered before add or delete item event + var eventTriger = ''; + + var focusEvent = function(e) { + eventTriger = eventTriger ? eventTriger : 'focus'; + click(e); + element[0].addEventListener('click', clickEvent, true); + element[0].addEventListener('blur', blurEvent, true); + } + + var clickEvent = function(e) { + eventTriger = eventTriger ? eventTriger : 'click'; + click(e); + } + + var blurEvent = function () { + eventTriger = '' + element[0].removeEventListener('click', clickEvent, true); + element[0].removeEventListener('blur', blurEvent, true); + } + + element[0].addEventListener('focus', focusEvent, true); //triggered before add or delete item event scope.$on('$destroy', function() { - element[0].removeEventListener('click', click, true); + element[0].removeEventListener('click', clickEvent, true); + element[0].removeEventListener('focus', focusEvent, true); + element[0].removeEventListener('blur', blurEvent, true); }); + + element.on('focus', focus); element.on('blur', blur); @@ -808,7 +833,7 @@ angular.module('oi.select') function click(event) { //query length less then minlength if (scope.query.length < options.minlength) return; - + //option is disabled if (oiUtils.contains(element[0], event.target, 'disabled')) return; @@ -820,8 +845,11 @@ angular.module('oi.select') } if (scope.isOpen && options.closeList && (event.target.nodeName !== 'INPUT' || !scope.query.length)) { //do not reset if you are editing the query - resetMatches({query: options.editItem && !editItemIsCorrected}); - scope.$evalAsync(); + if (event.type == eventTriger) { + resetMatches({query: options.editItem && !editItemIsCorrected}); + scope.$evalAsync(); + eventTriger = ''; + } } else { getMatches(scope.query); } diff --git a/dist/select.min.js b/dist/select.min.js index 43dd7c2..3563611 100644 --- a/dist/select.min.js +++ b/dist/select.min.js @@ -1 +1 @@ -angular.module("oi.select",[]),angular.module("oi.select").provider("oiSelect",function(){return{options:{debounce:500,searchFilter:"oiSelectCloseIcon",dropdownFilter:"oiSelectHighlight",listFilter:"oiSelectAscSort",groupFilter:"oiSelectGroup",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter tab blur",minlength:0},version:{full:"0.2.21",major:0,minor:2,dot:21},$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 s(){a||(a=!0,t(function(){r.triggerHandler("focus")}))}function u(){c=!0}function l(e){c=!1;var s=e.target,u=n(r[0],s);d&&!u&&i(),u&&"INPUT"!==s.nodeName&&t(function(){o[0].focus()}),!u&&a&&(a=!1)}var a,c,d;return e[0].addEventListener("click",l,!0),r[0].addEventListener("mousedown",u,!0),r[0].addEventListener("blur",i,!0),o.on("focus",s),function(){e[0].removeEventListener("click",l,!0),r[0].removeEventListener("mousedown",u,!0),r[0].removeEventListener("blur",i,!0),o.off("focus",s)}}function o(e,t){var n,r,o,i,u,a;t&&(r=e.offsetHeight,o=l(t,"height","margin"),i=e.scrollTop||0,n=s(t).top-s(e).top+i,u=n,a=n-r+o,n+o>r+i?e.scrollTop=a:i>n&&(e.scrollTop=u))}function i(e,t,n,r,o){function i(e){return parseFloat(o[e])}for(var s=n===(r?"border":"content")?4:"width"===t?1:0,u=0,l=["Top","Right","Bottom","Left"];4>s;s+=2)"margin"===n&&(u+=i(n+l[s])),r?("content"===n&&(u-=i("padding"+l[s])),"margin"!==n&&(u-=i("border"+l[s]+"Width"))):(u+=i("padding"+l[s]),"padding"!==n&&(u+=i("border"+l[s]+"Width")));return u}function s(e){var t,n,r=e.getBoundingClientRect(),o=e&&e.ownerDocument;if(o)return t=o.documentElement,n=u(o),{top:r.top+n.pageYOffset-t.clientTop,left:r.left+n.pageXOffset-t.clientLeft}}function u(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,s=window.getComputedStyle(e,null),u=!1;if(0>=o||null==o){if(o=s[t],(0>o||null==o)&&(o=e.style[t]),p.test(o))return o;o=parseFloat(o)||0}return o+i(e,t,n||(u?"border":"content"),r,s)}function a(e){for(var t in e)if(e.hasOwnProperty(t)&&e[t].length)return!1;return!0}function c(e,t,n,r,o){var i,s,u,l,a,c=o?[].concat(e):[];for(i=0,u=e.length;i=P&&l.contains(o[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!ft.closeList||"INPUT"===t.target.nodeName&&e.query.length?tt(e.query):(rt({query:ft.editItem&&!gt}),e.$evalAsync()))}function y(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function b(){e.isFocused=!1,q||h(),L("blur")||rt(),e.$evalAsync()}function L(r,o){o||(o=r,r=e.query);var i,s=ft.saveTrigger.split(" ").indexOf(o)+1,u=ft.newItem&&r,l="blur"!==o?e.order[e.selectorPosition]:null;return s&&(u||l&&!Y(l))?(e.showLoader=!0,i=t.when(l||x(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&&ot(ut,0),ft.newItemFn&&!l||n(angular.noop),rt()}).catch(function(){p("invalid-item"),e.showLoader=!1}),!0):void 0}function O(){var e=q&&et(d.$modelValue)?at:lt;st.attr("placeholder",e)}function A(t){return l.getValue(v,t,e.$parent,R)}function z(t){return l.getValue(v,t,e.$parent,T)}function X(t){return l.getValue(v,t,e.$parent,N)}function Y(t){return e.isEmptyList||l.getValue(v,t,e.$parent,_)}function J(t){return l.getValue(v,t,e.$parent,D)||""}function K(t){return l.getValue(H,t,e.$parent,M)}function Z(e){return e=e instanceof Array?e:e?[e]:[],e.filter(function(e){return void 0!==e&&(e instanceof Array&&e.length||T||X(e))})}function et(e){return!!Z(e).length}function tt(r,i){return e.isEmptyList=!1,k&&mt&&n.cancel(k),k=n(function(){var s=Q(e.$parent,{$query:r,$selectedAs:i})||"";return e.selectorPosition="prompt"===ft.newItem?!1:0,r||i||(e.oldQuery=null),(s.$promise&&!s.$resolved||angular.isFunction(s.then))&&(mt=ft.debounce),e.showLoader=!0,t.when(s.$promise||s).then(function(t){if(e.groups={},t&&$){var n=[];angular.forEach(t,function(e,t){if("$"!==t.toString().charAt(0)){var r={};r[E]=t,r[S]=e,n.push(r)}}),t=n}if(t&&!i){var s=q?e.output:[],u=Lt(t,r,X,St(e.$parent),o),a=l.intersection(u,s,A,A,!0),c=K(a);if(!c.length&&(e.isEmptyList=!0,ct)){var d={};N.assign(d,ct),c=[d[v]]}e.groups=it(c)}return nt(),t}).finally(function(){e.showLoader=!1,ft.closeList&&!ft.cleanModel&&n(function(){ot(ut,0)})})},mt)}function nt(){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(G&&o.sort(),t=0;t=P)return void p("limited");var n=e.groups[J(t)]=e.groups[J(t)]||[],r=T?z(t):t;n.splice(n.indexOf(t),1),q?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(r):[r]):(d.$setViewValue(r),h()),l.groupsIsEmpty(e.groups)&&(e.groups={}),q||ft.closeList||rt({query:!0}),f(),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(n){c.disabled||q&&0>n||(V=q?d.$modelValue[n]:d.$modelValue,t.when(vt(e.$parent,{$item:V})).then(function(){(q||e.inputHide)&&(q?(d.$modelValue.splice(n,1),d.$setViewValue([].concat(d.$modelValue))):(m(),ft.cleanModel&&d.$setViewValue(void 0)),(q||!e.backspaceFocus)&&(e.query=ht(V,C,X,gt,o)||""),q&&ft.closeList&&rt({query:!0}))}))},e.setSelection=function(t){F||e.selectorPosition===t?F=!1:ot(ut,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||q&&e.output.length||rt()}},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,ot(ut,e.selectorPosition===n?r:e.selectorPosition-1),F=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,ot(ut,e.selectorPosition===r?n:e.selectorPosition+1),F=!0,e.query.length||e.isOpen||tt(),e.inputHide&&m();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:q||(h(),ft.cleanModel&&d.$setViewValue(V)),rt();break;case 8:if(!e.query.length){if((!q||pt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output&&(!q||e.output.length)){e.removeItem(e.output.length-1),pt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&m(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=X(t);return $t(n,e.oldQuery||e.query,t,wt(e.$parent),o)},e.getDropdownLabel=function(t){var n=X(t);return yt(n,e.oldQuery||e.query,t,bt(e.$parent),o)},e.getGroupLabel=function(t,n){return Et(t,e.oldQuery||e.query,n,Ft(e.$parent),o)},e.getDisableWhen=Y,rt(),o[0].addEventListener("click",w,!0),e.$on("$destroy",function(){o[0].removeEventListener("click",w,!0)}),o.on("focus",y),o.on("blur",b)}}}}]),angular.module("oi.select").filter("oiSelectGroup",["$sce",function(e){return function(t){return e.trustAsHtml(t)}}]).filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';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),o=n.replace(new RegExp(r,"gi"),"$&")):o=n,e.trustAsHtml(o)}}]).filter("oiSelectAscSort",["oiSelectEscape",function(e){function t(t,n,r,o){var i,s,u,l,a=[],c=[],d=[],f=[];if(n){for(n=e(n).toLocaleLowerCase(),i=0,u=!1;i=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 s(){a||(a=!0,t(function(){r.triggerHandler("focus")}))}function u(){c=!0}function l(e){c=!1;var s=e.target,u=n(r[0],s);d&&!u&&i(),u&&"INPUT"!==s.nodeName&&t(function(){o[0].focus()}),!u&&a&&(a=!1)}var a,c,d;return e[0].addEventListener("click",l,!0),r[0].addEventListener("mousedown",u,!0),r[0].addEventListener("blur",i,!0),o.on("focus",s),function(){e[0].removeEventListener("click",l,!0),r[0].removeEventListener("mousedown",u,!0),r[0].removeEventListener("blur",i,!0),o.off("focus",s)}}function o(e,t){var n,r,o,i,u,a;t&&(r=e.offsetHeight,o=l(t,"height","margin"),i=e.scrollTop||0,n=s(t).top-s(e).top+i,u=n,a=n-r+o,n+o>r+i?e.scrollTop=a:i>n&&(e.scrollTop=u))}function i(e,t,n,r,o){function i(e){return parseFloat(o[e])}for(var s=n===(r?"border":"content")?4:"width"===t?1:0,u=0,l=["Top","Right","Bottom","Left"];4>s;s+=2)"margin"===n&&(u+=i(n+l[s])),r?("content"===n&&(u-=i("padding"+l[s])),"margin"!==n&&(u-=i("border"+l[s]+"Width"))):(u+=i("padding"+l[s]),"padding"!==n&&(u+=i("border"+l[s]+"Width")));return u}function s(e){var t,n,r=e.getBoundingClientRect(),o=e&&e.ownerDocument;if(o)return t=o.documentElement,n=u(o),{top:r.top+n.pageYOffset-t.clientTop,left:r.left+n.pageXOffset-t.clientLeft}}function u(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,s=window.getComputedStyle(e,null),u=!1;if(0>=o||null==o){if(o=s[t],(0>o||null==o)&&(o=e.style[t]),p.test(o))return o;o=parseFloat(o)||0}return o+i(e,t,n||(u?"border":"content"),r,s)}function a(e){for(var t in e)if(e.hasOwnProperty(t)&&e[t].length)return!1;return!0}function c(e,t,n,r,o){var i,s,u,l,a,c=o?[].concat(e):[];for(i=0,u=e.length;i=P&&l.contains(o[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!ft.closeList||"INPUT"===t.target.nodeName&&e.query.length?tt(e.query):t.type==It&&(rt({query:ft.editItem&&!gt}),e.$evalAsync(),It=""))}function y(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function b(){e.isFocused=!1,q||h(),L("blur")||rt(),e.$evalAsync()}function L(r,o){o||(o=r,r=e.query);var i,s=ft.saveTrigger.split(" ").indexOf(o)+1,u=ft.newItem&&r,l="blur"!==o?e.order[e.selectorPosition]:null;return s&&(u||l&&!Y(l))?(e.showLoader=!0,i=t.when(l||x(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&&ot(ut,0),ft.newItemFn&&!l||n(angular.noop),rt()}).catch(function(){p("invalid-item"),e.showLoader=!1}),!0):void 0}function O(){var e=q&&et(d.$modelValue)?at:lt;st.attr("placeholder",e)}function A(t){return l.getValue(v,t,e.$parent,R)}function z(t){return l.getValue(v,t,e.$parent,T)}function X(t){return l.getValue(v,t,e.$parent,N)}function Y(t){return e.isEmptyList||l.getValue(v,t,e.$parent,_)}function J(t){return l.getValue(v,t,e.$parent,D)||""}function K(t){return l.getValue(H,t,e.$parent,M)}function Z(e){return e=e instanceof Array?e:e?[e]:[],e.filter(function(e){return void 0!==e&&(e instanceof Array&&e.length||T||X(e))})}function et(e){return!!Z(e).length}function tt(r,i){return e.isEmptyList=!1,I&&mt&&n.cancel(I),I=n(function(){var s=Q(e.$parent,{$query:r,$selectedAs:i})||"";return e.selectorPosition="prompt"===ft.newItem?!1:0,r||i||(e.oldQuery=null),(s.$promise&&!s.$resolved||angular.isFunction(s.then))&&(mt=ft.debounce),e.showLoader=!0,t.when(s.$promise||s).then(function(t){if(e.groups={},t&&$){var n=[];angular.forEach(t,function(e,t){if("$"!==t.toString().charAt(0)){var r={};r[S]=t,r[E]=e,n.push(r)}}),t=n}if(t&&!i){var s=q?e.output:[],u=Lt(t,r,X,Et(e.$parent),o),a=l.intersection(u,s,A,A,!0),c=K(a);if(!c.length&&(e.isEmptyList=!0,ct)){var d={};N.assign(d,ct),c=[d[v]]}e.groups=it(c)}return nt(),t}).finally(function(){e.showLoader=!1,ft.closeList&&!ft.cleanModel&&n(function(){ot(ut,0)})})},mt)}function nt(){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(G&&o.sort(),t=0;t=P)return void p("limited");var n=e.groups[J(t)]=e.groups[J(t)]||[],r=T?z(t):t;n.splice(n.indexOf(t),1),q?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(r):[r]):(d.$setViewValue(r),h()),l.groupsIsEmpty(e.groups)&&(e.groups={}),q||ft.closeList||rt({query:!0}),f(),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(n){c.disabled||q&&0>n||(V=q?d.$modelValue[n]:d.$modelValue,t.when(vt(e.$parent,{$item:V})).then(function(){(q||e.inputHide)&&(q?(d.$modelValue.splice(n,1),d.$setViewValue([].concat(d.$modelValue))):(m(),ft.cleanModel&&d.$setViewValue(void 0)),(q||!e.backspaceFocus)&&(e.query=ht(V,C,X,gt,o)||""),q&&ft.closeList&&rt({query:!0}))}))},e.setSelection=function(t){k||e.selectorPosition===t?k=!1:ot(ut,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||q&&e.output.length||rt()}},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,ot(ut,e.selectorPosition===n?r:e.selectorPosition-1),k=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,ot(ut,e.selectorPosition===r?n:e.selectorPosition+1),k=!0,e.query.length||e.isOpen||tt(),e.inputHide&&m();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:q||(h(),ft.cleanModel&&d.$setViewValue(V)),rt();break;case 8:if(!e.query.length){if((!q||pt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output&&(!q||e.output.length)){e.removeItem(e.output.length-1),pt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&m(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=X(t);return $t(n,e.oldQuery||e.query,t,wt(e.$parent),o)},e.getDropdownLabel=function(t){var n=X(t);return yt(n,e.oldQuery||e.query,t,bt(e.$parent),o)},e.getGroupLabel=function(t,n){return St(t,e.oldQuery||e.query,n,kt(e.$parent),o)},e.getDisableWhen=Y,rt();var It="",Ct=function(e){It=It?It:"focus",w(e),o[0].addEventListener("click",Vt,!0),o[0].addEventListener("blur",qt,!0)},Vt=function(e){It=It?It:"click",w(e)},qt=function(){It="",o[0].removeEventListener("click",Vt,!0),o[0].removeEventListener("blur",qt,!0)};o[0].addEventListener("focus",Ct,!0),e.$on("$destroy",function(){o[0].removeEventListener("click",Vt,!0),o[0].removeEventListener("focus",Ct,!0),o[0].removeEventListener("blur",qt,!0)}),o.on("focus",y),o.on("blur",b)}}}}]),angular.module("oi.select").filter("oiSelectGroup",["$sce",function(e){return function(t){return e.trustAsHtml(t)}}]).filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';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),o=n.replace(new RegExp(r,"gi"),"$&")):o=n,e.trustAsHtml(o)}}]).filter("oiSelectAscSort",["oiSelectEscape",function(e){function t(t,n,r,o){var i,s,u,l,a=[],c=[],d=[],f=[];if(n){for(n=e(n).toLocaleLowerCase(),i=0,u=!1;i