Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ReferenceError: closestItem is not defined #1025

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ This file has utility functions used throughout the codebase. All functions are
* Used in collections and Mavo expressions.
* `revocably` is an object with helpers that add/remove DOM elements and still remember the place in the DOM, even if other elements have been added in the meantime.
* `inView` is an object that lets you execute code when something is in view in the viewport using an **intersectionObserver**.
* **resizeObserver** is used for the Mavo bar.

## node.js
* `uid` is a unique ID for all nodes in a Mavo object. It's used for debugging.
Expand Down
18 changes: 13 additions & 5 deletions src-css/_bar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
line-height: 1.5;
position: sticky;

min-width: 20em;
container: mv-bar / inline-size;

@supports (position: sticky) {
z-index: 2;
top: 5px;
Expand Down Expand Up @@ -175,20 +178,25 @@
}

// Displaying mv-bar in small spaces
&.mv-compact {
.mv-status > span {
display: none;
@container mv-bar (width <= 40em) {
.mv-status {
// Don't wrap the username on multiple lines
white-space: nowrap;

& > span {
display: none;
}
}
}

&.mv-tiny {
@container mv-bar (width <= 25em) {
& > button,
& > .mv-button {
// Hide text
width: 1em;
position: relative;
overflow: hidden;
text-indent: -999em;
text-indent: calc(-infinity * 1em);

&::before {
position: absolute;
Expand Down
2 changes: 1 addition & 1 deletion src/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ var _ = Mavo.Primitive = class Primitive extends Mavo.Node {
// Insert the rest of the lines as new items
// FIXME DRYfy the repetition between this code and the one below
let collection = this.closestCollection;
let index = closestItem?.index || 0;
let index = this.closestItem?.index || 0;

for (let i=1; i<lines.length; i++) {
let closestItem = this.closestItem;
Expand Down
66 changes: 1 addition & 65 deletions src/ui.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,8 @@ let _ = Mavo.UI.Bar = class Bar {
});
}

if (this.element.classList.contains("mv-compact")) {
this.noResize = true;
}

this.controls = _.getControls(this.template);

if (this.controls.length) {
// Measure height of 1 row
this.targetHeight = this.element.offsetHeight;
}

if (!this.custom) {
this.element.innerHTML = "";
}
Expand All @@ -63,6 +54,7 @@ let _ = Mavo.UI.Bar = class Bar {
this[id] = $.create("button", {
type: "button",
className: `mv-${id}`,
title: this.mavo._(id),
textContent: this.mavo._(id)
});
}
Expand Down Expand Up @@ -100,57 +92,9 @@ let _ = Mavo.UI.Bar = class Bar {
}
}

if (this.controls.length && !this.noResize) {
this.resize();

if (self.ResizeObserver) {
this.resizeObserver = Mavo.observeResize(this.element, entries => {
this.resize();
});
}
}

Mavo.observers.resume();
}

resize () {
if (!this.targetHeight) {
// We don't have a correct measurement for target height, abort
this.targetHeight = this.element.offsetHeight;
return;
}

this.resizeObserver?.disconnect();

this.element.classList.remove("mv-compact", "mv-tiny");

// Remove pointless tooltips
$$("button, .mv-button", this.element).forEach(button => {
if (button.title === button.textContent) {
button.title = "";
}
});

// Exceeded single row?
if (this.element.offsetHeight > this.targetHeight * 1.6) {
this.element.classList.add("mv-compact");

if (this.element.offsetHeight > this.targetHeight * 1.2) {
// Still too tall
this.element.classList.add("mv-tiny");

// Add tooltips, since only icons will be visible
$$("button, .mv-button", this.element).forEach(button => {
if (!button.title) {
button.title = button.textContent;
}
});
}
}

this.resizeObserver?.observe(this.element);
}

add (id) {
let o = _.controls[id];

Expand All @@ -159,10 +103,6 @@ let _ = Mavo.UI.Bar = class Bar {
}

Mavo.revocably.add(this[id], this.element);

if (!this.resizeObserver && !this.noResize) {
requestAnimationFrame(() => this.resize());
}
}

remove (id) {
Expand All @@ -173,10 +113,6 @@ let _ = Mavo.UI.Bar = class Bar {
if (o.cleanup) {
o.cleanup.call(this.mavo);
}

if (!this.resizeObserver && !this.noResize) {
requestAnimationFrame(() => this.resize());
}
}

toggle (id, add) {
Expand Down
25 changes: 0 additions & 25 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,31 +615,6 @@ var _ = $.extend(Mavo, {

escapeRegExp: s => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"),

observeResize: function(element, callbackOrObserver) {
if (!self.ResizeObserver) {
return;
}

var previousRect = null;
var ro = callbackOrObserver instanceof ResizeObserver? callbackOrObserver : new ResizeObserver(entries => {
var contentRect = entries[entries.length - 1].contentRect;

if (previousRect
&& previousRect.width == contentRect.width
&& previousRect.height == contentRect.height) {
return;
}

callbackOrObserver(entries);

previousRect = contentRect;
});

ro.observe(element);

return ro;
},

Observer: class Observer {
constructor (element, attribute, callback, o = {}) {
if (callback instanceof MutationObserver) {
Expand Down