angular-daterangepicker-config.js
3.56 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* global angular */
(function(angular) {
'use strict';
// daterangepicker 配置项扩展
angular
.module('daterangepicker')
.constant('daterangepickerCustom', {
options: {
language: 'zn-ch',
locale: {
format: 'YYYY-MM-DD',
applyLabel: '确定',
cancelLabel: '取消',
fromLabel: '开始',
toLabel: '结束',
monthNames: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
daysOfWeek: "日_一_二_三_四_五_六".split("_")
},
autoApply: true,
// valueFormat: 'YYYY-MM-DD 00:00:00'
valueFormat: false
}
})
.decorator('dateRangePickerDirective', ['$delegate', 'daterangepickerCustom', '$timeout', function($delegate, daterangepickerCustom, $timeout) {
var directive = $delegate[0];
var originalLinkFn = directive.link;
// 修改opts为可选
directive.$$isolateBindings["opts"].optional = true;
// 默认opts的值
directive.compile = function() {
return function newLinkFn(scope, element, attr, modelCtrl) {
var _opts = scope.opts = scope.opts || {};
var customOpts = scope.$eval(attr.option) || {};
_opts = scope.opts = angular.merge({}, daterangepickerCustom.options, _opts || {});
scope.opts.ranges = customOpts.range && daterangepickerCustom.options.ranges;
// 初始化model
/* var copyModel = scope.model || {};
copyModel = scope.model = {
startDate: copyModel.startDate ? moment(copyModel.startDate) : null,
endDate: copyModel.endDate ? moment(copyModel.endDate) : null
};*/
modelCtrl.$render();
// 重置link
originalLinkFn.apply($delegate[0], arguments);
modelCtrl.$parsers.push(function(val) {
if (!_opts.valueFormat) {
return val;
}
if (angular.isObject(val) && angular.isObject(val.startDate) && angular.isObject(val.endDate)) {
val.startDate = val.startDate.format(_opts.valueFormat);
val.endDate = val.endDate.format(_opts.valueFormat);
return val;
}
return val;
});
// 初始无选择直接apply时,同步值
element.on('apply.daterangepicker', function(e, picker) {
return scope.$apply(function() {
return scope.model = {
startDate: picker.startDate,
endDate: picker.endDate
};
});
});
// model 无初始化值时,自动初始化并同步
/*$timeout(function() {
// setViewValue 非严格比较更新
return modelCtrl.$setViewValue(angular.copy(copyModel, {}));
});*/
};
};
delete $delegate[0].link;
return $delegate;
}]);
})(angular);