• Jump To … +
    app.js Chronograph.js Crown.js DateIndicator.js DayIndicator.js DayNightIndicator.js Dial.js EquationOfTime.js Foudroyante.js MinuteRepeater.js MonthIndicator.js MoonPhase.js PowerReserve.js WeekIndicator.js YearIndicator.js
  • DayNightIndicator.js

  • ¶

    DayNightIndicator Class @params dial: object @params settings: object @params parentWatch: Watch instance

    Based upon the supplied dial, which by default is the 0th index of the dials array on the parent watch class, an indicator will be rotated to show the day/night value. Think of this complication more of an AM/PM indicator. From 12-06 AM the indicator is shown in full day. And from 18-24 the indicator is shown in full night. The hours in between the dial in shown split meaning the day is split into fourths and the indicator is rotated 25 degrees for each.

    class DayNightIndicator {
        constructor(dial, settings, parentWatch) {
            this.errorChecking(settings);
    
            this.element = document.getElementById(settings.id || settings);
            this.dial = dial;
            this.hands = dial.hands;
            this.parent = parentWatch;
            this.invert = settings.invert || false;
    
            this.hours = this.parent.rightNow.hours();
            this.isAM = this.hours < 12 ? true : false;
    
            this.hourAngle = 0;
            this.hourDivisor = dial.format === 12 ?
                30 :
                15;
    
            if (!this.parent.testing) this.init();
        }
    
        errorChecking(settings) {
            if (typeof settings === 'object') {
                if (!settings.id) throw new ReferenceError("The DayNightIndicstor class requires that an ID of the element be provided.");
            } else if (typeof settings !== 'string') {
                throw new ReferenceError('The Day/Night Indicator class expects either a settings object or a string containing the element\'s ID.');
            }
        }
    
        toggleAMPM() {
            this.isAM = !this.isAM;
        }
    
        removeTransitionDuration() {
            this.element.style.transition = 'none';
        }
    
        rotateIndicator() {
            let rotateValue = 0;
    
            if (this.hours >= 0 && this.hours < 6) {
                rotateValue = this.invert ? 180 : 0;
            } else if (this.hours >= 6 && this.hours < 12) {
                rotateValue = 90;
            } else if (this.hours >= 12 && this.hours < 18) {
                rotateValue = this.invert ? 0 : 180;
            } else {
                rotateValue = 270;
            }
    
            if (this.invert) rotateValue = rotateValue * -1;
    
            this.element.style.transform = `rotate(${rotateValue}deg)`;
        }
    
        getHourHandAngle() {
            this.hourAngle = this.parent.getCurrentRotateValue(this.hands.hour);
        }
    
        convertAngleToHours(name) {
            if (name !== this.dial.name) return;
    
            this.getHourHandAngle();
    
            if (this.hourAngle === 360) {
                this.toggleAMPM();
            }
    
            this.hours = Math.floor(this.hourAngle / this.hourDivisor);
            this.hours = this.isAM ? this.hours : this.hours + 12;
            this.hours = this.hours === 24 ? 0 : this.hours;
    
            this.rotateIndicator();
        }
    
        init() {
            this.removeTransitionDuration();
            this.rotateIndicator();
        }
    }
    
    module.exports = DayNightIndicator;