49612a5cf6ab956289dfdf91532989e419d96bdb
[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         /**
47          * @type Date
48          */
49         lastDateLoaded: null,
50
51         /**
52          * Initialisation function
53          */
54         init: function init() {
55             // instantiate the libs
56             this.config = new Config();
57             this.model = new Model();
58             this.ui = new Ui();
59
60             // initialise the modules
61             this.model.init(this);
62             this.ui.init(this);
63
64             return this;
65         },
66
67         /**
68          * Application exit from model
69          */
70         exit: function exit() {
71             console.log('app.exit()');
72             this.model.exit();
73         },
74
75         /**
76          * Toggle this.fullDay
77          * @returns {boolean} variable state after the toggle
78          */
79         switchFullDay: function switchFullDay() {
80             console.log('switchFullDay()');
81             this.fullDay = !this.fullDay;
82             return this.fullDay;
83         },
84
85         /**
86          * Read the radio buttons and set this.alarm and this.alarmN accordingly
87          */
88         switchAlarm: function switchAlarm() {
89             var duration = 0;
90             duration = this.ui.alarm.getDuration();
91
92             if (duration) {
93                 this.alarmN = this.model.getCalendarAlarm(duration, "EventManager Reminder");
94                 this.alarm = true;
95             } else {
96                 this.alarm = false;
97             }
98         },
99
100         /**
101          * Create a new event in the default calendar,
102          * based on values found in #title, #des, #location
103          * and this.fullDay variable
104          */
105         addEvent: function addEvent() {
106             var selectedDate = '',
107                 eventDate = null,
108                 duration = 0,
109                 calendarItemInit = null,
110                 fullDay = false;
111
112             fullDay = this.fullDay;
113             selectedDate = this.ui.home.getStartDate();
114
115             duration = this.calculateDuration(
116                 selectedDate,
117                 this.ui.home.getEndDate(),
118                 fullDay
119             );
120
121             eventDate = this.createTZDateFromString(selectedDate);
122
123             calendarItemInit = {
124                 startDate: eventDate,
125                 isAllDay: fullDay,
126                 duration: duration,
127                 summary: this.ui.home.getTitle(),
128                 description: this.ui.home.getDescription(),
129                 location: this.ui.home.getLocation()
130             };
131
132             this.calendarItemInit = calendarItemInit;
133
134             if (this.alarmN) {
135                 calendarItemInit.alarms = [this.alarmN];
136             }/*
137             else{
138                 ev.alarms = null;
139             }*/
140             try {
141                 this.model.addEventToDefaultCalendar(calendarItemInit);
142             } catch (e) {
143                 console.error(e);
144             }
145             this.loadEvents(eventDate);
146         },
147
148         /**
149          * Calculates time duration
150          *
151          * If fullDay, then duration  The duration must be n*60*24 minutes for an event lasting n days.
152          *
153          * @param {string} startDate
154          * @param {string} endDate
155          * @param {bool=} fullDay 'false' by default
156          * @returns {TimeDuration}
157          */
158         calculateDuration: function calculateDuration(startDate, endDate, fullDay) {
159             var duration = 0;
160
161             if (fullDay === undefined) {
162                 fullDay = false;
163             }
164
165             startDate = new Date(startDate);
166             endDate = new Date(endDate);
167
168             duration = Math.round((endDate.getTime() - startDate.getTime()) / 60000); // needs duration in minutes;
169
170             return this.model.getTimeDuration(duration);
171         },
172
173         /**
174          * Create a TZDate object for the given date string, all assuming
175          * using the local timezone
176          *
177          * @param {string} dateString Local date/datetime
178          */
179         createTZDateFromString: function (dateString) {
180             var date = null,
181                 tzDate = null;
182             date = new Date(dateString);
183             tzDate = this.model.createTZDateFromDate(date);
184             return tzDate;
185         },
186
187         /**
188          * Load all scheduled events
189          */
190         loadEvents: function loadEvents() {
191             console.log("App.loadEvents()");
192
193             this.model.getEventsFromDefaultCalendar(
194                 undefined, // we always load all events now
195                 this.ui.home.onEventSearchSuccess.bind(this.ui.home), // Load events into the UI
196                 this.ui.home.onEventSearchError.bind(this.ui.home)
197             );
198         }
199
200     };
201 }());