1
0

settings-admin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. (function( $ ) {
  7. // ocFederationAddServer
  8. $.fn.ocFederationAddServer = function() {
  9. /* Go easy on jquery and define some vars
  10. ========================================================================== */
  11. var $wrapper = $(this),
  12. // Buttons
  13. $btnAddServer = $wrapper.find("#ocFederationAddServerButton"),
  14. $btnSubmit = $wrapper.find("#ocFederationSubmit"),
  15. // Inputs
  16. $inpServerUrl = $wrapper.find("#serverUrl"),
  17. // misc
  18. $msgBox = $wrapper.find("#ocFederationAddServer .msg"),
  19. $srvList = $wrapper.find("#listOfTrustedServers");
  20. /* Interaction
  21. ========================================================================== */
  22. $btnAddServer.on('click', function() {
  23. $btnAddServer.addClass('hidden');
  24. $wrapper.find(".serverUrl").removeClass('hidden');
  25. $inpServerUrl
  26. .focus();
  27. });
  28. // trigger server removal
  29. $srvList.on('click', 'li > .icon-delete', function() {
  30. var $this = $(this).parent();
  31. var id = $this.attr('id');
  32. removeServer( id );
  33. });
  34. $btnSubmit.on("click", function()
  35. {
  36. addServer($inpServerUrl.val());
  37. });
  38. $inpServerUrl.on("change keyup", function (e) {
  39. console.log("typing away");
  40. var url = $(this).val();
  41. // toggle add-button visibility based on input length
  42. if ( url.length > 0 )
  43. $btnSubmit.removeClass("hidden")
  44. else
  45. $btnSubmit.addClass("hidden")
  46. if (e.keyCode === 13) { // add server on "enter"
  47. addServer(url);
  48. } else if (e.keyCode === 27) { // hide input filed again in ESC
  49. $btnAddServer.removeClass('hidden');
  50. $inpServerUrl.val("").addClass('hidden');
  51. $btnSubmit.addClass('hidden');
  52. }
  53. });
  54. };
  55. /* private Functions
  56. ========================================================================== */
  57. function addServer( url ) {
  58. OC.msg.startSaving('#ocFederationAddServer .msg');
  59. $.post(
  60. OC.generateUrl('/apps/federation/trusted-servers'),
  61. {
  62. url: url
  63. }
  64. ).done(function (data) {
  65. $("#serverUrl").attr('value', '');
  66. $("#listOfTrustedServers").prepend(
  67. $('<li>')
  68. .attr('id', data.id)
  69. .html('<span class="status indeterminate"></span>' +
  70. data.url +
  71. '<span class="icon icon-delete"></span>')
  72. );
  73. OC.msg.finishedSuccess('#ocFederationAddServer .msg', data.message);
  74. })
  75. .fail(function (jqXHR) {
  76. OC.msg.finishedError('#ocFederationAddServer .msg', JSON.parse(jqXHR.responseText).message);
  77. });
  78. };
  79. function removeServer( id ) {
  80. $.ajax({
  81. url: OC.generateUrl('/apps/federation/trusted-servers/' + id),
  82. type: 'DELETE',
  83. success: function(response) {
  84. $("#ocFederationSettings").find("#" + id).remove();
  85. }
  86. });
  87. }
  88. })( jQuery );
  89. window.addEventListener('DOMContentLoaded', function () {
  90. $('#ocFederationSettings').ocFederationAddServer();
  91. });