ReferencesManager.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS103: Rewrite code to no longer use __guard__
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. define([], function() {
  17. let ReferencesManager
  18. return (ReferencesManager = class ReferencesManager {
  19. constructor(ide, $scope) {
  20. this.ide = ide
  21. this.$scope = $scope
  22. this.$scope.$root._references = this.state = { keys: [] }
  23. this.$scope.$on('document:closed', (e, doc) => {
  24. let entity
  25. if (doc.doc_id) {
  26. entity = this.ide.fileTreeManager.findEntityById(doc.doc_id)
  27. }
  28. if (
  29. __guard__(entity != null ? entity.name : undefined, x =>
  30. x.match(/.*\.bib$/)
  31. )
  32. ) {
  33. return this.indexReferences([doc.doc_id], true)
  34. }
  35. })
  36. this.$scope.$on('references:should-reindex', (e, data) => {
  37. return this.indexAllReferences(true)
  38. })
  39. // When we join the project:
  40. // index all references files
  41. // and don't broadcast to all clients
  42. this.inited = false
  43. this.$scope.$on('project:joined', e => {
  44. // We only need to grab the references when the editor first loads,
  45. // not on every reconnect
  46. if (!this.inited) {
  47. this.inited = true
  48. return this.indexAllReferences(false)
  49. }
  50. })
  51. setTimeout(
  52. self =>
  53. self.ide.socket.on('references:keys:updated', keys =>
  54. // console.log '>> got keys from socket'
  55. self._storeReferencesKeys(keys)
  56. ),
  57. 1000,
  58. this
  59. )
  60. }
  61. _storeReferencesKeys(newKeys) {
  62. // console.log '>> storing references keys'
  63. const oldKeys = this.$scope.$root._references.keys
  64. return (this.$scope.$root._references.keys = _.union(oldKeys, newKeys))
  65. }
  66. indexReferences(docIds, shouldBroadcast) {
  67. const opts = {
  68. docIds,
  69. shouldBroadcast,
  70. _csrf: window.csrfToken
  71. }
  72. return this.ide.$http
  73. .post(`/project/${this.$scope.project_id}/references/index`, opts)
  74. .then(response => {
  75. return this._storeReferencesKeys(response.data.keys)
  76. })
  77. }
  78. indexAllReferences(shouldBroadcast) {
  79. const opts = {
  80. shouldBroadcast,
  81. _csrf: window.csrfToken
  82. }
  83. return this.ide.$http
  84. .post(`/project/${this.$scope.project_id}/references/indexAll`, opts)
  85. .then(response => {
  86. return this._storeReferencesKeys(response.data.keys)
  87. })
  88. }
  89. })
  90. })
  91. function __guard__(value, transform) {
  92. return typeof value !== 'undefined' && value !== null
  93. ? transform(value)
  94. : undefined
  95. }