2 /*global tizen, document, $, app, UiPanel, UiContact, TemplateManager, window, Helpers */
8 function Ui(contacts) {
13 (function () { // strict mode wrapper
23 * @type {TemplateManager}
25 templateManager: null,
30 init: function Ui_init() {
31 this.templateManager = new TemplateManager();
32 this.helpers = new Helpers();
33 $(document).ready(this.domInit.bind(this));
37 * When DOM is ready, initialise it (bind events)
39 domInit: function Ui_domInit() {
40 this.templateManager.loadToCache(['callView', 'callerHistory', 'callItemRow', 'callerCallItemRow', 'messageWindow', 'dateRow'], this.initPages.bind(this));
44 * UI pages initializer
46 initPages: function Ui_initPages() {
49 $('#callView').append($(this.templateManager.get('callView')).children()).trigger('pagecreate');
51 pages.push(this.templateManager.get('callerHistory'));
52 $('body').append(pages.join(''));
53 this.removeSearchBarToHeader();
56 app.showCallHistory();
62 addEvents: function Ui_addEvents() {
65 $('#callView .ui-btn-back').on('tap', app.exit.bind(app));
67 $('#callView').on('pagebeforeshow', function () {
68 app.showCallHistory();
71 $('#historyForCallerView').on('pagebeforeshow', function () {
72 self.hideCheckboxes();
73 $('#historyForCallerView .ui-content')
74 .scrollview('scrollTo', 0, 0);
75 $('#historyForCallerView .ui-content .ui-scrollview-view')
76 .css('-webkit-transform', 'translate3d(0px, 0px, 0px)');
77 $('#selectAllDetails').on('change', function () {
78 self.selectAllDetails();
82 $('#historyForCallerView').on('pageshow', function () {
83 $('#content').css('top', '160px');
84 $('#header').css('height', '160px');
85 $('#delete-toolbar').css('width', '70px');
88 $(window).on('resize', function () {
89 $('#delete-toolbar').css('width', '70px');
92 $('.selectAllBox').on('tap', function () {
94 self.selectAllDetailsEach();
97 $('#calllogList').on('tap', '.date', function (event) {
98 event.stopPropagation();
101 $('#calllogList').on('click', '.call', function onCalllogEntryClick(event) {
102 if ($(this).data('entries')[0].remoteParties[0].remoteParty !== "") {
103 app.showHistoryForCaller($(this).data('entries')[0].remoteParties[0].remoteParty);
104 $.mobile.changePage('#historyForCallerView');
108 $('#smsActionBtn').on('tap', function () {
109 self.hideCheckboxes();
110 app.sendSms($('#forCallerList').data('remoteParty'));
113 $('#callActionBtn').on('tap', function () {
114 self.hideCheckboxes();
115 app.makeCall($('#forCallerList').data('remoteParty'));
118 $('#deleteActionBtn').on('tap', function () {
119 self.changeDetailsToRemoveState();
122 $('#forCallerList').on('tap', 'li', function () {
123 var checkbox = $(this).find(':checkbox');
124 if (self.removeMode === true) {
125 if (checkbox.attr('checked')) {
126 checkbox.attr('checked', false)
127 .data('checkboxradio')
130 checkbox.attr('checked', true)
131 .data('checkboxradio')
134 self.setSelectAllDetails();
139 addEventsForCallerListCheckboxes: function Ui_addEventsForCallerListCheckboxes() {
141 $('#forCallerList :checkbox').on('change', function (event) {
142 if ($(this).attr('checked')) {
143 $(this).attr('checked', true);
145 $(this).attr('checked', false);
147 self.setSelectAllDetails();
151 selectAll: function Ui_selectAll() {
152 var selectAll = $('#selectAllDetails');
153 if (selectAll.attr('checked')) {
155 .attr('checked', false)
156 .data('checkboxradio')
160 .attr('checked', true)
161 .data('checkboxradio')
166 selectAllDetails: function Ui_selectAllDetails() {
167 if ($('#selectAllDetails').attr('checked')) {
168 $('#selectAllDetails').attr('checked', true);
170 $('#selectAllDetails').attr('checked', false);
172 this.selectAllDetailsEach();
176 * Returns number of selected call logs
177 * @return {number} length
179 getCountSelectedLogEntries: function Ui_getCountSelectedLogEntries() {
180 return $('#forCallerList li .toRemove label.ui-checkbox-on').length;
183 selectAllDetailsEach: function Ui_selectAllDetailsEach() {
184 $('#forCallerList').find('input').each(function () {
185 if ($('#selectAllDetails').attr('checked')) {
186 $(this).attr('checked', true)
187 .data('checkboxradio')
190 $(this).attr('checked', false)
191 .data('checkboxradio')
200 hideCheckboxes: function Ui_hideCheckboxes() {
201 $('#selectAllDetails')
202 .attr('checked', false)
203 .data('checkboxradio')
206 $('#forCallerList').find('input').each(function () {
207 $(this).attr('checked', false)
208 .data('checkboxradio')
211 this.changeDetailsToRemoveState('hide');
215 * Returns css classes for specified entry
217 * @param {CallHistoryEntry} entry
220 cssClassesForEntry: function Ui_cssClassesForEntry(entry) {
221 return 'call dir_' + entry.direction.toLowerCase() + ' type_' + entry.type.replace('.', '-').toLowerCase();
224 setSelectAllDetails: function Ui_setSelectAllDetails() {
225 if ($('#forCallerList input[type="checkbox"]').length === $('#forCallerList input[checked="checked"]').length) {
226 $('#selectAllDetails')
227 .attr('checked', true)
228 .data('checkboxradio')
231 $('#selectAllDetails')
232 .attr('checked', false)
233 .data('checkboxradio')
239 * Shows popup with specified message
240 * @param {string} message
242 showPopup: function Ui_showPopup(message) {
243 // before create new popup close close & remove old popup if exists
246 .append(this.templateManager.get('messageWindow'))
248 $('#popupMessage').html(message);
251 .on('tap', '#popupCancelActionBtn', this.closePopup.bind(this))
252 .on('tap', '#popupSubmitActionBtn', this.deleteCheckedLogs.bind(this));
254 $('#popup').popup('open', {'positionTo': 'window'});
260 closePopup: function Ui_closePopup() {
267 * Deletes checked log entries
269 deleteCheckedLogs: function Ui_deleteCheckedLogs(e) {
273 $('#selectAllDetails')
274 .attr('checked', false)
275 .data('checkboxradio')
278 $('#forCallerList li.call').each(function () {
279 if ($(this).find('form label').hasClass('ui-checkbox-on')) {
280 app.deleteLog($(this).data('entries')[0]);
285 if ($('#forCallerList li.call').length > 0) {
286 this.updateCallerHeaderNumberOfEntries($('#forCallerList li.call').length);
289 $('.ui-listview-filter .ui-input-text').val('');
290 $('.ui-listview-filter .ui-input-text').trigger('change');
291 $.mobile.changePage('#callView');
295 changeDetailsToRemoveState: function Ui_changeDetailsToRemoveState(set) {
296 var counter = this.getCountSelectedLogEntries();
298 if (set !== undefined) {
299 this.removeMode = false;
300 } else if (counter === 0) {
301 this.removeMode = !this.removeMode;
305 if (this.removeMode) {
306 $('#historyForCallerView .toRemove').removeClass('hidden');
307 $('#historyForCallerView .selectAllBox').removeClass('hidden');
309 $('#historyForCallerView .toRemove').addClass('hidden');
310 $('#historyForCallerView .selectAllBox').addClass('hidden');
312 } else if (counter > 1) {
313 this.showPopup('Are you sure you want to delete selected logs?');
315 this.showPopup('Are you sure you want to delete selected log?');
317 this.refreshScrollView();
321 * Renders call history list
323 * @param {CallHistoryEntry[]} callEntries
325 showCallHistory: function Ui_showCallHistory(callEntries) {
326 var pdate = null, // previous date
328 elements = [], // list elements
329 len = callEntries.length, // entries length
330 tempLength = 0, // length of temporary table;
333 current, // current entry object
334 today = this.helpers.getShortDate(new Date()), // today short date
341 function filterForSameEntry(element) {
342 return current.remoteParties[0].remoteParty === element.remoteParties[0].remoteParty;
345 $('.selectedCount').hide();
347 for (i = 0; i < len; i = i + 1) {
348 current = callEntries[i];
349 date = current.startTime.toLocaleDateString();
351 // if date is changed create new deyLog;
352 if (date !== pdate) {
356 dayLog.counters = [];
357 groupsOfDays.push(dayLog);
361 // group entries by remote Party;
362 filterResult = dayLog.entries.filter(filterForSameEntry);
363 if (filterResult.length) {
364 index = dayLog.entries.indexOf(filterResult[0]);
365 dayLog.counters[index] += 1;
367 dayLog.entries.push(current);
368 dayLog.counters[dayLog.entries.length - 1] = 1;
372 // Create UL list with dividers;
373 len = groupsOfDays.length;
374 for (i = 0; i < len; i += 1) {
375 dayLog = groupsOfDays[i];
376 tempLength = dayLog.entries.length;
377 entryShortDate = this.helpers.getShortDate(dayLog.entries[0].startTime);
380 elements.push($(this.templateManager.get('dateRow', {
381 'date': today === entryShortDate ? 'Today' : dayLog.date
384 for (j = 0; j < tempLength; j = j + 1) {
385 elements.push(this.getCallItemRow(dayLog.entries[j], dayLog.counters[j]));
393 /* workaround for UIFW & webkit scroll*/
394 $.mobile.activePage.css('min-height', 0);
398 * Returns HTML for single log entry
400 * @param {CallHistoryEntry} entry
401 * @param {number} counter
402 * @returns {HTMLPartial}
404 getCallItemRow: function Ui_getCallItemRow(entry, counter) {
405 var party = entry.remoteParties[0],
406 name = party.displayName || party.remoteParty,
409 if (party.displayName === "") {
411 } else if (party.remoteParty === "") {
416 name += ' (' + counter + ')';
419 tpl = this.templateManager.get('callItemRow', {
421 'callTime': entry.startTime.toLocaleTimeString(),
422 'cssClasses': this.cssClassesForEntry(entry)
426 .data('remoteParty', entry.remoteParties[0].remoteParty)
427 .data('entries', [entry])
428 .get(0); // return clean DOM element so array of those could be appended at once*/
432 * Returns HTML for single caller log entry
434 * @param {CallHistoryEntry} entry
435 * @returns {HTMLElement}
437 getCallerCallLogRow: function Ui_getCallerCallLogRow(entry) {
439 return $(this.templateManager.get('callerCallItemRow', {
440 'cssClass': this.cssClassesForEntry(entry),
441 'callTime': entry.startTime.toLocaleTimeString(),
442 'callDuration': this.helpers.secondsToHours(entry.duration)
443 })).data('entries', [entry]).get(0);
447 * Renders call log list for specified caller
449 * @param {string} remoteParty
450 * @param {CallHistoryEntry[]} entries
452 showHistoryForCaller: function Ui_showHistoryForCaller(remoteParty, entries) {
456 len = entries.length,
460 this.updateCallerHeader(entries[0], entries.length);
464 .data('remoteParty', remoteParty)
465 .data('modified', false)
468 // group caller log entries by date
469 for (i = 0; i < len; i = i + 1) {
470 date = entries[i].startTime.toLocaleDateString();
472 // if date is changed render new header
473 if (date !== pdate) {
474 elements.push($(this.templateManager.get('dateRow', {'date': date})).get(0));
477 elements.push(this.getCallerCallLogRow(entries[i]));
484 if (elements.length > 0) {
485 $('li#delete > a').addClass('ui-btn-active');
487 $('li#delete > a').removeClass('ui-btn-active');
490 $('#forCallerList').trigger('create');
492 this.addEventsForCallerListCheckboxes();
497 * @param {string} accountId
499 updateCallerHeaderAccountId: function Ui_updateCallerHeaderAccountId(accountId) {
500 $('.infoContainer .accountId').html(accountId);
504 * Update number of entries
505 * @param numberOfEntries
507 updateCallerHeaderNumberOfEntries: function Ui_updateCallerHeaderNumberOfEntries(numberOfEntries) {
508 $('.infoContainer .numberOfEntries').html(numberOfEntries);
512 * Updates caller main info
513 * @param {CallHistoryEntry} entry
514 * @param {number} numberOfEntries
516 updateCallerHeader: function Ui_updateCallerHeader(entry, numberOfEntries) {
518 photoURIdefault = '../images/no_photo.png',
522 $('.infoContainer .photo').css('background-image', photoURIdefault);
524 if (entry.remoteParties !== null) {
525 party = entry.remoteParties[0];
526 name = party.displayName || party.remoteParty;
527 if (party.displayName) {
528 this.updateCallerHeaderAccountId(party.remoteParty);
530 this.updateCallerHeaderNumberOfEntries(numberOfEntries);
532 if (party.contactRef) {
533 imgPath = app.getPhotoURIForContact(party.contactRef.addressBookId, party.contactRef.contactId);
534 if (imgPath !== false) {
535 $('.contact > .infoContainer > .photo').css('background-image', imgPath);
538 } else if (entry.contactId !== null) {
539 name = entry.accountId;
540 this.updateCallerHeaderAccountId(entry.accountId);
541 this.updateCallerHeaderNumberOfEntries(numberOfEntries);
543 name = entry.accountId;
544 this.updateCallerHeaderAccountId('');
545 this.updateCallerHeaderNumberOfEntries(numberOfEntries);
547 $('.contact > .infoContainer > .name').html(name);
552 * Remove search filter from content and appends it to header
554 removeSearchBarToHeader: function () {
555 $('#page-content').css('top', '102px');
557 .css('height', '102px')
558 .append($('#callView .ui-listview-filter'));
559 $.mobile.activePage.page('refresh');
560 $('.ui-input-cancel').remove(); // patch for WebUI bug;
565 * Patch for UI, bad refresh scrollView
567 refreshScrollView: function () {
568 var scrollView = $('.ui-scrollview-view'),