all files / src/component/ date.component.ts

22.73% Statements 15/66
0% Branches 0/81
7.14% Functions 1/14
23.33% Lines 14/60
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                                                                                                                                                                                    
import $ from 'jquery';
import 'jquery-datetimepicker/build/jquery.datetimepicker.min.css';
import 'jquery-datetimepicker';
 
export class CapivaraDate {
    public $constants;
    public $functions;
    public $bindings;
    private element;
    private oi;
    private inputElement;
 
    constructor($scope, $element) {
        this.element = $element;
        this.$constants = this.$constants || {};
    }
 
    $onViewInit() {
        this.inputElement = this.element.querySelector('input.cp-date');
        if (typeof this.$bindings.cpModel === 'string') {
            this.$bindings.cpModel = new Date(this.$bindings.cpModel);
        }
        if(this.$bindings.cpModel == 'Invalid Date'){
            delete this.$bindings.cpModel;
        }
        this.setModelInInput();
        this.createCpDate();
    }
 
    $onChanges = (changes) => {
        if (changes && changes[0] && changes[0].name == 'cpModel') {
            this.setModelInInput();
        }
    }
 
    /**
     * @method void Method responsible for start and configurate the CpDate
     */
    createCpDate() {
        $.datetimepicker.setLocale(this.$constants.language || 'pt-BR')
        $(this.inputElement).datetimepicker({
            timepicker: this.$constants.timepicker == undefined ? true : this.$constants.timepicker,
            datepicker: this.$constants.datepicker == undefined ? true : this.$constants.datepicker,
            format: this.$constants.format == undefined ? 'd/m/Y H:i' : this.$constants.format,
            mask: this.$constants.mask == undefined ? true : this.$constants.mask,
            inline: this.$constants.openedCalendar == undefined ? false : this.$constants.openedCalendar,
            allowTimes: this.$constants.allowedTimes || [],
            defaultTime: this.$constants.defaultTime == undefined ? false : this.$constants.defaultTime,
            defaultDate: this.$constants.defaultDate == undefined ? false : this.$constants.defaultDate,
            step: this.$constants.step || 60,
            weeks: this.$constants.weekNumbers == undefined ? false : this.$constants.weekNumbers,
            yearStart: this.$constants.yearStart || 1950,
            yearEnd: this.$constants.yearEnd || 2050,
            dayOfWeekStart: this.$constants.dayOfWeekStart || 0,
            disabledDates: this.$constants.disabledDates || [],
            minDate: this.$constants.minDate == undefined ? false : this.$constants.minDate,
            maxDate: this.$constants.maxDate == undefined ? false : this.$constants.maxDate,
            onChangeDateTime: (dp, $input) => {
                if(this.$constants.openCalendar){
                    this.setModelInInput();
                }
                $(this.inputElement).datetimepicker({mask: this.$constants.mask == undefined ? true : this.$constants.mask})
                this.$bindings.cpModel = dp;
               
            }
        });
    }
 
    /**
     * @method boolean Method responsible to check if the user selected an allowed time.
     * @param informedDate Date Object
     */
    isAllowedTimes(informedDate: Date): boolean {   
        if(!this.$constants.allowedTimes) return true;
        try{
            const fullTime = informedDate instanceof Date ? informedDate.getHours() + ':' + ('0' + informedDate.getMinutes()).slice(-2) : new Date(informedDate).getHours() + ':' + ('0' + new Date(informedDate).getMinutes()).slice(-2)
            return this.$constants.allowedTimes.filter((validTime) => validTime == fullTime).length > 0;
        }catch (e) { return false }
    }
 
    /**
     * @method boolean Method responsible to check if the user selected a min allowed date.
     * @param informedDate Date Object
     */
    isAllowedMinDate(informedDate: Date): boolean {
        if (!this.$constants.minDate) return true;
        try {
            const min = this.$constants.minDate instanceof Date ? this.$constants.minDate : new Date(this.$constants.minDate);
            return informedDate && (informedDate.getTime() >= min.getTime());
        } catch (e) { return false; }
    }
 
    /**
     * @method boolean Method responsible to check if the user selected a max allowed date.
     * @param informedDate Date Object
     */
    isAllowedMaxDate(informedDate: Date): boolean {
        if (!this.$constants.maxDate) return true;
        try {
            const max = this.$constants.maxDate instanceof Date ? this.$constants.maxDate : new Date(this.$constants.maxDate);
            max.setHours(23);
            max.setMinutes(59);
            max.setSeconds(59);
            return informedDate && (informedDate.getTime() <= max.getTime());
        } catch (e) { return false; }
    }
 
    /**
     * @method boolean Method responsible to check if the user selected a max allowed date.
     * @param informedDate Date Object
     */
    isDisabledDate(informedDate: Date): boolean {
        if (!this.$constants.disabledDates) return false;
        return this.$constants.disabledDates.filter((validDate) => {
            const date = validDate instanceof Date ? validDate : new Date(validDate);
            return (date.getDate() == informedDate.getDate() && date.getMonth() == informedDate.getMonth() && date.getFullYear() == informedDate.getFullYear());
        }).length > 0;
    }
 
    /**
     *@method void This method delete the value inside of the input 
     */
    resetInput() {
        $(this.inputElement).datetimepicker('reset');
    }
 
    /**
     * @method void Method responsible of setting the value of the model to the input
     */
    setModelInInput() {
        if (this.$bindings.cpModel instanceof Date &&
            this.isAllowedMinDate(this.$bindings.cpModel) &&
            this.isAllowedMaxDate(this.$bindings.cpModel) &&
            !this.isDisabledDate(this.$bindings.cpModel)  && 
            this.isAllowedTimes(this.$bindings.cpModel)) {
                $(this.inputElement).datetimepicker({value: this.$bindings.cpModel });
        } else {
            this.resetInput();
            $(this.inputElement).datetimepicker({mask: this.$constants.mask == undefined ? true : this.$constants.mask})
            this.$bindings.cpModel = null;
        }
    }
 
}