From eef3db301ef887a8d84efade11371487e6ce68cc Mon Sep 17 00:00:00 2001 From: zlodes Date: Thu, 9 Feb 2017 14:09:22 +0300 Subject: [PATCH 1/3] Added config-provider --- .gitignore | 3 ++- dist/angular-chosen.js | 28 +++++++++++++++++++++++----- dist/angular-chosen.min.js | 2 +- example/index.js | 9 ++++++++- src/chosen.coffee | 23 ++++++++++++++++++++--- 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 0cfc89f..4056f08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bower_components/ -node_modules/ \ No newline at end of file +node_modules/ +.idea diff --git a/dist/angular-chosen.js b/dist/angular-chosen.js index b005601..82c3612 100644 --- a/dist/angular-chosen.js +++ b/dist/angular-chosen.js @@ -5,12 +5,28 @@ * @license MIT */ (function() { - var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + var chosen, + indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; angular.module('localytics.directives', []); - angular.module('localytics.directives').directive('chosen', [ - '$timeout', function($timeout) { + chosen = angular.module('localytics.directives'); + + chosen.provider('chosen', function() { + var options; + options = {}; + return { + setOption: function(newOpts) { + angular.extend(options, newOpts); + }, + $get: function() { + return options; + } + }; + }); + + chosen.directive('chosen', [ + 'chosen', '$timeout', function(config, $timeout) { var CHOSEN_OPTION_WHITELIST, NG_OPTIONS_REGEXP, isEmpty, snakeCase; NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/; CHOSEN_OPTION_WHITELIST = ['persistentCreateOption', 'createOptionText', 'createOption', 'skipNoResults', 'noResultsText', 'allowSingleDeselect', 'disableSearchThreshold', 'disableSearch', 'enableSplitWordSearch', 'inheritSelectClasses', 'maxSelectedOptions', 'placeholderTextMultiple', 'placeholderTextSingle', 'searchContains', 'singleBackstrokeDelete', 'displayDisabledOptions', 'displaySelectedOptions', 'width', 'includeGroupLabelInSelected', 'maxShownResults']; @@ -37,11 +53,13 @@ require: '?ngModel', priority: 1, link: function(scope, element, attr, ngModel) { - var chosen, empty, initOrUpdate, match, options, origRender, startLoading, stopLoading, updateMessage, valuesExpr, viewWatch; + var directiveOptions, empty, initOrUpdate, match, options, origRender, startLoading, stopLoading, updateMessage, valuesExpr, viewWatch; scope.disabledValuesHistory = scope.disabledValuesHistory ? scope.disabledValuesHistory : []; element = $(element); element.addClass('localytics-chosen'); - options = scope.$eval(attr.chosen) || {}; + directiveOptions = scope.$eval(attr.chosen) || {}; + options = angular.copy(config); + angular.extend(options, directiveOptions); angular.forEach(attr, function(value, key) { if (indexOf.call(CHOSEN_OPTION_WHITELIST, key) >= 0) { return attr.$observe(key, function(value) { diff --git a/dist/angular-chosen.min.js b/dist/angular-chosen.min.js index 3843322..75edba9 100644 --- a/dist/angular-chosen.min.js +++ b/dist/angular-chosen.min.js @@ -4,4 +4,4 @@ * @link http://github.com/leocaseiro/angular-chosen * @license MIT */ -(function(){var e=[].indexOf||function(e){for(var t=0,n=this.length;t=0)return o.$observe(r,function(e){var t;return t=String(l.attr(o.$attr[r])).slice(0,2),p[s(r)]="{{"===t?e:a.$eval(e),y()})}),b=function(){return l.addClass("loading").attr("disabled",!0).trigger("chosen:updated")},v=function(){return l.removeClass("loading"),angular.isDefined(o.disabled)?l.attr("disabled",o.disabled):l.attr("disabled",!1),l.trigger("chosen:updated")},d=null,c=!1,f=function(){var e,t;if(d){if(t=$(l.parent()).find("div.chosen-drop"),t&&t.length>0&&parseInt(t.css("left"))>=0)return;return l.trigger("chosen:updated")}if(a.$evalAsync(function(){d=l.chosen(p).data("chosen")}),angular.isObject(d))return e=d.default_text},y=function(){return d&&c?l.attr("data-placeholder",d.results_none_found).attr("disabled",!0):l.removeAttr("data-placeholder"),l.trigger("chosen:updated")},u?(g=u.$render,u.$render=function(){return g(),f()},l.on("chosen:hiding_dropdown",function(){return a.$apply(function(){return u.$setTouched()})}),o.multiple&&(w=function(){return u.$viewValue},a.$watch(w,u.$render,!0))):f(),o.$observe("disabled",function(){return l.trigger("chosen:updated")}),o.ngOptions&&u)return h=o.ngOptions.match(r),S=h[7],a.$watchCollection(S,function(e,n){var r;return r=t(function(){return angular.isUndefined(e)?b():(c=i(e),v(),y())})}),a.$on("$destroy",function(e){if("undefined"!=typeof timer&&null!==timer)return t.cancel(timer)})}}}])}).call(this); \ No newline at end of file +(function(){var e,n=[].indexOf||function(e){for(var n=0,t=this.length;n=0)return c.$observe(t,function(e){var n;return n=String(u.attr(c.$attr[t])).slice(0,2),v[l(t)]="{{"===n?e:o.$eval(e),w()})}),y=function(){return u.addClass("loading").attr("disabled",!0).trigger("chosen:updated")},S=function(){return u.removeClass("loading"),angular.isDefined(c.disabled)?u.attr("disabled",c.disabled):u.attr("disabled",!1),u.trigger("chosen:updated")},e=null,h=!1,p=function(){var n,t;if(e){if(t=$(u.parent()).find("div.chosen-drop"),t&&t.length>0&&parseInt(t.css("left"))>=0)return;return u.trigger("chosen:updated")}if(o.$evalAsync(function(){e=u.chosen(v).data("chosen")}),angular.isObject(e))return n=e.default_text},w=function(){return e&&h?u.attr("data-placeholder",e.results_none_found).attr("disabled",!0):u.removeAttr("data-placeholder"),u.trigger("chosen:updated")},d?(b=d.$render,d.$render=function(){return b(),p()},u.on("chosen:hiding_dropdown",function(){return o.$apply(function(){return d.$setTouched()})}),c.multiple&&(m=function(){return d.$viewValue},o.$watch(m,d.$render,!0))):p(),c.$observe("disabled",function(){return u.trigger("chosen:updated")}),c.ngOptions&&d)return g=c.ngOptions.match(a),O=g[7],o.$watchCollection(O,function(e,n){var t;return t=r(function(){return angular.isUndefined(e)?y():(h=s(e),S(),w())})}),o.$on("$destroy",function(e){if("undefined"!=typeof timer&&null!==timer)return r.cancel(timer)})}}}])}).call(this); \ No newline at end of file diff --git a/example/index.js b/example/index.js index 7d49b7e..8e076e6 100644 --- a/example/index.js +++ b/example/index.js @@ -1,6 +1,13 @@ // Generated by CoffeeScript 1.6.2 (function() { - angular.module('chosenExampleApp', ['localytics.directives']).controller('IndexCtrl', [ + angular.module('chosenExampleApp', ['localytics.directives']) + .config(['chosenProvider', function (chosenProvider) { + chosenProvider.setOption({ + no_results_text: 'Haha! There is no results!', + placeholder_text_multiple: 'Choose a few!' + }); + }]) + .controller('IndexCtrl', [ '$scope', '$q', '$timeout', function($scope, $q, $timeout) { var simulateAjax; diff --git a/src/chosen.coffee b/src/chosen.coffee index 1e0b1ca..7dbbe98 100644 --- a/src/chosen.coffee +++ b/src/chosen.coffee @@ -1,7 +1,18 @@ angular.module('localytics.directives', []) -angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeout) -> - +chosen = angular.module('localytics.directives') + +chosen.provider 'chosen', -> + options = {} + { + setOption: (newOpts) -> + angular.extend options, newOpts + return + $get: -> + options + } + +chosen.directive 'chosen', ['chosen', '$timeout', (config, $timeout) -> # coffeelint: disable=max_line_length # This is stolen from Angular... NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/ @@ -48,7 +59,13 @@ angular.module('localytics.directives').directive 'chosen', ['$timeout', ($timeo element.addClass('localytics-chosen') # Take a hash of options from the chosen directive - options = scope.$eval(attr.chosen) or {} + directiveOptions = scope.$eval(attr.chosen) or {} + + # Clone options from configProvider + options = angular.copy(config) + + # Merge options from directive with options from configProvider + angular.extend(options, directiveOptions) # Options defined as attributes take precedence angular.forEach attr, (value, key) -> From 9897642b921f1e06961a1e58746de3a5fba12e9c Mon Sep 17 00:00:00 2001 From: Leo Caseiro Date: Mon, 13 Feb 2017 11:20:32 +1100 Subject: [PATCH 2/3] remove .idea from .gitignore. I believe each developer should have it's own global .gitignore that removes all editor settings. eq: .idea, .atom and so on --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4056f08..c346b13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ bower_components/ node_modules/ -.idea From 147a06353728022161fca46753af58bd158029be Mon Sep 17 00:00:00 2001 From: leocaseiro Date: Mon, 13 Feb 2017 11:28:54 +1100 Subject: [PATCH 3/3] feature(chosenProvider): add chosenProvider with default values (#231) Thanks @zlodes Fix #226 #77 --- README.md | 15 ++++++++++++++- package.json | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 04d2bfc..9d9f2ba 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Via [cdn](https://cdnjs.com/libraries/angular-chosen-localytics) Or download zip file -> [Download v1.5.1](https://github.com/leocaseiro/angular-chosen/archive/1.5.1.zip) +> [Download v1.6.0](https://github.com/leocaseiro/angular-chosen/archive/1.6.0.zip) @@ -47,6 +47,7 @@ If you use Yeoman or Bower install, you need to rename the `chosen.jquery.js` or * Supports usage of promises in `ngOptions` * Provides a 'loading' animation when 'ngOptions' collection is a promise backed by a remote source * Pass options to `Chosen` via attributes or by passing an object to the Chosen directive + * Provider with default values with `chosenProvider` ([read: Added config-provider](https://github.com/leocaseiro/angular-chosen/pull/231)) [since 1.6.0] # Usage @@ -144,3 +145,15 @@ Image of select defined above in loading state: > Note: Assigning promises directly to scope is now deprecated in Angular 1.2+. Assign the results of the promise to scope once the promise returns. The loader animation will still work as long as the collection expression evaluates to `undefined` while your data is loading! + + +### Default values with chosenProvider (thanks @zlodes) [since 1.6.0] +```javascript +angular.module('chosenExampleApp', ['localytics.directives']) + .config(['chosenProvider', function (chosenProvider) { + chosenProvider.setOption({ + no_results_text: 'There is no results!', + placeholder_text_multiple: 'Choose one or more!' + }); + }]); +``` diff --git a/package.json b/package.json index aaf3dd8..b857e78 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "angular-chosen-localytics", "filename": "chosen.js", "main": "dist/angular-chosen.js", - "version": "1.5.1", + "version": "1.6.0", "description": "Angular Chosen directive is an AngularJS Directive that brings the Chosen jQuery in a Angular way", "homepage": "http://github.com/leocaseiro/angular-chosen", "repository": { @@ -41,7 +41,8 @@ "lpsBetty", "nike-17", "vstene", - "frnan" + "frnan", + "zlodes" ], "dependencies": { "angular": "^1.5.7",