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.

90 lines
2.2 KiB

  1. module.exports = class Repository {
  2. constructor (id, data) {
  3. this.id = id
  4. this.isLoaded = false
  5. if (data) {
  6. this.data = data
  7. this.lang = this.data.lang || {}
  8. this.loadCallbacks = null
  9. }
  10. }
  11. file_get_contents (fileName, options, callback) {
  12. let param = []
  13. param.push('repo=' + encodeURIComponent(this.id))
  14. param.push('file=' + encodeURIComponent(fileName))
  15. param.push(config.categoriesRev)
  16. param = param.length ? '?' + param.join('&') : ''
  17. fetch('repo.php' + param)
  18. .then(res => res.text())
  19. .then(data => {
  20. global.setTimeout(() => {
  21. callback(null, data)
  22. }, 0)
  23. })
  24. .catch(err => {
  25. global.setTimeout(() => {
  26. callback(err)
  27. }, 0)
  28. })
  29. }
  30. load (callback) {
  31. if (this.loadCallbacks) {
  32. return this.loadCallbacks.push(callback)
  33. }
  34. this.loadCallbacks = [callback]
  35. var param = []
  36. param.push('repo=' + encodeURIComponent(this.id))
  37. param.push('lang=' + encodeURIComponent(ui_lang))
  38. param.push(config.categoriesRev)
  39. param = param.length ? '?' + param.join('&') : ''
  40. fetch('repo.php' + param)
  41. .then(res => res.json())
  42. .then(data => {
  43. this.data = data
  44. this.lang = this.data.lang || {}
  45. this.err = null
  46. global.setTimeout(() => {
  47. const cbs = this.loadCallbacks
  48. this.loadCallbacks = null
  49. cbs.forEach(cb => cb(null))
  50. }, 0)
  51. })
  52. .catch(err => {
  53. this.err = err
  54. global.setTimeout(() => {
  55. const cbs = this.loadCallbacks
  56. this.loadCallbacks = null
  57. cbs.forEach(cb => cb(err))
  58. }, 0)
  59. })
  60. }
  61. clearCache () {
  62. this.data = null
  63. }
  64. getCategory (id, options, callback) {
  65. if (!(id in this.data.categories)) {
  66. return callback(new Error('Repository ' + this.id + ': Category "' + id + '" not defined'), null)
  67. }
  68. callback(null, this.data.categories[id])
  69. }
  70. getTemplate (id, options, callback) {
  71. if (!(id in this.data.templates)) {
  72. return callback(new Error('Repository ' + this.id + ': Template "' + id + '" not defined'), null)
  73. }
  74. callback(null, this.data.templates[id])
  75. }
  76. }