2 /*global $, GraphSchedule, confirm, 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.fillTimesRanges = function (timesData) {
51 var self = this, len, i;
53 $('#availableTime').replaceWith(
54 $('<ul data-role="listview" id="availableTime"></ul>')
56 for (i = 0, len = timesData.length; i < len; i += 1) {
58 .append($(this.getAvailableTimeTemplate(timesData[i])));
60 $('#availableTime').trigger('create');
61 $('#availableTime').listview().listview('refresh');
62 $('#availableTime :jqmData(name=edit)').on('tap', function (e) {
65 self.editTimeRange($(this).data('val'));
67 $('#availableTime :jqmData(name=disable)').on('tap', function (e) {
69 self.app.disableTimeRange($(this).data('val'));
71 $('#availableTime :jqmData(name=delete)').on('tap', function (e) {
73 if (confirm('Are you sure?')) {
74 self.app.deleteTimeRange($(this).data('val'));
79 UI.prototype.fillTimeRangeForm = function fillTimeRangeForm(timeRange) {
80 var tmpData = new Date();
82 $('#startTime').attr('data-val',
83 new Date(tmpData.setHours(timeRange.start)));
84 $('#duration').attr('data-val', timeRange.duration);
86 if ($('#startTime').data('datetimepicker')) {
87 $('#startTime').data('datetimepicker').options.date
88 .setHours(timeRange.start);
89 $('#startTime').data('datetimepicker').ui
90 .find('.ui-datefield-hour').html(
91 (timeRange.start < 10) ? '0' + timeRange.start : timeRange.start
94 if ($('#duration').data('datetimepicker')) {
95 $('#duration').data('datetimepicker').options.date
96 .setHours(timeRange.duration);
97 $('#duration').data('datetimepicker').ui.find('.ui-datefield-hour')
99 (timeRange.duration < 10) ? '0'
100 + timeRange.duration : timeRange.duration
102 $('#duration').data('datetimepicker')._populateDataSelector = function (field, pat) {
103 var result = $.tizen.datetimepicker.prototype._populateDataSelector
104 .call(this, field, pat);
105 result.values = range(1, 20);
106 result.data = range(1, 20);
112 $('#formEnablePeriod')[0].value = timeRange.enabled ? 'on' : 'off';
113 $('#formEnablePeriod').slider('refresh');
116 UI.prototype.editTimeRange = function (nr, event) {
117 if (event && typeof event.stopPropagation === 'function') {
118 event.preventDefault();
119 event.stopPropagation();
122 if (this.app.editTimeRange(nr) >= 0) {
123 $('#updateTime').val('modify');
125 $('#updateTime').val('add');
128 // change page to form;
129 $.mobile.changePage("#rangesOfTimes");
132 UI.prototype.getExercisesList = function () {
133 return $('#exercises :jqmData(role=slider)').map(function (o, v) {
135 index : $(v).attr('data-index'),
136 checked : ($(v).val() === 'on') ? true : false
141 UI.prototype.addExercise = function () {
142 if ($("#newExerciseName").val()) {
143 if (this.app.addExercise($("#newExerciseName").val())) {
144 $("#newExerciseName").trigger('blur');
145 setTimeout(history.back.bind(history), 50);
149 name : 'Name of exercise is not correct.',
155 UI.prototype.configToUI = function () {
158 UI.prototype.updateMainUI = function () {
159 this.setStatusRun(this.app.config.trainingEnabled);
160 this.graphSchedule.setTimeRanges(this.app.periodsWeekToBoolArray());
163 UI.prototype.getTimeRangeFromForm = function () {
165 start : $('#startTime').data('datetimepicker').options.date
167 duration : $('#duration').data('datetimepicker').options.date
169 stop : $('#startTime').data('datetimepicker').options.date
171 + $('#duration').data('datetimepicker').options.date
173 style : $('.selectPeriodType :radio:checked').val(),
174 enabled : ($('#formEnablePeriod')[0].value === 'on' ? true : false)
178 UI.prototype.editTimeRangeAction = function (nr) {
179 if (this.app.saveTimeRange(nr, this.getTimeRangeFromForm())) {
183 message : 'Time start and stop is not present.',
189 UI.prototype.showNoticeInMonitor = function (notice, alarm) {
190 $('#communicate').html(notice);
191 $('#communicate').toggleClass('onAlert', alarm);
194 UI.prototype.changeButtonAddTime = function (text) {
195 $('#addTime').html(text);
198 UI.prototype.showErrors = function (errors) {
200 for (i = 0; i < errors.length; i += 1) {
201 alert(errors[i].name);
205 UI.prototype.showAlarmInMonitor = function (data) {
207 function formatNN(val) {
208 return (val < 10) ? ('0' + val) : val;
211 if (data && data.alarm && this.app.config.trainingEnabled) {
212 this.app.currentAlarm = this.app.findCurrentAlarm();
213 if (this.app.currentAlarm.length > 0) {
214 notice += 'Go... go... go...!<br>' + data.exerciseName + ' x '
215 + data.numberOfTimes;
217 notice += 'Next exercises set at: '
218 + formatNN(data.alarm.getHours()) + ':'
219 + formatNN(data.alarm.getMinutes()) + '<br>'
220 + data.exerciseName + ' x ' + data.numberOfTimes;
223 notice += '<br/>You have no workouts scheduled for now.<br/>';
225 this.showNoticeInMonitor(notice, false);
228 UI.prototype.getSentence = function UI_getSentence(type) {
229 return (this.sentence[type] || {
230 text : 'No sentence',
231 signature : 'anonymous'
235 UI.prototype.setSentence = function (type) {
236 var sentence = this.getSentence(type);
237 $('#sentence').html('"' + sentence.text + '"');
238 $('#signature').html('- ' + sentence.signature);
241 UI.prototype.showWaitOk = function () {
242 $('#mainControl').hide();
243 $('#one .ui-btn-back').hide();
245 $('#onAlertControl').tabbar();
246 $('#onAlertControl').show();
247 $('#onAlertControl').css('width', '');
250 UI.prototype.setStatusRun = function (bool) {
253 $('#status').removeClass('lazy').addClass('run');
255 this.setSentence('run');
256 // button in control bar;
257 $('#startStop .ui-btn-text').html('stop training');
259 $('#status').removeClass('run').addClass('lazy');
260 this.setSentence('lazy');
261 $('#communicate').html('');
262 $('#startStop .ui-btn-text').html('start training');
266 UI.prototype.bindEvents = function bindEvents() {
270 $('#one .ui-btn-back').on('tap', this.app.exit.bind(this.app));
272 $('#ok').on('tap', self.app.ok.bind(self.app));
273 $('#wait').on('tap', self.app.wait.bind(self.app));
274 $('#todayOffAll').on('tap', self.app.todayOffAll.bind(self.app));
276 $('#startStop').on('tap', function () {
277 self.app.appStartStop();
282 function (page, options) {
283 if (self.graphSchedule.ui) {
284 $('#one .schedule').append(self.graphSchedule.ui);
285 self.app.updateGraph();
286 self.graphSchedule.refresh();
287 self.graphSchedule.setVisibleWeekend(!self.app
289 self.graphSchedule.setVisibleWorkdays(self.app
292 $('#one .schedule').on('touchstart', function (ev) {
293 ev.stopPropagation();
296 // workaround for scroll lock;
297 $.mobile.activePage.css('position', 'fixed');
301 $('#two').on('pageshow', function (page, options) {
303 //FIXME (two scrollbar workaround)
306 var newHeight = $('#two').find('[data-role="content"]').prop('style').height;
307 $('#two').css({'min-height': newHeight, 'height': newHeight});
312 if (self.graphSchedule.ui) {
313 $('#two .scheduleOptions').append(self.graphSchedule.ui);
314 self.graphSchedule.refresh();
315 self.graphSchedule.setVisibleWeekend(true);
316 self.graphSchedule.setVisibleWorkdays(true);
320 $('#two').on('pageinit', function (page, options) {
321 $('.ui-radio input', $('#frequency')).change(function (ev) {
322 self.app.setFrequency(this.value);
326 $('.ui-radio input', $('#strength')).change(function (ev) {
327 self.app.setStrength(this.value);
331 $('#frequency')[0].select(self.app.config.frequency);
332 $('#strength')[0].select(self.app.config.strength);
334 $('#two .scheduleOptions').append(self.graphSchedule.ui);
335 $('#two .scheduleOptions').on('touchstart', function (ev) {
336 ev.stopPropagation();
339 $('#workdaysType').on('tap', function (ev) {
340 self.app.changeTypeOfPeriods('workday');
343 $('#weekendType').on('tap', function (ev) {
344 self.app.changeTypeOfPeriods('weekend');
347 $('#addTimeRange').on('tap', self.editTimeRange.bind(self, -1));
349 self.app.updateTimesRanges();
353 $('#selectExercises').on('pageinit', function (page, options) {
354 self.app.updateExercises();
358 $('#customExercises').on('pageinit', function (page, options) {
359 $('#btnNewExercise').on('click', self.addExercise.bind(self));
362 $('#customExercises').on('pageshow', function (page, options) {
363 $('#newExerciseName').val('');
364 $('#newExerciseName').trigger('focus');
367 $('#rangesOfTimes').on('pageinit', function (page, options) {
368 $("#updateTime").on("tap", function (e) {
371 self.editTimeRangeAction(self.app.currentEditingTimePeriodId);
375 $('#rangesOfTimes').on('pageshow', function (page, options) {
376 $('#updateTime').data('button').refresh();
377 self.fillTimeRangeForm(self.app.currentEditingTimePeriod);
380 $('#increasingStrength').on('change', function () {
381 self.app.config.increasingStrength = this.checked;
382 self.app.saveConfig();
388 UI.prototype.onGraphSchedule = function onGraphSchedule(onInitEnd) {
391 $('#one .schedule').append(this.graphSchedule.ui);
392 this.app.updateGraph();
393 this.graphSchedule.refresh();
394 this.graphSchedule.setVisibleWeekend(!this.app.todayIsWorkday());
395 this.graphSchedule.setVisibleWorkdays(this.app.todayIsWorkday());
397 if (typeof onInitEnd === 'function') {
402 UI.prototype.initialize = function (onInitEnd) {
403 $.mobile.tizen.disableSelection(document);
407 $('html').css('font-size', '');
408 $('body').css('font-size', '');
410 this.graphSchedule = new GraphSchedule({
411 onSuccess : this.onGraphSchedule.bind(this, onInitEnd)