2 /*global $, GraphSchedule, range, history, setTimeout */
12 text : 'He does not seem to me to be a free man who does not sometimes do nothing.',
13 signature : 'Marcus T. Cicero'
16 text : 'A journey of a thousand miles begins with a single step.',
24 UI.prototype.fillExercises = function (exercisesData) {
25 var i, len, self = this;
27 $('#exercises').replaceWith(
28 $('<ul data-role="listview" id="exercises"></ul>')
30 for (i = 0, len = exercisesData.length; i < len; i += 1) {
31 $('#exercises').append(
32 $(this.getExercisesTemplate(exercisesData[i], i))
35 $('#exercises').listview();
36 $('#exercises :jqmData(role="slider")').slider();
37 $('#exercises li').on('tap', function () {
38 var $toggle = $(this).find(':jqmData(role="slider")');
39 $toggle.val(($toggle.val() === 'off') ? 'on' : 'off');
40 $toggle.slider('refresh');
41 self.app.saveExercises(self.getExercisesList());
43 $('#exercises :jqmData(role="slider")').on('change', function (e) {
46 self.app.saveExercises(self.getExercisesList());
50 UI.prototype.popup = function (text, callback) {
51 var i, popup = $("#popup"),
52 buttons = {'Cancel': function () { $("#popup").popup('close') }};
53 // to hide default button give {'Close': false} in callback
56 if (callback.hasOwnProperty(i)) {
57 buttons[i] = callback[i];
61 $(".ui-popup-button-bg", popup).empty();
63 if (buttons.hasOwnProperty(i)) {
67 .attr({'data-role': 'button', 'data-inline': 'true'})
68 .bind('tap', buttons[i])
69 .appendTo($(".ui-popup-button-bg", popup));
73 $(".text", popup).text(text);
75 popup.trigger("create");
76 popup.popup('open', {positionTo: 'window'});
79 UI.prototype.clearTabbars = function () {
80 $('[data-role = "tabbar"] li > a').removeClass('ui-focus, ui-btn-active');
83 UI.prototype.fillTimesRanges = function (timesData) {
84 var self = this, len, i;
86 $('#availableTime').replaceWith(
87 $('<ul data-role="listview" id="availableTime"></ul>')
89 for (i = 0, len = timesData.length; i < len; i += 1) {
91 .append($(this.getAvailableTimeTemplate(timesData[i])));
93 $('#availableTime').trigger('create');
94 $('#availableTime').listview().listview('refresh');
95 $('#availableTime :jqmData(name=edit)').on('tap', function (e) {
96 if (self.app.config.trainingEnabled) {
97 self.popup('You should stop the training first');
101 self.editTimeRange($(this).data('val'));
104 $('#availableTime :jqmData(name=disable)').on('tap', function (e) {
105 if (self.app.config.trainingEnabled) {
106 self.popup('You should stop the training first');
109 self.app.disableTimeRange($(this).data('val'));
112 $('#availableTime :jqmData(name=delete)').on('tap', function (e) {
113 var value = $(this).data('val');
115 if (self.app.config.trainingEnabled) {
116 self.popup('You should stop the training first');
119 self.popup('Are you sure?', {
120 'Delete': function () {
121 self.app.deleteTimeRange(value);
122 $("#popup").popup('close');
128 UI.prototype.fillTimeRangeForm = function fillTimeRangeForm(timeRange) {
129 var tmpData = new Date();
131 $('#startTime').attr('data-val',
132 new Date(tmpData.setHours(timeRange.start)));
133 $('#duration').attr('data-val', timeRange.duration);
135 if ($('#startTime').data('datetimepicker')) {
136 $('#startTime').data('datetimepicker').options.date
137 .setHours(timeRange.start);
138 $('#startTime').data('datetimepicker').ui
139 .find('.ui-datefield-hour').html(
140 (timeRange.start < 10) ? '0' + timeRange.start : timeRange.start
143 if ($('#duration').data('datetimepicker')) {
144 $('#duration').data('datetimepicker').options.date
145 .setHours(timeRange.duration);
146 $('#duration').data('datetimepicker').ui.find('.ui-datefield-hour')
148 (timeRange.duration < 10) ? '0'
149 + timeRange.duration : timeRange.duration
151 $('#duration').data('datetimepicker')._populateDataSelector = function (field, pat) {
152 var result = $.tizen.datetimepicker.prototype._populateDataSelector
153 .call(this, field, pat);
154 result.values = range(1, 20);
155 result.data = range(1, 20);
160 $.each($("input[name='periodType']"), function () {
161 $(this).attr('checked', $(this).val() === timeRange.style)
162 .checkboxradio('refresh');
164 $('#formEnablePeriod')[0].value = timeRange.enabled ? 'on' : 'off';
165 $('#formEnablePeriod').slider('refresh');
168 UI.prototype.editTimeRange = function (nr, event) {
169 if (event && typeof event.stopPropagation === 'function') {
170 event.preventDefault();
171 event.stopPropagation();
174 if (this.app.editTimeRange(nr) >= 0) {
175 $('#updateTime').val('modify');
176 $('#rangesOfTimes h1').text('Edit Time');
178 $('#updateTime').val('add');
179 $('#rangesOfTimes h1').text('Add Time');
182 // change page to form;
183 $.mobile.changePage("#rangesOfTimes");
186 UI.prototype.getExercisesList = function () {
187 return $('#exercises :jqmData(role=slider)').map(function (o, v) {
189 index : $(v).attr('data-index'),
190 checked : ($(v).val() === 'on') ? true : false
195 UI.prototype.addExercise = function () {
196 var trimmedName = $("#newExerciseName").val().trim();
197 trimmedName = $('<span>').text(trimmedName).html();
198 if (trimmedName !== '') {
199 if (this.app.addExercise(trimmedName)) {
200 $("#newExerciseName").trigger('blur');
201 setTimeout(history.back.bind(history), 50);
205 name : 'Name of exercise is not correct.',
211 UI.prototype.configToUI = function () {
214 UI.prototype.updateMainUI = function () {
215 this.setStatusRun(this.app.config.trainingEnabled);
216 this.graphSchedule.setTimeRanges(this.app.periodsWeekToBoolArray());
219 UI.prototype.getTimeRangeFromForm = function () {
221 start : $('#startTime').data('datetimepicker').options.date
223 duration : $('#duration').data('datetimepicker').options.date
225 stop : $('#startTime').data('datetimepicker').options.date
227 + $('#duration').data('datetimepicker').options.date
229 style : $('.selectPeriodType :radio:checked').val(),
230 enabled : ($('#formEnablePeriod')[0].value === 'on' ? true : false)
234 UI.prototype.editTimeRangeAction = function (nr) {
235 if (this.app.saveTimeRange(nr, this.getTimeRangeFromForm())) {
239 message : 'Time start and stop is not present.',
245 UI.prototype.showNoticeInMonitor = function (notice, alarm) {
246 $('#communicate').html(notice);
247 $('#communicate').toggleClass('onAlert', alarm);
250 UI.prototype.changeButtonAddTime = function (text) {
251 $('#addTime').html(text);
254 UI.prototype.showErrors = function (errors) {
256 for (i = 0; i < errors.length; i += 1) {
257 this.popup(errors[i].name);
259 this.unblockButtons();
262 UI.prototype.unblockButtons = function () {
263 $('#btnNewExercise').data('disabled', false);
266 UI.prototype.showAlarmInMonitor = function (data) {
268 function formatNN(val) {
269 return (val < 10) ? ('0' + val) : val;
272 if (data && data.alarm && this.app.config.trainingEnabled) {
273 this.app.currentAlarm = this.app.findCurrentAlarm();
274 if (this.app.currentAlarm.length > 0) {
275 notice += 'Go... go... go...!<br>' + data.exerciseName + ' x '
276 + data.numberOfTimes;
278 notice += 'Next exercises set at: '
279 + formatNN(data.alarm.getHours()) + ':'
280 + formatNN(data.alarm.getMinutes()) + '<br>'
281 + data.exerciseName + ' x ' + data.numberOfTimes;
284 notice += '<br/>You have no workouts scheduled for now.<br/>';
286 this.showNoticeInMonitor(notice, false);
289 UI.prototype.getSentence = function UI_getSentence(type) {
290 return (this.sentence[type] || {
291 text : 'No sentence',
292 signature : 'anonymous'
296 UI.prototype.setSentence = function (type) {
297 var sentence = this.getSentence(type);
298 $('#sentence').html('"' + sentence.text + '"');
299 $('#signature').html('- ' + sentence.signature);
302 UI.prototype.showWaitOk = function () {
303 $('#mainControl').hide();
304 $('#one .ui-btn-back').hide();
306 $('#onAlertControl').tabbar();
307 $('#onAlertControl').show();
308 $('#onAlertControl').css('width', '');
311 UI.prototype.setStatusRun = function (bool) {
314 $('#status').removeClass('lazy').addClass('run');
316 this.setSentence('run');
317 // button in control bar;
318 $('#startStop .ui-btn-text').html('Stop Training');
320 $('#status').removeClass('run').addClass('lazy');
321 this.setSentence('lazy');
322 $('#communicate').html('');
323 $('#startStop .ui-btn-text').html('Start Training');
327 UI.prototype.bindEvents = function bindEvents() {
333 $('#one .ui-btn-back').on('tap', this.app.exit.bind(this.app));
335 $('#ok').on('tap', self.app.ok.bind(self.app));
336 $('#wait').on('tap', self.app.wait.bind(self.app));
337 $('#todayOffAll').on('tap', self.app.todayOffAll.bind(self.app));
339 $('[data-role = "tabbar"] li > a').on('click', function () {
343 $('#startStop').on('click', function () {
344 self.app.appStartStop();
347 document.addEventListener('tizenhwkey', function(e) {
348 if (e.keyName == 'back') {
349 if ($.mobile.popup.active) {
350 $.mobile.popup.active.close();
351 } else if ($.mobile.activePage.attr('id') === 'one') {
352 tizen.application.getCurrentApplication().exit();
361 function (page, options) {
362 if (self.graphSchedule.ui) {
363 $('#one .schedule').append(self.graphSchedule.ui);
364 self.app.updateGraph();
365 self.graphSchedule.refresh();
366 self.graphSchedule.setVisibleWeekend(!self.app
368 self.graphSchedule.setVisibleWorkdays(self.app
371 $('#one .schedule').on('touchstart', function (ev) {
372 ev.stopPropagation();
375 // workaround for scroll lock;
376 $.mobile.activePage.css('position', 'fixed');
378 if ($('a.ui-btn-back').is(':hidden')) {
379 $('div#mainControl').removeClass('ui-tabbar-margin-back');
384 $('#two').on('pageshow', function (page, options) {
385 self.fixPageHeight('#two');
387 if (self.graphSchedule.ui) {
388 $('#two .scheduleOptions').append(self.graphSchedule.ui);
389 self.graphSchedule.refresh();
390 self.graphSchedule.setVisibleWeekend(true);
391 self.graphSchedule.setVisibleWorkdays(true);
394 if ($('a.ui-btn-back').is(':hidden')) {
395 $('div#selectExercisesBar').removeClass('ui-tabbar-margin-back');
399 $('#two').on('pageinit', function (page, options) {
400 $('.ui-radio input', $('#frequency')).change(function (ev) {
402 self.startProgress();
403 setTimeout(function (){
404 self.app.setFrequency(that.value);
410 $('.ui-radio input', $('#strength')).change(function (ev) {
411 self.app.setStrength(this.value);
415 $('#frequency')[0].select(self.app.config.frequency);
416 $('#strength')[0].select(self.app.config.strength);
418 $('#two .scheduleOptions').append(self.graphSchedule.ui);
419 $('#two .scheduleOptions').on('touchstart', function (ev) {
420 ev.stopPropagation();
423 $('#workdaysType').on('tap', function (ev) {
424 var that = $(this).children("a");
425 setTimeout(function(){
426 that.removeClass("ui-btn-active");
429 self.app.changeTypeOfPeriods('workday');
432 $('#weekendType').on('tap', function (ev) {
433 var that = $(this).children("a");
434 setTimeout(function(){
435 that.removeClass("ui-btn-active");
438 self.app.changeTypeOfPeriods('weekend');
441 $('#addTimeRange').on('tap', function (e) {
442 if (self.app.config.trainingEnabled) {
443 self.popup('You should stop the training first');
445 self.editTimeRange(-1, e);
449 self.app.updateTimesRanges();
453 $('#selectExercises').on('pageinit', function (page, options) {
454 self.app.updateExercises();
458 $('#selectExercises').on('pageshow', function (page, options) {
459 self.fixPageHeight('#selectExercises');
462 $('#customExercises').on('pageinit', function (page, options) {
463 $('#btnNewExercise').on('click', function (e) {
466 if ($(this).data('disabled') !== true) {
467 $(this).data('disabled', true);
473 $('#customExercises').on('pageshow', function (page, options) {
474 self.unblockButtons();
476 $('#newExerciseName').val('');
479 $('#rangesOfTimes').on('pageinit', function (page, options) {
480 $("#updateTime").on("click", function (e) {
483 self.startProgress('Saving');
484 self.editTimeRangeAction(self.app.currentEditingTimePeriodId);
488 $('#rangesOfTimes').on('pageshow', function (page, options) {
489 self.fixPageHeight('#rangesOfTimes');
491 $('#updateTime').data('button').refresh();
492 self.fillTimeRangeForm(self.app.currentEditingTimePeriod);
495 $('#increasingStrength').on('change', function () {
496 self.app.config.increasingStrength = this.checked;
497 self.app.saveConfig();
502 $('#add_custom_workout_btn').on('tap', function (e) {
505 $.mobile.changePage('#customExercises');
509 UI.prototype.onGraphSchedule = function onGraphSchedule(onInitEnd) {
512 $('#one .schedule').append(this.graphSchedule.ui);
513 this.app.updateGraph();
514 this.graphSchedule.refresh();
515 this.graphSchedule.setVisibleWeekend(!this.app.todayIsWorkday());
516 this.graphSchedule.setVisibleWorkdays(this.app.todayIsWorkday());
518 if (typeof onInitEnd === 'function') {
523 UI.prototype.initialize = function (onInitEnd) {
524 $.mobile.tizen.disableSelection(document);
528 $('html, body').css('font-size', '');
529 $('[data-role=footer]').height('');
531 this.graphSchedule = new GraphSchedule({
532 onSuccess : this.onGraphSchedule.bind(this, onInitEnd)
535 this.fixContentHeight();
538 UI.prototype.fixPageHeight = function (element) {
539 //FIXME (two scrollbar workaround)
542 var newHeight = $(element).find('[data-role="content"]').prop('style').height;
543 $(element).css({'min-height': newHeight, 'height': newHeight});
549 UI.prototype.fixContentHeight = function () {
550 var contentHeight = screen.availHeight - $('div[data-role="header"]').outerHeight() - $('div[data-role="footer"]').outerHeight();
551 $('div[data-role="content"]').css('height', contentHeight);
554 UI.prototype.startProgress = function (title) {
555 title = title || "Updating schedule";
556 $("#progress").show();
557 $("#progresstitle").text(title);
558 $("#progressbar").progressbar({value: false});
561 UI.prototype.endProgress = function () {
562 $("#progress").fadeOut();