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.

legacy-loader.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. /** @typedef {import('jquery')} jQuery */
  25. import $ from 'jquery'
  26. import { generateFilePath } from '@nextcloud/router'
  27. const loadedScripts = {}
  28. const loadedStyles = []
  29. /**
  30. * Load a script for the server and load it. If the script is already loaded,
  31. * the event handler will be called directly
  32. *
  33. * @param {string} app the app id to which the script belongs
  34. * @param {string} script the filename of the script
  35. * @param {Function} ready event handler to be called when the script is loaded
  36. * @return {jQuery.Deferred}
  37. * @deprecated 16.0.0 Use OCP.Loader.loadScript
  38. */
  39. export const addScript = (app, script, ready) => {
  40. console.warn('OC.addScript is deprecated, use OCP.Loader.loadScript instead')
  41. let deferred
  42. const path = generateFilePath(app, 'js', script + '.js')
  43. if (!loadedScripts[path]) {
  44. deferred = $.Deferred()
  45. $.getScript(path, () => deferred.resolve())
  46. loadedScripts[path] = deferred
  47. } else {
  48. if (ready) {
  49. ready()
  50. }
  51. }
  52. return loadedScripts[path]
  53. }
  54. /**
  55. * Loads a CSS file
  56. *
  57. * @param {string} app the app id to which the css style belongs
  58. * @param {string} style the filename of the css file
  59. * @deprecated 16.0.0 Use OCP.Loader.loadStylesheet
  60. */
  61. export const addStyle = (app, style) => {
  62. console.warn('OC.addStyle is deprecated, use OCP.Loader.loadStylesheet instead')
  63. const path = generateFilePath(app, 'css', style + '.css')
  64. if (loadedStyles.indexOf(path) === -1) {
  65. loadedStyles.push(path)
  66. if (document.createStyleSheet) {
  67. document.createStyleSheet(path)
  68. } else {
  69. style = $('<link rel="stylesheet" type="text/css" href="' + path + '"/>')
  70. $('head').append(style)
  71. }
  72. }
  73. }