diff --git a/CHANGELOG.md b/CHANGELOG.md index c9eedaa..f4750fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,10 @@ #### Features - **oi-select-options:** - - cleanModel: changed behavior (clean model by click) + - cleanModel: changed behavior (clean model by click). It affected `newItem: 'prompt'` #### Bug Fixes - +- **dropdown** fixed dropdown opening in IE, open dropdown by pushing on letter keys (for single select) - **oi-select-options:** - closeList: fixed diff --git a/README.md b/README.md index d5d8f81..12eb46a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ #oi.select — AngularJS directive of select element -**[Download 0.2.11](https://github.com/tamtakoe/oi.select/tree/master/dist)** +**[Download 0.2.12](https://github.com/tamtakoe/oi.select/tree/master/dist)** ## Features @@ -91,7 +91,7 @@ Use `oi-select` directive: * `listFilter` — filter name for items order in dropdown. Use `none` to disable filtering * `editItem` — function which get `lastQuery`, `removedItem` and `getLabel(item)` and return string for input after element was removed (default: ''). `editItem = true` allows you to edit a deleted item. `editItem = 'correct'` same as `true` but does not edit the first time * `saveTrigger` — Trigger on which element is stored in the model. May be `enter`, `blur` (default: `enter`) - * `cleanModel` [deprecated] — Clean model on blur for single select. + * `cleanModel` — Clean model on click for single select. * `closeList` — close dropdown list by default after choosing or removing item (default: true) * `newItem` — Mode of adding new items from query (default: false). May be `autocomplete` (priority save matches), `prompt` (priority save new item) * `newItemModel` — New items model (default: model = query). `$query` value from model will be changed to query string. diff --git a/dist/select-tpls.js b/dist/select-tpls.js index 16ec0dd..2e27866 100644 --- a/dist/select-tpls.js +++ b/dist/select-tpls.js @@ -80,7 +80,9 @@ angular.module('oi.select') element[0].addEventListener('blur', blurHandler, true); inputElement.on('focus', focusHandler); - function blurHandler() { + function blurHandler(event) { + if (event.target.nodeName !== 'INPUT') return; //for IE + isBlur = false; if (isMousedown) { @@ -414,7 +416,7 @@ angular.module('oi.select') }; } - if (options.cleanModel) { + if (options.cleanModel && (!editItem || editItemCorrect)) { element.addClass('cleanMode'); } @@ -584,7 +586,10 @@ angular.module('oi.select') scope.query = editItemFn(removedItem, lastQuery, getLabel); } - editItemCorrect = false; + if (editItem) { + editItemCorrect = false; + element.removeClass('cleanMode'); + } if (multiple && options.closeList) { resetMatches({query: true}); @@ -669,6 +674,9 @@ angular.module('oi.select') break; } default: /* any key */ + if (scope.inputHide) { + cleanInput(); + } scope.backspaceFocus = false; return false; //preventDefaults } diff --git a/dist/select-tpls.min.js b/dist/select-tpls.min.js index 98050e0..1f21259 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",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter"},version:{full:"0.2.12",major:0,minor:2,dot:12},$get:function(){return{options:this.options,version:this.version}}}}).factory("oiSelectEditItem",function(){return function(e,t,n){return 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.classList.contains(n))return!0}else if(o===e)return!0;o=o.parentNode}return!1}function o(o,r){function i(){return 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),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]),g.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){var t=[];return angular.forEach(e,function(e,n){"$"!==n.toString().charAt(0)&&t.push(e)}),t}function d(e,t,n,o,r){var i,s,l,u,a,c=r?[].concat(e):[];for(i=0,l=e.length;i=D&&u.contains(r[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!et.closeList||"INPUT"===t.target.nodeName&&e.query.length?j(e.query):(B({query:et.editItem&&!nt}),e.$evalAsync()))}function w(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function y(){e.isFocused=!1,A||g(),I("blur"),e.$evalAsync()}function I(o){var r=new RegExp(o).test(et.saveTrigger),i=et.newItem&&e.query,s=angular.isNumber(e.selectorPosition),l=e.order[e.selectorPosition],u=t.reject(),a="blur"!==o||e.query||!et.newItem;r&&(i||s&&l)&&(e.showLoader=!0,u=t.when("blur"!==o&&l||e.query&&Y(e.$parent,{$query:e.query}))),u.then(function(t){a&&e.addItem(t)}).finally(function(){var t=e.order.length-1;e.selectorPosition===t&&z(J,0),et.newItemFn&&!s||n(angular.noop),B()})}function k(){var e=A&&d.$modelValue&&d.$modelValue.length?"":K;G.attr("placeholder",e)}function S(t){return u.getValue(b,t,e.$parent,O)}function q(t){return u.getValue(b,t,e.$parent,E)}function F(t){return String(u.getValue(b,t,e.$parent,L))}function Q(t){return u.getValue(b,t,e.$parent,x)}function R(t){return u.getValue(b,t,e.$parent,P)||""}function U(t){return u.getValue(V,t,e.$parent,C)}function j(o,r){var i=H(e.$parent,{$query:o,$selectedAs:r}),s=0;return e.selectorPosition="prompt"===et.newItem?!1:0,o||r||(e.oldQuery=null),m&&(i.$promise&&!i.$resolved||angular.isFunction(i.then))&&(n.cancel(m),s=et.debounce),m=n(function(){return e.showLoader=!0,t.when(i.$promise||i).then(function(t){if(!r){var n=A?e.output:[],i=ut(u.objToArr(t),o,F,at(e.$parent)),s=u.intersection(i,n,S,S,!0),l=U(s);e.groups=X(l),W()}return t}).finally(function(){e.showLoader=!1})},s)}function W(){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(angular.version.major<=1&&angular.version.minor<=3&&r.sort(),t=0;t=D)return r.addClass("limited"),void n(function(){r.removeClass("limited")},150);var o=e.groups[R(t)]=e.groups[R(t)]||[],i=E?q(t):t;o.splice(o.indexOf(t),1),A?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(i):[i]):(d.$setViewValue(i),g()),u.groupsIsEmpty(e.groups)&&(e.groups={}),A||et.closeList||B({query:!0}),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(t){c.disabled||!A&&!e.inputHide||(A&&t>=0&&(v=d.$modelValue[t],d.$modelValue.splice(t,1),d.$setViewValue([].concat(d.$modelValue))),A||(v=d.$modelValue,p(),et.cleanModel&&d.$setViewValue(void 0)),nt||!A&&e.backspaceFocus||(e.query=ot(v,h,F)),nt=!1,A&&et.closeList&&B({query:!0}))},e.setSelection=function(t){N||e.selectorPosition===t?N=!1:z(J,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||A&&e.output.length||B()}},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,z(J,e.selectorPosition===n?o:e.selectorPosition-1),N=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,z(J,e.selectorPosition===o?n:e.selectorPosition+1),N=!0,e.query.length||e.isOpen||j(),e.inputHide&&p();break;case 37:case 39:break;case 13:I("enter"),t.preventDefault();break;case 27:A||(g(),et.cleanModel&&d.$setViewValue(v)),B();break;case 8:if(!e.query.length){if((!A||tt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output){e.removeItem(e.output.length-1),tt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=F(t);return rt(n,e.oldQuery||e.query,t,it(e.$parent))},e.getDropdownLabel=function(t){var n=F(t);return st(n,e.oldQuery||e.query,t,lt(e.$parent))},e.getDisableWhen=Q,A&&(d.$isEmpty=function(e){return!e||!e.length}),B(),r[0].addEventListener("click",$,!0),r.on("focus",w),r.on("blur",y)}}}}]),angular.module("oi.select").filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce",function(e){return function(t,n){var o;return n.length>0||angular.isNumber(n)?(t=t.toString(),n=n.toString().replace(/\s+.*/,"").replace(/\\/g,"\\\\"),o=t.replace(new RegExp(n,"gi"),"$&")):o=t,e.trustAsHtml(o)}}]).filter("oiSelectAscSort",function(){function e(e,t,n,o){var r,i,s,l,u=[],a=[],c=[];if(t){for(t=String(t).replace(/\s+.*/,"").replace(/\\/g,"\\\\"),r=0,s=!1;r
')}]); \ 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",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter"},version:{full:"0.2.12",major:0,minor:2,dot:12},$get:function(){return{options:this.options,version:this.version}}}}).factory("oiSelectEditItem",function(){return function(e,t,n){return 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.classList.contains(n))return!0}else if(o===e)return!0;o=o.parentNode}return!1}function o(o,r){function i(e){return"INPUT"===e.target.nodeName?(d=!1,c?void(d=!0):void t(function(){o.triggerHandler("blur")})):void 0}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),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]),g.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){var t=[];return angular.forEach(e,function(e,n){"$"!==n.toString().charAt(0)&&t.push(e)}),t}function d(e,t,n,o,r){var i,s,l,u,a,c=r?[].concat(e):[];for(i=0,l=e.length;i=D&&u.contains(r[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!et.closeList||"INPUT"===t.target.nodeName&&e.query.length?j(e.query):(B({query:et.editItem&&!nt}),e.$evalAsync()))}function w(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function y(){e.isFocused=!1,A||g(),I("blur"),e.$evalAsync()}function I(o){var r=new RegExp(o).test(et.saveTrigger),i=et.newItem&&e.query,s=angular.isNumber(e.selectorPosition),l=e.order[e.selectorPosition],u=t.reject(),a="blur"!==o||e.query||!et.newItem;r&&(i||s&&l)&&(e.showLoader=!0,u=t.when("blur"!==o&&l||e.query&&Y(e.$parent,{$query:e.query}))),u.then(function(t){a&&e.addItem(t)}).finally(function(){var t=e.order.length-1;e.selectorPosition===t&&z(J,0),et.newItemFn&&!s||n(angular.noop),B()})}function k(){var e=A&&d.$modelValue&&d.$modelValue.length?"":K;G.attr("placeholder",e)}function S(t){return u.getValue(b,t,e.$parent,O)}function q(t){return u.getValue(b,t,e.$parent,P)}function F(t){return String(u.getValue(b,t,e.$parent,E))}function Q(t){return u.getValue(b,t,e.$parent,x)}function R(t){return u.getValue(b,t,e.$parent,L)||""}function U(t){return u.getValue(V,t,e.$parent,C)}function j(o,r){var i=H(e.$parent,{$query:o,$selectedAs:r}),s=0;return e.selectorPosition="prompt"===et.newItem?!1:0,o||r||(e.oldQuery=null),m&&(i.$promise&&!i.$resolved||angular.isFunction(i.then))&&(n.cancel(m),s=et.debounce),m=n(function(){return e.showLoader=!0,t.when(i.$promise||i).then(function(t){if(!r){var n=A?e.output:[],i=ut(u.objToArr(t),o,F,at(e.$parent)),s=u.intersection(i,n,S,S,!0),l=U(s);e.groups=X(l),W()}return t}).finally(function(){e.showLoader=!1})},s)}function W(){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(angular.version.major<=1&&angular.version.minor<=3&&r.sort(),t=0;t=D)return r.addClass("limited"),void n(function(){r.removeClass("limited")},150);var o=e.groups[R(t)]=e.groups[R(t)]||[],i=P?q(t):t;o.splice(o.indexOf(t),1),A?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(i):[i]):(d.$setViewValue(i),g()),u.groupsIsEmpty(e.groups)&&(e.groups={}),A||et.closeList||B({query:!0}),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(t){c.disabled||!A&&!e.inputHide||(A&&t>=0&&(v=d.$modelValue[t],d.$modelValue.splice(t,1),d.$setViewValue([].concat(d.$modelValue))),A||(v=d.$modelValue,p(),et.cleanModel&&d.$setViewValue(void 0)),nt||!A&&e.backspaceFocus||(e.query=ot(v,h,F)),tt&&(nt=!1,r.removeClass("cleanMode")),A&&et.closeList&&B({query:!0}))},e.setSelection=function(t){N||e.selectorPosition===t?N=!1:z(J,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||A&&e.output.length||B()}},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,z(J,e.selectorPosition===n?o:e.selectorPosition-1),N=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,z(J,e.selectorPosition===o?n:e.selectorPosition+1),N=!0,e.query.length||e.isOpen||j(),e.inputHide&&p();break;case 37:case 39:break;case 13:I("enter"),t.preventDefault();break;case 27:A||(g(),et.cleanModel&&d.$setViewValue(v)),B();break;case 8:if(!e.query.length){if((!A||tt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output){e.removeItem(e.output.length-1),tt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&p(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=F(t);return rt(n,e.oldQuery||e.query,t,it(e.$parent))},e.getDropdownLabel=function(t){var n=F(t);return st(n,e.oldQuery||e.query,t,lt(e.$parent))},e.getDisableWhen=Q,A&&(d.$isEmpty=function(e){return!e||!e.length}),B(),r[0].addEventListener("click",$,!0),r.on("focus",w),r.on("blur",y)}}}}]),angular.module("oi.select").filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce",function(e){return function(t,n){var o;return n.length>0||angular.isNumber(n)?(t=t.toString(),n=n.toString().replace(/\s+.*/,"").replace(/\\/g,"\\\\"),o=t.replace(new RegExp(n,"gi"),"$&")):o=t,e.trustAsHtml(o)}}]).filter("oiSelectAscSort",function(){function e(e,t,n,o){var r,i,s,l,u=[],a=[],c=[];if(t){for(t=String(t).replace(/\s+.*/,"").replace(/\\/g,"\\\\"),r=0,s=!1;r
')}]); \ No newline at end of file diff --git a/dist/select.js b/dist/select.js index f9b993b..9c84aeb 100644 --- a/dist/select.js +++ b/dist/select.js @@ -80,7 +80,9 @@ angular.module('oi.select') element[0].addEventListener('blur', blurHandler, true); inputElement.on('focus', focusHandler); - function blurHandler() { + function blurHandler(event) { + if (event.target.nodeName !== 'INPUT') return; //for IE + isBlur = false; if (isMousedown) { @@ -414,7 +416,7 @@ angular.module('oi.select') }; } - if (options.cleanModel) { + if (options.cleanModel && (!editItem || editItemCorrect)) { element.addClass('cleanMode'); } @@ -584,7 +586,10 @@ angular.module('oi.select') scope.query = editItemFn(removedItem, lastQuery, getLabel); } - editItemCorrect = false; + if (editItem) { + editItemCorrect = false; + element.removeClass('cleanMode'); + } if (multiple && options.closeList) { resetMatches({query: true}); @@ -669,6 +674,9 @@ angular.module('oi.select') break; } default: /* any key */ + if (scope.inputHide) { + cleanInput(); + } scope.backspaceFocus = false; return false; //preventDefaults } diff --git a/dist/select.min.js b/dist/select.min.js index 72fac9c..2e17029 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",editItem:!1,newItem:!1,closeList:!0,saveTrigger:"enter"},version:{full:"0.2.12",major:0,minor:2,dot:12},$get:function(){return{options:this.options,version:this.version}}}}).factory("oiSelectEditItem",function(){return function(e,t,n){return 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.classList.contains(n))return!0}else if(r===e)return!0;r=r.parentNode}return!1}function r(r,o){function i(){return 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=T&&l.contains(o[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!et.closeList||"INPUT"===t.target.nodeName&&e.query.length?U(e.query):(W({query:et.editItem&&!nt}),e.$evalAsync()))}function w(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function b(){e.isFocused=!1,x||g(),I("blur"),e.$evalAsync()}function I(r){var o=new RegExp(r).test(et.saveTrigger),i=et.newItem&&e.query,u=angular.isNumber(e.selectorPosition),s=e.order[e.selectorPosition],l=t.reject(),a="blur"!==r||e.query||!et.newItem;o&&(i||u&&s)&&(e.showLoader=!0,l=t.when("blur"!==r&&s||e.query&&Y(e.$parent,{$query:e.query}))),l.then(function(t){a&&e.addItem(t)}).finally(function(){var t=e.order.length-1;e.selectorPosition===t&&z(J,0),et.newItemFn&&!u||n(angular.noop),W()})}function S(){var e=x&&d.$modelValue&&d.$modelValue.length?"":K;G.attr("placeholder",e)}function V(t){return l.getValue(y,t,e.$parent,O)}function F(t){return l.getValue(y,t,e.$parent,E)}function k(t){return String(l.getValue(y,t,e.$parent,L))}function Q(t){return l.getValue(y,t,e.$parent,C)}function R(t){return l.getValue(y,t,e.$parent,P)||""}function j(t){return l.getValue(q,t,e.$parent,A)}function U(r,o){var i=H(e.$parent,{$query:r,$selectedAs:o}),u=0;return e.selectorPosition="prompt"===et.newItem?!1:0,r||o||(e.oldQuery=null),m&&(i.$promise&&!i.$resolved||angular.isFunction(i.then))&&(n.cancel(m),u=et.debounce),m=n(function(){return e.showLoader=!0,t.when(i.$promise||i).then(function(t){if(!o){var n=x?e.output:[],i=lt(l.objToArr(t),r,k,at(e.$parent)),u=l.intersection(i,n,V,V,!0),s=j(u);e.groups=X(s),B()}return t}).finally(function(){e.showLoader=!1})},u)}function B(){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(angular.version.major<=1&&angular.version.minor<=3&&o.sort(),t=0;t=T)return o.addClass("limited"),void n(function(){o.removeClass("limited")},150);var r=e.groups[R(t)]=e.groups[R(t)]||[],i=E?F(t):t;r.splice(r.indexOf(t),1),x?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(i):[i]):(d.$setViewValue(i),g()),l.groupsIsEmpty(e.groups)&&(e.groups={}),x||et.closeList||W({query:!0}),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(t){c.disabled||!x&&!e.inputHide||(x&&t>=0&&(v=d.$modelValue[t],d.$modelValue.splice(t,1),d.$setViewValue([].concat(d.$modelValue))),x||(v=d.$modelValue,f(),et.cleanModel&&d.$setViewValue(void 0)),nt||!x&&e.backspaceFocus||(e.query=rt(v,h,k)),nt=!1,x&&et.closeList&&W({query:!0}))},e.setSelection=function(t){N||e.selectorPosition===t?N=!1:z(J,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||x&&e.output.length||W()}},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,z(J,e.selectorPosition===n?r:e.selectorPosition-1),N=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,z(J,e.selectorPosition===r?n:e.selectorPosition+1),N=!0,e.query.length||e.isOpen||U(),e.inputHide&&f();break;case 37:case 39:break;case 13:I("enter"),t.preventDefault();break;case 27:x||(g(),et.cleanModel&&d.$setViewValue(v)),W();break;case 8:if(!e.query.length){if((!x||tt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output){e.removeItem(e.output.length-1),tt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=k(t);return ot(n,e.oldQuery||e.query,t,it(e.$parent))},e.getDropdownLabel=function(t){var n=k(t);return ut(n,e.oldQuery||e.query,t,st(e.$parent))},e.getDisableWhen=Q,x&&(d.$isEmpty=function(e){return!e||!e.length}),W(),o[0].addEventListener("click",$,!0),o.on("focus",w),o.on("blur",b)}}}}]),angular.module("oi.select").filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce",function(e){return function(t,n){var r;return n.length>0||angular.isNumber(n)?(t=t.toString(),n=n.toString().replace(/\s+.*/,"").replace(/\\/g,"\\\\"),r=t.replace(new RegExp(n,"gi"),"$&")):r=t,e.trustAsHtml(r)}}]).filter("oiSelectAscSort",function(){function e(e,t,n,r){var o,i,u,s,l=[],a=[],c=[];if(t){for(t=String(t).replace(/\s+.*/,"").replace(/\\/g,"\\\\"),o=0,u=!1;or+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=T&&l.contains(o[0],t.target,"select-dropdown")||(e.inputHide&&e.removeItem(0),!e.isOpen||!et.closeList||"INPUT"===t.target.nodeName&&e.query.length?j(e.query):(W({query:et.editItem&&!nt}),e.$evalAsync()))}function w(){e.isFocused||(e.isFocused=!0,c.disabled||(e.backspaceFocus=!1))}function b(){e.isFocused=!1,x||g(),I("blur"),e.$evalAsync()}function I(r){var o=new RegExp(r).test(et.saveTrigger),i=et.newItem&&e.query,u=angular.isNumber(e.selectorPosition),s=e.order[e.selectorPosition],l=t.reject(),a="blur"!==r||e.query||!et.newItem;o&&(i||u&&s)&&(e.showLoader=!0,l=t.when("blur"!==r&&s||e.query&&Y(e.$parent,{$query:e.query}))),l.then(function(t){a&&e.addItem(t)}).finally(function(){var t=e.order.length-1;e.selectorPosition===t&&z(J,0),et.newItemFn&&!u||n(angular.noop),W()})}function S(){var e=x&&d.$modelValue&&d.$modelValue.length?"":K;G.attr("placeholder",e)}function V(t){return l.getValue(y,t,e.$parent,O)}function F(t){return l.getValue(y,t,e.$parent,E)}function k(t){return String(l.getValue(y,t,e.$parent,P))}function Q(t){return l.getValue(y,t,e.$parent,C)}function R(t){return l.getValue(y,t,e.$parent,L)||""}function U(t){return l.getValue(q,t,e.$parent,A)}function j(r,o){var i=H(e.$parent,{$query:r,$selectedAs:o}),u=0;return e.selectorPosition="prompt"===et.newItem?!1:0,r||o||(e.oldQuery=null),m&&(i.$promise&&!i.$resolved||angular.isFunction(i.then))&&(n.cancel(m),u=et.debounce),m=n(function(){return e.showLoader=!0,t.when(i.$promise||i).then(function(t){if(!o){var n=x?e.output:[],i=lt(l.objToArr(t),r,k,at(e.$parent)),u=l.intersection(i,n,V,V,!0),s=U(u);e.groups=X(s),B()}return t}).finally(function(){e.showLoader=!1})},u)}function B(){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(angular.version.major<=1&&angular.version.minor<=3&&o.sort(),t=0;t=T)return o.addClass("limited"),void n(function(){o.removeClass("limited")},150);var r=e.groups[R(t)]=e.groups[R(t)]||[],i=E?F(t):t;r.splice(r.indexOf(t),1),x?d.$setViewValue(angular.isArray(d.$modelValue)?d.$modelValue.concat(i):[i]):(d.$setViewValue(i),g()),l.groupsIsEmpty(e.groups)&&(e.groups={}),x||et.closeList||W({query:!0}),e.oldQuery=e.oldQuery||e.query,e.query="",e.backspaceFocus=!1}},e.removeItem=function(t){c.disabled||!x&&!e.inputHide||(x&&t>=0&&(v=d.$modelValue[t],d.$modelValue.splice(t,1),d.$setViewValue([].concat(d.$modelValue))),x||(v=d.$modelValue,f(),et.cleanModel&&d.$setViewValue(void 0)),nt||!x&&e.backspaceFocus||(e.query=rt(v,h,k)),tt&&(nt=!1,o.removeClass("cleanMode")),x&&et.closeList&&W({query:!0}))},e.setSelection=function(t){_||e.selectorPosition===t?_=!1:z(J,t)},e.keyUp=function(t){switch(t.keyCode){case 8:e.query.length||x&&e.output.length||W()}},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,z(J,e.selectorPosition===n?r:e.selectorPosition-1),_=!0;break;case 40:e.selectorPosition=angular.isNumber(e.selectorPosition)?e.selectorPosition:n-1,z(J,e.selectorPosition===r?n:e.selectorPosition+1),_=!0,e.query.length||e.isOpen||j(),e.inputHide&&f();break;case 37:case 39:break;case 13:I("enter"),t.preventDefault();break;case 27:x||(g(),et.cleanModel&&d.$setViewValue(v)),W();break;case 8:if(!e.query.length){if((!x||tt)&&(e.backspaceFocus=!0),e.backspaceFocus&&e.output){e.removeItem(e.output.length-1),tt&&t.preventDefault();break}e.backspaceFocus=!e.backspaceFocus;break}default:return e.inputHide&&f(),e.backspaceFocus=!1,!1}},e.getSearchLabel=function(t){var n=k(t);return ot(n,e.oldQuery||e.query,t,it(e.$parent))},e.getDropdownLabel=function(t){var n=k(t);return ut(n,e.oldQuery||e.query,t,st(e.$parent))},e.getDisableWhen=Q,x&&(d.$isEmpty=function(e){return!e||!e.length}),W(),o[0].addEventListener("click",$,!0),o.on("focus",w),o.on("blur",b)}}}}]),angular.module("oi.select").filter("oiSelectCloseIcon",["$sce",function(e){return function(t){var n='×';return e.trustAsHtml(t+n)}}]).filter("oiSelectHighlight",["$sce",function(e){return function(t,n){var r;return n.length>0||angular.isNumber(n)?(t=t.toString(),n=n.toString().replace(/\s+.*/,"").replace(/\\/g,"\\\\"),r=t.replace(new RegExp(n,"gi"),"$&")):r=t,e.trustAsHtml(r)}}]).filter("oiSelectAscSort",function(){function e(e,t,n,r){var o,i,u,s,l=[],a=[],c=[];if(t){for(t=String(t).replace(/\s+.*/,"").replace(/\\/g,"\\\\"),o=0,u=!1;oAutocomplete oi-options="item.name for (key, item) in shopObj" ng-model="bundle" -multiple oi-select-options="{ newItem: 'autocomplete', newItemModel: {id: null, name: $query, category: 'shoes'}, diff --git a/docs/examples/prompt/controller.js b/docs/examples/prompt/controller.js index fad9472..9ca4fbe 100644 --- a/docs/examples/prompt/controller.js +++ b/docs/examples/prompt/controller.js @@ -4,6 +4,6 @@ angular.module('selectDemo') $scope.shopObj = ShopObj.get(); $scope.shopObj.$promise.then(function(data) { - $scope.bundle = [data[5]]; + $scope.bundle = data[5]; }); }); diff --git a/docs/examples/prompt/template.html b/docs/examples/prompt/template.html index 0c06877..60c3a67 100644 --- a/docs/examples/prompt/template.html +++ b/docs/examples/prompt/template.html @@ -6,7 +6,6 @@

