ReferencesManager.coffee 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. $.post(
  41. "/project/#{@$scope.project_id}/references/index",
  42. opts,
  43. (data) =>
  44. # console.log ">> got keys ", data
  45. @_storeReferencesKeys(data.keys)
  46. )
  47. indexAllReferences: (shouldBroadcast) ->
  48. opts =
  49. shouldBroadcast: shouldBroadcast
  50. _csrf: window.csrfToken
  51. $.post(
  52. "/project/#{@$scope.project_id}/references/indexAll",
  53. opts,
  54. (data) =>
  55. # console.log ">> got keys ", data
  56. @_storeReferencesKeys(data.keys)
  57. )