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

Commit 5fc2b19

Browse files
committed
feat(calendar.js) added a watcher for the calendars options closes #55
Signed-off-by: Josh Kurz <josh.kurz@turner.com>
1 parent 239d424 commit 5fc2b19

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

demo/calendarDemo.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function CalendarCtrl($scope) {
88
var d = date.getDate();
99
var m = date.getMonth();
1010
var y = date.getFullYear();
11+
12+
$scope.changeTo = 'Hungarian';
1113
/* event source that pulls from google.com */
1214
$scope.eventSource = {
1315
url: "http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic",
@@ -107,6 +109,19 @@ function CalendarCtrl($scope) {
107109
eventDrop: $scope.alertOnDrop,
108110
eventResize: $scope.alertOnResize
109111
}
112+
};
113+
$scope.uiConfig = {calendar: {}};
114+
115+
$scope.changeLang = function() {
116+
if($scope.changeTo === 'Hungarian'){
117+
$scope.uiConfig.calendar.dayNames = ["Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"];
118+
$scope.uiConfig.calendar.dayNamesShort = ["Vas", "Hét", "Kedd", "Sze", "Csüt", "Pén", "Szo"];
119+
$scope.changeTo= 'English';
120+
} else {
121+
$scope.uiConfig.calendar.dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
122+
$scope.uiConfig.calendar.dayNamesShort = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
123+
$scope.changeTo = 'Hungarian';
124+
};
110125
};
111126
/* event sources array*/
112127
$scope.eventSources = [$scope.events, $scope.eventSource, $scope.eventsF];

demo/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ <h3>Why?</h3>
6666
<p>Why Not?</p>
6767

6868
<div class="btn-group calTools">
69-
<button class="btn btn-info" ng-click="addRemoveEventSource(eventSources,eventSource)">
70-
Toggle an event source
69+
<button class="btn" ng-click="changeLang()">
70+
{{changeTo}}
71+
</button>
72+
<button class="btn" ng-click="addRemoveEventSource(eventSources,eventSource)">
73+
Toggle Source
7174
</button>
72-
7375
<button type="button" class="btn btn-primary" ng-click="addEvent()">
7476
Add Event
7577
</button>

src/calendar.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,27 @@ angular.module('ui.calendar', [])
155155

156156
return {
157157
restrict: 'A',
158-
scope: {eventSources:'=ngModel',config:'=',calendarWatchEvent: '&'},
158+
scope: {eventSources:'=ngModel',calendarWatchEvent: '&'},
159159
controller: 'uiCalendarCtrl',
160160
link: function(scope, elm, attrs, controller) {
161161

162162
var sources = scope.eventSources,
163163
sourcesChanged = false;
164164
eventSourcesWatcher = controller.changeWatcher(sources, controller.sourcesFingerprint),
165-
eventsWatcher = controller.changeWatcher(controller.allEvents, controller.eventsFingerprint);
165+
eventsWatcher = controller.changeWatcher(controller.allEvents, controller.eventsFingerprint),
166+
options = null;
167+
168+
function getOptions(){
169+
options = { eventSources: sources };
170+
angular.extend(options, uiCalendarConfig, attrs.uiCalendar ? scope.$parent.$eval(attrs.uiCalendar) : {});
171+
var options2 = {};
172+
for(var o in options){
173+
if(o !== 'eventSources'){
174+
options2[o] = options[o];
175+
}
176+
}
177+
return JSON.stringify(options2);
178+
}
166179

167180
scope.destroy = function(){
168181
if(attrs.calendar) {
@@ -173,8 +186,7 @@ angular.module('ui.calendar', [])
173186
};
174187

175188
scope.init = function(){
176-
var options = { eventSources: sources };
177-
angular.extend(options, uiCalendarConfig, attrs.uiCalendar ? scope.$parent.$eval(attrs.uiCalendar) : {});
189+
getOptions();
178190
scope.calendar.fullCalendar(options);
179191
};
180192

@@ -212,7 +224,14 @@ angular.module('ui.calendar', [])
212224
return false;
213225
}
214226
});
215-
}
227+
228+
scope.$watch(getOptions, function(newO,oldO){
229+
if(newO !== oldO){
230+
scope.destroy();
231+
scope.init();
232+
}
233+
});
234+
}
216235
};
217236
}]);
218237

test/calendar.spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('uiCalendar', function () {
7575
height: 200,
7676
weekends: false,
7777
defaultView: 'month'
78-
}
78+
}
7979
};
8080

8181
}));
@@ -172,6 +172,14 @@ describe('uiCalendar', function () {
172172
expect(fullCalendarParam1).toEqual(scope.events[0]);
173173
});
174174

175+
it('make sure that if the calendars options change then the fullcalendar method is called with the new options', function () {
176+
expect($.fn.fullCalendar.mostRecentCall.args[0].weekends).toEqual(false);
177+
scope.uiConfig.calendar.weekends = true;
178+
scope.$apply();
179+
expect($.fn.fullCalendar.callCount).toEqual(2);
180+
expect($.fn.fullCalendar.mostRecentCall.args[0].weekends).toEqual(true);
181+
});
182+
175183
});
176184

177185
describe('calendarCtrl', function(){

0 commit comments

Comments
 (0)