benjamincharity
6/23/2015 - 7:38 PM

Create a 90 day calendar.

Create a 90 day calendar.

/* global jQuery */
'use strict';

angular.module('clutch.directives').directive('flipCalendar',
function($state, $rootScope, ngDialog, $q, cfpLoadingBar) {

  return {
    restrict: 'A',
    templateUrl: 'partials/authenticated/flip/calendar.html',
    scope: {
      category: '@'
    },
    link: function(scope) {
      // turn our string into an object
      var category = jQuery.parseJSON( scope.category );
      var weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];


      // Helper function
      // Return an array of days
      //
      // @param {int} The month number, 0 based
      // @param {int} The year, not zero based, required to account for leap years
      // @return {Date[]} List with date objects for each day of the month
      //
      var getDaysInMonth = function(month, year) {
        var date = new Date(year, month, 1);
        var days = [];
        while (date.getMonth() === month) {
          days.push(moment(date)._d);
          date.setDate(date.getDate() + 1);
        }
        return days;
      };

      //
      // Helper function
      // Turn a count: '6' into an array: '[1,2,3,4,5,6]'
      function createDaysArray(count) {
        var i;
        var array = [];
        for(i = 0; i < count; i++){
          array.push(i);
        }
        return array;
      }


      //
      // build our object for all months
      var months = {};
          months.month1 = {};
          months.month2 = {};
          months.month3 = {};

      months.month1.date = moment();
      months.month2.date = moment().add(1, 'months');
      months.month3.date = moment().add(2, 'months');

      months.month1.days = getDaysInMonth(moment(months.month1.date).month(), moment(months.month1.date).year());
      months.month2.days = getDaysInMonth(moment(months.month2.date).month(), moment(months.month2.date).year());
      months.month3.days = getDaysInMonth(moment(months.month3.date).month(), moment(months.month3.date).year());

      months.month1.startsOn = moment(months.month1.date).startOf('month')._d.toString().substring(0,3);
      months.month2.startsOn = moment(months.month2.date).startOf('month')._d.toString().substring(0,3);
      months.month3.startsOn = moment(months.month3.date).startOf('month')._d.toString().substring(0,3);

      months.month1.blankDaysCount = weekdays.indexOf(months.month1.startsOn);
      months.month2.blankDaysCount = weekdays.indexOf(months.month2.startsOn);
      months.month3.blankDaysCount = weekdays.indexOf(months.month3.startsOn);

      months.month1.blankDays = createDaysArray(months.month1.blankDaysCount);
      months.month2.blankDays = createDaysArray(months.month2.blankDaysCount);
      months.month3.blankDays = createDaysArray(months.month3.blankDaysCount);

      months.month1.prettyMonth = moment(months.month1.date).format('MMMM YYYY');
      months.month2.prettyMonth = moment(months.month2.date).format('MMMM YYYY');
      months.month3.prettyMonth = moment(months.month3.date).format('MMMM YYYY');


      //
      // Expose items to scope
      //
      scope.months = months;
      var now = moment();
      scope.today = now.hours(0).minutes(0).seconds(0)._d;
      scope.yesterday = moment().hours(0).minutes(0).seconds(0).subtract(1, 'days')._d;


      //
      // create our confirm modal
      var openConfirmModal = function () {
        ngDialog.open({
          template: 'partials/authenticated/flip/confirm.html',
          className: 'modal  confirm',
          scope: scope
        });
      };


      //
      // function to open the modal
      //
      // Note: for some reason comparing `day < today` returns true rather than
      // `day == today` returning true..
      scope.openConfirm = function(day) {
        scope.prettyDate = moment(day).format('MMMM Do');
        openConfirmModal();
        scope.day = day;
        scope.daysUntil = moment(day).fromNow();
      };


      //
      // Confirm request function
      scope.confirmDay = function(day) {
        cfpLoadingBar.start();

        // create the new flip request object
        var Flip = Parse.Object.extend('Flips');
        var newflipRequest = new Flip();
        var databaseDate = moment(day).toISOString();

        newflipRequest.set('user', Parse.User.current());
        newflipRequest.set('vehicle', {
          __type: 'Pointer',
          className: 'VehicleTypes',
          objectId: category.objectId
        });
        newflipRequest.set('requestedDate', databaseDate);

        newflipRequest.save(null, {
          success: function() {
            cfpLoadingBar.complete();
            ngDialog.close();
            $state.go('home');
          },
          error: function(currentUser, error) {
            console.log(error);
            cfpLoadingBar.complete();
            scope.$apply(function () {
              scope.scheduleError = error.message;
            });
          }
        });

      };

    }
  };
});