Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Added resourceWatcher #354

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ angular.module('ui.calendar', [])
};

var eventSerialId = 1;
var resourceSerialId = 1;
// @return {String} fingerprint of the event object and its properties
this.eventFingerprint = function(e) {
if (!e._id) {
Expand All @@ -50,6 +51,16 @@ angular.module('ui.calendar', [])
(e.allDay || '') + (e.className || '') + extraSignature;
};

// @return {String} fingerprint of the event object and its properties
this.resourceFingerprint = function(r) {
if (!r._id) {
r._id = resourceSerialId++;
}

// This extracts all the information we need from the resource. http://jsperf.com/angular-calendar-events-fingerprint/3
return r.id;
};

var sourceSerialId = 1, sourceEventsSerialId = 1;
// @return {String} fingerprint of the source object and its events array
this.sourceFingerprint = function(source) {
Expand Down Expand Up @@ -98,6 +109,7 @@ angular.module('ui.calendar', [])
this.changeWatcher = function(arraySource, tokenFn) {
var self;
var getTokens = function() {
// console.log(arraySource);
var array = angular.isFunction(arraySource) ? arraySource() : arraySource;
var result = [], token, el;
for (var i = 0, n = array.length; i < n; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis fails here since test files where not updated. Therefore scope.resources is undefined.
I believe line 238 should be modified (see comment there)

Expand Down Expand Up @@ -214,15 +226,16 @@ angular.module('ui.calendar', [])
.directive('uiCalendar', ['uiCalendarConfig', function(uiCalendarConfig) {
return {
restrict: 'A',
scope: {eventSources:'=ngModel',calendarWatchEvent: '&'},
scope: {eventSources:'=ngModel',calendarWatchEvent: '&', resources: '='},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the API consistent, we should also add a calendarWatchResource binding for custom finger prints of resources

controller: 'uiCalendarCtrl',
link: function(scope, elm, attrs, controller) {

var sources = scope.eventSources,
evResources = scope.resources,
sourcesChanged = false,
calendar,
eventSourcesWatcher = controller.changeWatcher(sources, controller.sourceFingerprint),
eventsWatcher = controller.changeWatcher(controller.allEvents, controller.eventFingerprint),
resourceWatcher = controller.changeWatcher(scope.resources, controller.resourceFingerprint),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change controller.changeWatcher(scope.resources, controller.resourceFingerprint) to controller.changeWatcher(scope.resources || [], controller.resourceFingerprint)

options = null;

function getOptions(){
Expand All @@ -233,7 +246,8 @@ angular.module('ui.calendar', [])

var localeFullCalendarConfig = controller.getLocaleConfig(fullCalendarConfig);
angular.extend(localeFullCalendarConfig, fullCalendarConfig);
options = { eventSources: sources };
options = { eventSources: sources,
resources: evResources };
angular.extend(options, localeFullCalendarConfig);
//remove calendars from options
options.calendars = null;
Expand Down Expand Up @@ -265,6 +279,7 @@ angular.module('ui.calendar', [])
calendar.fullCalendar(options);
if(attrs.calendar) {
uiCalendarConfig.calendars[attrs.calendar] = calendar;
window.calendar = calendar;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure why we need to expose the calendar on window

}
};
scope.$on('$destroy', function() {
Expand Down Expand Up @@ -299,7 +314,7 @@ angular.module('ui.calendar', [])

eventsWatcher.onAdded = function(event) {
if (calendar && calendar.fullCalendar) {
calendar.fullCalendar('renderEvent', event, (event.stick ? true : false));
calendar.fullCalendar('renderEvent', event, true);
}
};

Expand All @@ -309,6 +324,18 @@ angular.module('ui.calendar', [])
}
};

resourceWatcher.onAdded = function(resource) {
if (calendar && calendar.fullCalendar) {
calendar.fullCalendar('addResource', resource);
}
};

resourceWatcher.onRemoved = function(resource) {
if (calendar && calendar.fullCalendar) {
calendar.fullCalendar('removeResource', resource);
}
};

eventsWatcher.onChanged = function(event) {
if (calendar && calendar.fullCalendar) {
var clientEvents = calendar.fullCalendar('clientEvents', event._id);
Expand All @@ -320,6 +347,7 @@ angular.module('ui.calendar', [])
}
};

resourceWatcher.subscribe(scope);
eventSourcesWatcher.subscribe(scope);
eventsWatcher.subscribe(scope, function() {
if (sourcesChanged === true) {
Expand Down