2015-12-17 10:13:02 -05:00
|
|
|
define [
|
|
|
|
], () ->
|
2016-02-08 12:04:27 -05:00
|
|
|
class ReferencesManager
|
2015-12-17 10:13:02 -05:00
|
|
|
constructor: (@ide, @$scope) ->
|
|
|
|
|
2015-12-29 06:02:59 -05:00
|
|
|
@$scope.$root._references = @state = keys: []
|
2015-12-18 11:20:58 -05:00
|
|
|
|
2015-12-17 10:13:02 -05:00
|
|
|
@$scope.$on 'document:closed', (e, doc) =>
|
|
|
|
if doc.doc_id
|
|
|
|
entity = @ide.fileTreeManager.findEntityById doc.doc_id
|
|
|
|
if entity?.name?.match /.*\.bib$/
|
2016-01-22 09:23:59 -05:00
|
|
|
@indexReferences([doc.doc_id], true)
|
2015-12-17 10:13:02 -05:00
|
|
|
|
2016-05-19 09:58:12 -04:00
|
|
|
@$scope.$on 'references:should-reindex', (e, data) =>
|
|
|
|
@indexAllReferences(true)
|
|
|
|
|
2016-01-27 08:33:42 -05:00
|
|
|
# When we join the project:
|
|
|
|
# index all references files
|
|
|
|
# and don't broadcast to all clients
|
2016-10-24 11:36:40 -04:00
|
|
|
@inited = false
|
2015-12-29 08:30:57 -05:00
|
|
|
@$scope.$on 'project:joined', (e) =>
|
2016-10-24 11:36:40 -04:00
|
|
|
# We only need to grab the references when the editor first loads,
|
|
|
|
# not on every reconnect
|
|
|
|
if !@inited
|
|
|
|
@inited = true
|
|
|
|
@indexAllReferences(false)
|
2016-01-21 12:01:24 -05:00
|
|
|
|
2016-01-22 10:59:43 -05:00
|
|
|
setTimeout(
|
|
|
|
(self) ->
|
|
|
|
self.ide.socket.on 'references:keys:updated', (keys) ->
|
2016-01-27 08:33:42 -05:00
|
|
|
# console.log '>> got keys from socket'
|
2016-01-22 10:59:43 -05:00
|
|
|
self._storeReferencesKeys(keys)
|
2016-01-27 11:00:53 -05:00
|
|
|
, 1000
|
2016-01-22 10:59:43 -05:00
|
|
|
, this
|
|
|
|
)
|
|
|
|
|
|
|
|
_storeReferencesKeys: (newKeys) ->
|
2016-01-27 08:33:42 -05:00
|
|
|
# console.log '>> storing references keys'
|
|
|
|
oldKeys = @$scope.$root._references.keys
|
|
|
|
@$scope.$root._references.keys = _.union(oldKeys, newKeys)
|
2016-01-22 10:59:43 -05:00
|
|
|
|
2016-01-22 09:23:59 -05:00
|
|
|
indexReferences: (docIds, shouldBroadcast) ->
|
|
|
|
opts =
|
|
|
|
docIds: docIds
|
|
|
|
shouldBroadcast: shouldBroadcast
|
|
|
|
_csrf: window.csrfToken
|
2017-09-29 09:51:00 -04:00
|
|
|
@ide.$http.post(
|
2016-01-22 09:23:59 -05:00
|
|
|
"/project/#{@$scope.project_id}/references/index",
|
2017-09-29 09:51:00 -04:00
|
|
|
opts
|
|
|
|
).then (response) =>
|
|
|
|
@_storeReferencesKeys(response.data.keys)
|
2016-01-27 11:00:53 -05:00
|
|
|
|
|
|
|
indexAllReferences: (shouldBroadcast) ->
|
|
|
|
opts =
|
|
|
|
shouldBroadcast: shouldBroadcast
|
|
|
|
_csrf: window.csrfToken
|
2017-09-29 09:51:00 -04:00
|
|
|
@ide.$http.post(
|
2016-01-27 11:00:53 -05:00
|
|
|
"/project/#{@$scope.project_id}/references/indexAll",
|
2017-09-29 09:51:00 -04:00
|
|
|
opts
|
|
|
|
).then (response) =>
|
|
|
|
@_storeReferencesKeys(response.data.keys)
|