[EventManager]update EventManager(tizen_2.1)
[samples/web/EventManager.git] / js / app.js
1 /*jslint devel:true*/
2 /*global Config, Model, Ui*/
3
4 var App = null;
5
6 (function () { // strict mode wrapper
7         'use strict';
8
9         /**
10          * Creates a new application object
11          *
12          * @class Application
13          */
14         App = function App() {};
15
16         App.prototype = {
17                 /**
18                  * @type Array
19                  */
20                 requires: ['js/app.config.js', 'js/app.model.js', 'js/app.ui.js', 'js/app.ui.templateManager.js'],
21                 /**
22                  * @type Config
23                  */
24                 config: null,
25                 /**
26                  * @type Model
27                  */
28                 model: null,
29                 /**
30                  * @type Ui
31                  */
32                 ui: null,
33                 /**
34                  * @type bool
35                  */
36                 fullDay: false,
37                 /**
38                  * @type bool
39                  */
40                 alarm: false,
41                 /**
42                  * @type CalendarAlarm
43                  */
44                 alarmN: null,
45                 /**
46                  * @type Date
47                  */
48                 lastDateLoaded: null,
49                 /**
50                  * @type Integer
51                  */
52                 eventId: 0,
53
54                 /**
55                  * Initialisation function
56                  */
57                 init: function init() {
58                         // instantiate the libs
59                         this.config = new Config();
60                         this.model = new Model();
61                         this.ui = new Ui();
62
63                         // initialise the modules
64                         this.model.init(this);
65                         this.ui.init(this);
66
67                         return this;
68                 },
69
70                 /**
71                  * Application exit from model
72                  */
73                 exit: function exit() {
74                         this.model.exit();
75                 },
76
77                 /**
78                  * Toggle this.fullDay
79                  * @returns {boolean} variable state after the toggle
80                  */
81                 switchFullDay: function switchFullDay() {
82                         this.fullDay = !this.fullDay;
83                         return this.fullDay;
84                 },
85
86                 /**
87                  * Read the radio buttons and set this.alarm and this.alarmN accordingly
88                  */
89                 switchAlarm: function switchAlarm() {
90                         var duration = 0;
91                         duration = this.ui.alarm.getDuration();
92
93                         if (duration) {
94                                 this.alarmN = this.model.getCalendarAlarm(duration, "EventManager Reminder");
95                                 this.alarm = true;
96                         } else {
97                                 this.alarm = false;
98                         }
99                         app.ui.alarm.updateDurationLabel();
100                 },
101
102                 /**
103                  * Create a new event in the default calendar,
104                  * based on values found in #title, #des, #location
105                  * and this.fullDay variable
106                  */
107                 addEvent: function addEvent(e) {
108                         var selectedDate = '',
109                                 eventDate = null,
110                                 duration = 0,
111                                 calendarItemInit = null,
112                                 fullDay = false;
113
114                         fullDay = this.fullDay;
115                         selectedDate = this.ui.home.getStartDate();
116
117                         duration = this.calculateDuration(
118                                 selectedDate,
119                                 this.ui.home.getEndDate(),
120                                 fullDay
121                         );
122
123                         eventDate = this.createTZDateFromString(selectedDate);
124
125                         calendarItemInit = {
126                                 startDate: eventDate,
127                                 isAllDay: fullDay,
128                                 duration: duration,
129                                 summary: this.ui.home.getTitle()
130                         };
131
132                         this.calendarItemInit = calendarItemInit;
133
134                         if (this.alarmN) {
135                                 calendarItemInit.alarms = [this.alarmN];
136                         }
137                         try {
138                                 this.model.addEventToDefaultCalendar(calendarItemInit);
139                         } catch (ex) {
140                                 console.error(ex.message);
141                         }
142                         this.loadEvents(eventDate);
143                 },
144
145                 updateEvent: function() {
146                         var new_values, selectedDate, duration;
147
148                         selectedDate = this.ui.home.getStartDate();
149
150                         duration = this.calculateDuration(
151                                         selectedDate,
152                                         this.ui.home.getEndDate(),
153                                         false
154                                 );
155
156                         new_values = {
157                                 startDate: this.createTZDateFromString(selectedDate),
158                                 duration: duration,
159                                 summary: this.ui.home.getTitle(),
160                         }
161
162                         if (this.alarmN) {
163                                 // blocked by api issue
164                                 //new_values.alarms = [this.alarmN];
165                         }
166
167                         this.model.updateEvent(app.eventId, new_values);
168                         this.loadEvents();
169                 },
170
171                 /**
172                  * Calculates time duration
173                  *
174                  * If fullDay, then duration  The duration must be n*60*24 minutes for an event lasting n days.
175                  *
176                  * @param {string} startDate
177                  * @param {string} endDate
178                  * @param {bool=} fullDay 'false' by default
179                  * @returns {TimeDuration}
180                  */
181                 calculateDuration: function calculateDuration(startDate, endDate, fullDay) {
182                         var duration = 0;
183
184                         if (fullDay === undefined) {
185                                 fullDay = false;
186                         }
187
188                         startDate = new Date(startDate);
189                         endDate = new Date(endDate);
190
191                         duration = Math.round((endDate.getTime() - startDate.getTime()) / 60000); // needs duration in minutes;
192
193                         return this.model.getTimeDuration(duration);
194                 },
195
196                 /**
197                  * Create a TZDate object for the given date string, all assuming
198                  * using the local timezone
199                  *
200                  * @param {string} dateString Local date/datetime
201                  */
202                 createTZDateFromString: function (dateString) {
203                         var date = null,
204                                 tzDate = null;
205                         date = new Date(dateString);
206                         tzDate = this.model.createTZDateFromDate(date);
207                         return tzDate;
208                 },
209
210                 /**
211                  * Load all scheduled events
212                  */
213                 loadEvents: function loadEvents() {
214
215                         this.model.getEventsFromDefaultCalendar(
216                                 undefined, // we always load all events now
217                                 this.ui.home.onEventSearchSuccess.bind(this.ui.home), // Load events into the UI
218                                 this.ui.home.onEventSearchError.bind(this.ui.home)
219                         );
220                 }
221
222         };
223 }());