appsettings.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  3. *
  4. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /* eslint-disable */
  24. import $ from 'jquery'
  25. import { filePath } from './routing'
  26. import { generateFilePath } from "@nextcloud/router"
  27. /**
  28. * Opens a popup with the setting for an app.
  29. * @param {string} appid The ID of the app e.g. 'calendar', 'contacts' or 'files'.
  30. * @param {boolean|string} loadJS If true 'js/settings.js' is loaded. If it's a string
  31. * it will attempt to load a script by that name in the 'js' directory.
  32. * @param {boolean} [cache] If true the javascript file won't be forced refreshed. Defaults to true.
  33. * @param {string} [scriptName] The name of the PHP file to load. Defaults to 'settings.php' in
  34. * the root of the app directory hierarchy.
  35. *
  36. * @deprecated 17.0.0 this method is unused and will be removed with Nextcloud 18
  37. */
  38. export const appSettings = args => {
  39. console.warn('OC.appSettings is deprecated and will be removed with Nextcloud 18')
  40. if (typeof args === 'undefined' || typeof args.appid === 'undefined') {
  41. throw {
  42. name: 'MissingParameter',
  43. message: 'The parameter appid is missing'
  44. }
  45. }
  46. var props = { scriptName: 'settings.php', cache: true }
  47. $.extend(props, args)
  48. var settings = $('#appsettings')
  49. if (settings.length === 0) {
  50. throw {
  51. name: 'MissingDOMElement',
  52. message: 'There has be be an element with id "appsettings" for the popup to show.'
  53. }
  54. }
  55. var popup = $('#appsettings_popup')
  56. if (popup.length === 0) {
  57. $('body').prepend('<div class="popup hidden" id="appsettings_popup"></div>')
  58. popup = $('#appsettings_popup')
  59. popup.addClass(settings.hasClass('topright') ? 'topright' : 'bottomleft')
  60. }
  61. if (popup.is(':visible')) {
  62. popup.hide().remove()
  63. } else {
  64. const arrowclass = settings.hasClass('topright') ? 'up' : 'left'
  65. $.get(generateFilePath(props.appid, '', props.scriptName), function(data) {
  66. popup.html(data).ready(function() {
  67. popup.prepend('<span class="arrow ' + arrowclass + '"></span><h2>' + t('core', 'Settings') + '</h2><a class="close"></a>').show()
  68. popup.find('.close').bind('click', function() {
  69. popup.remove()
  70. })
  71. if (typeof props.loadJS !== 'undefined') {
  72. var scriptname
  73. if (props.loadJS === true) {
  74. scriptname = 'settings.js'
  75. } else if (typeof props.loadJS === 'string') {
  76. scriptname = props.loadJS
  77. } else {
  78. throw {
  79. name: 'InvalidParameter',
  80. message: 'The "loadJS" parameter must be either boolean or a string.'
  81. }
  82. }
  83. if (props.cache) {
  84. $.ajaxSetup({ cache: true })
  85. }
  86. $.getScript(generateFilePath(props.appid, 'js', scriptname))
  87. .fail(function(jqxhr, settings, e) {
  88. throw e
  89. })
  90. }
  91. }).show()
  92. }, 'html')
  93. }
  94. }