Code coverage report for app/components/_common/directives/datepicker-init.directive.js

Statements: 100% (17 / 17)      Branches: 100% (4 / 4)      Functions: 100% (4 / 4)      Lines: 100% (15 / 15)      Ignored: none     

All files » app/components/_common/directives/ » datepicker-init.directive.js
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                                        
function DatepickerInitDirective () {
    return {
        require: 'ngModel',
        restrict: 'A',
        link
    };
 
    function link (scope, element, attrs, ngModelCtrl) {
        const pickadate = element.pickadate({
            format: 'yyyy-m-d'
        });
        const watcher = scope.$watch(attrs.ngModel, (value) => {
            // set initial model value to date picker, only once
            if (value) {
                const picker = pickadate.pickadate('picker');
                picker.set('select', value);
                // cancel the watcher
                watcher();
            }
        });
        ngModelCtrl.$formatters.unshift((modelValue) => {
            if (modelValue) {
                const date = new Date(modelValue);
                return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
            }
        });
    }
}
 
DatepickerInitDirective.$inject = [];
 
export default DatepickerInitDirective;