PageSourceSearch

https://www.hkolympic.org/wp-content/plugins/eclass-integration/pu…-integration-calendar-api.js?ver=2019.06.28

js hkolympic.org collected 2026-10-02 11:45:20 UTC 11,350 bytes, 359 lines download raw bytes

1jQuery(function($){
2	'use strict';
3
4	window.CalendarApi = function CalendarApi(){
5
6		//// CONST START ////
7		var SCHOOL_EVENT;
8		var ACADEMIC_EVENT;
9		var GROUP_EVENT;
10		var PUBLIC_HOLIDAY_EVENT;
11		var SCHOOL_HOLIDAY_EVENT;
12		if(ECLASS_CALENDAR_API.school_type == 'EJ50'){
13			SCHOOL_EVENT = 0;
14			ACADEMIC_EVENT = 1;
15			SCHOOL_HOLIDAY_EVENT = 2;
16			GROUP_EVENT = 3;
17			PUBLIC_HOLIDAY_EVENT = -1;
18		}else{
19			SCHOOL_EVENT = 0;
20			ACADEMIC_EVENT = 1;
21			GROUP_EVENT = 2;
22			PUBLIC_HOLIDAY_EVENT = 3;
23			SCHOOL_HOLIDAY_EVENT = 4;
24		}
25		//// CONST END ////
26		
27		//// Private var START ////
28		var _self = this;
29		var _onDataLoadFuncs = [];
30		
31		var _allEventId = [];
32		
33		var _day_holiday = [];
34		var _day_school_holiday = [];
35		var _day_school_event = [];
36		var _day_academic = [];
37		var _day_group_event = [];
38		//// Private var END ////
39		
40		//// Public var START ////
41		this.isLoading = false;
42		this.allData = {};
43		//// Public var END ////
44		
45		
46		//// Private func START ////
47		var _onDataLoad = function(){
48			$.each(_onDataLoadFuncs, function(index, func){
49				func(_self.allData);
50			});
51		}
52		
53		var _getCalendarEvent = function(startDate, endDate){
54		    return $.ajax({
55		        url: ECLASS_CALENDAR_API.url,
56		        type: 'get',
57		        data: {
58		            'action':'eclass_calendar_api',
59		            '_ajax_nonce':ECLASS_CALENDAR_API._ajax_nonce,
60		            'start_date': startDate,
61		            'end_date': endDate
62		        },
63		        complete : function(){
64		        	_self.isLoading = false;
65		        },
66		        success: function( response ) {
67		        	var data = [];
68		        	if(
69	        			typeof(response['Events']) == 'undefined' ||
70	        			typeof(response['Events']['Event']) == 'undefined'
71    				){
72			            _onDataLoad();
73			            return;
74		        	}
75		            
76		        	//// Parse data START ////
77		            if(typeof(response['Events']['Event']['EventID']) == 'undefined'){
78		            	$.each(response['Events']['Event'], function(index, event){
79		            		if(typeof(_self.allData[event['EventDate']]) == 'undefined'){
80		            			_self.allData[event['EventDate']] = [];
81		            		}
82		            		if($.inArray(event['EventID'], _allEventId) == -1){
83			            		_allEventId.push(event['EventID']);
84			            		_self.allData[event['EventDate']].push(event);
85			            		data.push(event);
86		            		}
87		            	});
88		            }else{
89	            		if(typeof(_self.allData[event['EventDate']]) == 'undefined'){
90	            			_self.allData[event['EventDate']] = [];
91	            		}
92	            		if($.inArray(event['EventID'], _allEventId) == -1){
93		            		_allEventId.push(event['EventID']);
94			            	_self.allData[event['EventDate']].push(response['Events']['Event']);
95			            	data.push(response['Events']['Event']);
96	            		}
97		            }
98		            _onDataLoad();
99		        	//// Parse data END ////
100		        	
101		            $.each(data, function(index, event){
102		            	switch(parseInt(event['EventType'])){
103		            	case SCHOOL_EVENT:
104		            		_day_school_event.push(event['EventDate']);
105		            		break;
106		            	case ACADEMIC_EVENT:
107		            		_day_academic.push(event['EventDate']);
108		            		break;
109		            	case GROUP_EVENT:
110		            		_day_group_event.push(event['EventDate']);
111		            		break;
112		            	case PUBLIC_HOLIDAY_EVENT:
113		            		_day_holiday.push(event['EventDate']);
114		            		break;
115		            	case SCHOOL_HOLIDAY_EVENT:
116		            		_day_school_holiday.push(event['EventDate']);
117		            		break;
118		            	}
119		            });
120		        },
121		    });
122		}
123
124		this.loadData = (function(data){
125			var deferred = $.Deferred();
126
127			var _data = $.extend({}, {
128	            'action':'eclass_calendar_api',
129	            '_ajax_nonce':ECLASS_CALENDAR_API._ajax_nonce,
130	            'start_date': '',
131	            'end_date': ''
132	        }, data);
133			
134			$.ajax({
135		        url: ECLASS_CALENDAR_API.url,
136		        type: 'get',
137		        data: _data,
138		        success: function( response ) {
139		        	var data = [];
140		        	if(
141	        			typeof(response['Events']) == 'undefined' ||
142	        			typeof(response['Events']['Event']) == 'undefined'
143    				){
144			        	deferred.resolve(data);
145			            return;
146		        	}
147		            
148		        	//// Parse data START ////
149		            if(typeof(response['Events']['Event']['EventID']) == 'undefined'){
150		            	$.each(response['Events']['Event'], function(index, event){
151		            		if(typeof(_self.allData[event['EventDate']]) == 'undefined'){
152		            			_self.allData[event['EventDate']] = [];
153		            		}
154		            		if(true||$.inArray(event['EventID'], _allEventId) == -1){
155			            		_allEventId.push(event['EventID']);
156			            		_self.allData[event['EventDate']].push(event);
157			            		data.push(event);
158		            		}
159		            	});
160		            }else{
161	            		if(typeof(_self.allData[event['EventDate']]) == 'undefined'){
162	            			_self.allData[event['EventDate']] = [];
163	            		}
164	            		if(true||$.inArray(event['EventID'], _allEventId) == -1){
165		            		_allEventId.push(event['EventID']);
166			            	_self.allData[event['EventDate']].push(response['Events']['Event']);
167			            	data.push(response['Events']['Event']);
168	            		}
169		            }
170		        	//// Parse data END ////
171		        	
172		            $.each(data, function(index, event){
173		            	switch(parseInt(event['EventType'])){
174		            	case SCHOOL_EVENT:
175		            		_day_school_event.push(event['EventDate']);
176		            		break;
177		            	case ACADEMIC_EVENT:
178		            		_day_academic.push(event['EventDate']);
179		            		break;
180		            	case GROUP_EVENT:
181		            		_day_group_event.push(event['EventDate']);
182		            		break;
183		            	case PUBLIC_HOLIDAY_EVENT:
184		            		_day_holiday.push(event['EventDate']);
185		            		break;
186		            	case SCHOOL_HOLIDAY_EVENT:
187		            		_day_school_holiday.push(event['EventDate']);
188		            		break;
189		            	}
190		            });
191		            
192
193		        	deferred.resolve(data);
194		        },
195		    });
196			
197		    return deferred.promise();
198		});
199		//// Private func END ////
200		
201
202		//// Public func START ////
203		this.onDataLoad = function(func){
204			if(typeof(func) == 'function'){
205				_onDataLoadFuncs.push(func);
206			}else{
207				throw new TypeError('func must be a function');
208			}
209		}; // End onDataLoad()
210		
211		this.initCalendarAPI = function(){
212			_self.isLoading = true;
213			
214			var currentDate = new Date();
215			var startDate = new Date(currentDate.getFullYear(), currentDate.getMonth() - 3, 1);
216			startDate = startDate.toISOString().slice(0,10);
217			var endDate = new Date(currentDate.getFullYear(), currentDate.getMonth() + 3, 0);
218			endDate = endDate.toISOString().slice(0,10);
219			
220			_getCalendarEvent(startDate, endDate).done(function(){
221				$("#calendar").datepicker("refresh");
222			});
223			
224			_self.initCalendarDatepicker();
225		}; // End initCalendarAPI()
226		
227		this.initCalendarDatepicker = function(){
228			$("#calendar").datepicker({
229				onChangeMonthYear : function(year, month){
230					var startYear = year;
231					var startMonth = month - 3;
232					var endYear = year;
233					var endMonth = month + 3;
234					
235					if(startMonth < 1){
236						startYear--;
237						startMonth += 12;
238					}
239					if(endMonth > 12){
240						endYear++;
241						endMonth -= 12;
242					}
243					startMonth = ('0' + startMonth).slice(-2);
244					endMonth = ('0' + endMonth).slice(-2);
245
246					var startDate = startYear + '-' + startMonth + '-01';
247					var endDate = (new Date(endYear, endMonth-1, 0)).toISOString().slice(0,10);
248					
249					_self.isLoading = true;
250					_getCalendarEvent(startDate, endDate);
251				},
252				beforeShowDay : function(date){
253					var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
254					var localISOTime = (new Date(date - tzoffset)).toISOString();
255					
256					var d = localISOTime.slice(0,10);
257					var customClass = '';
258					var hasDateEvent = false;
259					
260					if(date.getDay() == 0){
261						customClass += ' day_sunday';
262					}
263					if($.inArray(d, _day_holiday) != -1){
264						customClass += ' day_holiday';
265						hasDateEvent = true;
266					}
267					if($.inArray(d, _day_school_holiday) != -1){
268						customClass += ' day_school_holiday';
269						hasDateEvent = true;
270					}
271					if($.inArray(d, _day_school_event) != -1){
272						customClass += ' day_school_event';
273						hasDateEvent = true;
274					}
275					if($.inArray(d, _day_academic) != -1){
276						customClass += ' day_academic';
277						hasDateEvent = true;
278					}
279					if($.inArray(d, _day_group_event) != -1){
280						customClass += ' day_group_event';
281						hasDateEvent = true;
282					}
283					
284					return [hasDateEvent, customClass, ""];
285				},
286				onSelect : function(){
287					var date = $(this).datepicker('getDate');
288					var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
289					var localISOTime = (new Date(date - tzoffset)).toISOString();
290					
291					var d = localISOTime.slice(0,10);
292					
293					var $modal = $("#calendarModal");
294					var $date = $modal.find('#date');
295					var $tbody = $modal.find('#event tbody');
296					
297					$date.html(d);
298					
299					$tbody.empty();
300					$.each(_self.allData[d], function(date, event){
301						var $tr = $('<tr>');
302						var $tdEventType = $('<td>');
303						var $tdEventName = $('<td>');
304						
305						var eventType = '';
306
307						if(ECLASS_CALENDAR_API.school_type == 'EJ50'){
308							switch(parseInt(event['EventType'])){
309			            	case SCHOOL_EVENT:
310			            		eventType = '學校事項';
311			            		break;
312			            	case ACADEMIC_EVENT:
313			            		eventType = '教學事項';
314			            		break;
315			            	case GROUP_EVENT:
316			            		eventType = '小組事項';
317			            		break;
318			            	case PUBLIC_HOLIDAY_EVENT:
319			            	case SCHOOL_HOLIDAY_EVENT:
320			            		eventType = '假期';
321			            		break;
322							}
323						}else{
324							switch(parseInt(event['EventType'])){
325							case SCHOOL_EVENT:
326								eventType = '學校事項';
327								break;
328							case ACADEMIC_EVENT:
329								eventType = '教學事項';
330								break;
331							case GROUP_EVENT:
332								eventType = '小組事項';
333								break;
334							case PUBLIC_HOLIDAY_EVENT:
335								eventType = '公眾假期';
336								break;
337							case SCHOOL_HOLIDAY_EVENT:
338								eventType = '學校假期';
339								break;
340							}
341						}
342						$tdEventType.append(eventType);
343						$tdEventName.append(event['Title']);
344						
345						$tr.append($tdEventType);
346						$tr.append($tdEventName);
347						$tbody.append($tr);
348					});
349					$modal.modal();
350				},
351				firstDay: 7
352			});
353		}; // End initCalendarDatepicker()
354		
355		//// Public func END ////
356	}; // End class CalendarApi
357	
358    
359});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.