Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
angular-gobox / src / factories / date_picker.js
Size: Mime:
(function () {
    angular.module('angularGobox').factory('DatePicker', DatePickerFactory);

    DatePickerFactory.$inject = ['$rootScope'];

    function DatePickerFactory($rootScope) {
        var $scope = $rootScope.$new();

        var dateFormat = 'dddd, Do MMMM';
        var timeFormat = 'HH:MM';

        // saturday is 6, sunday is 0
        var SAT = 6, SUN = 0;

        function DatePicker(date, occupiedTimeslots) {
            var picker = this;
            picker.refDate = date || new Date();
            picker.occupiedTimeslots = occupiedTimeslots || [];

            calculateDates(picker);

            $scope.$watch(function () {
                return picker.date;
            }, _.partial(calculateTimes, picker));
            $scope.$watch(function () {
                return picker.time;
            }, function (time) {
                picker.datetime = time && moment(picker.date).add(time, 'hours').toDate();
            });
        }

        // private

        function hours(i) {
            return ('0' + i + ':00').slice(-5, 6);
        }

        function add(a, b) {
            return a + b;
        }

        function calculateDates(picker) {
            // We don't want to allow new trips during the weekend.
            // So, if today is saturday (day num 6), we add an offset of 1 day
            // to the possibleDates calculation. Since the earliest available date
            // is at minimum today + 1 day, this means that we skip sunday.
            var offset = picker.refDate.getDay() === SAT ? 1 : 0;

            picker.possibleDates = _.map(_.times(30, _.partial(add, 1)), function (i) {
                var date = moment(picker.refDate).startOf('day').add(i + offset, 'days');
                return {
                    label: date.format(dateFormat),
                    value: date.toDate()
                };
            });
            picker.date = picker.possibleDates[0].value;
        }

        function calculateTimes(picker) {
            var day = picker.date.getDay();
            var tomorrow = moment(picker.refDate).startOf('day').add(1, 'days');

            var firstHour = 8;
            var lastHour = 19;

            // weekends
            if (day === SAT || day === SUN) {
                firstHour = 9;
                lastHour = 17;
            }

            // If the picked date is tomorrow, the earliest available time is 14:00.
            if (tomorrow.diff(moment(picker.date).startOf('day'), 'days') === 0) {
                firstHour = 14;
            }

            var span = lastHour - firstHour + 1;

            picker.possibleTimes = _.map(_.times(span, _.partial(add, firstHour)), function (i) {
                return {
                    label: '' + hours(i) + ' - ' + hours(i + 1),
                    value: i
                };
            });

            removeOccupiedTimeslots(picker);

            picker.time = picker.possibleTimes[0].value;
        }

        function removeOccupiedTimeslots(picker) {
            picker.possibleTimes = _.filter(picker.possibleTimes, function (possibleTime) {
                // Remove all possible times that are on the same date && time as already occupied timeslots.
                return _.filter(picker.occupiedTimeslots, function (occupiedTimeslot) {
                        return moment(picker.date).diff(moment(occupiedTimeslot.timeslot), 'days') === 0 && moment(occupiedTimeslot.timeslot).hour() === possibleTime.value;
                    }).length < 1;
            });
        }

        return DatePicker;
    }
})();