Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
fix: ensure angular 1.6 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lewis committed Dec 14, 2016
1 parent dedb742 commit 76a7fab
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 83 deletions.
146 changes: 77 additions & 69 deletions src/directives/mwlCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
22 changes: 15 additions & 7 deletions src/directives/mwlCalendarMonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ angular

});

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

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

if (!dayClickedFirstRun) {
Expand Down Expand Up @@ -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() {

Expand Down
22 changes: 15 additions & 7 deletions src/directives/mwlCalendarYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ angular

});

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

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

if (!monthClickedFirstRun) {
Expand Down Expand Up @@ -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() {

Expand Down

0 comments on commit 76a7fab

Please sign in to comment.