diff --git a/Gruntfile.js b/Gruntfile.js index fbf8083..1354dea 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -14,6 +14,12 @@ module.exports = function(grunt) { }, file: 'package.json' }, + eslint: { + options: { + configFile: 'eslint.json' + }, + target: ['src/angular-selector.js'] + }, copy: { main: { files: { @@ -56,7 +62,7 @@ module.exports = function(grunt) { }); // Default tasks. - grunt.registerTask('default', ['copy', 'uglify', 'cssmin', 'sync-json']); + grunt.registerTask('default', ['eslint', 'copy', 'uglify', 'cssmin', 'sync-json']); grunt.registerTask('update-patch', ['bumpup:patch', 'default']); }; \ No newline at end of file diff --git a/bower.json b/bower.json index 75aca0f..0f8f0c0 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-selector", - "version": "1.1.0", + "version": "1.1.1", "authors": [ "Indri Muska " ], diff --git a/dist/angular-selector.js b/dist/angular-selector.js index b84fdf6..e2cf891 100644 --- a/dist/angular-selector.js +++ b/dist/angular-selector.js @@ -94,7 +94,7 @@ }; // Remote fetching - scope.fetch = function () { + scope.fetch = function () { var promise, search = scope.search || ''; if (!angular.isDefined(scope.remote)) throw 'Remote attribute is not defined'; @@ -191,8 +191,7 @@ var label = input.parent()[0], styles = getStyles(label), marginTop = parseFloat(styles.marginTop || 0), - marginLeft = parseFloat(styles.marginLeft || 0), - marginRight = parseFloat(styles.marginRight || 0); + marginLeft = parseFloat(styles.marginLeft || 0); dropdown.css({ top: (label.offsetTop + label.offsetHeight + marginTop) + 'px', @@ -444,7 +443,7 @@ }); }; scope.api.unset = function (value) { - var values = !value ? scope.selectedValues : (scope.selectedValues || []).filter(function (option) { return scope.optionEquals(option, value); }); + var values = !value ? scope.selectedValues : (scope.selectedValues || []).filter(function (option) { return scope.optionEquals(option, value); }), indexes = scope.selectedValues.map(function (option, index) { return scope.inOptions(values, option) ? index : -1; diff --git a/dist/angular-selector.min.js b/dist/angular-selector.min.js index 7a6fa5d..beb5b88 100644 --- a/dist/angular-selector.min.js +++ b/dist/angular-selector.min.js @@ -1,2 +1,2 @@ -/*! angular-selector - v1.1.0 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ -!function(a){var 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},c=function(){function c(a){return a instanceof HTMLElement?a.ownerDocument&&a.ownerDocument.defaultView.opener?a.ownerDocument.defaultView.getComputedStyle(a):window.getComputedStyle(a):{}}function d(a,b,c,d,e){this.restrict="EAC",this.replace=!0,this.transclude=!0,this.scope={name:"@?",value:"=model",disabled:"=?disable",multiple:"=?multi",placeholder:"@?",valueAttr:"@",labelAttr:"@?",groupAttr:"@?",options:"=?",create:"&?",rtl:"=?",api:"=?",change:"&?",remote:"&?",remoteParam:"@?",removeButton:"=?",viewItemTemplate:"=?",dropdownItemTemplate:"=?",dropdownGroupTemplate:"=?"},this.templateUrl="selector/selector.html",$filter=a,$timeout=b,$window=c,$http=d,$q=e}return d.prototype.$inject=["$filter","$timeout","$window","$http","$q"],d.prototype.link=function(d,e,f,g,h){h(d,function(d,g){var h=a.element(e[0].querySelector(".selector-input input")),i=a.element(e[0].querySelector(".selector-dropdown")),j=$q.defer(),k={api:{},selectedValues:[],highlighted:0,valueAttr:null,labelAttr:"label",groupAttr:"group",options:[],remoteParam:"q",removeButton:!0,viewItemTemplate:"selector/item-default.html",dropdownItemTemplate:"selector/item-default.html",dropdownGroupTemplate:"selector/group-default.html"};a.isDefined(g.value)||(g.value=g.multiple?[]:""),a.forEach(k,function(b,c){a.isDefined(g[c])||(g[c]=b)}),a.forEach(["name","valueAttr","labelAttr"],function(a){f[a]||(f[a]=g[a])}),g.optionValue=function(a){return null==g.valueAttr?a:a[g.valueAttr]},g.optionEquals=function(b,c){return a.equals(g.optionValue(b),a.isDefined(c)?c:g.value)},g.setValue=function(a){g.multiple?g.value=null==g.valueAttr?a||[]:(a||[]).map(function(a){return a[g.valueAttr]}):g.value=null==g.valueAttr?a||{}:(a||{})[g.valueAttr]},g.hasValue=function(){return g.multiple?(g.value||[]).length>0:null==g.valueAttr?!a.equals({},g.value):!!g.value},g.fetch=function(){var b,c=g.search||"";if(!a.isDefined(g.remote))throw"Remote attribute is not defined";if(g.loading=!0,g.options=[],b=g.remote({search:c}),"function"!=typeof b.then){var d={method:"GET",cache:!0,params:{}};a.extend(d,g.remote()),a.extend(d.params,g.remote().params),d.params[g.remoteParam]=c,b=$http(d)}b.then(function(a){g.options=a.data||a,g.filterSelected(),g.loading=!1,j.resolve()},function(a){throw g.loading=!1,j.reject(),"Error while fetching data: "+(a.message||a)})},a.isDefined(g.remote)||(g.remote=!1,j.resolve()),g.remote&&g.$watch("search",g.fetch),g.optionToObject=function(b,c){var d={},e=a.element(b);a.forEach(b.dataset,function(a,b){b.match(/^\$/)||(d[b]=a)}),b.value&&(d[g.valueAttr||"value"]=b.value),e.text()&&(d[g.labelAttr]=e.text().trim()),a.isDefined(c)&&(d[g.groupAttr]=c),g.options.push(d),!e.attr("selected")||!g.multiple&&g.hasValue()||(g.multiple?(g.value||(g.value=[]),g.value.push(g.optionValue(d))):g.value||(g.value=g.optionValue(d)))},g.fillWithHtml=function(){g.options=[],a.forEach(d,function(b){var c=(b.tagName||"").toLowerCase();"option"==c&&g.optionToObject(b),"optgroup"==c&&a.forEach(b.querySelectorAll("option"),function(a){g.optionToObject(a,(b.attributes.label||{}).value)})}),g.updateSelected()},g.initialize=function(){g.remote||a.isArray(g.options)&&g.options.length||g.fillWithHtml(),g.hasValue()&&(g.multiple?a.isArray(g.value)||(g.value=[g.value]):a.isArray(g.value)&&(g.value=g.value[0]),g.updateSelected(),g.filterSelected(),g.updateValue())},g.$watch("multiple",function(){$timeout(g.setInputWidth),j.promise.then(g.initialize,g.initialize)}),g.dropdownPosition=function(){var a=h.parent()[0],b=c(a),d=parseFloat(b.marginTop||0),e=parseFloat(b.marginLeft||0);parseFloat(b.marginRight||0);i.css({top:a.offsetTop+a.offsetHeight+d+"px",left:a.offsetLeft+e+"px",width:a.offsetWidth+"px"})},g.open=function(){g.isOpen=!0,g.dropdownPosition()},g.close=function(){g.isOpen=!1,g.resetInput()},g.decrementHighlighted=function(){g.highlight(g.highlighted-1),g.scrollToHighlighted()},g.incrementHighlighted=function(){g.highlight(g.highlighted+1),g.scrollToHighlighted()},g.highlight=function(a){g.filteredOptions.length&&(g.highlighted=(g.filteredOptions.length+a)%g.filteredOptions.length)},g.scrollToHighlighted=function(){var a=i[0],b=a.querySelectorAll("li.selector-option")[g.highlighted],d=c(b),e=parseFloat(d.marginTop||0),f=parseFloat(d.marginBottom||0);g.filteredOptions.length&&(b.offsetTop+b.offsetHeight+f>a.scrollTop+a.offsetHeight&&$timeout(function(){a.scrollTop=b.offsetTop+b.offsetHeight+f-a.offsetHeight}),b.offsetTop-e0:b.indexOf(c)>=0},g.filterSelected=function(){g.filteredOptions=$filter("filter")(g.options||[],g.search),g.multiple&&(g.filteredOptions=g.filteredOptions.filter(function(b){var c=a.isArray(g.selectedValues)?g.selectedValues:[g.selectedValues];return!g.inOptions(c,b)})),g.highlighted>=g.filteredOptions.length&&g.highlight(g.filteredOptions.length-1)},g.measureWidth=function(){var b,d=c(h[0]),e=a.element('');return e.text(h.val()||(g.hasValue()?"":g.placeholder)||""),a.element(document.body).append(e),a.forEach(["fontFamily","fontSize","fontWeight","fontStyle","letterSpacing","textTransform","wordSpacing","textIndent"],function(a){e.css(a,d[a])}),b=e[0].offsetWidth,e.remove(),b},g.setInputWidth=function(){var a=g.measureWidth()+1;h.css("width",a+"px")},g.resetInput=function(){h.val(""),g.search="",g.setInputWidth()},g.$watch("[search, options, value]",function(){g.setInputWidth(),g.filterSelected(),g.isOpen&&$timeout(g.dropdownPosition)},!0),g.updateValue=function(b){a.isDefined(b)||(b=g.selectedValues),g.setValue(g.multiple?b:b[0])},g.$watch("selectedValues",function(b,c){a.equals(b,c)||(g.updateValue(),a.isFunction(g.change)&&g.change(g.multiple?{newValue:b,oldValue:c}:{newValue:b[0],oldValue:c[0]}))},!0),g.$watch("options",function(b,c){a.equals(b,c)||g.remote||g.updateSelected()}),g.updateSelected=function(){g.multiple?g.selectedValues=(g.value||[]).map(function(a){return $filter("filter")(g.options,function(b){return g.optionEquals(b,a)})[0]}).filter(function(b){return a.isDefined(b)}):g.selectedValues=(g.options||[]).filter(function(a){return g.optionEquals(a)}).slice(0,1)},g.$watch("value",function(b,c){a.equals(b,c)||g.remote||g.updateSelected()},!0),h.on("focus",function(){$timeout(function(){g.$apply(g.open)})}).on("blur",function(){g.$apply(g.close)}).on("keydown",function(a){g.$apply(function(){g.keydown(a)})}).on("input",function(){g.setInputWidth()}),i.on("mousedown",function(a){a.preventDefault()}),a.element($window).on("resize",function(){g.dropdownPosition()}),a.forEach(["open","close","fetch"],function(a){g.api[a]=g[a]}),g.api.focus=function(){h[0].focus()},g.api.set=function(b){var c=(g.filteredOptions||[]).filter(function(a){return g.optionEquals(a,b)});a.forEach(c,function(a){g.set(a)})},g.api.unset=function(b){var c=b?(g.selectedValues||[]).filter(function(a){return g.optionEquals(a,b)}):g.selectedValues;indexes=g.selectedValues.map(function(a,b){return g.inOptions(c,a)?b:-1}).filter(function(a){return a>=0}),a.forEach(indexes,function(a,b){g.unset(a-b)})}})},d}();a.module("selector",[]).run(["$templateCache",function(a){a.put("selector/selector.html",'
  • Add
'),a.put("selector/item-default.html",''),a.put("selector/group-default.html",'')}]).directive("selector",["$filter","$timeout","$window","$http","$q",function(a,b,d,e,f){return new c(a,b,d,e,f)}])}(window.angular); \ No newline at end of file +/*! angular-selector - v1.1.1 - https://github.com/indrimuska/angular-selector - (c) 2015 Indri Muska - MIT */ +!function(a){var 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},c=function(){function c(a){return a instanceof HTMLElement?a.ownerDocument&&a.ownerDocument.defaultView.opener?a.ownerDocument.defaultView.getComputedStyle(a):window.getComputedStyle(a):{}}function d(a,b,c,d,e){this.restrict="EAC",this.replace=!0,this.transclude=!0,this.scope={name:"@?",value:"=model",disabled:"=?disable",multiple:"=?multi",placeholder:"@?",valueAttr:"@",labelAttr:"@?",groupAttr:"@?",options:"=?",create:"&?",rtl:"=?",api:"=?",change:"&?",remote:"&?",remoteParam:"@?",removeButton:"=?",viewItemTemplate:"=?",dropdownItemTemplate:"=?",dropdownGroupTemplate:"=?"},this.templateUrl="selector/selector.html",$filter=a,$timeout=b,$window=c,$http=d,$q=e}return d.prototype.$inject=["$filter","$timeout","$window","$http","$q"],d.prototype.link=function(d,e,f,g,h){h(d,function(d,g){var h=a.element(e[0].querySelector(".selector-input input")),i=a.element(e[0].querySelector(".selector-dropdown")),j=$q.defer(),k={api:{},selectedValues:[],highlighted:0,valueAttr:null,labelAttr:"label",groupAttr:"group",options:[],remoteParam:"q",removeButton:!0,viewItemTemplate:"selector/item-default.html",dropdownItemTemplate:"selector/item-default.html",dropdownGroupTemplate:"selector/group-default.html"};a.isDefined(g.value)||(g.value=g.multiple?[]:""),a.forEach(k,function(b,c){a.isDefined(g[c])||(g[c]=b)}),a.forEach(["name","valueAttr","labelAttr"],function(a){f[a]||(f[a]=g[a])}),g.optionValue=function(a){return null==g.valueAttr?a:a[g.valueAttr]},g.optionEquals=function(b,c){return a.equals(g.optionValue(b),a.isDefined(c)?c:g.value)},g.setValue=function(a){g.multiple?g.value=null==g.valueAttr?a||[]:(a||[]).map(function(a){return a[g.valueAttr]}):g.value=null==g.valueAttr?a||{}:(a||{})[g.valueAttr]},g.hasValue=function(){return g.multiple?(g.value||[]).length>0:null==g.valueAttr?!a.equals({},g.value):!!g.value},g.fetch=function(){var b,c=g.search||"";if(!a.isDefined(g.remote))throw"Remote attribute is not defined";if(g.loading=!0,g.options=[],b=g.remote({search:c}),"function"!=typeof b.then){var d={method:"GET",cache:!0,params:{}};a.extend(d,g.remote()),a.extend(d.params,g.remote().params),d.params[g.remoteParam]=c,b=$http(d)}b.then(function(a){g.options=a.data||a,g.filterSelected(),g.loading=!1,j.resolve()},function(a){throw g.loading=!1,j.reject(),"Error while fetching data: "+(a.message||a)})},a.isDefined(g.remote)||(g.remote=!1,j.resolve()),g.remote&&g.$watch("search",g.fetch),g.optionToObject=function(b,c){var d={},e=a.element(b);a.forEach(b.dataset,function(a,b){b.match(/^\$/)||(d[b]=a)}),b.value&&(d[g.valueAttr||"value"]=b.value),e.text()&&(d[g.labelAttr]=e.text().trim()),a.isDefined(c)&&(d[g.groupAttr]=c),g.options.push(d),!e.attr("selected")||!g.multiple&&g.hasValue()||(g.multiple?(g.value||(g.value=[]),g.value.push(g.optionValue(d))):g.value||(g.value=g.optionValue(d)))},g.fillWithHtml=function(){g.options=[],a.forEach(d,function(b){var c=(b.tagName||"").toLowerCase();"option"==c&&g.optionToObject(b),"optgroup"==c&&a.forEach(b.querySelectorAll("option"),function(a){g.optionToObject(a,(b.attributes.label||{}).value)})}),g.updateSelected()},g.initialize=function(){g.remote||a.isArray(g.options)&&g.options.length||g.fillWithHtml(),g.hasValue()&&(g.multiple?a.isArray(g.value)||(g.value=[g.value]):a.isArray(g.value)&&(g.value=g.value[0]),g.updateSelected(),g.filterSelected(),g.updateValue())},g.$watch("multiple",function(){$timeout(g.setInputWidth),j.promise.then(g.initialize,g.initialize)}),g.dropdownPosition=function(){var a=h.parent()[0],b=c(a),d=parseFloat(b.marginTop||0),e=parseFloat(b.marginLeft||0);i.css({top:a.offsetTop+a.offsetHeight+d+"px",left:a.offsetLeft+e+"px",width:a.offsetWidth+"px"})},g.open=function(){g.isOpen=!0,g.dropdownPosition()},g.close=function(){g.isOpen=!1,g.resetInput()},g.decrementHighlighted=function(){g.highlight(g.highlighted-1),g.scrollToHighlighted()},g.incrementHighlighted=function(){g.highlight(g.highlighted+1),g.scrollToHighlighted()},g.highlight=function(a){g.filteredOptions.length&&(g.highlighted=(g.filteredOptions.length+a)%g.filteredOptions.length)},g.scrollToHighlighted=function(){var a=i[0],b=a.querySelectorAll("li.selector-option")[g.highlighted],d=c(b),e=parseFloat(d.marginTop||0),f=parseFloat(d.marginBottom||0);g.filteredOptions.length&&(b.offsetTop+b.offsetHeight+f>a.scrollTop+a.offsetHeight&&$timeout(function(){a.scrollTop=b.offsetTop+b.offsetHeight+f-a.offsetHeight}),b.offsetTop-e0:b.indexOf(c)>=0},g.filterSelected=function(){g.filteredOptions=$filter("filter")(g.options||[],g.search),g.multiple&&(g.filteredOptions=g.filteredOptions.filter(function(b){var c=a.isArray(g.selectedValues)?g.selectedValues:[g.selectedValues];return!g.inOptions(c,b)})),g.highlighted>=g.filteredOptions.length&&g.highlight(g.filteredOptions.length-1)},g.measureWidth=function(){var b,d=c(h[0]),e=a.element('');return e.text(h.val()||(g.hasValue()?"":g.placeholder)||""),a.element(document.body).append(e),a.forEach(["fontFamily","fontSize","fontWeight","fontStyle","letterSpacing","textTransform","wordSpacing","textIndent"],function(a){e.css(a,d[a])}),b=e[0].offsetWidth,e.remove(),b},g.setInputWidth=function(){var a=g.measureWidth()+1;h.css("width",a+"px")},g.resetInput=function(){h.val(""),g.search="",g.setInputWidth()},g.$watch("[search, options, value]",function(){g.setInputWidth(),g.filterSelected(),g.isOpen&&$timeout(g.dropdownPosition)},!0),g.updateValue=function(b){a.isDefined(b)||(b=g.selectedValues),g.setValue(g.multiple?b:b[0])},g.$watch("selectedValues",function(b,c){a.equals(b,c)||(g.updateValue(),a.isFunction(g.change)&&g.change(g.multiple?{newValue:b,oldValue:c}:{newValue:b[0],oldValue:c[0]}))},!0),g.$watch("options",function(b,c){a.equals(b,c)||g.remote||g.updateSelected()}),g.updateSelected=function(){g.multiple?g.selectedValues=(g.value||[]).map(function(a){return $filter("filter")(g.options,function(b){return g.optionEquals(b,a)})[0]}).filter(function(b){return a.isDefined(b)}):g.selectedValues=(g.options||[]).filter(function(a){return g.optionEquals(a)}).slice(0,1)},g.$watch("value",function(b,c){a.equals(b,c)||g.remote||g.updateSelected()},!0),h.on("focus",function(){$timeout(function(){g.$apply(g.open)})}).on("blur",function(){g.$apply(g.close)}).on("keydown",function(a){g.$apply(function(){g.keydown(a)})}).on("input",function(){g.setInputWidth()}),i.on("mousedown",function(a){a.preventDefault()}),a.element($window).on("resize",function(){g.dropdownPosition()}),a.forEach(["open","close","fetch"],function(a){g.api[a]=g[a]}),g.api.focus=function(){h[0].focus()},g.api.set=function(b){var c=(g.filteredOptions||[]).filter(function(a){return g.optionEquals(a,b)});a.forEach(c,function(a){g.set(a)})},g.api.unset=function(b){var c=b?(g.selectedValues||[]).filter(function(a){return g.optionEquals(a,b)}):g.selectedValues,d=g.selectedValues.map(function(a,b){return g.inOptions(c,a)?b:-1}).filter(function(a){return a>=0});a.forEach(d,function(a,b){g.unset(a-b)})}})},d}();a.module("selector",[]).run(["$templateCache",function(a){a.put("selector/selector.html",'
  • Add
'),a.put("selector/item-default.html",''),a.put("selector/group-default.html",'')}]).directive("selector",["$filter","$timeout","$window","$http","$q",function(a,b,d,e,f){return new c(a,b,d,e,f)}])}(window.angular); \ No newline at end of file diff --git a/eslint.json b/eslint.json new file mode 100644 index 0000000..c07d66d --- /dev/null +++ b/eslint.json @@ -0,0 +1,17 @@ +{ + "env": { + "browser": true + }, + "globals": { + "angular": 1 + }, + "extends": "eslint:recommended", + "rules": { + "indent": [2, "tab", {"SwitchCase": 1}], + "quotes": [2, "single"], + "eqeqeq": 0, + "strict": 0, + "no-undef": 0, + "semi": [2, "always"] + } +} \ No newline at end of file diff --git a/package.json b/package.json index 7263d9a..17db3be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-selector", - "version": "1.1.0", + "version": "1.1.1", "description": "A native AngularJS directive that transform a simple `