Prompt

oi-options="item.name for (key, item) in shopObj" ng-model="bundle" -multiple oi-select-options="{ newItem: 'prompt', newItemModel: {id: null, name: $query, category: 'shoes'}, diff --git a/src/directives.js b/src/directives.js index 71a6446..bae5ca5 100644 --- a/src/directives.js +++ b/src/directives.js @@ -80,7 +80,7 @@ angular.module('oi.select') }; } - if (options.cleanModel) { + if (options.cleanModel && (!editItem || editItemCorrect)) { element.addClass('cleanMode'); } @@ -250,7 +250,10 @@ angular.module('oi.select') scope.query = editItemFn(removedItem, lastQuery, getLabel); } - editItemCorrect = false; + if (editItem) { + editItemCorrect = false; + element.removeClass('cleanMode'); + } if (multiple && options.closeList) { resetMatches({query: true}); @@ -335,6 +338,9 @@ angular.module('oi.select') break; } default: /* any key */ + if (scope.inputHide) { + cleanInput(); + } scope.backspaceFocus = false; return false; //preventDefaults } diff --git a/src/services.js b/src/services.js index c847985..78af4dc 100644 --- a/src/services.js +++ b/src/services.js @@ -79,7 +79,9 @@ angular.module('oi.select') element[0].addEventListener('blur', blurHandler, true); inputElement.on('focus', focusHandler); - function blurHandler() { + function blurHandler(event) { + if (event.target.nodeName !== 'INPUT') return; //for IE + isBlur = false; if (isMousedown) {