Skip to content

Commit

Permalink
release(1.6.0) add chosenProvider, thanks @zlodes
Browse files Browse the repository at this point in the history
Merge branch 'zlodes-config-provider'
  • Loading branch information
leocaseiro committed Feb 13, 2017
2 parents a68cb17 + 147a063 commit b021ac0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bower_components/
node_modules/
node_modules/
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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

Expand Down Expand Up @@ -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!'
});
}]);
```
28 changes: 23 additions & 5 deletions dist/angular-chosen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-chosen.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion example/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -41,7 +41,8 @@
"lpsBetty",
"nike-17",
"vstene",
"frnan"
"frnan",
"zlodes"
],
"dependencies": {
"angular": "^1.5.7",
Expand Down
23 changes: 20 additions & 3 deletions src/chosen.coffee
Original file line number Diff line number Diff line change
@@ -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]+?))?$/
Expand Down Expand Up @@ -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) ->
Expand Down

0 comments on commit b021ac0

Please sign in to comment.