Skip to content

Commit

Permalink
Merge pull request #2282 from laws-africa/panes
Browse files Browse the repository at this point in the history
Panes
  • Loading branch information
longhotsummer authored Dec 9, 2024
2 parents 57064cb + 92bb5dd commit 9f48800
Show file tree
Hide file tree
Showing 19 changed files with 1,050 additions and 746 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"esversion": 6
"esversion": 8
}
5 changes: 4 additions & 1 deletion indigo_app/js/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import LinterPopup from './LinterPopup.vue';
import TaxonomyTOC from './TaxonomyTOC.vue';
import WorkChooser from './work_chooser';
import WorkListCard from './work_list_card';
import { VSplitter, HSplitter } from './splitters';
import { FacetGroup, RemoveFacetButton } from './facets';

export const vueComponents = {
Expand All @@ -15,5 +16,7 @@ export const components = {
WorkChooser,
WorkListCard,
FacetGroup,
RemoveFacetButton
RemoveFacetButton,
HSplitter,
VSplitter
};
137 changes: 137 additions & 0 deletions indigo_app/js/components/splitters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
export class VSplitter {
constructor (splitter) {
this.cursor = 'col-resize';
this.splitter = splitter;
this.container = splitter.parentElement;
this.firstPane = splitter.previousElementSibling;
this.secondPane = splitter.nextElementSibling;
this.isDragging = false;

this.splitter.addEventListener('mousedown', (e) => this.onMouseDown(e));
this.mouseUp = this.onMouseUp.bind(this);
this.mouseMove = this.onMouseMove.bind(this);

if (this.splitter.id) {
this.storageKey = `splitter:${this.splitter.id}`;
this.loadState();
} else {
this.storageKey = null;
}
}

loadState () {
let percentage = window.localStorage.getItem(this.storageKey);
if (percentage) {
try {
percentage = Math.max(10, Math.min(90, parseFloat(percentage)));
this.setFirstPanePercentage(percentage);
} catch {
window.localStorage.removeItem(this.storageKey);
}
}
}

saveState (width) {
if (this.storageKey) {
window.localStorage.setItem(this.storageKey, width.toString());
}
}

onMouseDown (e) {
this.isDragging = true;
document.body.classList.add('splitter-dragging');
document.body.style.cursor = this.cursor;
document.body.style.userSelect = 'none';
document.addEventListener('mouseup', this.mouseUp);
document.addEventListener('mousemove', this.mouseMove);
}

onMouseUp (e) {
if (this.isDragging) {
this.isDragging = false;
document.body.classList.remove('splitter-dragging');
document.body.style.cursor = 'default';
document.body.style.userSelect = null;
document.removeEventListener('mouseup', this.mouseUp);
document.removeEventListener('mousemove', this.mouseMove);
}
}

onMouseMove (e) {
if (!this.isDragging) return;

// Calculate new widths
const containerOffsetLeft = this.container.getBoundingClientRect().left;
const pointerRelativeXpos = e.clientX - containerOffsetLeft;
const containerWidth = this.container.clientWidth;

this.setFirstPanePercentage((pointerRelativeXpos / containerWidth) * 100);
}

setFirstPanePercentage (leftPanePercentage) {
const containerWidth = this.container.clientWidth;
const splitterWidth = this.splitter.offsetWidth;

let rightPanePercentage = 100 - leftPanePercentage - (splitterWidth / containerWidth) * 100;

// Set minimum widths to prevent collapse
const minPercentage = (50 / containerWidth) * 100; // Convert 50px to percentage

if (leftPanePercentage < minPercentage) {
leftPanePercentage = minPercentage;
rightPanePercentage = 100 - minPercentage - (splitterWidth / containerWidth) * 100;
} else if (rightPanePercentage < minPercentage) {
rightPanePercentage = minPercentage;
leftPanePercentage = 100 - minPercentage - (splitterWidth / containerWidth) * 100;
}

// Apply new widths
this.firstPane.style.flexBasis = `${leftPanePercentage}%`;
this.secondPane.style.flexBasis = `${rightPanePercentage}%`;

this.saveState(leftPanePercentage);
}
}

export class HSplitter extends VSplitter {
constructor (splitter) {
super(splitter);
this.cursor = 'row-resize';
}

onMouseMove (e) {
if (!this.isDragging) return;

// Calculate new heights
const containerOffsetTop = this.container.getBoundingClientRect().top;
const pointerRelativeYpos = e.clientY - containerOffsetTop;
const containerHeight = this.container.clientHeight;

// Calculate percentage heights
this.setFirstPanePercentage((pointerRelativeYpos / containerHeight) * 100);
}

setFirstPanePercentage (firstPanePercentage) {
const containerHeight = this.container.clientHeight;
const splitterHeight = this.splitter.offsetHeight;

let bottomPanePercentage = 100 - firstPanePercentage - (splitterHeight / containerHeight) * 100;

// Set minimum widths to prevent collapse
const minPercentage = (50 / containerHeight) * 100; // Convert 50px to percentage

if (firstPanePercentage < minPercentage) {
firstPanePercentage = minPercentage;
bottomPanePercentage = 100 - minPercentage - (splitterHeight / containerHeight) * 100;
} else if (bottomPanePercentage < minPercentage) {
bottomPanePercentage = minPercentage;
firstPanePercentage = 100 - minPercentage - (splitterHeight / containerHeight) * 100;
}

// Apply new widths
this.firstPane.style.flexBasis = `${firstPanePercentage}%`;
this.secondPane.style.flexBasis = `${bottomPanePercentage}%`;

this.saveState(firstPanePercentage);
}
}
2 changes: 1 addition & 1 deletion indigo_app/static/javascript/indigo-app.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions indigo_app/static/javascript/indigo/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Indigo.DocumentContent = Backbone.Model.extend({
initialize: function(options) {
this.document = options.document;
this.document.content = this;
this.xmlDocument = null;
this.on('change:content', this.contentChanged, this);
this.on('change:dom', this.domChanged, this);
Expand Down
4 changes: 2 additions & 2 deletions indigo_app/static/javascript/indigo/views/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
* Handle all the annotations in a document
*/
Indigo.DocumentAnnotationsView = Backbone.View.extend({
el: ".document-workspace-content",
el: ".document-primary-pane",
events: {
'click #new-annotation-floater': 'newAnnotation',
'click .next-annotation': 'nextAnnotation',
Expand Down Expand Up @@ -624,7 +624,7 @@
const handler = () => {
this.gutter.removeEventListener('layoutComplete', handler);

const container = item.closest('.document-sheet-container');
const container = item.closest('.document-primary-pane-content-pane');
if (container) {
container.scrollTo({
top: parseFloat(item.style.top.replace('px', '')) - 50,
Expand Down
46 changes: 45 additions & 1 deletion indigo_app/static/javascript/indigo/views/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
'click .document-workspace-buttons .save-and-publish': 'saveAndPublish',
'click .document-workspace-buttons .save-and-unpublish': 'saveAndUnpublish',
'click .document-toolbar-wrapper .delete-document': 'delete',
'click .document-secondary-pane-toggle': 'toggleDocumentSecondaryPane',
'indigo:pane-toggled': 'onPaneToggled',
},

initialize: function() {
Expand All @@ -131,9 +133,17 @@

this.$saveBtn = $('.document-workspace-buttons .btn.save');
this.$menu = $('.document-toolbar-menu');
this.secondaryPaneToggle = this.el.querySelector('.document-toolbar-wrapper .document-secondary-pane-toggle');
this.dirty = false;
Indigo.offlineNoticeView.autoShow();

this.panes = {
'document-secondary-pane': document.querySelector('.document-secondary-pane')
};
this.splitters = {
'document-secondary-pane': this.panes['document-secondary-pane'].previousElementSibling
};

this.detectUnsupportedBrowsers();

// stop disable menus
Expand Down Expand Up @@ -197,7 +207,7 @@
editorView: this.bodyEditorView,
});

const akn = this.el.querySelector('.document-workspace-content la-akoma-ntoso');
const akn = this.el.querySelector('.document-primary-pane-content-pane la-akoma-ntoso');
this.popupManager = new window.indigoAkn.PopupEnrichmentManager(akn);
this.popupManager.addProvider(new window.enrichments.PopupIssuesProvider(this.document.issues));

Expand Down Expand Up @@ -335,5 +345,39 @@
e.preventDefault();
e.stopImmediatePropagation();
},

showPane: function (pane) {
if (this.panes[pane] && this.panes[pane].classList.contains('d-none')) {
this.panes[pane].classList.remove('d-none');
this.splitters[pane].classList.remove('d-none');
this.el.dispatchEvent(new CustomEvent('indigo:pane-toggled', {detail: {pane, visible: true}}));
}
},

hidePane: function (pane) {
if (this.panes[pane] && !this.panes[pane].classList.contains('d-none')) {
this.panes[pane].classList.add('d-none');
this.splitters[pane].classList.add('d-none');
this.el.dispatchEvent(new CustomEvent('indigo:pane-toggled', {detail: {pane, visible: false}}));
}
},

togglePane: function (pane) {
if (this.panes[pane]) {
const hidden = this.panes[pane].classList.toggle('d-none');
this.splitters[pane].classList.toggle('d-none', hidden);
this.el.dispatchEvent(new CustomEvent('indigo:pane-toggled', {detail: {pane, visible: !hidden}}));
}
},

toggleDocumentSecondaryPane: function () {
this.togglePane('document-secondary-pane');
},

onPaneToggled: function (e) {
if (e.originalEvent.detail.pane === 'document-secondary-pane') {
this.secondaryPaneToggle.classList.toggle('active', e.originalEvent.detail.visible);
}
}
});
})(window);
Loading

0 comments on commit 9f48800

Please sign in to comment.