angular-daterangepicker-config.js 3.56 KB
/* 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);