PageRenderTime 24ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/settings/js/panels/wifi_join_hidden/panel.js

https://github.com/mozilla-b2g/gaia
JavaScript | 98 lines | 81 code | 7 blank | 10 comment | 5 complexity | 48755733d017c1174c4681a9ef865734 MD5 | raw file
  1. define(function(require) {
  2. 'use strict';
  3. var DialogPanel = require('modules/dialog_panel');
  4. var WifiHelper = require('shared/wifi_helper');
  5. var WifiUtils = require('modules/wifi_utils');
  6. return function ctor_joinHiddenWifi() {
  7. var elements = {};
  8. var network;
  9. var isHomeKeyPressed = false;
  10. return DialogPanel({
  11. onInit: function(panel) {
  12. elements.panel = panel;
  13. elements.ssid = panel.querySelector('input[name="ssid"]');
  14. elements.eap = panel.querySelector('select[name="eap"]');
  15. elements.password = panel.querySelector('input[name="password"]');
  16. elements.identity = panel.querySelector('input[name="identity"]');
  17. elements.securitySelect =
  18. panel.querySelector('select[name="security"]');
  19. elements.submitButton = panel.querySelector('button[type=submit]');
  20. elements.showPassword = panel.querySelector(
  21. 'gaia-checkbox[name=show-pwd]');
  22. elements.ssid.addEventListener('input', this._onSSIDchange);
  23. elements.securitySelect.onchange = this._onSecurityChange;
  24. },
  25. onBeforeShow: function(panel) {
  26. if (!isHomeKeyPressed) {
  27. network = {};
  28. this._onSecurityChange.call(elements.securitySelect);
  29. WifiUtils.initializeAuthFields(panel, network);
  30. }
  31. isHomeKeyPressed = false;
  32. },
  33. onShow: function() {
  34. elements.ssid.focus();
  35. },
  36. onHide: function() {
  37. isHomeKeyPressed = document.hidden;
  38. if (!isHomeKeyPressed) {
  39. elements.password.value = '';
  40. elements.identity.value = '';
  41. elements.showPassword.checked = false;
  42. }
  43. },
  44. onSubmit: function() {
  45. // We have to keep these information in network object
  46. network.ssid = elements.ssid.value;
  47. network.hidden = true;
  48. return Promise.resolve({
  49. password: elements.password.value,
  50. identity: elements.identity.value,
  51. eap: elements.eap.value,
  52. network: network
  53. });
  54. },
  55. _onSecurityChange: function() {
  56. var key = this.selectedIndex ? this.value : '';
  57. var password = elements.password.value;
  58. var identity = elements.identity.value;
  59. var eap = elements.eap.value;
  60. elements.panel.dataset.security = key;
  61. elements.submitButton.disabled =
  62. !WifiHelper.isValidInput(key, password, identity, eap) ||
  63. !WifiHelper.isSSIDValid(elements.ssid.value);
  64. WifiHelper.setSecurity(network, [key]);
  65. WifiUtils.changeDisplay(elements.panel, key);
  66. },
  67. _onSSIDchange: function(event) {
  68. // Bug 1082394, during composition, we should not change the input
  69. // value. Otherwise, the input value will be cleared unexpectedly.
  70. // Besides, it seems unnecessary to change input value before
  71. // composition is committed.
  72. if (event.isComposing) {
  73. return;
  74. }
  75. // Make sure ssid length is no more than 32 bytes.
  76. var str = this.value;
  77. // Non-ASCII chars in SSID will be encoded by UTF-8, and length of
  78. // each char might be longer than 1 byte.
  79. // Use encodeURIComponent() to encode ssid, then calculate correct
  80. // length.
  81. var encoder = new TextEncoder('utf-8');
  82. while (encoder.encode(str).length > 32) {
  83. str = str.substring(0, str.length - 1);
  84. }
  85. if (str !== this.value) {
  86. this.value = str;
  87. }
  88. }
  89. });
  90. };
  91. });