From ff7955dfdb300e504ea5c9b2e890a60f6819d125 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Thu, 8 Jun 2017 15:12:18 +0100 Subject: [PATCH] Update labels when docs are uploaded or removed --- .../Features/Labels/LabelsController.coffee | 2 +- .../Uploads/ProjectUploadController.coffee | 1 + services/web/public/coffee/ide.coffee | 2 +- .../aceEditor/labels/LabelsManager.coffee | 34 +++++++------------ .../ide/file-tree/FileTreeManager.coffee | 2 +- .../controllers/FileTreeController.coffee | 6 ++-- 6 files changed, 20 insertions(+), 27 deletions(-) diff --git a/services/web/app/coffee/Features/Labels/LabelsController.coffee b/services/web/app/coffee/Features/Labels/LabelsController.coffee index fa54764402..a2783b4c3b 100644 --- a/services/web/app/coffee/Features/Labels/LabelsController.coffee +++ b/services/web/app/coffee/Features/Labels/LabelsController.coffee @@ -20,7 +20,7 @@ module.exports = LabelsController = if err? logger.err {project_id, doc_id, err}, "[LabelsController] error getting labels from doc" return next(err) - EditorRealTimeController.emitToRoom project_id, 'doc:labels:updated', { + EditorRealTimeController.emitToRoom project_id, 'docLabelsUpdated', { docId: doc_id, labels: docLabels } res.json {projectId: project_id, docId: doc_id, labels: docLabels} diff --git a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee index 8e4464828f..209faec104 100644 --- a/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee +++ b/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee @@ -51,3 +51,4 @@ module.exports = ProjectUploadController = project_id: project_id, file_path: path, file_name: name, folder_id: folder_id "uploaded file" res.send success: true, entity_id: entity?._id + diff --git a/services/web/public/coffee/ide.coffee b/services/web/public/coffee/ide.coffee index 161532cff0..285dc016e2 100644 --- a/services/web/public/coffee/ide.coffee +++ b/services/web/public/coffee/ide.coffee @@ -172,7 +172,7 @@ define [ # User can append ?ft=somefeature to url to activate a feature toggle ide.featureToggle = location?.search?.match(/^\?ft=(\w+)$/)?[1] - ide.socket.on 'doc:labels:updated', (data) -> + ide.socket.on 'docLabelsUpdated', (data) -> $scope.$broadcast 'doc:labels:updated', data angular.bootstrap(document.body, ["SharelatexApp"]) diff --git a/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee b/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee index 8917f117e4..e4aa96d15e 100644 --- a/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee +++ b/services/web/public/coffee/ide/editor/directives/aceEditor/labels/LabelsManager.coffee @@ -27,6 +27,13 @@ define [ 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, entityId) => + @loadDocLabelsFromServer(entityId) + onChange = (change) => if change.remote return @@ -66,34 +73,17 @@ define [ ) loadCurrentDocLabelsFromServer: () -> + currentDocId = @$scope.docId + @loadDocLabelsFromServer(currentDocId) + + loadDocLabelsFromServer: (docId) -> $.get( - "/project/#{window.project_id}/#{@$scope.docId}/labels" + "/project/#{window.project_id}/#{docId}/labels" , (data) => if data.docId and data.labels @state.documents[data.docId] = data.labels ) - # loadLabelsFromOpenDoc: () -> - # docId = @$scope.docId - # docText = @editor.getValue() - # labels = [] - # re = /\\label\{([^\}\n\\]{0,80})\}/g - # while (labelMatch = re.exec(docText)) and labels.length < 1000 - # if labelMatch[1] - # labels.push(labelMatch[1]) - # @state.documents[docId] = labels - - # scheduleLoadLabelsFromOpenDoc: () -> - # # De-bounce loading labels with a timeout - # if @loadLabelsTimeout - # clearTimeout(@loadLabelsTimeout) - # @loadLabelsTimeout = setTimeout( - # () => - # @loadLabelsFromOpenDoc() - # , 1000 - # , this - # ) - scheduleLoadCurrentDocLabelsFromServer: () -> # De-bounce loading labels with a timeout if @loadLabelsTimeout diff --git a/services/web/public/coffee/ide/file-tree/FileTreeManager.coffee b/services/web/public/coffee/ide/file-tree/FileTreeManager.coffee index dd6f813430..0881e75cc8 100644 --- a/services/web/public/coffee/ide/file-tree/FileTreeManager.coffee +++ b/services/web/public/coffee/ide/file-tree/FileTreeManager.coffee @@ -71,7 +71,7 @@ define [ @$scope.$apply () => @_deleteEntityFromScope entity @recalculateDocList() - @$scope.$emit "entity:deleted", entity + @$scope.$broadcast "entity:deleted", entity @ide.socket.on "reciveEntityMove", (entity_id, folder_id) => entity = @findEntityById(entity_id) diff --git a/services/web/public/coffee/ide/file-tree/controllers/FileTreeController.coffee b/services/web/public/coffee/ide/file-tree/controllers/FileTreeController.coffee index 0702aa5d53..0eeb86c9c1 100644 --- a/services/web/public/coffee/ide/file-tree/controllers/FileTreeController.coffee +++ b/services/web/public/coffee/ide/file-tree/controllers/FileTreeController.coffee @@ -107,8 +107,8 @@ define [ ] App.controller "UploadFileModalController", [ - "$scope", "ide", "$modalInstance", "$timeout", "parent_folder", "$window" - ($scope, ide, $modalInstance, $timeout, parent_folder, $window) -> + "$scope", "$rootScope", "ide", "$modalInstance", "$timeout", "parent_folder", "$window" + ($scope, $rootScope, ide, $modalInstance, $timeout, parent_folder, $window) -> $scope.parent_folder_id = parent_folder?.id $scope.tooManyFiles = false $scope.rateLimitHit = false @@ -134,6 +134,8 @@ define [ $scope.onComplete = (error, name, response) -> $timeout (() -> uploadCount-- + if response.success + $rootScope.$broadcast 'file:upload:complete', response.entity_id if uploadCount == 0 and response? and response.success $modalInstance.close("done") ), 250