Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit adb3c42

Browse files
committed
Merge pull request #110 from Lefka/master
Added a config provider
2 parents 04f51c7 + 35a7a62 commit adb3c42

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ When setting `clearOnBlurPlaceholder` to `true`, it will show the placeholder te
7272

7373
Whereas, `maskDefinitions` is an object, so any custom object you supply will be merged together with the defaults using `angular.extend()`. This allows you to override the defaults selectively, if you wish.
7474

75+
#### Global customization
76+
In addition to customizing behaviors for a specific element, you can also customize the behaviors globally. To do this, simply use the `uiMaskConfig` provider in your app configuration. Example:
77+
78+
```sh
79+
app.config(['uiMask.ConfigProvider', function(uiMaskConfigProvider) {
80+
uiMaskConfigProvider.maskDefinitions({'A': /[a-z], '*': /[a-zA-Z0-9]/});
81+
uiMaskConfigProvider.clearOnBlur(false);
82+
uiMaskConfigProvider.eventsToHandle(['input', 'keyup', 'click']);
83+
}
84+
```
85+
7586
#### maskDefinitions
7687
The keys in `maskDefinitions` represent the special tokens/characters used in your mask declaration to delimit acceptable ranges of inputs. For example, we use '9' here to accept any numeric values for a phone number: `ui-mask="(999) 999-9999"`. The values associated with each token are regexen. Each regex defines the ranges of values that will be acceptable as inputs in the position of that token.
7788

dist/mask.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-ui-mask
33
* https://github.com/angular-ui/ui-mask
4-
* Version: 1.7.2 - 2016-01-29T01:38:58.683Z
4+
* Version: 1.7.2 - 2016-02-01T14:35:21.404Z
55
* License: MIT
66
*/
77

@@ -22,7 +22,29 @@ angular.module('ui.mask', [])
2222
clearOnBlurPlaceholder: false,
2323
eventsToHandle: ['input', 'keyup', 'click', 'focus']
2424
})
25-
.directive('uiMask', ['uiMaskConfig', function(maskConfig) {
25+
.provider('uiMask.Config', function() {
26+
var options = {};
27+
28+
this.clearOnBlur = function(clearOnBlur) {
29+
return options.clearOnBlur = clearOnBlur;
30+
};
31+
this.clearOnBlurPlaceholder = function(clearOnBlurPlaceholder) {
32+
return options.clearOnBlurPlaceholder = clearOnBlurPlaceholder;
33+
};
34+
this.eventsToHandle = function(eventsToHandle) {
35+
return options.eventsToHandle = eventsToHandle;
36+
};
37+
this.$get = ['uiMaskConfig', function(uiMaskConfig) {
38+
var tempOptions = uiMaskConfig;
39+
for(var prop in options)
40+
{
41+
tempOptions[prop] = options[prop];
42+
}
43+
44+
return tempOptions;
45+
}];
46+
})
47+
.directive('uiMask', ['uiMask.Config', function(maskConfig) {
2648
function isFocused (elem) {
2749
return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex);
2850
}

dist/mask.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mask.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,29 @@ angular.module('ui.mask', [])
1212
clearOnBlurPlaceholder: false,
1313
eventsToHandle: ['input', 'keyup', 'click', 'focus']
1414
})
15-
.directive('uiMask', ['uiMaskConfig', function(maskConfig) {
15+
.provider('uiMask.Config', function() {
16+
var options = {};
17+
18+
this.clearOnBlur = function(clearOnBlur) {
19+
return options.clearOnBlur = clearOnBlur;
20+
};
21+
this.clearOnBlurPlaceholder = function(clearOnBlurPlaceholder) {
22+
return options.clearOnBlurPlaceholder = clearOnBlurPlaceholder;
23+
};
24+
this.eventsToHandle = function(eventsToHandle) {
25+
return options.eventsToHandle = eventsToHandle;
26+
};
27+
this.$get = ['uiMaskConfig', function(uiMaskConfig) {
28+
var tempOptions = uiMaskConfig;
29+
for(var prop in options)
30+
{
31+
tempOptions[prop] = options[prop];
32+
}
33+
34+
return tempOptions;
35+
}];
36+
})
37+
.directive('uiMask', ['uiMask.Config', function(maskConfig) {
1638
function isFocused (elem) {
1739
return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex);
1840
}

test/maskSpec.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ describe("uiMask", function () {
33

44
var formHtml = "<form name='test'><input name='input' ng-model='x' ui-mask='{{mask}}'></form>";
55
var inputHtml = "<input name='input' ng-model='x' ui-mask='{{mask}}' ui-options='options'>";
6-
var compileElement, scope, config, timeout;
6+
var compileElement, scope, config, timeout, uiMaskConfigProvider;
77

88
beforeEach(module("ui.mask"));
99
beforeEach(function() {
@@ -21,7 +21,10 @@ describe("uiMask", function () {
2121
});
2222
}
2323
}
24-
});
24+
})
25+
.config(['uiMask.ConfigProvider', function(configProvider) {
26+
uiMaskConfigProvider = configProvider;
27+
}]);
2528
module("test");
2629
});
2730
beforeEach(inject(function ($rootScope, $compile, uiMaskConfig, $timeout) {
@@ -500,7 +503,7 @@ describe("uiMask", function () {
500503

501504
input.triggerHandler("blur");
502505
expect(input.attr("placeholder")).toBe("MM/DD/YYYY");
503-
})
506+
});
504507

505508
it("should allow text input to be the same character as ui-mask-placeholder-char", function() {
506509
var placeholderHtml = "<input name='input' ng-model='x' ui-mask='(999) 999-9999' placeholder='Phone Number' ui-mask-placeholder-char='5'>",
@@ -675,4 +678,19 @@ describe("uiMask", function () {
675678
});
676679
});
677680

681+
describe("Configuration Provider", function() {
682+
it("should return default values", inject(function($injector) {
683+
var service = $injector.invoke(uiMaskConfigProvider.$get);
684+
expect(service.clearOnBlur).toEqual(true);
685+
expect(service.clearOnBlurPlaceholder).toEqual(false);
686+
}));
687+
688+
it("should merge default values with configured values", inject(function($injector) {
689+
uiMaskConfigProvider.clearOnBlur(false);
690+
var service = $injector.invoke(uiMaskConfigProvider.$get);
691+
expect(service.clearOnBlur).toEqual(false);
692+
expect(service.clearOnBlurPlaceholder).toEqual(false);
693+
}));
694+
});
695+
678696
});

0 commit comments

Comments
 (0)