You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings-admin.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @author Björn Schießle <schiessle@owncloud.com>
  3. *
  4. * @copyright Copyright (c) 2015, ownCloud, Inc.
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. (function( $ ) {
  21. // ocFederationAddServer
  22. $.fn.ocFederationAddServer = function() {
  23. /* Go easy on jquery and define some vars
  24. ========================================================================== */
  25. var $wrapper = $(this),
  26. // Buttons
  27. $btnAddServer = $wrapper.find("#ocFederationAddServerButton"),
  28. $btnSubmit = $wrapper.find("#ocFederationSubmit"),
  29. // Inputs
  30. $inpServerUrl = $wrapper.find("#serverUrl"),
  31. $inpAutoAddServers = $wrapper.find("#autoAddServers"),
  32. // misc
  33. $msgBox = $wrapper.find("#ocFederationAddServer .msg"),
  34. $srvList = $wrapper.find("#listOfTrustedServers");
  35. /* Interaction
  36. ========================================================================== */
  37. $btnAddServer.on('click', function() {
  38. $btnAddServer.addClass('hidden');
  39. $inpServerUrl
  40. .removeClass('hidden')
  41. .focus();
  42. });
  43. // trigger server removal
  44. $srvList.on('click', 'li > .icon-delete', function() {
  45. var $this = $(this).parent();
  46. var id = $this.attr('id');
  47. removeServer( id );
  48. });
  49. $inpAutoAddServers.on("change", function() {
  50. $.post(
  51. OC.generateUrl('/apps/federation/auto-add-servers'),
  52. {
  53. autoAddServers: $(this).is(":checked")
  54. }
  55. );
  56. });
  57. $btnSubmit.on("click", function()
  58. {
  59. addServer($inpServerUrl.val());
  60. });
  61. $inpServerUrl.on("change keyup", function (e) {
  62. console.log("typing away");
  63. var url = $(this).val();
  64. // toggle add-button visiblity based on input length
  65. if ( url.length > 0 )
  66. $btnSubmit.removeClass("hidden")
  67. else
  68. $btnSubmit.addClass("hidden")
  69. if (e.keyCode === 13) { // add server on "enter"
  70. addServer(url);
  71. } else if (e.keyCode === 27) { // hide input filed again in ESC
  72. $btnAddServer.removeClass('hidden');
  73. $inpServerUrl.val("").addClass('hidden');
  74. $btnSubmit.addClass('hidden');
  75. }
  76. });
  77. };
  78. /* private Functions
  79. ========================================================================== */
  80. function addServer( url ) {
  81. OC.msg.startSaving('#ocFederationAddServer .msg');
  82. $.post(
  83. OC.generateUrl('/apps/federation/trusted-servers'),
  84. {
  85. url: url
  86. }
  87. ).done(function (data) {
  88. $("#serverUrl").attr('value', '');
  89. $("#listOfTrustedServers").prepend(
  90. $('<li>')
  91. .attr('id', data.id)
  92. .html('<span class="status indeterminate"></span>' +
  93. data.url +
  94. '<span class="icon icon-delete"></span>')
  95. );
  96. OC.msg.finishedSuccess('#ocFederationAddServer .msg', data.message);
  97. })
  98. .fail(function (jqXHR) {
  99. OC.msg.finishedError('#ocFederationAddServer .msg', JSON.parse(jqXHR.responseText).message);
  100. });
  101. };
  102. function removeServer( id ) {
  103. $.ajax({
  104. url: OC.generateUrl('/apps/federation/trusted-servers/' + id),
  105. type: 'DELETE',
  106. success: function(response) {
  107. $("#ocFederationSettings").find("#" + id).remove();
  108. }
  109. });
  110. }
  111. })( jQuery );
  112. window.addEventListener('DOMContentLoaded', function () {
  113. $('#ocFederationSettings').ocFederationAddServer();
  114. });