diff --git a/bower.json b/bower.json index d90466f..fb22433 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-selector", - "version": "1.3.14", + "version": "1.3.15", "authors": [ "Indri Muska " ], diff --git a/dist/angular-selector.js b/dist/angular-selector.js index 015759d..3e02aa6 100644 --- a/dist/angular-selector.js +++ b/dist/angular-selector.js @@ -185,12 +185,14 @@ if (!angular.isDefined(scope.remoteValidation)) scope.remoteValidation = false; if (scope.remote) - $q.when(!scope.hasValue() || !scope.remoteValidation - ? angular.noop - : scope.fetchValidation(scope.value) - ).then(function () { - scope.$watch('search', function () { - $timeout(scope.fetch); + $timeout(function () { + $q.when(!scope.hasValue() || !scope.remoteValidation + ? angular.noop + : scope.fetchValidation(scope.value) + ).then(function () { + scope.$watch('search', function () { + $timeout(scope.fetch); + }); }); }); @@ -273,6 +275,7 @@ scope.close = function () { scope.isOpen = false; scope.resetInput(); + if (scope.remote) $timeout(scope.fetch); }; scope.decrementHighlighted = function () { scope.highlight(scope.highlighted - 1); @@ -600,4 +603,4 @@ return new Selector($filter, $timeout, $window, $http, $q); }]); -})(window.angular); +})(window.angular); \ No newline at end of file diff --git a/dist/angular-selector.min.js b/dist/angular-selector.min.js index 9f7c102..c0d8b3d 100644 --- a/dist/angular-selector.min.js +++ b/dist/angular-selector.min.js @@ -1,2 +1,2 @@ -/*! angular-selector - v1.3.14 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ -!function(a){var b,c,d,e,f,g={up:38,down:40,left:37,right:39,escape:27,enter:13,backspace:8,"delete":46,shift:16,leftCmd:91,rightCmd:93,ctrl:17,alt:18,tab:9},h=function(){function h(a){return a instanceof HTMLElement?a.ownerDocument&&a.ownerDocument.defaultView.opener?a.ownerDocument.defaultView.getComputedStyle(a):window.getComputedStyle(a):{}}function i(a,g,h,i,j){this.restrict="EAC",this.replace=!0,this.transclude=!0,this.scope={name:"@?",value:"=model",disabled:"=?disable",required:"=?require",multiple:"=?multi",placeholder:"@?",valueAttr:"@",labelAttr:"@?",groupAttr:"@?",options:"=?",debounce:"=?",create:"&?",rtl:"=?",api:"=?",change:"&?",remote:"&?",remoteParam:"@?",remoteValidation:"&?",remoteValidationParam:"@?",removeButton:"=?",softDelete:"=?",closeAfterSelection:"=?",viewItemTemplate:"=?",dropdownItemTemplate:"=?",dropdownCreateTemplate:"=?",dropdownGroupTemplate:"=?"},this.templateUrl="selector/selector.html",b=a,c=g,d=h,e=i,f=j}return i.prototype.$inject=["$filter","$timeout","$window","$http","$q"],i.prototype.link=function(i,j,k,l,m){m(i,function(i,l){var m=b("filter"),n=a.element(j[0].querySelector(".selector-input input")),o=a.element(j[0].querySelector(".selector-dropdown")),p=n.controller("ngModel"),q=j.find("select").controller("ngModel"),r=f.defer(),s={api:{},search:"",selectedValues:[],highlighted:0,valueAttr:null,labelAttr:"label",groupAttr:"group",options:[],debounce:0,remoteParam:"q",remoteValidationParam:"value",removeButton:!0,viewItemTemplate:"selector/item-default.html",dropdownItemTemplate:"selector/item-default.html",dropdownCreateTemplate:"selector/item-create.html",dropdownGroupTemplate:"selector/group-default.html"};a.isDefined(l.value)||(l.value=l.multiple?[]:""),a.forEach(s,function(b,c){a.isDefined(l[c])||(l[c]=b)}),a.forEach(["name","valueAttr","labelAttr"],function(a){k[a]||(k[a]=l[a])}),l.getObjValue=function(b,c){var d;if(!a.isDefined(b)||!a.isDefined(c))return b;if(c=a.isArray(c)?c:c.split("."),d=c.shift(),d.indexOf("[")>0){var e=d.match(/(\w+)\[(\d+)\]/);null!==e&&(b=b[e[1]],d=e[2])}return 0===c.length?b[d]:l.getObjValue(b[d],c)},l.setObjValue=function(b,c,d){var e;if(a.isDefined(b)||(b={}),c=a.isArray(c)?c:c.split("."),e=c.shift(),e.indexOf("[")>0){var f=e.match(/(\w+)\[(\d+)\]/);null!==f&&(b=b[f[1]],e=f[2])}return b[e]=0===c.length?d:l.setObjValue(b[e],c,d),b},l.optionValue=function(a){return null==l.valueAttr?a:l.getObjValue(a,l.valueAttr)},l.optionEquals=function(b,c){return a.equals(l.optionValue(b),a.isDefined(c)?c:l.value)},l.setValue=function(a){l.multiple?l.value=null==l.valueAttr?a||[]:(a||[]).map(function(a){return l.getObjValue(a,l.valueAttr)}):l.value=null==l.valueAttr?a||{}:l.getObjValue(a||{},l.valueAttr)},l.hasValue=function(){return l.multiple?(l.value||[]).length>0:null==l.valueAttr?!a.equals({},l.value):!!l.value},l.request=function(b,c,d,g){var h,i={};if(l.disabled)return f.reject();if(!a.isDefined(d))throw"Remote attribute is not defined";if(l.loading=!0,l.options=[],i[b]=c,h=d(i),"function"!=typeof h.then){var j={method:"GET",cache:!0,params:{}};a.extend(j,h),a.extend(j.params,h.params),j.params[g]=c,h=e(j)}return h.then(function(a){l.options=a.data||a,l.filterOptions(),l.loading=!1,r.resolve()},function(a){throw l.loading=!1,r.reject(),"Error while fetching data: "+(a.message||a)}),h},l.fetch=function(){return l.request("search",l.search||"",l.remote,l.remoteParam)},l.fetchValidation=function(a){return l.request("value",a,l.remoteValidation,l.remoteValidationParam)},a.isDefined(l.remote)?a.isDefined(l.remoteValidation)||(l.remoteValidation=!1):(l.remote=!1,l.remoteValidation=!1,r.resolve()),l.remote&&f.when(l.hasValue()&&l.remoteValidation?l.fetchValidation(l.value):a.noop).then(function(){l.$watch("search",function(){c(l.fetch)})}),l.optionToObject=function(b,c){var d={},e=a.element(b);a.forEach(b.dataset,function(a,b){b.match(/^\$/)||(d[b]=a)}),b.value&&l.setObjValue(d,l.valueAttr||"value",b.value),e.text()&&l.setObjValue(d,l.labelAttr,e.text().trim()),a.isDefined(c)&&l.setObjValue(d,l.groupAttr,c),l.options.push(d),!e.attr("selected")||!l.multiple&&l.hasValue()||(l.multiple?(l.value||(l.value=[]),l.value.push(l.optionValue(d))):l.value||(l.value=l.optionValue(d)))},l.fillWithHtml=function(){l.options=[],a.forEach(i,function(b){var c=(b.tagName||"").toLowerCase();"option"==c&&l.optionToObject(b),"optgroup"==c&&a.forEach(b.querySelectorAll("option"),function(a){l.optionToObject(a,(b.attributes.label||{}).value)})}),l.updateSelected()},l.initialize=function(){l.remote||a.isArray(l.options)&&l.options.length||l.fillWithHtml(),l.hasValue()&&(l.multiple?a.isArray(l.value)||(l.value=[l.value]):a.isArray(l.value)&&(l.value=l.value[0]),l.updateSelected(),l.filterOptions(),l.updateValue())},l.$watch("multiple",function(){c(l.setInputWidth),r.promise.then(l.initialize,l.initialize)}),l.dropdownPosition=function(){var a=n.parent()[0],b=h(a),c=parseFloat(b.marginTop||0),d=parseFloat(b.marginLeft||0);o.css({top:a.offsetTop+a.offsetHeight+c+"px",left:a.offsetLeft+d+"px",width:a.offsetWidth+"px"})},l.open=function(){l.isOpen=!0,l.dropdownPosition()},l.close=function(){l.isOpen=!1,l.resetInput()},l.decrementHighlighted=function(){l.highlight(l.highlighted-1),l.scrollToHighlighted()},l.incrementHighlighted=function(){l.highlight(l.highlighted+1),l.scrollToHighlighted()},l.highlight=function(a){k.create&&l.search&&-1==a?l.highlighted=-1:l.filteredOptions.length&&(l.highlighted=(l.filteredOptions.length+a)%l.filteredOptions.length)},l.scrollToHighlighted=function(){var a=o[0],b=a.querySelectorAll("li.selector-option")[l.highlighted],d=h(b),e=parseFloat(d.marginTop||0),f=parseFloat(d.marginBottom||0);l.filteredOptions.length&&(b.offsetTop+b.offsetHeight+f>a.scrollTop+a.offsetHeight&&c(function(){a.scrollTop=b.offsetTop+b.offsetHeight+f-a.offsetHeight}),b.offsetTop-e0:b.indexOf(c)>=0},l.filterOptions=function(){l.filteredOptions=m(l.options||[],l.search),l.multiple&&(l.filteredOptions=l.filteredOptions.filter(function(b){var c=a.isArray(l.selectedValues)?l.selectedValues:[l.selectedValues];return!l.inOptions(c,b)})),l.highlighted>=l.filteredOptions.length&&l.highlight(l.filteredOptions.length-1),-1==l.highlighted&&l.filteredOptions.length>0&&l.highlight(0)},l.measureWidth=function(){var b,c=h(n[0]),d=a.element('');return d.text(n.val()||(l.hasValue()?"":l.placeholder)||""),a.element(document.body).append(d),a.forEach(["fontFamily","fontSize","fontWeight","fontStyle","letterSpacing","textTransform","wordSpacing","textIndent"],function(a){d.css(a,c[a])}),b=d[0].offsetWidth,d.remove(),b},l.setInputWidth=function(){var a=l.measureWidth()+1;n.css("width",a+"px")},l.resetInput=function(){n.val(""),l.setInputWidth(),c(function(){l.search=""})},l.$watch("[search, options, value]",function(){l.filterOptions(),c(function(){l.setInputWidth(),l.isOpen&&l.dropdownPosition()})},!0),l.updateValue=function(b){a.isDefined(b)||(b=l.selectedValues||[]),l.setValue(l.multiple?b:b[0])},l.$watch("selectedValues",function(b,c){a.equals(b,c)||(l.updateValue(),a.isFunction(l.change)&&l.change(l.multiple?{newValue:b,oldValue:c}:{newValue:(b||[])[0],oldValue:(c||[])[0]}))},!0),l.$watchCollection("options",function(b,c){a.equals(b,c)||l.remote||l.updateSelected()}),l.updateSelected=function(){l.multiple?l.selectedValues=(l.value||[]).map(function(a){return m(l.options,function(b){return l.optionEquals(b,a)})[0]}).filter(function(b){return a.isDefined(b)}):l.selectedValues=(l.options||[]).filter(function(a){return l.optionEquals(a)}).slice(0,1)},l.$watch("value",function(b,c){a.equals(b,c)||f.when(l.remote&&l.remoteValidation&&l.hasValue()?l.fetchValidation(b):a.noop).then(function(){l.updateSelected(),l.filterOptions(),l.updateValue()})},!0),n=a.element(j[0].querySelector(".selector-input input")).on("focus",function(){c(function(){l.$apply(l.open)})}).on("blur",function(){l.$apply(l.close)}).on("keydown",function(a){l.$apply(function(){l.keydown(a)})}).on("input",function(){l.setInputWidth()}),o.on("mousedown",function(a){a.preventDefault()}),a.element(d).on("resize",function(){l.dropdownPosition()}),l.$watch(function(){return p.$pristine},function(a){q[a?"$setPristine":"$setDirty"]()}),l.$watch(function(){return p.$touched},function(a){q[a?"$setTouched":"$setUntouched"]()}),a.forEach(["open","close","fetch"],function(a){l.api[a]=l[a]}),l.api.focus=function(){n[0].focus()},l.api.set=function(a){return l.value=a},l.api.unset=function(b){var c=b?(l.selectedValues||[]).filter(function(a){return l.optionEquals(a,b)}):l.selectedValues,d=l.selectedValues.map(function(a,b){return l.inOptions(c,a)?b:-1}).filter(function(a){return a>=0});a.forEach(d,function(a,b){l.unset(a-b)})}})},i}();a.module("selector",[]).run(["$templateCache",function(a){a.put("selector/selector.html",'
'),a.put("selector/item-create.html",'Add '),a.put("selector/item-default.html",''),a.put("selector/group-default.html",'')}]).directive("selector",["$filter","$timeout","$window","$http","$q",function(a,b,c,d,e){return new h(a,b,c,d,e)}])}(window.angular); \ No newline at end of file +/*! angular-selector - v1.3.15 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ +!function(a){var b,c,d,e,f,g={up:38,down:40,left:37,right:39,escape:27,enter:13,backspace:8,"delete":46,shift:16,leftCmd:91,rightCmd:93,ctrl:17,alt:18,tab:9},h=function(){function h(a){return a instanceof HTMLElement?a.ownerDocument&&a.ownerDocument.defaultView.opener?a.ownerDocument.defaultView.getComputedStyle(a):window.getComputedStyle(a):{}}function i(a,g,h,i,j){this.restrict="EAC",this.replace=!0,this.transclude=!0,this.scope={name:"@?",value:"=model",disabled:"=?disable",required:"=?require",multiple:"=?multi",placeholder:"@?",valueAttr:"@",labelAttr:"@?",groupAttr:"@?",options:"=?",debounce:"=?",create:"&?",rtl:"=?",api:"=?",change:"&?",remote:"&?",remoteParam:"@?",remoteValidation:"&?",remoteValidationParam:"@?",removeButton:"=?",softDelete:"=?",closeAfterSelection:"=?",viewItemTemplate:"=?",dropdownItemTemplate:"=?",dropdownCreateTemplate:"=?",dropdownGroupTemplate:"=?"},this.templateUrl="selector/selector.html",b=a,c=g,d=h,e=i,f=j}return i.prototype.$inject=["$filter","$timeout","$window","$http","$q"],i.prototype.link=function(i,j,k,l,m){m(i,function(i,l){var m=b("filter"),n=a.element(j[0].querySelector(".selector-input input")),o=a.element(j[0].querySelector(".selector-dropdown")),p=n.controller("ngModel"),q=j.find("select").controller("ngModel"),r=f.defer(),s={api:{},search:"",selectedValues:[],highlighted:0,valueAttr:null,labelAttr:"label",groupAttr:"group",options:[],debounce:0,remoteParam:"q",remoteValidationParam:"value",removeButton:!0,viewItemTemplate:"selector/item-default.html",dropdownItemTemplate:"selector/item-default.html",dropdownCreateTemplate:"selector/item-create.html",dropdownGroupTemplate:"selector/group-default.html"};a.isDefined(l.value)||(l.value=l.multiple?[]:""),a.forEach(s,function(b,c){a.isDefined(l[c])||(l[c]=b)}),a.forEach(["name","valueAttr","labelAttr"],function(a){k[a]||(k[a]=l[a])}),l.getObjValue=function(b,c){var d;if(!a.isDefined(b)||!a.isDefined(c))return b;if(c=a.isArray(c)?c:c.split("."),d=c.shift(),d.indexOf("[")>0){var e=d.match(/(\w+)\[(\d+)\]/);null!==e&&(b=b[e[1]],d=e[2])}return 0===c.length?b[d]:l.getObjValue(b[d],c)},l.setObjValue=function(b,c,d){var e;if(a.isDefined(b)||(b={}),c=a.isArray(c)?c:c.split("."),e=c.shift(),e.indexOf("[")>0){var f=e.match(/(\w+)\[(\d+)\]/);null!==f&&(b=b[f[1]],e=f[2])}return b[e]=0===c.length?d:l.setObjValue(b[e],c,d),b},l.optionValue=function(a){return null==l.valueAttr?a:l.getObjValue(a,l.valueAttr)},l.optionEquals=function(b,c){return a.equals(l.optionValue(b),a.isDefined(c)?c:l.value)},l.setValue=function(a){l.multiple?l.value=null==l.valueAttr?a||[]:(a||[]).map(function(a){return l.getObjValue(a,l.valueAttr)}):l.value=null==l.valueAttr?a||{}:l.getObjValue(a||{},l.valueAttr)},l.hasValue=function(){return l.multiple?(l.value||[]).length>0:null==l.valueAttr?!a.equals({},l.value):!!l.value},l.request=function(b,c,d,g){var h,i={};if(l.disabled)return f.reject();if(!a.isDefined(d))throw"Remote attribute is not defined";if(l.loading=!0,l.options=[],i[b]=c,h=d(i),"function"!=typeof h.then){var j={method:"GET",cache:!0,params:{}};a.extend(j,h),a.extend(j.params,h.params),j.params[g]=c,h=e(j)}return h.then(function(a){l.options=a.data||a,l.filterOptions(),l.loading=!1,r.resolve()},function(a){throw l.loading=!1,r.reject(),"Error while fetching data: "+(a.message||a)}),h},l.fetch=function(){return l.request("search",l.search||"",l.remote,l.remoteParam)},l.fetchValidation=function(a){return l.request("value",a,l.remoteValidation,l.remoteValidationParam)},a.isDefined(l.remote)?a.isDefined(l.remoteValidation)||(l.remoteValidation=!1):(l.remote=!1,l.remoteValidation=!1,r.resolve()),l.remote&&c(function(){f.when(l.hasValue()&&l.remoteValidation?l.fetchValidation(l.value):a.noop).then(function(){l.$watch("search",function(){c(l.fetch)})})}),l.optionToObject=function(b,c){var d={},e=a.element(b);a.forEach(b.dataset,function(a,b){b.match(/^\$/)||(d[b]=a)}),b.value&&l.setObjValue(d,l.valueAttr||"value",b.value),e.text()&&l.setObjValue(d,l.labelAttr,e.text().trim()),a.isDefined(c)&&l.setObjValue(d,l.groupAttr,c),l.options.push(d),!e.attr("selected")||!l.multiple&&l.hasValue()||(l.multiple?(l.value||(l.value=[]),l.value.push(l.optionValue(d))):l.value||(l.value=l.optionValue(d)))},l.fillWithHtml=function(){l.options=[],a.forEach(i,function(b){var c=(b.tagName||"").toLowerCase();"option"==c&&l.optionToObject(b),"optgroup"==c&&a.forEach(b.querySelectorAll("option"),function(a){l.optionToObject(a,(b.attributes.label||{}).value)})}),l.updateSelected()},l.initialize=function(){l.remote||a.isArray(l.options)&&l.options.length||l.fillWithHtml(),l.hasValue()&&(l.multiple?a.isArray(l.value)||(l.value=[l.value]):a.isArray(l.value)&&(l.value=l.value[0]),l.updateSelected(),l.filterOptions(),l.updateValue())},l.$watch("multiple",function(){c(l.setInputWidth),r.promise.then(l.initialize,l.initialize)}),l.dropdownPosition=function(){var a=n.parent()[0],b=h(a),c=parseFloat(b.marginTop||0),d=parseFloat(b.marginLeft||0);o.css({top:a.offsetTop+a.offsetHeight+c+"px",left:a.offsetLeft+d+"px",width:a.offsetWidth+"px"})},l.open=function(){l.isOpen=!0,l.dropdownPosition()},l.close=function(){l.isOpen=!1,l.resetInput(),l.remote&&c(l.fetch)},l.decrementHighlighted=function(){l.highlight(l.highlighted-1),l.scrollToHighlighted()},l.incrementHighlighted=function(){l.highlight(l.highlighted+1),l.scrollToHighlighted()},l.highlight=function(a){k.create&&l.search&&-1==a?l.highlighted=-1:l.filteredOptions.length&&(l.highlighted=(l.filteredOptions.length+a)%l.filteredOptions.length)},l.scrollToHighlighted=function(){var a=o[0],b=a.querySelectorAll("li.selector-option")[l.highlighted],d=h(b),e=parseFloat(d.marginTop||0),f=parseFloat(d.marginBottom||0);l.filteredOptions.length&&(b.offsetTop+b.offsetHeight+f>a.scrollTop+a.offsetHeight&&c(function(){a.scrollTop=b.offsetTop+b.offsetHeight+f-a.offsetHeight}),b.offsetTop-e0:b.indexOf(c)>=0},l.filterOptions=function(){l.filteredOptions=m(l.options||[],l.search),l.multiple&&(l.filteredOptions=l.filteredOptions.filter(function(b){var c=a.isArray(l.selectedValues)?l.selectedValues:[l.selectedValues];return!l.inOptions(c,b)})),l.highlighted>=l.filteredOptions.length&&l.highlight(l.filteredOptions.length-1),-1==l.highlighted&&l.filteredOptions.length>0&&l.highlight(0)},l.measureWidth=function(){var b,c=h(n[0]),d=a.element('');return d.text(n.val()||(l.hasValue()?"":l.placeholder)||""),a.element(document.body).append(d),a.forEach(["fontFamily","fontSize","fontWeight","fontStyle","letterSpacing","textTransform","wordSpacing","textIndent"],function(a){d.css(a,c[a])}),b=d[0].offsetWidth,d.remove(),b},l.setInputWidth=function(){var a=l.measureWidth()+1;n.css("width",a+"px")},l.resetInput=function(){n.val(""),l.setInputWidth(),c(function(){l.search=""})},l.$watch("[search, options, value]",function(){l.filterOptions(),c(function(){l.setInputWidth(),l.isOpen&&l.dropdownPosition()})},!0),l.updateValue=function(b){a.isDefined(b)||(b=l.selectedValues||[]),l.setValue(l.multiple?b:b[0])},l.$watch("selectedValues",function(b,c){a.equals(b,c)||(l.updateValue(),a.isFunction(l.change)&&l.change(l.multiple?{newValue:b,oldValue:c}:{newValue:(b||[])[0],oldValue:(c||[])[0]}))},!0),l.$watchCollection("options",function(b,c){a.equals(b,c)||l.remote||l.updateSelected()}),l.updateSelected=function(){l.multiple?l.selectedValues=(l.value||[]).map(function(a){return m(l.options,function(b){return l.optionEquals(b,a)})[0]}).filter(function(b){return a.isDefined(b)}):l.selectedValues=(l.options||[]).filter(function(a){return l.optionEquals(a)}).slice(0,1)},l.$watch("value",function(b,c){a.equals(b,c)||f.when(l.remote&&l.remoteValidation&&l.hasValue()?l.fetchValidation(b):a.noop).then(function(){l.updateSelected(),l.filterOptions(),l.updateValue()})},!0),n=a.element(j[0].querySelector(".selector-input input")).on("focus",function(){c(function(){l.$apply(l.open)})}).on("blur",function(){l.$apply(l.close)}).on("keydown",function(a){l.$apply(function(){l.keydown(a)})}).on("input",function(){l.setInputWidth()}),o.on("mousedown",function(a){a.preventDefault()}),a.element(d).on("resize",function(){l.dropdownPosition()}),l.$watch(function(){return p.$pristine},function(a){q[a?"$setPristine":"$setDirty"]()}),l.$watch(function(){return p.$touched},function(a){q[a?"$setTouched":"$setUntouched"]()}),a.forEach(["open","close","fetch"],function(a){l.api[a]=l[a]}),l.api.focus=function(){n[0].focus()},l.api.set=function(a){return l.value=a},l.api.unset=function(b){var c=b?(l.selectedValues||[]).filter(function(a){return l.optionEquals(a,b)}):l.selectedValues,d=l.selectedValues.map(function(a,b){return l.inOptions(c,a)?b:-1}).filter(function(a){return a>=0});a.forEach(d,function(a,b){l.unset(a-b)})}})},i}();a.module("selector",[]).run(["$templateCache",function(a){a.put("selector/selector.html",'
'),a.put("selector/item-create.html",'Add '),a.put("selector/item-default.html",''),a.put("selector/group-default.html",'')}]).directive("selector",["$filter","$timeout","$window","$http","$q",function(a,b,c,d,e){return new h(a,b,c,d,e)}])}(window.angular); \ No newline at end of file diff --git a/package.json b/package.json index d0c881b..138a584 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-selector", - "version": "1.3.14", + "version": "1.3.15", "description": "A native AngularJS directive that transform a simple `