2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /*jslint devel: true*/
18 /*global $, app, tizen, TemplateManager, window */
28 (function () { // strict mode wrapper
32 templateManager: null,
35 * UI module initialisation
37 init: function UI_init(app, exerciseId) {
39 this.currentExercise = this.app.getExercise('id', exerciseId);
40 this.templateManager = new TemplateManager();
41 $(document).ready(this.domInit.bind(this));
44 this.home.context = this;
45 this.new_exercise.context = this;
46 this.notification.context = this;
50 * When DOM is ready, initialise it
52 domInit: function UI_domInit() {
53 this.templateManager.loadToCache(
59 'exerciseNotification'
61 this.initPages.bind(this)
63 // Disable text selection
64 $.mobile.tizen.disableSelection(document);
68 * Append pages to body and initialise them
70 initPages: function UI_initPages() {
73 pages.push(this.templateManager.get('home'));
74 pages.push(this.templateManager.get('new_exercise'));
75 pages.push(this.templateManager.get('notification'));
77 $('body').append(pages.join(''));
80 this.new_exercise.init();
81 this.notification.init();
83 window.addEventListener('tizenhwkey', function (e) {
84 var activePageId = $.mobile.activePage.attr('id');
85 if (e.keyName === 'back') {
86 if (activePageId === 'home') {
88 } else if (activePageId === 'notification') {
90 } else if (activePageId === 'new_exercise') {
91 $.mobile.changePage('#home');
98 $('input[type=checkbox]').change(function () {
102 $('#name').on('keyup change', function () {
103 var checked = $('#newExerciseDays input:checkbox:checked');
104 if (checked.length > 0) {
105 $('#add-exercise-btn').removeClass('ui-disabled');
107 $('#add-exercise-btn').addClass('ui-disabled');
111 if (this.currentExercise) {
112 // app run from alarm
113 $.mobile.changePage('#notification', 'pop', false, true);
115 $.mobile.changePage('#home', 'pop', false, true);
120 * Contains methods related to the home page
126 * Initialize home page
128 init: function UI_home_init() {
133 * Bind events to the home page
135 addEvents: function () {
138 $('#home').on('pagebeforeshow', function () {
142 $('.removeExercise').live('click', function () {
143 var exerciseId = $(this).data('exerciseid');
144 app.ui.popup('Are you sure?', {
146 $('#popup').popup('close');
149 self.context.app.removeExercise(
151 self.displayList.bind(self)
153 $('#popup').popup('close');
160 * Build exercises HTML list and adds it to page
161 * @param {Array|undefined} exercises list
163 displayList: function (exercises) {
164 var len, list = '', exercise;
165 exercises = exercises || this.context.app.getAllExercises();
166 len = exercises.length - 1;
168 exercise = $.extend({}, exercises[len]); // copy object
169 exercise.startTime = exercise
171 .match(/.+T([0-9]+\:[0-9]+)\:[0-9]+/)[1];
172 list += this.context.templateManager.get(
178 $('#alarms_list').html(list);
184 * Contains methods related to the new exercise page
190 * Initialize new exercise page
197 * Bind events to new exercise page
199 addEvents: function () {
203 toggleSaveButton = function () {
204 var $button = $('#add-exercise-btn');
205 if (numberOfChecked && isName) {
206 $button.removeClass('ui-disabled');
208 $button.addClass('ui-disabled');
212 $('#new_exercise').on('pagebeforeshow', function () {
218 $('#comment').val('');
219 checked = $('#newExerciseDays input:checkbox:checked');
220 len = checked.length - 1;
223 .attr('checked', false)
224 .data('checkboxradio')
232 $('#add-exercise-btn').on('click', function () {
233 var exercise = {}, days, len;
235 days = $('#newExerciseDays input:checkbox:checked');
236 len = days.length - 1;
239 exercise.days.unshift($(days[len]).data('day'));
243 exercise.name = $('#name').val();
244 exercise.startTime = $('#startTime')
245 .datetimepicker('value');
246 exercise.comment = $('#comment').val();
248 this.app.addExercise(exercise, function () {
249 $.mobile.changePage('#home');
252 }.bind(this.context));
254 $('#add-exercise-cancel-btn').on('click', function () {
258 $('#name').on('focusout', function () {
259 isName = ($(this).val().length > 0);
263 $('#newExerciseDays [type=checkbox]').on('change', function () {
264 if ($(this).is(':checked')) {
265 numberOfChecked += 1;
267 numberOfChecked -= 1;
277 * Contains methods related to the notification page
283 * Initialize notification page
290 * Bind events to notification page
292 addEvents: function () {
293 $('#notification').on('pagebeforeshow', function () {
296 exercise = $.extend({}, this.context.currentExercise);
297 exercise.startTime = exercise
299 .match(/.+T([0-9]+\:[0-9]+)\:[0-9]+/)[1];
300 html = this.context.templateManager.get(
301 'exerciseNotification',
304 $('.notificationContainer').html(html);
307 $('.exit').on('click', function () {
317 * Create and display popup widget
318 * @param {String} text information
319 * @param {Object} buttons template object
321 Ui.prototype.popup = function (text, buttons) {
322 var i, popup = $('#popup'), popupNumber = Object.keys(buttons).length;
324 // if the popup HTML wasn't initialized
326 if (!popup.hasClass('ui-popup')) {
330 // if buttons template wasn't add,
331 // use default template
335 $('#popup').popup('close');
341 $('.ui-popup-button-bg', popup).empty();
343 popup[0].className = popup[0]
345 .replace(/\bcenter_basic.*?\b/g, '');
346 popup.addClass('center_basic_' + popupNumber + 'btn');
348 // adds buttons to popup HTML element
350 if (buttons.hasOwnProperty(i)) {
352 $('<a/>').text(i).attr({
353 'data-role': 'button',
354 'data-inline': 'true'
355 }).bind('click', buttons[i]).appendTo(
356 $('.ui-popup-button-bg', popup)
361 // adds text to popup HTML element
362 $('.ui-popup-text p', popup).text(text);
364 popup.trigger('create');
366 popup.popup('open', {