From 780914206110c556185d08181f3b7f3c3d4131b4 Mon Sep 17 00:00:00 2001 From: Greg Kempe Date: Thu, 9 Jan 2025 13:14:21 +0200 Subject: [PATCH] replace DocumentEditorView with SourceEditorView --- .../javascript/indigo/views/document.js | 19 +++---- .../indigo/views/document_editor.js | 53 ++----------------- .../indigo/views/document_issues.js | 4 +- 3 files changed, 14 insertions(+), 62 deletions(-) diff --git a/indigo_app/static/javascript/indigo/views/document.js b/indigo_app/static/javascript/indigo/views/document.js index 6e3665489..a0bf32297 100644 --- a/indigo_app/static/javascript/indigo/views/document.js +++ b/indigo_app/static/javascript/indigo/views/document.js @@ -178,14 +178,11 @@ this.revisionsView = new Indigo.DocumentRevisionsView({document: this.document, documentContent: this.documentContent}); this.tocView = new Indigo.DocumentTOCView({model: this.documentContent, document: this.document}); - this.bodyEditorView = new Indigo.DocumentEditorView({ + this.sourceEditorView = new Indigo.SourceEditorView({ model: this.document, - documentContent: this.documentContent, tocView: this.tocView, }); - this.bodyEditorView.on('dirty', this.setDirty, this); - this.bodyEditorView.on('clean', this.setClean, this); - this.bodyEditorView.editorReady.then(function() { + this.sourceEditorView.editorReady.then(function() { // select the appropriate element in the toc // TODO: there's a race condition here: the TOC might not be built yet if (Indigo.queryParams.toc && self.tocView.selectItemById(Indigo.queryParams.toc)) { @@ -198,13 +195,13 @@ model: this.document, prefocus: parseInt(Indigo.queryParams.anntn), }); - this.annotationsView.listenTo(this.bodyEditorView.sourceEditor, 'rendered', this.annotationsView.renderAnnotations); + this.annotationsView.listenTo(this.sourceEditorView, 'rendered', this.annotationsView.renderAnnotations); this.activityView = new Indigo.DocumentActivityView({document: this.document}); this.issuesView = new Indigo.DocumentIssuesView({ document: this.document, documentContent: this.documentContent, - editorView: this.bodyEditorView, + editorView: this.sourceEditorView, }); const akn = this.el.querySelector('.document-primary-pane-content-pane la-akoma-ntoso'); @@ -236,7 +233,7 @@ }, isDirty: function(e) { - return this.dirty || this.bodyEditorView.isDirty(); + return this.dirty || this.sourceEditorView.isDirty(); }, setDirty: function() { @@ -247,7 +244,7 @@ setClean: function() { // disable the save button if all views are clean - if (!this.bodyEditorView.dirty && !this.attachmentsView.dirty) { + if (!this.sourceEditorView.isDirty() && !this.attachmentsView.dirty) { this.dirty = false; this.$saveBtn .prop('disabled', true) @@ -283,7 +280,7 @@ }, save: async function() { - if (!this.bodyEditorView.canCancelEdits()) return; + if (!this.sourceEditorView.confirmAndDiscardChanges()) return; this.$saveBtn .prop('disabled', true) @@ -297,7 +294,7 @@ await Indigo.deferredToAsync(this.attachmentsView.save()); // TODO: a better way of reloading the page (will redirect to provision chooser for now) - if (this.bodyEditorView.sourceEditor.aknTextEditor.reloadOnSave) { + if (this.sourceEditorView.aknTextEditor.reloadOnSave) { window.location.reload(); } } catch { diff --git a/indigo_app/static/javascript/indigo/views/document_editor.js b/indigo_app/static/javascript/indigo/views/document_editor.js index 6a1cb05b7..8ddf2776c 100644 --- a/indigo_app/static/javascript/indigo/views/document_editor.js +++ b/indigo_app/static/javascript/indigo/views/document_editor.js @@ -27,9 +27,8 @@ }, initialize: function(options) { - this.parent = options.parent; this.name = 'source'; - this.document = this.parent.model; + this.document = this.model; this.xmlElement = null; this.quickEditTemplate = $('')[0]; this.sheetInner = document.querySelector('.document-workspace .document-sheet-container .sheet-inner'); @@ -59,7 +58,7 @@ this.listenTo(this.document.content, 'mutation', this.onDomMutated); // setup table editor - this.tableEditor = new Indigo.TableEditorView({parent: this, documentContent: this.parent.documentContent}); + this.tableEditor = new Indigo.TableEditorView({parent: this, documentContent: this.document.content}); this.tableEditor.on('start', this.onTableEditStart, this); this.tableEditor.on('finish', this.onTableEditFinish, this); this.tableEditor.on('discard', this.editActivityCancelled, this); @@ -92,7 +91,7 @@ quickEdit: function(e) { const htmlElement = e.currentTarget.parentElement.parentElement; const elemId = htmlElement.id; - const element = this.parent.documentContent.xmlDocument.querySelector('[eId="' + elemId + '"]'); + const element = this.document.content.xmlDocument.querySelector('[eId="' + elemId + '"]'); if (element && this.confirmAndDiscardChanges()) { this.editXmlElement(element); @@ -320,7 +319,7 @@ if (!this.comparisonDocumentId) return; data.document = this.document.toJSON(); - data.document.content = this.parent.documentContent.toXml(); + data.document.content = this.document.content.toXml(); data.element_id = this.xmlElement.getAttribute('eId'); if (!data.element_id && this.xmlElement.tagName !== "akomaNtoso") { @@ -510,48 +509,4 @@ return this.aknTextEditor.dirty || this.tableEditor.editing; } }); - - // Handle the document editor, tracking changes and saving it back to the server. - // TODO: this doesn't really do much any more and the remaining functionality could - // be moved into SourceEditorView and/or DocumentDetailView - Indigo.DocumentEditorView = Backbone.View.extend({ - el: 'body', - - initialize: function(options) { - this.dirty = false; - - this.documentContent = options.documentContent; - // XXX: check - this.documentContent.on('change', this.setDirty, this); - this.documentContent.on('sync', this.setClean, this); - - // setup the editor views - this.sourceEditor = new Indigo.SourceEditorView({parent: this, tocView: options.tocView}); - - // this is a deferred to indicate when the editor is ready to edit - this.editorReady = this.sourceEditor.editorReady; - }, - - setDirty: function() { - if (!this.dirty) { - this.dirty = true; - this.trigger('dirty'); - } - }, - - setClean: function() { - if (this.dirty) { - this.dirty = false; - this.trigger('clean'); - } - }, - - isDirty: function() { - return this.dirty || this.sourceEditor.isDirty(); - }, - - canCancelEdits: function() { - return this.sourceEditor.confirmAndDiscardChanges(); - }, - }); })(window); diff --git a/indigo_app/static/javascript/indigo/views/document_issues.js b/indigo_app/static/javascript/indigo/views/document_issues.js index 1e72b9d1e..0023daf95 100644 --- a/indigo_app/static/javascript/indigo/views/document_issues.js +++ b/indigo_app/static/javascript/indigo/views/document_issues.js @@ -19,7 +19,7 @@ this.$akn = this.$('#document-sheet la-akoma-ntoso'); this.nodes = []; - this.listenTo(this.editorView.sourceEditor, 'rendered', this.render); + this.listenTo(this.editorView, 'rendered', this.render); this.listenTo(this.model, 'reset change add remove', this.render); this.listenTo(this.documentContent, 'change', this.runLinters); this.listenTo(this.attachments, 'reset change add remove', this.runLinters); @@ -81,7 +81,7 @@ for (var i = 0; i < targets.length; i++) { var target = targets[i]; - var gutter = self.editorView.sourceEditor.ensureGutterActions(target); + var gutter = self.editorView.ensureGutterActions(target); var node = $(self.template(issue.toJSON()))[0]; self.nodes.push(node);