ReferencesManager.coffee 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. @$scope.$on 'project:joined', (e) =>
  17. @indexAllReferences(false)
  18. setTimeout(
  19. (self) ->
  20. self.ide.socket.on 'references:keys:updated', (keys) ->
  21. # console.log '>> got keys from socket'
  22. self._storeReferencesKeys(keys)
  23. , 1000
  24. , this
  25. )
  26. _storeReferencesKeys: (newKeys) ->
  27. # console.log '>> storing references keys'
  28. oldKeys = @$scope.$root._references.keys
  29. @$scope.$root._references.keys = _.union(oldKeys, newKeys)
  30. indexReferences: (docIds, shouldBroadcast) ->
  31. opts =
  32. docIds: docIds
  33. shouldBroadcast: shouldBroadcast
  34. _csrf: window.csrfToken
  35. $.post(
  36. "/project/#{@$scope.project_id}/references/index",
  37. opts,
  38. (data) =>
  39. # console.log ">> got keys ", data
  40. @_storeReferencesKeys(data.keys)
  41. )
  42. indexAllReferences: (shouldBroadcast) ->
  43. opts =
  44. shouldBroadcast: shouldBroadcast
  45. _csrf: window.csrfToken
  46. $.post(
  47. "/project/#{@$scope.project_id}/references/indexAll",
  48. opts,
  49. (data) =>
  50. # console.log ">> got keys ", data
  51. @_storeReferencesKeys(data.keys)
  52. )