summaryrefslogtreecommitdiff
path: root/js/app.model.js
blob: bdec7905d9408023443dea1e5a1f87fea1133bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*jslint devel:true*/
/*global tizen  */

/**
 * @class Model
 */
var Model = function Model() {
	'use strict';
};

(function () { // strict mode wrapper
	'use strict';
	Model.prototype = {

		/**
		 * Model module initialisation
		 */
		init: function Model_init(app) {
			this.app = app;
		},

		/**
		 * Creates a TimeDuration object corresponding to the given duration
		 * in minutes
		 *
		 * @param {int} mins Duration in minutes
		 * @return {TimeDuration}
		 */
		getTimeDuration: function Model_getTimeDuration(mins) {
			return new tizen.TimeDuration(mins, "MINS");
		},

		/**
		 * Creates a CalendarAlarm with the given duration
		 *
		 * @param {int} minutes Alarm duration in minutes
		 * @returns {CalendarAlarm}
		 */
		getCalendarAlarm: function Model_getCalendarAlarm(minutes, description) {
			var timeDuration,
				calendarAlarm;

			timeDuration = this.getTimeDuration(minutes);
			calendarAlarm = new tizen.CalendarAlarm(timeDuration, "DISPLAY", description);
			return calendarAlarm;
		},

		/**
		 * Create a new event and add it to the default calendar
		 *
		 * @param {Object} calendarItemInit
		 * @config {TZDate} [startDate]
		 * @config {bool} [isAllDay]
		 * @config {TimeDuration} [duration]
		 * @config {string} [summary]
		 * @config {string} [description]
		 * @config {string} [location]
		 */
		addEventToDefaultCalendar: function Model_addEventToDefaultCalendar(calendarItemInit) {
			var calendar = null,
				event = null;


			calendar = this.getCalendar();
			event = new tizen.CalendarEvent(calendarItemInit);
			calendar.add(event);
		},

		getCalendar: function Model_getCalendar() {
			return tizen.calendar.getDefaultCalendar("EVENT"); // get the default calendar
		},

		/**
		 * Find all events in the default calendar on the given date
		 *
		 * @param {string} date date (in local time)
		 * @param {function} onSuccess success callback
		 * @param {function} onError error callback
		 */
		getEventsFromDefaultCalendar: function Model_getEventsFromDefaultCalendar(date, onSuccess, onError) {
			var
				/**
				 * @type {Calendar}
				 */
				calendar = null,
				/**
				 * @type {AttributeRangeFilter}
				 */
				filter = null;


			calendar = this.getCalendar();
			if (date) {
				// events on 'date''
				filter = this.getStartDateFilter(new Date(date));
			} else {
				// all future events
				filter = this.getStartDateFilter(new Date(), true);
			}

			calendar.find(onSuccess, onError, filter);
		},

		/**
		 * Create a startDate attribute range filter for the given day
		 *
		 * @param {Date} date
		 * @param {bool} noEndDate
		 * @returns {AttributeRangeFilter}
		 */
		getStartDateFilter: function Model_getStartDateFilter(date, noEndDate) {
			var tzDate = null,
				endTzDate = null,
				filter = null;

			// calculate start date for the filter
			tzDate = new tizen.TZDate(date.getFullYear(), date.getMonth(), date.getDate());

			if (noEndDate) {
				endTzDate = undefined;
			} else {
				endTzDate = tzDate.addDuration(new tizen.TimeDuration("1", "DAYS")); // calculate end date
			}
			filter = new tizen.AttributeRangeFilter("startDate", tzDate, endTzDate);
			return filter;
		},

		isEventExists: function(event_id, success, error) {
			var myCalendar = this.getCalendar();
			error = error || new Function;
			success = success || new Function;
			try {
				myCalendar.get(new tizen.CalendarEventId(event_id));
				success();
			} catch(e) {
				error(e);
			}
		},

		/**
		 * @param {string} event_id
		 */
		deleteEvent: function Model_deleteEvent(event_id) {
			var myCalendar = this.getCalendar();
			myCalendar.remove(new tizen.CalendarEventId(event_id));
			this.app.loadEvents(); // reload the list
		},

		/**
		 * @param {string} event_id
		 */
		editEvent: function Model_editEvent(event_id) {
			var myCalendar = this.getCalendar();
			return myCalendar.get(new tizen.CalendarEventId(event_id));
		},

		/**
		 * @param {string} event_id
		 * @param {Object} new_values
		 */
		updateEvent: function Model_updateEvent(event_id, new_values) {
			var myCalendar = this.getCalendar(), new_event, prop,
				event = myCalendar.get(new tizen.CalendarEventId(event_id));

			for (prop in new_values) {
				if (new_values.hasOwnProperty(prop)) {
					event[prop] = new_values[prop]; // copy new values into the event object
				}
			}

			myCalendar.update(event, false); // save to myCalendar
		},

		/**
		 * @param {Date} date
		 * @returns {TZDate} date with timezone info
		 */
		createTZDateFromDate: function Model_createTZDateFromDate(date) {
			return new tizen.TZDate(date);
		},

		/**
		 * Exit from the application
		 */
		exit: function () {
			tizen.application.getCurrentApplication().exit();
		}
	};
}());