custom.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* VPN Client app for YunoHost
  2. * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  3. * Contribute at https://github.com/labriqueinternet/vpnclient_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 tabsClick() {
  19. var tab = $(this).parent().attr('data-tab');
  20. $('.nav').find('li.active').removeClass('active');
  21. $(this).parent().addClass('active');
  22. $('.tabs').hide();
  23. $('.tab' + tab).show();
  24. return false;
  25. }
  26. function ready() {
  27. $('.btn-group').button();
  28. $('[data-toggle="tooltip"]').tooltip();
  29. $('.switch').bootstrapToggle();
  30. $('.nav-tabs a').click(tabsClick);
  31. $('.fileinput').click(function() {
  32. if(!$(this).hasClass('btn-danger')) {
  33. var realinputid = '#' + $(this).attr('id').replace(/_chooser.*/, '');
  34. $(realinputid).click();
  35. }
  36. });
  37. $('.deletefile').click(function() {
  38. var chooserbtnid = '#' + $(this).attr('id').replace(/_deletebtn$/, '_chooserbtn');
  39. var choosertxtid = '#' + $(this).attr('id').replace(/_deletebtn$/, '_choosertxt');
  40. var fileinputid = '#' + $(this).attr('id').replace(/_deletebtn$/, '');
  41. var deleteinputid = '#' + $(this).attr('id').replace(/btn$/, '');
  42. $(deleteinputid).click();
  43. $(chooserbtnid).toggleClass('btn-danger');
  44. $(chooserbtnid).toggleClass('not-allowed');
  45. $(choosertxtid).toggleClass('btn-danger');
  46. $(choosertxtid).val($(choosertxtid).hasClass('btn-danger') ? 'Removal requested' : '');
  47. $(fileinputid).val('');
  48. if($(this).attr('id').search('_key') >= 0) {
  49. if($(choosertxtid).hasClass('btn-danger') != $('#crt_client_choosertxt').hasClass('btn-danger')) {
  50. $('#crt_client_deletebtn').click();
  51. }
  52. } else if($(this).attr('id').search('_ta') < 0) {
  53. if($(choosertxtid).hasClass('btn-danger') != $('#crt_client_key_choosertxt').hasClass('btn-danger')) {
  54. $('#crt_client_key_deletebtn').click();
  55. }
  56. }
  57. });
  58. $('input[type="file"]').change(function() {
  59. var choosertxtid = '#' + $(this).attr('id') + '_choosertxt';
  60. $(choosertxtid).val($(this).val().replace(/^.*[\/\\]/, ''));
  61. });
  62. $('#form').on("submit", function(event) {
  63. event.preventDefault()
  64. $('#save').prop('disabled', true);
  65. $('#save-loading').show();
  66. $.ajax({
  67. url: this.action,
  68. type: this.method,
  69. contentType: false,
  70. processData: false,
  71. cache: false,
  72. data: new FormData(this),
  73. headers: {
  74. 'X-Requested-With': 'jQuery',
  75. },
  76. timeout: 5000,
  77. dataType: "html",
  78. // success: function() {}, // XXX will never happen because the VPN connection will be restarted after the form is posted.
  79. complete: function() {
  80. console.log("Forcing page reload after a few seconds...");
  81. setTimeout(function() {document.location.reload();}, 45000)
  82. },
  83. });
  84. })
  85. $('#status .close').click(function() {
  86. $(this).parent().hide();
  87. });
  88. $('#statusbtn').click(function() {
  89. if($('#status-loading').is(':hidden')) {
  90. $('#status').hide();
  91. $('#status-loading').show();
  92. $.ajax({
  93. url: '?/status',
  94. }).done(function(data) {
  95. $('#status-loading').hide();
  96. $('#status-text').html('<ul>' + data + '</ul>');
  97. $('#status').show('slow');
  98. });
  99. }
  100. });
  101. $('#raw_openvpn_btn').click(function() {
  102. $('#raw_openvpn_btnpanel').hide();
  103. $('#raw_openvpn_panel').show('low');
  104. });
  105. $('#service_enabled').change(function() {
  106. if($('#service_enabled').parent().hasClass('off')) {
  107. $('.enabled').hide('slow');
  108. } else {
  109. $('.enabled').show('slow');
  110. }
  111. });
  112. }
  113. $(document).ready(ready)