ReferencesManager.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. define [
  2. ], () ->
  3. class ReferencesManager
  4. constructor: (@ide, @$scope) ->
  5. @$scope.$root._references = @state = keys: []
  6. @$scope.$on 'document:closed', (e, doc) =>
  7. if doc.doc_id
  8. entity = @ide.fileTreeManager.findEntityById doc.doc_id
  9. if entity?.name?.match /.*\.bib$/
  10. @indexReferences([doc.doc_id], true)
  11. @$scope.$on 'references:should-reindex', (e, data) =>
  12. @indexAllReferences(true)
  13. # When we join the project:
  14. # index all references files
  15. # and don't broadcast to all clients
  16. @inited = false
  17. @$scope.$on 'project:joined', (e) =>
  18. # We only need to grab the references when the editor first loads,
  19. # not on every reconnect
  20. if !@inited
  21. @inited = true
  22. @indexAllReferences(false)
  23. setTimeout(
  24. (self) ->
  25. self.ide.socket.on 'references:keys:updated', (keys) ->
  26. # console.log '>> got keys from socket'
  27. self._storeReferencesKeys(keys)
  28. , 1000
  29. , this
  30. )
  31. _storeReferencesKeys: (newKeys) ->
  32. # console.log '>> storing references keys'
  33. oldKeys = @$scope.$root._references.keys
  34. @$scope.$root._references.keys = _.union(oldKeys, newKeys)
  35. indexReferences: (docIds, shouldBroadcast) ->
  36. opts =
  37. docIds: docIds
  38. shouldBroadcast: shouldBroadcast
  39. _csrf: window.csrfToken
  40. @ide.$http.post(
  41. "/project/#{@$scope.project_id}/references/index",
  42. opts
  43. ).then (response) =>
  44. @_storeReferencesKeys(response.data.keys)
  45. indexAllReferences: (shouldBroadcast) ->
  46. opts =
  47. shouldBroadcast: shouldBroadcast
  48. _csrf: window.csrfToken
  49. @ide.$http.post(
  50. "/project/#{@$scope.project_id}/references/indexAll",
  51. opts
  52. ).then (response) =>
  53. @_storeReferencesKeys(response.data.keys)