This repository was archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathng_model_options.dart
54 lines (45 loc) · 1.85 KB
/
ng_model_options.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
part of angular.directive;
@Decorator(selector: 'input[ng-model-options]')
class NgModelOptions {
int _debounceDefaultValue = 0;
int _debounceBlurValue;
int _debounceChangeValue;
int _debounceInputValue;
static const String _DEBOUNCE_DEFAULT_KEY = "default";
static const String _DEBOUNCE_BLUR_KEY = "blur";
static const String _DEBOUNCE_CHANGE_KEY = "change";
static const String _DEBOUNCE_INPUT_KEY = "input";
NgModelOptions(NodeAttrs attrs) {
Map options = convert.JSON.decode(attrs["ng-model-options"].replaceFirst("debounce", "'debounce'").replaceAll("'", "\""));
if (options["debounce"].containsKey(_DEBOUNCE_DEFAULT_KEY)){
_debounceDefaultValue = options["debounce"][_DEBOUNCE_DEFAULT_KEY];
}
_debounceBlurValue = options["debounce"][_DEBOUNCE_BLUR_KEY];
_debounceChangeValue = options["debounce"][_DEBOUNCE_CHANGE_KEY];
_debounceInputValue = options["debounce"][_DEBOUNCE_INPUT_KEY];
}
async.Timer _blurTimer;
void executeBlurFunc(func()) {
var delay = _debounceBlurValue == null ? _debounceDefaultValue : _debounceBlurValue;
_blurTimer = _runFuncDebounced(delay, func,_blurTimer);
}
async.Timer _changeTimer;
void executeChangeFunc(func()) {
var delay = _debounceChangeValue == null ? _debounceDefaultValue : _debounceChangeValue;
_changeTimer = _runFuncDebounced(delay, func, _changeTimer);
}
async.Timer _inputTimer;
void executeInputFunc(func()) {
var delay = _debounceInputValue == null ? _debounceDefaultValue : _debounceInputValue;
_inputTimer = _runFuncDebounced(delay, func, _inputTimer);
}
async.Timer _runFuncDebounced(int delay, func(), async.Timer timer){
if (timer != null && timer.isActive) timer.cancel();
if(delay == 0){
func();
return null;
} else {
return new async.Timer(new Duration(milliseconds: delay), func);
}
}
}