This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): Add support for global configuration
Add support for global configuration so multiple directives sharing the same configuration can be set up only once and in one place. That configuration is achieved by using a provider within the module .config function. Closes #48.
- Loading branch information
Showing
10 changed files
with
224 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,48 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @ngdoc service | ||
* @name tagsInput.service:tiConfiguration | ||
* @ngdoc provider | ||
* @name tagsInput.provider:tagsInputConfig | ||
* | ||
* @description | ||
* Loads and initializes options from HTML attributes. Used internally by tagsInput and autoComplete directives. | ||
* Sets global default configuration options for tagsInput and autoComplete directives. It's also used internally to parse and | ||
* initialize options from HTML attributes. | ||
*/ | ||
tagsInput.service('tiConfiguration', function($interpolate) { | ||
this.load = function(scope, attrs, options) { | ||
tagsInput.provider('tagsInputConfig', function() { | ||
var globalDefaults = {}; | ||
|
||
/** | ||
* @ngdoc function | ||
* @name setDefaults | ||
* @description Sets the default configuration option for a directive. | ||
* | ||
* @param {string} directive Name of the directive to be configured. Must be either 'tagsInput' or 'autoComplete'. | ||
* @param {object} defaults Object containing options and their values. | ||
*/ | ||
this.setDefaults = function(directive, defaults) { | ||
globalDefaults[directive] = defaults; | ||
return this; | ||
}; | ||
|
||
this.$get = function($interpolate) { | ||
var converters = {}; | ||
converters[String] = function(value) { return value; }; | ||
converters[Number] = function(value) { return parseInt(value, 10); }; | ||
converters[Boolean] = function(value) { return value === 'true'; }; | ||
converters[Boolean] = function(value) { return value.toLowerCase() === 'true'; }; | ||
converters[RegExp] = function(value) { return new RegExp(value); }; | ||
|
||
scope.options = {}; | ||
return { | ||
load: function(directive, scope, attrs, options) { | ||
scope.options = {}; | ||
|
||
angular.forEach(options, function(value, key) { | ||
var interpolatedValue = attrs[key] && $interpolate(attrs[key])(scope.$parent), | ||
converter = converters[options[key].type]; | ||
angular.forEach(options, function(value, key) { | ||
var interpolatedValue = attrs[key] && $interpolate(attrs[key])(scope.$parent), | ||
converter = converters[value[0]], | ||
getDefault = function(key) { return globalDefaults[directive] ? globalDefaults[directive][key] : value[1]; }; | ||
|
||
scope.options[key] = interpolatedValue ? converter(interpolatedValue) : options[key].defaultValue; | ||
}); | ||
scope.options[key] = interpolatedValue ? converter(interpolatedValue) : getDefault(key); | ||
}); | ||
} | ||
}; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
var tagsInput = angular.module('ngTagsInput', []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.