diff --git a/dist/angular-selector.js b/dist/angular-selector.js index c29ef2f..d1e5517 100644 --- a/dist/angular-selector.js +++ b/dist/angular-selector.js @@ -165,6 +165,7 @@ promise .then(function (data) { scope.options = data.data || data; + allOptions = allOptions.concat(scope.options); scope.filterOptions(); scope.loading = false; initDeferred.resolve(); @@ -492,12 +493,15 @@ // Update selected values scope.updateSelected = function () { if (!scope.multiple) scope.selectedValues = (scope.options || []).filter(function (option) { return scope.optionEquals(option); }).slice(0, 1); - else - scope.selectedValues = (scope.value || []).map(function (value) { - return filter(scope.options, function (option) { - return scope.optionEquals(option, value); - })[0]; - }).filter(function (value) { return angular.isDefined(value); }).slice(0, scope.limit); + else { + var val; + val = (scope.value || []).map(function (value) { + return filter(scope.options.concat(allOptions), function (option) { + return scope.optionEquals(option, value); + })[0]; + }).filter(function (value) { return angular.isDefined(value); }).slice(0, scope.limit); + scope.selectedValues = val; + } }; scope.$watch('value', function (newValue, oldValue) { if (angular.equals(newValue, oldValue)) return; @@ -616,4 +620,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 9eb15c8..7a24b6d 100644 --- a/dist/angular-selector.min.js +++ b/dist/angular-selector.min.js @@ -1,2 +1 @@ -/*! angular-selector - v1.5.0 - 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",disableSearch:"=?",required:"=?require",multiple:"=?multi",placeholder:"@?",valueAttr:"@",labelAttr:"@?",groupAttr:"@?",options:"=?",debounce:"=?",create:"&?",limit:"=?",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:"",disableSearch:!1,selectedValues:[],highlighted:0,valueAttr:null,labelAttr:"label",groupAttr:"group",options:[],debounce:0,limit:1/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.multiple&&(l.value=[]),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:!!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.multiple&&(l.selectedValues||[]).length>=l.limit||(l.isOpen=!0,l.dropdownPosition(),c(l.scrollToHighlighted))},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&&a==-1?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-e=l.limit||(a.isDefined(b)||(b=l.filteredOptions[l.highlighted]),l.multiple?(l.selectedValues||(l.selectedValues=[]),l.selectedValues.indexOf(b)<0&&l.selectedValues.push(b)):l.selectedValues=[b],(!l.multiple||l.closeAfterSelection||(l.selectedValues||[]).length>=l.limit)&&l.close(),l.resetInput(),q.$setDirty())},l.unset=function(b){l.multiple?l.selectedValues.splice(a.isDefined(b)?b:l.selectedValues.length-1,1):l.selectedValues=[],l.resetInput(),q.$setDirty()},l.keydown=function(a){switch(a.keyCode){case g.up:if(!l.isOpen)break;l.decrementHighlighted(),a.preventDefault();break;case g.down:l.isOpen?l.incrementHighlighted():l.open(),a.preventDefault();break;case g.escape:l.highlight(0),l.close();break;case g.enter:l.isOpen&&(k.create&&l.search&&l.highlighted==-1?l.createOption(a.target.value):l.filteredOptions.length&&l.set(),a.preventDefault());break;case g.backspace:if(!n.val()){var b=l.getObjValue(l.selectedValues.slice(-1)[0]||{},l.labelAttr||"");l.unset(),l.open(),l.softDelete&&!l.disableSearch&&c(function(){l.search=b}),a.preventDefault()}break;case g.left:case g.right:case g.shift:case g.ctrl:case g.alt:case g.tab:case g.leftCmd:case g.rightCmd:break;default:!l.multiple&&l.hasValue()?a.preventDefault():(l.open(),l.highlight(0))}},l.inOptions=function(b,c){return l.remote?b.filter(function(b){return a.equals(c,b)}).length>0:b.indexOf(c)>=0},l.filterOptions=function(){if(l.filteredOptions=m(l.options||[],l.search),a.isArray(l.selectedValues)||(l.selectedValues=[]),l.multiple)l.filteredOptions=l.filteredOptions.filter(function(a){return!l.inOptions(l.selectedValues,a)});else{var b=l.filteredOptions.indexOf(l.selectedValues[0]);b>=0&&l.highlight(b)}},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)}).slice(0,l.limit):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 +!function(a){var c,d,e,f,g,b={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,b,h,i,j){this.replace=!0,this.transclude=!0,this.scope={name:"@?",value:"=model",disabled:"=?disable",disableSearch:"=?",required:"=?require",multiple:"=?multi",placeholder:"@?",valueAttr:"@",labelAttr:"@?",groupAttr:"@?",options:"=?",debounce:"=?",create:"&?",limit:"=?",rtl:"=?",api:"=?",change:"&?",remote:"&?",remoteParam:"@?",remoteValidation:"&?",remoteValidationParam:"@?",removeButton:"=?",softDelete:"=?",closeAfterSelection:"=?",viewItemTemplate:"=?",dropdownItemTemplate:"=?",dropdownCreateTemplate:"=?",dropdownGroupTemplate:"=?"},this.templateUrl="selector/selector.html",c=a,d=b,e=h,f=i,g=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=c("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=g.defer(),s=[],t={api:{},search:"",disableSearch:!1,selectedValues:[],highlighted:0,valueAttr:null,labelAttr:"label",groupAttr:"group",options:[],debounce:0,limit:1/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.multiple&&(l.value=[]),a.forEach(t,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:!!l.value},l.request=function(b,c,d,e){var h,i={};if(l.disabled)return g.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[e]=c,h=f(j)}return h.then(function(a){l.options=a.data||a,s=s.concat(l.options),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&&d(function(){g.when(l.hasValue()&&l.remoteValidation?l.fetchValidation(l.value):a.noop).then(function(){l.$watch("search",function(){d(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(){d(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.multiple&&(l.selectedValues||[]).length>=l.limit||(l.isOpen=!0,l.dropdownPosition(),d(l.scrollToHighlighted))},l.close=function(){l.isOpen=!1,l.resetInput(),l.remote&&d(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&&a==-1?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],c=h(b),e=parseFloat(c.marginTop||0),f=parseFloat(c.marginBottom||0);l.filteredOptions.length&&(b.offsetTop+b.offsetHeight+f>a.scrollTop+a.offsetHeight&&d(function(){a.scrollTop=b.offsetTop+b.offsetHeight+f-a.offsetHeight}),b.offsetTop-e=l.limit||(a.isDefined(b)||(b=l.filteredOptions[l.highlighted]),l.multiple?(l.selectedValues||(l.selectedValues=[]),l.selectedValues.indexOf(b)<0&&l.selectedValues.push(b)):l.selectedValues=[b],(!l.multiple||l.closeAfterSelection||(l.selectedValues||[]).length>=l.limit)&&l.close(),l.resetInput(),q.$setDirty())},l.unset=function(b){l.multiple?l.selectedValues.splice(a.isDefined(b)?b:l.selectedValues.length-1,1):l.selectedValues=[],l.resetInput(),q.$setDirty()},l.keydown=function(a){switch(a.keyCode){case b.up:if(!l.isOpen)break;l.decrementHighlighted(),a.preventDefault();break;case b.down:l.isOpen?l.incrementHighlighted():l.open(),a.preventDefault();break;case b.escape:l.highlight(0),l.close();break;case b.enter:l.isOpen&&(k.create&&l.search&&l.highlighted==-1?l.createOption(a.target.value):l.filteredOptions.length&&l.set(),a.preventDefault());break;case b.backspace:if(!n.val()){var c=l.getObjValue(l.selectedValues.slice(-1)[0]||{},l.labelAttr||"");l.unset(),l.open(),l.softDelete&&!l.disableSearch&&d(function(){l.search=c}),a.preventDefault()}break;case b.left:case b.right:case b.shift:case b.ctrl:case b.alt:case b.tab:case b.leftCmd:case b.rightCmd:break;default:!l.multiple&&l.hasValue()?a.preventDefault():(l.open(),l.highlight(0))}},l.inOptions=function(b,c){return l.remote?b.filter(function(b){return a.equals(c,b)}).length>0:b.indexOf(c)>=0},l.filterOptions=function(){if(l.remote?l.filteredOptions=l.options:l.filteredOptions=m(l.options||[],l.search),a.isArray(l.selectedValues)||(l.selectedValues=[]),l.multiple)l.filteredOptions=l.filteredOptions.filter(function(a){return!l.inOptions(l.selectedValues,a)});else{var b=l.filteredOptions.indexOf(l.selectedValues[0]);b>=0&&l.highlight(b)}},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;l.measureWidth()||l.value?n.css("width",a+"px"):n.css("width","100%")},l.resetInput=function(){n.val(""),l.setInputWidth(),d(function(){l.search=""})},l.$watch("[search, options, value]",function(){l.filterOptions(),d(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(){if(l.multiple){var b;b=(l.value||[]).map(function(a){return m(l.options.concat(s),function(b){return l.optionEquals(b,a)})[0]}).filter(function(b){return a.isDefined(b)}).slice(0,l.limit),l.selectedValues=b}else l.selectedValues=(l.options||[]).filter(function(a){return l.optionEquals(a)}).slice(0,1)},l.$watch("value",function(b,c){a.equals(b,c)||g.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(){d(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(e).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/src/angular-selector.js b/src/angular-selector.js index ef56c73..229ac72 100755 --- a/src/angular-selector.js +++ b/src/angular-selector.js @@ -164,6 +164,7 @@ promise .then(function (data) { scope.options = data.data || data; + allOptions = allOptions.concat(scope.options); scope.filterOptions(); scope.loading = false; initDeferred.resolve(); @@ -491,12 +492,15 @@ // Update selected values scope.updateSelected = function () { if (!scope.multiple) scope.selectedValues = (scope.options || []).filter(function (option) { return scope.optionEquals(option); }).slice(0, 1); - else - scope.selectedValues = (scope.value || []).map(function (value) { - return filter(scope.options, function (option) { - return scope.optionEquals(option, value); - })[0]; - }).filter(function (value) { return angular.isDefined(value); }).slice(0, scope.limit); + else { + var val; + val = (scope.value || []).map(function (value) { + return filter(scope.options.concat(allOptions), function (option) { + return scope.optionEquals(option, value); + })[0]; + }).filter(function (value) { return angular.isDefined(value); }).slice(0, scope.limit); + scope.selectedValues = val; + } }; scope.$watch('value', function (newValue, oldValue) { if (angular.equals(newValue, oldValue)) return; @@ -615,4 +619,4 @@ return new Selector($filter, $timeout, $window, $http, $q); }]); -})(window.angular); +})(window.angular); \ No newline at end of file