From 4cb41a988a9b7515b65a1703dc9698b6d475f38b Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Fri, 2 Jun 2017 15:12:59 +0100 Subject: [PATCH] WIP: initial work on server-side labels --- .../Features/Labels/LabelsController.coffee | 14 ++++++++++ .../Features/Labels/LabelsHandler.coffee | 28 +++++++++++++++++++ services/web/app/coffee/router.coffee | 3 ++ .../aceEditor/labels/LabelsManager.coffee | 17 ++++++++++- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 services/web/app/coffee/Features/Labels/LabelsController.coffee create mode 100644 services/web/app/coffee/Features/Labels/LabelsHandler.coffee diff --git a/services/web/app/coffee/Features/Labels/LabelsController.coffee b/services/web/app/coffee/Features/Labels/LabelsController.coffee new file mode 100644 index 0000000000..40271a15c4 --- /dev/null +++ b/services/web/app/coffee/Features/Labels/LabelsController.coffee @@ -0,0 +1,14 @@ +EditorRealTimeController = require "../Editor/EditorRealTimeController" +LabelsHandler = require './LabelsHandler' +logger = require 'logger-sharelatex' + + +module.exports = LabelsController = + + getAllLabels: (req, res, next) -> + project_id = req.params.Project_id + LabelsHandler.getAllLabelsForProject project_id, (err, projectLabels) -> + if err? + logger.err {project_id, err}, "[LabelsController] error getting all labels from project" + return next(err) + res.json {projectId: project_id, labels: projectLabels} diff --git a/services/web/app/coffee/Features/Labels/LabelsHandler.coffee b/services/web/app/coffee/Features/Labels/LabelsHandler.coffee new file mode 100644 index 0000000000..0ede211934 --- /dev/null +++ b/services/web/app/coffee/Features/Labels/LabelsHandler.coffee @@ -0,0 +1,28 @@ +ProjectEntityHandler = require "../Project/ProjectEntityHandler" + + +module.exports = LabelsHandler = + + labelCaptureRegex: () -> + /\\label\{([^\}\n\\]{0,80})\}/g + + getAllLabelsForProject: (projectId, callback=(err, projectLabels)->) -> + ProjectEntityHandler.getAllDocs projectId, (err, docs) -> + if err? + return callback(err) + LabelsHandler.extractLabelsFromDocs docs, (err, projectLabels) -> + if err? + return callback(err) + callback(null, projectLabels) + + extractLabelsFromDocs: (docs, callback=(err, projectLabels)->) -> + projectLabels = {} # docId => List[Label] + for docPath, doc of docs + docLabels = [] + for line in doc.lines + re = LabelsHandler.labelCaptureRegex() + while (labelMatch = re.exec(line)) + if labelMatch[1] + docLabels.push(labelMatch[1]) + projectLabels[doc._id] = docLabels + callback(null, projectLabels) diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 549cc50e81..a77ea68ee5 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -43,6 +43,7 @@ SudoModeController = require('./Features/SudoMode/SudoModeController') SudoModeMiddlewear = require('./Features/SudoMode/SudoModeMiddlewear') AnalyticsRouter = require('./Features/Analytics/AnalyticsRouter') AnnouncementsController = require("./Features/Announcements/AnnouncementsController") +LabelsController = require('./Features/Labels/LabelsController') logger = require("logger-sharelatex") _ = require("underscore") @@ -193,6 +194,8 @@ module.exports = class Router webRouter.get '/Project/:Project_id/download/zip', AuthorizationMiddlewear.ensureUserCanReadProject, ProjectDownloadsController.downloadProject webRouter.get '/project/download/zip', AuthorizationMiddlewear.ensureUserCanReadMultipleProjects, ProjectDownloadsController.downloadMultipleProjects + webRouter.get '/project/:Project_id/labels', AuthorizationMiddlewear.ensureUserCanReadProject, AuthenticationController.requireLogin(), LabelsController.getAllLabels + webRouter.get '/tag', AuthenticationController.requireLogin(), TagsController.getAllTags webRouter.post '/tag', AuthenticationController.requireLogin(), TagsController.createTag webRouter.post '/tag/:tag_id/rename', AuthenticationController.requireLogin(), TagsController.renameTag 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 0e504b4ff8..872e543f1c 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 @@ -12,6 +12,9 @@ define [ class LabelsManager constructor: (@$scope, @editor, @element) -> + window.GET_LABELS = () => + @loadProjectLabelsFromServer() + @state = documents: {} # map of DocId => List[Label] @@ -35,10 +38,22 @@ define [ e.session.on "change", onChange setTimeout( () => - @scheduleLoadLabelsFromOpenDoc() + # @scheduleLoadLabelsFromOpenDoc() + @loadProjectLabelsFromServer() , 0 ) + loadProjectLabelsFromServer: () -> + $.get( + "/project/#{window.project_id}/labels" + , (data) => + console.log ">>", data + if data.labels + for docId, docLabels of data.labels + @state.documents[docId] = docLabels + console.log @state.documents + ) + loadLabelsFromOpenDoc: () -> docId = @$scope.docId docText = @editor.getValue()