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.

loader.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  3. *
  4. * @author Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. const loadedScripts = {}
  23. const loadedStylesheets = {}
  24. /**
  25. * @namespace OCP
  26. * @class Loader
  27. */
  28. export default {
  29. /**
  30. * Load a script asynchronously
  31. *
  32. * @param {string} app the app name
  33. * @param {string} file the script file name
  34. * @returns {Promise}
  35. */
  36. loadScript: function(app, file) {
  37. const key = app + file
  38. if (Object.prototype.hasOwnProperty.call(loadedScripts, key)) {
  39. return Promise.resolve()
  40. }
  41. loadedScripts[key] = true
  42. return new Promise(function(resolve, reject) {
  43. const scriptPath = OC.filePath(app, 'js', file)
  44. const script = document.createElement('script')
  45. script.src = scriptPath
  46. script.setAttribute('nonce', btoa(OC.requestToken))
  47. script.onload = () => resolve()
  48. script.onerror = () => reject(new Error(`Failed to load script from ${scriptPath}`))
  49. document.head.appendChild(script)
  50. })
  51. },
  52. /**
  53. * Load a stylesheet file asynchronously
  54. *
  55. * @param {string} app the app name
  56. * @param {string} file the script file name
  57. * @returns {Promise}
  58. */
  59. loadStylesheet: function(app, file) {
  60. const key = app + file
  61. if (Object.prototype.hasOwnProperty.call(loadedStylesheets, key)) {
  62. return Promise.resolve()
  63. }
  64. loadedStylesheets[key] = true
  65. return new Promise(function(resolve, reject) {
  66. const stylePath = OC.filePath(app, 'css', file)
  67. const link = document.createElement('link')
  68. link.href = stylePath
  69. link.type = 'text/css'
  70. link.rel = 'stylesheet'
  71. link.onload = () => resolve()
  72. link.onerror = () => reject(new Error(`Failed to load stylesheet from ${stylePath}`))
  73. document.head.appendChild(link)
  74. })
  75. },
  76. }