custom.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Wifi Hotspot app for YunoHost
  2. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  3. * Contribute at https://github.com/jvaubourg/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 dropDownClick() {
  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. }
  45. function updateNbSsidRemaining() {
  46. multissid = $('#devlist .active').data('multissid');
  47. current = $('.ssid').length;
  48. remaining = multissid - current;
  49. $('.ssid').each(function(i) {
  50. if(i >= multissid) {
  51. $(this).removeClass('panel-default');
  52. $(this).addClass('panel-danger');
  53. } else {
  54. $(this).removeClass('panel-danger');
  55. $(this).addClass('panel-default');
  56. }
  57. });
  58. $('.ssid').find('.deletessid').hide();
  59. $('.ssid').last().find('.deletessid').show();
  60. $('.ssid').first().find('.deletessid').hide();
  61. if(remaining <= 0) {
  62. $('#newssid').attr('disabled', true);
  63. $('#newssid').removeClass('btn-success');
  64. $('#newssid').addClass('btn-danger');
  65. } else {
  66. $('#newssid').attr('disabled', false);
  67. $('#newssid').removeClass('btn-danger');
  68. $('#newssid').addClass('btn-success');
  69. }
  70. $('#newssid span').text(remaining);
  71. }
  72. function deleteClick() {
  73. $(this).closest('.ssid').remove();
  74. updateNbSsidRemaining();
  75. }
  76. $(document).ready(function() {
  77. $('.btn-group').button();
  78. $('[data-toggle="tooltip"]').tooltip();
  79. $('.dropdown-menu li').click(dropDownClick);
  80. $('.switch').bootstrapToggle();
  81. $('#save').click(function() {
  82. $(this).prop('disabled', true);
  83. $('#save-loading').show();
  84. $('#form').submit();
  85. });
  86. $('#saveconfirm').click(function() {
  87. $(this).hide();
  88. $('#saveconfirmation').show();
  89. });
  90. $('#status .close').click(function() {
  91. $(this).parent().hide();
  92. });
  93. $('#statusbtn').click(function() {
  94. if($('#status-loading').is(':hidden')) {
  95. $('#status').hide();
  96. $('#status-loading').show();
  97. $.ajax({
  98. url: '?/status',
  99. }).done(function(data) {
  100. $('#status-loading').hide();
  101. $('#status-text').html('<ul>' + data + '</ul>');
  102. $('#status').show('slow');
  103. });
  104. }
  105. });
  106. $('.wifiparty').click(function() {
  107. var screen = $('#wifiparty_screen');
  108. var passphrase = $(this).closest('.ssid').find('.wifiparty_passphrase').clone();
  109. screen.find('#wifiparty_ssid span').last().text($(this).closest('.ssid').find('input[type=text]').first().val());
  110. screen.find('.wifiparty_passphrase').remove();
  111. screen.append(passphrase);
  112. screen.find('.wifiparty_passphrase').show();
  113. screen.show('slow');
  114. });
  115. $('#wifiparty_zoomin_ssid').mousedown(function() {
  116. $('#wifiparty_ssid').css('fontSize', (parseInt($('#wifiparty_ssid').css('fontSize')) + 5) + "px");
  117. });
  118. $('#wifiparty_zoomout_ssid').mousedown(function() {
  119. $('#wifiparty_ssid').css('fontSize', (parseInt($('#wifiparty_ssid').css('fontSize')) - 5) + "px");
  120. });
  121. $('#wifiparty_zoomin_passphrase').mousedown(function() {
  122. $('#wifiparty_screen .wifiparty_passphrase').css('fontSize', (parseInt($('#wifiparty_screen .wifiparty_passphrase').css('fontSize')) + 7) + "px");
  123. });
  124. $('#wifiparty_zoomout_passphrase').mousedown(function() {
  125. $('#wifiparty_screen .wifiparty_passphrase').css('fontSize', (parseInt($('#wifiparty_screen .wifiparty_passphrase').css('fontSize')) - 7) + "px");
  126. });
  127. $('#wifiparty_close').click(function() {
  128. $('#wifiparty_screen').hide();
  129. });
  130. $('.wifi_secure').change(wifiSecureBtn);
  131. $('#service_enabled').change(function() {
  132. if($('#service_enabled').parent().hasClass('off')) {
  133. $('.enabled').hide('slow');
  134. } else {
  135. $('.enabled').show('slow');
  136. }
  137. });
  138. $('.nav-tabs a').click(tabsClick);
  139. $('#newssid').click(function() {
  140. var clone = $('#ssids').children().first().clone();
  141. var id = parseInt($('.ssid').length);
  142. clone.find('.dropdownmenu').each(function(i) {
  143. var initial = $('#ssids').children().first().find('.dropdownmenu');
  144. var clone = initial.eq(i).clone(true, true);
  145. $(this).after(clone);
  146. $(this).remove();
  147. });
  148. clone.find('[name]').each(function() {
  149. $(this).attr('name', $(this).attr('name').replace('[0]', '[' + id + ']'));
  150. });
  151. clone.find('[data-toggle="tooltip"]').tooltip();
  152. clone.find('.deletessid').click(deleteClick);
  153. clone.find('.wifiparty_passphrase').remove();
  154. clone.find('.wifiparty').attr('disabled', true);
  155. clone.find('input[type=text]').each(function() {
  156. if($(this).attr('name').match('dns')) {
  157. $(this).val($(this).attr('placeholder'));
  158. } else if($(this).attr('name').match('ip4_nat_prefix')) {
  159. var o1 = parseInt(Math.random() * (255 - 1) + 1);
  160. var o2 = parseInt(Math.random() * (255 - 1) + 1);
  161. $(this).val('10.' + o1 + '.' + o2);
  162. } else if($(this).attr('name').match('wifi_ssid')) {
  163. $(this).val('myNeutralNetwork' + (id + 1));
  164. } else {
  165. $(this).val('');
  166. }
  167. });
  168. clone.find('input[type=checkbox]').each(function() {
  169. $(this).parent().after($(this));
  170. $(this).prev().remove();
  171. $(this).attr('checked', false);
  172. });
  173. clone.find('.switch').bootstrapToggle();
  174. clone.find('.wifi_secure').change(wifiSecureBtn);
  175. clone.find('.nav-tabs a').click(tabsClick);
  176. clone.find('.dropdown-menu li').click(dropDownClick);
  177. clone.find('.wifi_passphrase').hide();
  178. clone.find('h3').each(function() {
  179. $(this).text($(this).data('label') + ' ' + (id + 1));
  180. });
  181. $('#ssids').append(clone);
  182. updateNbSsidRemaining();
  183. });
  184. $('.deletessid').click(deleteClick);
  185. updateNbSsidRemaining();
  186. });
  187. $(document).keydown(function(e) {
  188. if(e.keyCode == 27) {
  189. $('#wifiparty_close').click();
  190. }
  191. });