Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fix: ensure angular 1.6 compatibility

  • Loading branch information...
Matt Lewis
Matt Lewis committed Dec 14, 2016
1 parent dedb742 commit 76a7fabd86429f5d61c1924d16aab6fc100be300
Showing with 107 additions and 83 deletions.
  1. +77 −69 src/directives/mwlCalendar.js
  2. +15 −7 src/directives/mwlCalendarMonth.js
  3. +15 −7 src/directives/mwlCalendarYear.js
@@ -9,13 +9,6 @@ angular

var vm = this;

if (vm.slideBoxDisabled) {
$log.warn(LOG_PREFIX, 'The `slide-box-disabled` option is deprecated and will be removed in the next release. ' +
'Instead set `cell-auto-open-disabled` to true');
}

vm.events = vm.events || [];

vm.changeView = function(view, newDay) {
vm.view = view;
vm.viewDate = newDay;
@@ -37,84 +30,99 @@ angular

};

var previousDate = moment(vm.viewDate);
var previousView = vm.view;
vm.$onInit = function() {

function checkEventIsValid(event) {
if (!event.startsAt) {
$log.warn(LOG_PREFIX, 'Event is missing the startsAt field', event);
} else if (!angular.isDate(event.startsAt)) {
$log.warn(LOG_PREFIX, 'Event startsAt should be a javascript date object. Do `new Date(event.startsAt)` to fix it.', event);
if (vm.slideBoxDisabled) {
$log.warn(LOG_PREFIX, 'The `slide-box-disabled` option is deprecated and will be removed in the next release. ' +
'Instead set `cell-auto-open-disabled` to true');
}

if (event.endsAt) {
if (!angular.isDate(event.endsAt)) {
$log.warn(LOG_PREFIX, 'Event endsAt should be a javascript date object. Do `new Date(event.endsAt)` to fix it.', event);
vm.events = vm.events || [];

var previousDate = moment(vm.viewDate);
var previousView = vm.view;

function checkEventIsValid(event) {
if (!event.startsAt) {
$log.warn(LOG_PREFIX, 'Event is missing the startsAt field', event);
} else if (!angular.isDate(event.startsAt)) {
$log.warn(LOG_PREFIX, 'Event startsAt should be a javascript date object. Do `new Date(event.startsAt)` to fix it.', event);
}
if (moment(event.startsAt).isAfter(moment(event.endsAt))) {
$log.warn(LOG_PREFIX, 'Event cannot start after it finishes', event);

if (event.endsAt) {
if (!angular.isDate(event.endsAt)) {
$log.warn(LOG_PREFIX, 'Event endsAt should be a javascript date object. Do `new Date(event.endsAt)` to fix it.', event);
}
if (moment(event.startsAt).isAfter(moment(event.endsAt))) {
$log.warn(LOG_PREFIX, 'Event cannot start after it finishes', event);
}
}
}
}

function refreshCalendar() {
function refreshCalendar() {

if (calendarTitle[vm.view] && angular.isDefined($attrs.viewTitle)) {
vm.viewTitle = calendarTitle[vm.view](vm.viewDate);
}
if (calendarTitle[vm.view] && angular.isDefined($attrs.viewTitle)) {
vm.viewTitle = calendarTitle[vm.view](vm.viewDate);
}

vm.events.forEach(function(event, index) {
checkEventIsValid(event);
event.calendarEventId = index;
});
vm.events.forEach(function(event, index) {
checkEventIsValid(event);
event.calendarEventId = index;
});

//if on-timespan-click="calendarDay = calendarDate" is set then don't update the view as nothing needs to change
var currentDate = moment(vm.viewDate);
var shouldUpdate = true;
if (
previousDate.clone().startOf(vm.view).isSame(currentDate.clone().startOf(vm.view)) &&
!previousDate.isSame(currentDate) &&
vm.view === previousView
) {
shouldUpdate = false;
//if on-timespan-click="calendarDay = calendarDate" is set then don't update the view as nothing needs to change
var currentDate = moment(vm.viewDate);
var shouldUpdate = true;
if (
previousDate.clone().startOf(vm.view).isSame(currentDate.clone().startOf(vm.view)) &&
!previousDate.isSame(currentDate) &&
vm.view === previousView
) {
shouldUpdate = false;
}
previousDate = currentDate;
previousView = vm.view;

if (shouldUpdate) {
// a $timeout is required as $broadcast is synchronous so if a new events array is set the calendar won't update
$timeout(function() {
$scope.$broadcast('calendar.refreshView');
});
}
}
previousDate = currentDate;
previousView = vm.view;

if (shouldUpdate) {
// a $timeout is required as $broadcast is synchronous so if a new events array is set the calendar won't update
$timeout(function() {
$scope.$broadcast('calendar.refreshView');
calendarHelper.loadTemplates().then(function() {
vm.templatesLoaded = true;

var eventsWatched = false;

//Refresh the calendar when any of these variables change.
$scope.$watchGroup([
'vm.viewDate',
'vm.view',
'vm.cellIsOpen',
function() {
return moment.locale() + $locale.id; //Auto update the calendar when the locale changes
}
], function() {
if (!eventsWatched) {
eventsWatched = true;
//need to deep watch events hence why it isn't included in the watch group
$scope.$watch('vm.events', refreshCalendar, true); //this will call refreshCalendar when the watcher starts (i.e. now)
} else {
refreshCalendar();
}
});
}
}

calendarHelper.loadTemplates().then(function() {
vm.templatesLoaded = true;

var eventsWatched = false;

//Refresh the calendar when any of these variables change.
$scope.$watchGroup([
'vm.viewDate',
'vm.view',
'vm.cellIsOpen',
function() {
return moment.locale() + $locale.id; //Auto update the calendar when the locale changes
}
], function() {
if (!eventsWatched) {
eventsWatched = true;
//need to deep watch events hence why it isn't included in the watch group
$scope.$watch('vm.events', refreshCalendar, true); //this will call refreshCalendar when the watcher starts (i.e. now)
} else {
refreshCalendar();
}
}).catch(function(err) {
$log.error('Could not load all calendar templates', err);
});

}).catch(function(err) {
$log.error('Could not load all calendar templates', err);
});
};

if (angular.version.minor < 5) {
vm.$onInit();
}

})
.directive('mwlCalendar', function() {
@@ -47,13 +47,6 @@ angular

});

if (vm.cellAutoOpenDisabled) {
$scope.$watchGroup([
'vm.cellIsOpen',
'vm.viewDate'
], toggleCell);
}

vm.dayClicked = function(day, dayClickedFirstRun, $event) {

if (!dayClickedFirstRun) {
@@ -150,6 +143,21 @@ angular
delete vm.dateRangeSelect;
};

vm.$onInit = function() {

if (vm.cellAutoOpenDisabled) {
$scope.$watchGroup([
'vm.cellIsOpen',
'vm.viewDate'
], toggleCell);
}

};

if (angular.version.minor < 5) {
vm.$onInit();
}

})
.directive('mwlCalendarMonth', function() {

@@ -40,13 +40,6 @@ angular

});

if (vm.cellAutoOpenDisabled) {
$scope.$watchGroup([
'vm.cellIsOpen',
'vm.viewDate'
], toggleCell);
}

vm.monthClicked = function(month, monthClickedFirstRun, $event) {

if (!monthClickedFirstRun) {
@@ -89,6 +82,21 @@ angular
});
};

vm.$onInit = function() {

if (vm.cellAutoOpenDisabled) {
$scope.$watchGroup([
'vm.cellIsOpen',
'vm.viewDate'
], toggleCell);
}

};

if (angular.version.minor < 5) {
vm.$onInit();
}

})
.directive('mwlCalendarYear', function() {

0 comments on commit 76a7fab

Please sign in to comment.
You can’t perform that action at this time.