[EventManager]update EventManager(tizen_2.1)
[samples/web/EventManager.git] / js / app.model.js
1 /*jslint devel:true*/
2 /*global tizen  */
3
4 /**
5  * @class Model
6  */
7 var Model = function Model() {
8         'use strict';
9 };
10
11 (function () { // strict mode wrapper
12         'use strict';
13         Model.prototype = {
14
15                 /**
16                  * Model module initialisation
17                  */
18                 init: function Model_init(app) {
19                         this.app = app;
20                 },
21
22                 /**
23                  * Creates a TimeDuration object corresponding to the given duration
24                  * in minutes
25                  *
26                  * @param {int} mins Duration in minutes
27                  * @return {TimeDuration}
28                  */
29                 getTimeDuration: function Model_getTimeDuration(mins) {
30                         return new tizen.TimeDuration(mins, "MINS");
31                 },
32
33                 /**
34                  * Creates a CalendarAlarm with the given duration
35                  *
36                  * @param {int} minutes Alarm duration in minutes
37                  * @returns {CalendarAlarm}
38                  */
39                 getCalendarAlarm: function Model_getCalendarAlarm(minutes, description) {
40                         var timeDuration,
41                                 calendarAlarm;
42
43                         timeDuration = this.getTimeDuration(minutes);
44                         calendarAlarm = new tizen.CalendarAlarm(timeDuration, "DISPLAY", description);
45
46                         return calendarAlarm;
47                 },
48
49                 /**
50                  * Create a new event and add it to the default calendar
51                  *
52                  * @param {Object} calendarItemInit
53                  * @config {TZDate} [startDate]
54                  * @config {bool} [isAllDay]
55                  * @config {TimeDuration} [duration]
56                  * @config {string} [summary]
57                  * @config {string} [description]
58                  * @config {string} [location]
59                  */
60                 addEventToDefaultCalendar: function Model_addEventToDefaultCalendar(calendarItemInit) {
61                         var calendar = null,
62                                 event = null;
63
64
65                         calendar = this.getCalendar();
66                         event = new tizen.CalendarEvent(calendarItemInit);
67                         calendar.add(event);
68                 },
69
70                 getCalendar: function Model_getCalendar() {
71                         return tizen.calendar.getDefaultCalendar("EVENT"); // get the default calendar
72                 },
73
74                 /**
75                  * Find all events in the default calendar on the given date
76                  *
77                  * @param {string} date date (in local time)
78                  * @param {function} onSuccess success callback
79                  * @param {function} onError error callback
80                  */
81                 getEventsFromDefaultCalendar: function Model_getEventsFromDefaultCalendar(date, onSuccess, onError) {
82                         var
83                                 /**
84                                  * @type {Calendar}
85                                  */
86                                 calendar = null,
87                                 /**
88                                  * @type {AttributeRangeFilter}
89                                  */
90                                 filter = null;
91
92
93                         calendar = this.getCalendar();
94
95                         if (date) {
96                                 // events on 'date''
97                                 filter = this.getStartDateFilter(new Date(date));
98                         } else {
99                                 // all future events
100                                 filter = this.getStartDateFilter(new Date(), true);
101                         }
102
103                         calendar.find(onSuccess, onError, filter);
104                 },
105
106                 /**
107                  * Create a startDate attribute range filter for the given day
108                  *
109                  * @param {Date} date
110                  * @param {bool} noEndDate
111                  * @returns {AttributeRangeFilter}
112                  */
113                 getStartDateFilter: function Model_getStartDateFilter(date, noEndDate) {
114                         var tzDate = null,
115                                 endTzDate = null,
116                                 filter = null;
117
118                         // calculate start date for the filter
119                         tzDate = new tizen.TZDate(date.getFullYear(), date.getMonth(), date.getDate());
120
121                         if (noEndDate) {
122                                 endTzDate = undefined;
123                         } else {
124                                 endTzDate = tzDate.addDuration(new tizen.TimeDuration("1", "DAYS")); // calculate end date
125                         }
126                         filter = new tizen.AttributeRangeFilter("startDate", tzDate, endTzDate);
127                         return filter;
128                 },
129
130                 /**
131                  * @param {string} event_id
132                  */
133                 deleteEvent: function Model_deleteEvent(event_id) {
134                         var myCalendar = this.getCalendar();
135                         myCalendar.remove(new tizen.CalendarEventId(event_id));
136                         this.app.loadEvents(); // reload the list
137                 },
138
139                 /**
140                  * @param {string} event_id
141                  */
142                 editEvent: function Model_editEvent (event_id) {
143                         var myCalendar = this.getCalendar();
144                         return myCalendar.get(new tizen.CalendarEventId(event_id));
145                 },
146
147                 /**
148                  * @param {string} event_id
149                  * @param {Object} new_values
150                  */
151                 updateEvent: function Model_updateEvent(event_id, new_values) {
152                         var myCalendar = this.getCalendar(), new_event
153                                 event = myCalendar.get(new tizen.CalendarEventId(event_id));
154
155                         for (var prop in new_values) {
156                                 if (new_values.hasOwnProperty(prop)) {
157                                         event[prop] = new_values[prop]; // copy new values into the event object
158                                 }
159                         }
160
161                         myCalendar.update(event, false); // save to myCalendar
162                 },
163
164                 /**
165                  * @param {Date} date
166                  * @returns {TZDate} date with timezone info
167                  */
168                 createTZDateFromDate: function Model_createTZDateFromDate(date) {
169                         return new tizen.TZDate(date);
170                 },
171
172                 /**
173                  * Exit from the application
174                  */
175                 exit: function () {
176                         tizen.application.getCurrentApplication().exit();
177                 }
178         };
179 }());
180
181