metadata.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS205: Consider reworking code to avoid use of IIFEs
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import App from '../../../base'
  15. export default App.factory('metadata', [
  16. '$http',
  17. 'ide',
  18. function ($http, ide) {
  19. const debouncer = {} // DocId => Timeout
  20. const state = { documents: {} }
  21. const metadata = { state }
  22. metadata.onBroadcastDocMeta = function (data) {
  23. if (data.docId != null && data.meta != null) {
  24. state.documents[data.docId] = data.meta
  25. window.dispatchEvent(
  26. new CustomEvent('project:metadata', { detail: state.documents })
  27. )
  28. }
  29. }
  30. metadata.onEntityDeleted = function (e, entity) {
  31. if (entity.type === 'doc') {
  32. return delete state.documents[entity.id]
  33. }
  34. }
  35. metadata.getAllLabels = () =>
  36. _.flattenDeep(
  37. (() => {
  38. const result = []
  39. for (const docId in state.documents) {
  40. const meta = state.documents[docId]
  41. result.push(meta.labels)
  42. }
  43. return result
  44. })()
  45. )
  46. metadata.getAllPackages = function () {
  47. const packageCommandMapping = {}
  48. for (const _docId in state.documents) {
  49. const meta = state.documents[_docId]
  50. for (const packageName in meta.packages) {
  51. const commandSnippets = meta.packages[packageName]
  52. packageCommandMapping[packageName] = commandSnippets
  53. }
  54. }
  55. return packageCommandMapping
  56. }
  57. metadata.loadProjectMetaFromServer = () =>
  58. $http
  59. .get(`/project/${window.project_id}/metadata`)
  60. .then(function (response) {
  61. const { data } = response
  62. if (data.projectMeta) {
  63. return (() => {
  64. const result = []
  65. for (const docId in data.projectMeta) {
  66. const docMeta = data.projectMeta[docId]
  67. result.push((state.documents[docId] = docMeta))
  68. }
  69. window.dispatchEvent(
  70. new CustomEvent('project:metadata', { detail: state.documents })
  71. )
  72. return result
  73. })()
  74. }
  75. })
  76. metadata.loadDocMetaFromServer = docId =>
  77. $http
  78. .post(`/project/${window.project_id}/doc/${docId}/metadata`, {
  79. // Don't broadcast metadata when there are no other users in the
  80. // project.
  81. broadcast: ide.$scope.onlineUsersCount > 0,
  82. _csrf: window.csrfToken,
  83. })
  84. .then(function (response) {
  85. const { data } = response
  86. // handle the POST response like a broadcast event when there are no
  87. // other users in the project.
  88. metadata.onBroadcastDocMeta(data)
  89. })
  90. metadata.scheduleLoadDocMetaFromServer = function (docId) {
  91. if (ide.$scope.permissionsLevel === 'readOnly') {
  92. // The POST request is blocked for users without write permission.
  93. // The user will not be able to consume the meta data for edits anyways.
  94. return
  95. }
  96. // De-bounce loading labels with a timeout
  97. const existingTimeout = debouncer[docId]
  98. if (existingTimeout != null) {
  99. clearTimeout(existingTimeout)
  100. delete debouncer[docId]
  101. }
  102. return (debouncer[docId] = setTimeout(() => {
  103. // TODO: wait for the document to be saved?
  104. metadata.loadDocMetaFromServer(docId)
  105. return delete debouncer[docId]
  106. }, 2000))
  107. }
  108. window.addEventListener('editor:metadata-outdated', () => {
  109. metadata.scheduleLoadDocMetaFromServer(
  110. ide.$scope.editor.sharejs_doc.doc_id
  111. )
  112. })
  113. return metadata
  114. },
  115. ])