custom.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Wifi Hotspot app for YunoHost
  2. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  3. * Contribute at https://github.com/labriqueinternet/hotspot_ynh
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. function wifiSecureBtn() {
  19. if($(this).parent().hasClass('off')) {
  20. $(this).closest('.form-group').next().hide('slow');
  21. } else {
  22. $(this).closest('.form-group').next().show('slow');
  23. }
  24. }
  25. function tabsClick() {
  26. var ssid = $(this).closest('.ssid');
  27. var tab = $(this).parent().attr('data-tab');
  28. ssid.find('li.active').removeClass('active');
  29. $(this).parent().addClass('active');
  30. ssid.find('.tabs').hide();
  31. ssid.find('.tab' + tab).show();
  32. return false;
  33. }
  34. function deviceDropDownClick() {
  35. var menu = $(this).parent();
  36. var items = menu.children();
  37. var button = menu.prev();
  38. var input = button.prev();
  39. items.removeClass('active');
  40. $(this).addClass('active');
  41. button.text($(this).text());
  42. button.append(' <span class="caret"></span>');
  43. input.val($(this).text());
  44. updateNbSsidRemaining();
  45. }
  46. function updateNbSsidRemaining() {
  47. multissid = $('#devlist .active').data('multissid');
  48. current = $('.ssid').length;
  49. remaining = multissid - current;
  50. remaining = isNaN(remaining) ? 0 : remaining;
  51. $('.ssid').each(function(i) {
  52. if(i >= multissid) {
  53. $(this).removeClass('panel-default');
  54. $(this).addClass('panel-danger');
  55. } else {
  56. $(this).removeClass('panel-danger');
  57. $(this).addClass('panel-default');
  58. }
  59. });
  60. $('.ssid').find('.deletessid').hide();
  61. $('.ssid').last().find('.deletessid').show();
  62. $('.ssid').first().find('.deletessid').hide();
  63. if(remaining <= 0) {
  64. $('#newssid').attr('disabled', true);
  65. $('#newssid').removeClass('btn-success');
  66. $('#newssid').addClass('btn-danger');
  67. } else {
  68. $('#newssid').attr('disabled', false);
  69. $('#newssid').removeClass('btn-danger');
  70. $('#newssid').addClass('btn-success');
  71. }
  72. $('#newssid span').text(remaining);
  73. }
  74. function deleteClick() {
  75. $(this).closest('.ssid').remove();
  76. updateNbSsidRemaining();
  77. }
  78. $(document).ready(function() {
  79. $('.btn-group').button();
  80. $('[data-toggle="tooltip"]').tooltip();
  81. $('.dropdown-menu li').click(deviceDropDownClick);
  82. $('.switch').bootstrapToggle();
  83. $('#save').click(function() {
  84. $(this).prop('disabled', true);
  85. $('#save-loading').show();
  86. $('#form').submit();
  87. });
  88. $('#saveconfirm').click(function() {
  89. $(this).hide();
  90. $('#saveconfirmation').show();
  91. });
  92. $('#status .close').click(function() {
  93. $(this).parent().hide();
  94. });
  95. $('#statusbtn').click(function() {
  96. if($('#status-loading').is(':hidden')) {
  97. $('#status').hide();
  98. $('#status-loading').show();
  99. $.ajax({
  100. url: '?/status',
  101. }).done(function(data) {
  102. $('#status-loading').hide();
  103. $('#status-text').html('<ul>' + data + '</ul>');
  104. $('#status').show('slow');
  105. });
  106. }
  107. });
  108. $('.wifiparty').click(function() {
  109. var screen = $('#wifiparty_screen');
  110. var passphrase = $(this).closest('.ssid').find('.wifiparty_passphrase').clone();
  111. screen.find('#wifiparty_ssid span').last().text($(this).closest('.ssid').find('input[type=text]').first().val());
  112. screen.find('.wifiparty_passphrase').remove();
  113. screen.append(passphrase);
  114. screen.find('.wifiparty_passphrase').show();
  115. screen.show('slow');
  116. });
  117. $('#wifiparty_zoomin_ssid').mousedown(function() {
  118. $('#wifiparty_ssid').css('fontSize', (parseInt($('#wifiparty_ssid').css('fontSize')) + 5) + "px");
  119. });
  120. $('#wifiparty_zoomout_ssid').mousedown(function() {
  121. $('#wifiparty_ssid').css('fontSize', (parseInt($('#wifiparty_ssid').css('fontSize')) - 5) + "px");
  122. });
  123. $('#wifiparty_zoomin_passphrase').mousedown(function() {
  124. $('#wifiparty_screen .wifiparty_passphrase').css('fontSize', (parseInt($('#wifiparty_screen .wifiparty_passphrase').css('fontSize')) + 7) + "px");
  125. });
  126. $('#wifiparty_zoomout_passphrase').mousedown(function() {
  127. $('#wifiparty_screen .wifiparty_passphrase').css('fontSize', (parseInt($('#wifiparty_screen .wifiparty_passphrase').css('fontSize')) - 7) + "px");
  128. });
  129. $('#wifiparty_close').click(function() {
  130. $('#wifiparty_screen').hide();
  131. });
  132. $('.wifi_secure').change(wifiSecureBtn);
  133. $('#service_enabled').change(function() {
  134. if($('#service_enabled').parent().hasClass('off')) {
  135. $('.enabled').hide('slow');
  136. } else {
  137. $('.enabled').show('slow');
  138. }
  139. });
  140. $('.nav-tabs a').click(tabsClick);
  141. $('#newssid').click(function() {
  142. var clone = $('#ssids').children().first().clone();
  143. var id = parseInt($('.ssid').length);
  144. clone.find('[name]').each(function() {
  145. $(this).attr('name', $(this).attr('name').replace('[0]', '[' + id + ']'));
  146. });
  147. clone.find('[data-toggle="tooltip"]').tooltip();
  148. clone.find('.deletessid').click(deleteClick);
  149. clone.find('.wifiparty_passphrase').remove();
  150. clone.find('.wifiparty').attr('disabled', true);
  151. clone.find('input[type=text]').each(function() {
  152. if($(this).attr('name').match('dns')) {
  153. $(this).val($(this).attr('placeholder'));
  154. } else if($(this).attr('name').match('ip4_nat_prefix')) {
  155. var o1 = parseInt(Math.random() * (255 - 1) + 1);
  156. var o2 = parseInt(Math.random() * (255 - 1) + 1);
  157. $(this).val('10.' + o1 + '.' + o2);
  158. } else if($(this).attr('name').match('wifi_ssid')) {
  159. $(this).val('myNeutralNetwork' + (id + 1));
  160. } else {
  161. $(this).val('');
  162. }
  163. });
  164. clone.find('input[type=checkbox]').each(function() {
  165. $(this).parent().after($(this));
  166. $(this).prev().remove();
  167. $(this).attr('checked', false);
  168. });
  169. clone.find('.switch').bootstrapToggle();
  170. clone.find('.wifi_secure').change(wifiSecureBtn);
  171. clone.find('.nav-tabs a').click(tabsClick);
  172. clone.find('.wifi_passphrase').hide();
  173. clone.find('h3').each(function() {
  174. $(this).text($(this).data('label') + ' ' + (id + 1));
  175. });
  176. $('#ssids').append(clone);
  177. updateNbSsidRemaining();
  178. });
  179. $('.deletessid').click(deleteClick);
  180. updateNbSsidRemaining();
  181. });
  182. $(document).keydown(function(e) {
  183. if(e.keyCode == 27) {
  184. $('#wifiparty_close').click();
  185. }
  186. });