Source: panels/call_fdn_list/panel.js

  1. define(function(require) {
  2. 'use strict';
  3. var DsdsSettings = require('dsds_settings');
  4. var DialogService = require('modules/dialog_service');
  5. var FdnContext = require('modules/fdn_context');
  6. var SettingsPanel = require('modules/settings_panel');
  7. return function ctor_settings_panel() {
  8. return SettingsPanel({
  9. _currentContact: null,
  10. onInit: function(panel) {
  11. this._elements = {};
  12. this._elements.fdnContactButton = panel.querySelector('.fdnContact');
  13. this._elements.fdnActionMenu =
  14. panel.querySelector('.call-fdnList-action');
  15. this._elements.fdnActionMenuName =
  16. panel.querySelector('.fdnAction-name');
  17. this._elements.fdnActionMenuNumber =
  18. panel.querySelector('.fdnAction-number');
  19. this._elements.fdnActionMenuCall =
  20. panel.querySelector('.fdnAction-call');
  21. this._elements.fdnActionMenuEdit =
  22. panel.querySelector('.fdnAction-edit');
  23. this._elements.fdnActionMenuRemove =
  24. panel.querySelector('.fdnAction-delete');
  25. this._elements.fdnActionMenuCancel =
  26. panel.querySelector('.fdnAction-cancel');
  27. this._elements.contactsContainer =
  28. panel.querySelector('.fdn-contactsContainer');
  29. this._bindEvents();
  30. },
  31. onBeforeShow: function() {
  32. this._renderAuthorizedNumbers();
  33. },
  34. _bindEvents: function() {
  35. // add FDN contact
  36. this._elements.fdnContactButton.onclick = () => {
  37. DialogService.show('call-fdnList-add', {
  38. mode: 'add'
  39. }).then((result) => {
  40. var type = result.type;
  41. var value = result.value;
  42. if (type === 'submit') {
  43. this._updateContact('add', {
  44. name: value.name,
  45. number: value.number
  46. });
  47. }
  48. });
  49. };
  50. // edit FDN contact
  51. this._elements.fdnActionMenuEdit.onclick = () => {
  52. // hide action menu first
  53. this._hideActionMenu();
  54. // then show dialog
  55. DialogService.show('call-fdnList-add', {
  56. name: this._currentContact.name,
  57. number: this._currentContact.number,
  58. mode: 'edit'
  59. }).then((result) => {
  60. var type = result.type;
  61. var value = result.value;
  62. if (type === 'submit') {
  63. this._updateContact('edit', {
  64. name: value.name,
  65. number: value.number
  66. });
  67. }
  68. });
  69. };
  70. // remove FDN contact
  71. this._elements.fdnActionMenuRemove.onclick = () => {
  72. this._hideActionMenu();
  73. this._updateContact('remove');
  74. };
  75. // call that fdn
  76. this._elements.fdnActionMenuCall.onclick = () => {
  77. var activity = new window.MozActivity({
  78. name: 'dial',
  79. data: {
  80. type: 'webtelephony/number',
  81. number: this._currentContact.number
  82. }
  83. });
  84. activity.onerror = () => {
  85. console.error('we are not able to call mozActivity to dialer with' +
  86. ' number ' + this._currentContact.number);
  87. };
  88. };
  89. this._elements.fdnActionMenuCancel.onclick =
  90. this._hideActionMenu.bind(this);
  91. },
  92. /**
  93. * we will render all registered FDN numbers on screen.
  94. *
  95. * @type {Function}
  96. * @return {Promise}
  97. */
  98. _renderAuthorizedNumbers: function() {
  99. this._elements.contactsContainer.innerHTML = '';
  100. var cardIndex = DsdsSettings.getIccCardIndexForCallSettings();
  101. return FdnContext.getContacts(cardIndex).then((contacts) => {
  102. for (var i = 0, l = contacts.length; i < l; i++) {
  103. var li = this._renderFdnContact(contacts[i]);
  104. this._elements.contactsContainer.appendChild(li);
  105. }
  106. });
  107. },
  108. /**
  109. * render needed UI for each contact item.
  110. *
  111. * @type {Function}
  112. * @param {Object} contact
  113. */
  114. _renderFdnContact: function(contact) {
  115. var li = document.createElement('li');
  116. var nameContainer = document.createElement('span');
  117. var numberContainer = document.createElement('small');
  118. nameContainer.textContent = contact.name;
  119. numberContainer.textContent = contact.number;
  120. li.appendChild(numberContainer);
  121. li.appendChild(nameContainer);
  122. li.onclick = () => {
  123. this._showActionMenu(contact);
  124. };
  125. return li;
  126. },
  127. /**
  128. * show specific contact on the menu.
  129. *
  130. * @type {Function}
  131. * @param {Object} contact
  132. */
  133. _showActionMenu: function(contact) {
  134. this._currentContact = contact;
  135. this._elements.fdnActionMenuName.textContent = contact.name;
  136. this._elements.fdnActionMenuNumber.textContent = contact.number;
  137. this._elements.fdnActionMenu.hidden = false;
  138. },
  139. /**
  140. * hide the whole action menu.
  141. *
  142. * @type {Function}
  143. */
  144. _hideActionMenu: function() {
  145. this._elements.fdnActionMenu.hidden = true;
  146. },
  147. /**
  148. * update information on each contact item based on passed in parameters.
  149. *
  150. * @type {Function}
  151. * @param {String} action
  152. * @param {Object} options
  153. * @return {Promise}
  154. */
  155. _updateContact: function(action, options) {
  156. // `action' is either `add', `edit' or `remove': these three actions all
  157. // rely on the same mozIccManager.updateContact() method.
  158. options = options || {};
  159. var cardIndex = DsdsSettings.getIccCardIndexForCallSettings();
  160. var name = options.name;
  161. var number = options.number;
  162. var contact = FdnContext.createAction(action, {
  163. cardIndex: cardIndex,
  164. contact: {
  165. id: this._currentContact && this._currentContact.id,
  166. name: name,
  167. number: number
  168. }
  169. });
  170. return DialogService.show('simpin-dialog', {
  171. method: 'get_pin2',
  172. cardIndex: cardIndex,
  173. pinOptions: {
  174. fdnContact: contact
  175. }
  176. }).then((result) => {
  177. var type = result.type;
  178. if (type === 'submit') {
  179. this._renderAuthorizedNumbers();
  180. }
  181. });
  182. }
  183. });
  184. };
  185. });