10
10
11
11
angular . module ( 'ui.calendar' , [ ] )
12
12
. constant ( 'uiCalendarConfig' , { } )
13
- . controller ( 'uiCalendarCtrl' , [ '$scope' , function ( $scope ) {
14
-
15
- var sourceSerialId = 1 ,
13
+ . controller ( 'uiCalendarCtrl' , [ '$scope' , '$timeout' , function ( $scope , $timeout ) {
14
+
15
+ var sourceSerialId = 1 ,
16
16
eventSerialId = 1 ,
17
- sources = $scope . eventSources ;
18
- extraEventSignature = $scope . calendarWatchEvent ? $scope . calendarWatchEvent : angular . noop ;
17
+ sources = $scope . eventSources ,
18
+ extraEventSignature = $scope . calendarWatchEvent ? $scope . calendarWatchEvent : angular . noop ,
19
+
20
+ wrapFunctionWithScopeApply = function ( functionToWrap ) {
21
+ var wrapper ;
22
+
23
+ if ( functionToWrap ) {
24
+ wrapper = function ( ) {
25
+ // This happens outside of angular context so we need to wrap it in a timeout which has an implied apply.
26
+ // In this way the function will be safely executed on the next digest.
27
+
28
+ var args = arguments ;
29
+ $timeout ( function ( ) {
30
+ functionToWrap . apply ( this , args ) ;
31
+ } ) ;
32
+ } ;
33
+ }
34
+
35
+ return wrapper ;
36
+ } ,
37
+
38
+ addRefetchEventsCommand = function ( settings , calendar ) {
39
+ if ( ! settings ) {
40
+ return ;
41
+ }
42
+
43
+ settings . refetchEvents = function ( ) {
44
+ calendar . fullCalendar ( 'refetchEvents' ) ;
45
+ } ;
46
+ } ;
19
47
20
48
this . eventsFingerprint = function ( e ) {
21
49
if ( ! e . __uiCalId ) {
@@ -132,7 +160,23 @@ angular.module('ui.calendar', [])
132
160
} ;
133
161
} ;
134
162
163
+ this . addCommands = function ( settings , calendar ) {
164
+ addRefetchEventsCommand ( settings , calendar ) ;
165
+ } ;
166
+
167
+ this . getFullCalendarConfig = function ( calendarSettings , uiCalendarConfig ) {
168
+ var config = { } ;
135
169
170
+ angular . extend ( config , uiCalendarConfig ) ;
171
+ angular . extend ( config , calendarSettings ) ;
172
+
173
+ config . dayClick = wrapFunctionWithScopeApply ( config . dayClick ) ;
174
+ config . eventClick = wrapFunctionWithScopeApply ( config . eventClick ) ;
175
+ config . eventDrop = wrapFunctionWithScopeApply ( config . eventDrop ) ;
176
+ config . eventResize = wrapFunctionWithScopeApply ( config . eventResize ) ;
177
+
178
+ return config ;
179
+ } ;
136
180
} ] )
137
181
. directive ( 'uiCalendar' , [ 'uiCalendarConfig' , '$locale' , function ( uiCalendarConfig , $locale ) {
138
182
// Configure to use locale names by default
@@ -160,14 +204,22 @@ angular.module('ui.calendar', [])
160
204
link : function ( scope , elm , attrs , controller ) {
161
205
162
206
var sources = scope . eventSources ,
163
- sourcesChanged = false ;
207
+ sourcesChanged = false ,
164
208
eventSourcesWatcher = controller . changeWatcher ( sources , controller . sourcesFingerprint ) ,
165
209
eventsWatcher = controller . changeWatcher ( controller . allEvents , controller . eventsFingerprint ) ,
166
210
options = null ;
167
-
211
+
168
212
function getOptions ( ) {
213
+ var calendarSettings = attrs . uiCalendar ? scope . $parent . $eval ( attrs . uiCalendar ) : { } ,
214
+ fullCalendarConfig ;
215
+
216
+ controller . addCommands ( calendarSettings , scope . calendar ) ;
217
+
218
+ fullCalendarConfig = controller . getFullCalendarConfig ( calendarSettings , uiCalendarConfig ) ;
219
+
169
220
options = { eventSources : sources } ;
170
- angular . extend ( options , uiCalendarConfig , attrs . uiCalendar ? scope . $parent . $eval ( attrs . uiCalendar ) : { } ) ;
221
+ angular . extend ( options , fullCalendarConfig ) ;
222
+
171
223
var options2 = { } ;
172
224
for ( var o in options ) {
173
225
if ( o !== 'eventSources' ) {
@@ -184,7 +236,7 @@ angular.module('ui.calendar', [])
184
236
scope . calendar = elm . html ( '' ) ;
185
237
}
186
238
} ;
187
-
239
+
188
240
scope . init = function ( ) {
189
241
scope . calendar . fullCalendar ( options ) ;
190
242
} ;
@@ -233,5 +285,4 @@ angular.module('ui.calendar', [])
233
285
} ) ;
234
286
}
235
287
} ;
236
- } ] ) ;
237
-
288
+ } ] ) ;
0 commit comments