2015-12-17 15:13:02 +00:00
|
|
|
define [
|
|
|
|
], () ->
|
2016-02-08 17:04:27 +00:00
|
|
|
class ReferencesManager
|
2015-12-17 15:13:02 +00:00
|
|
|
constructor: (@ide, @$scope) ->
|
|
|
|
|
2015-12-29 11:02:59 +00:00
|
|
|
@$scope.$root._references = @state = keys: []
|
2015-12-18 16:20:58 +00:00
|
|
|
|
2015-12-17 15:13:02 +00: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 14:23:59 +00:00
|
|
|
@indexReferences([doc.doc_id], true)
|
2015-12-17 15:13:02 +00:00
|
|
|
|
2016-05-19 13:58:12 +00:00
|
|
|
@$scope.$on 'references:should-reindex', (e, data) =>
|
|
|
|
@indexAllReferences(true)
|
|
|
|
|
2016-01-27 13:33:42 +00:00
|
|
|
# When we join the project:
|
|
|
|
# index all references files
|
|
|
|
# and don't broadcast to all clients
|
2015-12-29 13:30:57 +00:00
|
|
|
@$scope.$on 'project:joined', (e) =>
|
2016-01-27 16:00:53 +00:00
|
|
|
@indexAllReferences(false)
|
2016-01-21 17:01:24 +00:00
|
|
|
|
2016-01-22 15:59:43 +00:00
|
|
|
setTimeout(
|
|
|
|
(self) ->
|
|
|
|
self.ide.socket.on 'references:keys:updated', (keys) ->
|
2016-01-27 13:33:42 +00:00
|
|
|
# console.log '>> got keys from socket'
|
2016-01-22 15:59:43 +00:00
|
|
|
self._storeReferencesKeys(keys)
|
2016-01-27 16:00:53 +00:00
|
|
|
, 1000
|
2016-01-22 15:59:43 +00:00
|
|
|
, this
|
|
|
|
)
|
|
|
|
|
|
|
|
_storeReferencesKeys: (newKeys) ->
|
2016-01-27 13:33:42 +00:00
|
|
|
# console.log '>> storing references keys'
|
|
|
|
oldKeys = @$scope.$root._references.keys
|
|
|
|
@$scope.$root._references.keys = _.union(oldKeys, newKeys)
|
2016-01-22 15:59:43 +00:00
|
|
|
|
2016-01-22 14:23:59 +00:00
|
|
|
indexReferences: (docIds, shouldBroadcast) ->
|
|
|
|
opts =
|
|
|
|
docIds: docIds
|
|
|
|
shouldBroadcast: shouldBroadcast
|
|
|
|
_csrf: window.csrfToken
|
2016-01-21 17:01:24 +00:00
|
|
|
$.post(
|
2016-01-22 14:23:59 +00:00
|
|
|
"/project/#{@$scope.project_id}/references/index",
|
|
|
|
opts,
|
2016-01-21 17:01:24 +00:00
|
|
|
(data) =>
|
2016-01-27 13:33:42 +00:00
|
|
|
# console.log ">> got keys ", data
|
2016-01-22 15:59:43 +00:00
|
|
|
@_storeReferencesKeys(data.keys)
|
2015-12-18 16:00:24 +00:00
|
|
|
)
|
2016-01-27 16:00:53 +00:00
|
|
|
|
|
|
|
indexAllReferences: (shouldBroadcast) ->
|
|
|
|
opts =
|
|
|
|
shouldBroadcast: shouldBroadcast
|
|
|
|
_csrf: window.csrfToken
|
|
|
|
$.post(
|
|
|
|
"/project/#{@$scope.project_id}/references/indexAll",
|
|
|
|
opts,
|
|
|
|
(data) =>
|
|
|
|
# console.log ">> got keys ", data
|
|
|
|
@_storeReferencesKeys(data.keys)
|
|
|
|
)
|