/*jslint devel: true*/ /*global $, app, tizen, TemplateManager */ /** * @class Ui */ function Ui() { 'use strict'; } (function () { // strict mode wrapper 'use strict'; Ui.prototype = { templateManager: null, /** * UI module initialisation */ init: function UI_init(app) { this.app = app; this.templateManager = new TemplateManager(); $(document).ready(this.domInit.bind(this)); // init inner objects this.home.context = this; this.alarm.context = this; this.new_event.context = this; }, /** * When DOM is ready, initialise it */ domInit: function UI_domInit() { this.templateManager.loadToCache(['home', 'alarm', 'new_event', 'event', 'all_day_event'], this.initPages.bind(this)); // Disable text selection $.mobile.tizen.disableSelection(document); }, /** * Append pages to body and initialise them */ initPages: function UI_initPages() { var pages = []; pages.push(this.templateManager.get('home')); pages.push(this.templateManager.get('alarm')); pages.push(this.templateManager.get('new_event')); $('body').append(pages.join('')); this.fixContentHeight(); this.home.init(); this.alarm.init(); this.new_event.init(); document.addEventListener('webkitvisibilitychange', function (event) { $('input').blur(); $(".ui-popup").popup('close'); if ($.mobile.activePage.attr('id') !== "new_event") { if (document.webkitVisibilityState === 'visible') { app.loadEvents(); app.ui.checkTimeFormat(); if (app.eventId !== 0) { app.model.isEventExists(app.eventId, null, function () { $.mobile.changePage('#home'); }); } } } }); $(window).on('softkeyboardchange', function (e) { if (e.originalEvent.state === "off") { $("input:radio").checkboxradio("refresh"); } }); document.addEventListener('tizenhwkey', function(e) { if (e.keyName == "back") { if ($.mobile.activePage.attr('id') === 'home') { tizen.application.getCurrentApplication().exit(); } else { history.back(); } } }); $.mobile.changePage('#home', 'pop', false, true); }, /** * Contains method related to time format 12/24h * @namespace */ checkTimeFormat: function UI_checkTimeFormat () { var date = tizen.time.getDateFormat(true), time = tizen.time.getTimeFormat(); if (time === "h:m:s") { //(h:m:s) 24hours format and (ap h:m:s) 12hours format $("#demo-date-1, #demo-date-2").datetimepicker("option", "format", "MMM dd yyyy HH:mm"); } else { $("#demo-date-1, #demo-date-2").datetimepicker("option", "format", "MMM dd yyyy hh:mm tt"); } }, /** * Contains methods related to the #home page * @namespace */ home: { init: function UI_home_init() { var app = this.context.app, self = this, alarm = this.context.alarm; $('#exit_btn').on('tap', app.exit.bind(app)); $("input:radio").checkboxradio(); alarm.selectOption(); // buttons in the events list $('#events_list').on('tap', '.remove_event_btn', function () { var eventId = $(this).parents('.event').data('eventid'); app.model.deleteEvent(eventId); }); $('#events_list').on('click', '.edit_event_btn', function () { var eventId = $(this).parents('.event').data('eventid'), event = app.model.editEvent(eventId), field, date, duration, key, properties = ['summary', 'startDate', 'endDate']; app.ui.new_event.dateResetLock(true); app.eventId = eventId; properties.forEach(function(element){ if (event.hasOwnProperty(element)) { field = $('#new_event input[name="' + element + '"]'); if (field.length !== 0) { if (field.attr('type') === 'datetime') { date = self.TZD2Date(event[element]); field.datetimepicker('value', date); field.datetimepicker(); app.ui.new_event.setSelectAllDay(event.isAllDay); } else { field.val(event[element]); } } } }); $('#new_event h1').text('Edit Event'); if (event.alarms.length !== 0) { duration = app.retrieveTimeDurationInMinutes(event.alarms[0].before); } if(typeof duration == "undefined") { duration = -1; } alarm.selectOption(alarm.getValue(duration || 0)); $.mobile.changePage("#new_event"); // set select allDay property app.ui.new_event.setSelectAllDay(event.isAllDay); }); $('#newEventBtn').on('tap', function () { app.ui.new_event.dateResetLock(false); app.eventId = 0; $("#demo-date-1, #demo-date-2").datetimepicker(); $("#demo-date-1, #demo-date-2").datetimepicker(new Date()); // workaround - if just inietied once agan, datepickers remembers the date $('#new_event h1').text('New Event'); $('#title').val(''); app.ui.new_event.setSelectAllDay(false); alarm.selectOption(); }); this.loadEvents(); }, TZD2Date: function (tzdate) { return new Date( tzdate.getFullYear(), tzdate.getMonth(), tzdate.getDate(), tzdate.getHours(), tzdate.getMinutes(), tzdate.getSeconds(), tzdate.getMilliseconds() ); }, /** * Get start date value from the form (#demo-date-1 field) * * @returns {string} */ getStartDate: function UI_home_getStartDate() { var startDate = $('#demo-date-1').attr('data-date'); return startDate; }, /** * Get info if event is allDay event * * @returns {boolean} */ getAllDayInfo: function UI_home_getAllDayInfo() { var isAllDay = $('select#allDay').val() == '1' ? true : false; return isAllDay; }, /** * Get end date value from the form (#demo-date-2 field) * * @returns {string} */ getEndDate: function UI_home_getEndDate() { var endDate = $('#demo-date-2').attr('data-date'); return endDate; }, /** * Get the title from the form (#title field) * * @returns {string} */ getTitle: function UI_home_getTitle() { return $('#title').val(); }, /** * Get the description from the form (#des field) * * @returns {string} */ getDescription: function UI_home_getDescription() { return $('#des').val(); }, /** * Get the location from the form (#location field) * * @returns {string} */ getLocation: function UI_home_getLocation() { return $('#location').val(); }, /** * Wrapper for app.loadEvents * @param {Object} e event * @param {Date} date selected date */ loadEvents: function UI_home_loadEvents(e, date) { this.context.app.loadEvents(date); }, /** * Returns text for separating list items with events * Skips repeated values * * @param {Object} event * @returns {string} */ getSeparatorText: function UI_home_getSeparatorText(event) { var previous = ''; // redefine itself this.getSeparatorText = function (event) { if (event === undefined) { previous = ''; return undefined; } var startDate = event.startDate, str = this.formatDate(startDate); if (previous === str) { return ''; // skip it - already returned } previous = str; // store in the closure for future comparison return str; }; return this.getSeparatorText(event); }, formatDate: function UI_home_formatDate(date) { var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; this.formatDate = function UI_home_formatDate(date) { return date.getDate() + ". " + monthNames[date.getMonth()] + " " + date.getFullYear(); }; return this.formatDate(date); }, /** * Format hour * @param {TZDate} date * @returns {string} */ formatHour: function UI_home_formatHour(date) { return date.getHours() + ':' + this.pad(date.getMinutes()); }, /** * Zero-pads a positive number to 2 digits */ pad: function UI_home_pad(number) { return number < 10 ? '0' + number : number; }, /** * Creates HTML representing the given array of alarms * * @param {Alarm[]} alarms * @returns {string} */ getAlarmsHtml: function UI_home_getAlarmsHtml(alarms) { var alarm = '', j, len; len = alarms.length; if (len) { alarm += '
'; for (j = 0; j < len; j += 1) { alarm += alarms[j].before.length; alarm += ' ' + alarms[j].before.unit; } alarm += '
'; } return alarm; }, /** * Load the events into the #event_popup. * * Callback function for app.loadEvents. * @param {Array} events */ onEventSearchSuccess: function UI_home_onEventSearchSuccess(events) { var i = 0, j = 0, str = "", event, alarm = '', dividerText = '', templateParameters = {}, tmplName; // content str = ''; for (i = 0; i < events.length; i += 1) { event = events[i]; dividerText = this.getSeparatorText(event); if (dividerText) { str += '