Move state management into LabelsMaster on the ide

This commit is contained in:
Shane Kilkelly 2017-06-09 11:28:27 +01:00
parent 965a46291b
commit 5443b92e7d
5 changed files with 53 additions and 51 deletions

View file

@ -61,6 +61,7 @@ div.full-size(
track-changes= "editor.trackChanges",
doc-id="editor.open_doc_id"
renderer-data="reviewPanel.rendererData"
labels-master="labelsMaster"
)
include ./review-panel

View file

@ -9,6 +9,7 @@ define [
"ide/pdf/PdfManager"
"ide/binary-files/BinaryFilesManager"
"ide/references/ReferencesManager"
"ide/labels/LabelsMaster"
"ide/review-panel/ReviewPanelManager"
"ide/SafariScrollPatcher"
"ide/FeatureOnboardingController"
@ -45,6 +46,7 @@ define [
PdfManager
BinaryFilesManager
ReferencesManager
LabelsMaster
ReviewPanelManager
SafariScrollPatcher
) ->
@ -117,6 +119,8 @@ define [
ide.project_id = $scope.project_id = window.project_id
ide.$scope = $scope
$scope.labelsMaster = ide.labelsMaster = new LabelsMaster(ide, $scope)
ide.referencesSearchManager = new ReferencesManager(ide, $scope)
ide.connectionManager = new ConnectionManager(ide, $scope)
ide.fileTreeManager = new FileTreeManager(ide, $scope)

View file

@ -37,6 +37,7 @@ define [
return {
scope: {
labelsMaster: '='
theme: "="
showPrintMargin: "="
keybindings: "="

View file

@ -11,30 +11,9 @@ define [
class LabelsManager
constructor: (@$scope, @editor, @element) ->
window.GET_LABELS = () =>
@loadProjectLabelsFromServer()
window.GET_DOC_LABELS = () =>
@loadCurrentDocLabelsFromServer()
@state =
documents: {} # map of DocId => List[Label]
@labelsMaster = @$scope.labelsMaster
@loadLabelsTimeout = null
@$scope.$on 'doc:labels:updated', (e, data) =>
if data.docId and data.labels
@state.documents[data.docId] = data.labels
@$scope.$on 'entity:deleted', (e, entity) =>
if entity.type == 'doc'
delete @state.documents[entity.id]
@$scope.$on 'file:upload:complete', (e, upload) =>
if upload.entity_type == 'doc'
@loadDocLabelsFromServer(upload.entity_id)
onChange = (change) =>
if change.remote
return
@ -49,41 +28,14 @@ define [
(commandFragment?.length > 2 and commandFragment.slice(0,7) == '\\label{'))
)
@scheduleLoadCurrentDocLabelsFromServer()
# @scheduleLoadLabelsFromOpenDoc()
@editor.on "changeSession", (e) =>
e.oldSession.off "change", onChange
e.session.on "change", onChange
# setTimeout(
# () =>
# # @scheduleLoadLabelsFromOpenDoc()
# @loadProjectLabelsFromServer()
# , 0
# )
# Load now
@loadProjectLabelsFromServer()
loadProjectLabelsFromServer: () ->
$.get(
"/project/#{window.project_id}/labels"
, (data) =>
if data.projectLabels
for docId, docLabels of data.projectLabels
@state.documents[docId] = docLabels
)
loadCurrentDocLabelsFromServer: () ->
currentDocId = @$scope.docId
@loadDocLabelsFromServer(currentDocId)
loadDocLabelsFromServer: (docId) ->
$.get(
"/project/#{window.project_id}/#{docId}/labels"
, (data) =>
if data.docId and data.labels
@state.documents[data.docId] = data.labels
)
@labelsMaster.loadDocLabelsFromServer(currentDocId)
scheduleLoadCurrentDocLabelsFromServer: () ->
# De-bounce loading labels with a timeout
@ -97,4 +49,4 @@ define [
)
getAllLabels: () ->
_.flatten(labels for docId, labels of @state.documents)
@labelsMaster.getAllLabels()

View file

@ -0,0 +1,44 @@
define [
], () ->
class LabelsMaster
constructor: (@ide, @$scope) ->
@_state = {
documents: {}
}
@$scope.$on 'doc:labels:updated', (e, data) =>
if data.docId and data.labels
@_state.documents[data.docId] = data.labels
@$scope.$on 'entity:deleted', (e, entity) =>
if entity.type == 'doc'
delete @_state.documents[entity.id]
@$scope.$on 'file:upload:complete', (e, upload) =>
if upload.entity_type == 'doc'
@loadDocLabelsFromServer(upload.entity_id)
# load project labels now
@loadProjectLabelsFromServer()
getAllLabels: () ->
_.flatten(labels for docId, labels of @_state.documents)
## Loaders
loadProjectLabelsFromServer: () ->
$.get(
"/project/#{window.project_id}/labels"
, (data) =>
if data.projectLabels
for docId, docLabels of data.projectLabels
@_state.documents[docId] = docLabels
)
loadDocLabelsFromServer: (docId) ->
$.get(
"/project/#{window.project_id}/#{docId}/labels"
, (data) =>
if data.docId and data.labels
@_state.documents[data.docId] = data.labels
)