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 sorting on mobile and with hidden layers #344

Merged
merged 38 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fd4758a
fix: fix double click on draw; close #256;
A-Behairi Oct 18, 2023
e1fe90d
fix: text on styled icons on safari
A-Behairi Oct 19, 2023
f9e61b2
Merge remote-tracking branch 'origin/main' into layercontrol/fix/fix-…
A-Behairi Oct 19, 2023
438b5da
fix: hover effect on icons
A-Behairi Oct 19, 2023
ef7cb52
Merge branch 'main' into layercontrol/fix/mobile..
A-Behairi Oct 19, 2023
d7f2877
fix: layercontrol mobile sorting
A-Behairi Oct 19, 2023
83ad0c2
Merge branch 'layercontrol/fix/fix-tool-button-hover-highlight' into …
A-Behairi Oct 19, 2023
48ebba9
chore: clean up
A-Behairi Oct 19, 2023
a9cef85
fix: layers sort
A-Behairi Oct 23, 2023
ed5a9c7
chore: fix format
A-Behairi Oct 23, 2023
b468ede
Merge branch 'main' into layercontrol/fix/mobile-sorting
silvester-pari Oct 30, 2023
a335c2a
fix: layercontrol tests
A-Behairi Oct 30, 2023
1880b3d
chore: fix formatting
A-Behairi Oct 30, 2023
6781e95
fix: update layers list after layers length change
A-Behairi Oct 30, 2023
87df123
chore: add style override property
A-Behairi Nov 2, 2023
761f0ad
Merge remote-tracking branch 'origin/main' into layercontrol/fix/mobi…
A-Behairi Nov 6, 2023
36ed4a0
fix: map reassignment
A-Behairi Nov 6, 2023
3e64ece
chore: update mockLayer
A-Behairi Nov 6, 2023
249613c
chore: layer unlisten on update
A-Behairi Nov 6, 2023
6b463fe
chore: format
A-Behairi Nov 6, 2023
b9fc8b6
Merge branch 'main' into layercontrol/fix/mobile-sorting
A-Behairi Nov 14, 2023
51aa73f
Merge branch 'main' into layercontrol/fix/mobile-sorting
A-Behairi Nov 23, 2023
086e910
fix: nodes sort manipulation
A-Behairi Nov 23, 2023
42a6bd8
fix: list rerender and update
A-Behairi Nov 27, 2023
870cfb8
fix: sort layers containing optional flag
A-Behairi Nov 27, 2023
0259b69
Revert "fix: sort layers containing optional flag"
A-Behairi Nov 27, 2023
b938862
Merge branch 'main' into layercontrol/fix/mobile-sorting
A-Behairi Nov 28, 2023
9bb88c4
Merge branch 'main' into layercontrol/fix/mobile-sorting
A-Behairi Nov 28, 2023
b10b7eb
fix: sort layers containing optional layers
A-Behairi Nov 28, 2023
aa0c592
fix: added opt layer to the top
A-Behairi Nov 28, 2023
e4021bd
fix: added functions to mockCollection
A-Behairi Nov 28, 2023
d13c054
chore: fomat
A-Behairi Nov 28, 2023
3e5bda3
fix: reordering optional & hidden layers
A-Behairi Nov 28, 2023
ac4ab75
fix: loop through layers and remove/add via index
silvester-pari Nov 29, 2023
7105869
Merge pull request #477 from EOX-A/layercontrol/fix/mobile-sorting-2
silvester-pari Nov 29, 2023
c94170a
Merge branch 'main' into layercontrol/fix/mobile-sorting
silvester-pari Nov 29, 2023
1ac40b2
chore(deps): add lodash debounce package
silvester-pari Nov 29, 2023
603dd94
chore: remove code not needed with new approach
silvester-pari Nov 29, 2023
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
3 changes: 2 additions & 1 deletion elements/layercontrol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"dayjs": "^1.11.8",
"lit": "^3.0.2"
"lit": "^3.0.2",
"lodash.debounce": "^4.0.8"
}
}
122 changes: 65 additions & 57 deletions elements/layercontrol/src/components/layerList.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LitElement, html } from "lit";
import { when } from "lit/directives/when.js";
import { keyed } from "lit/directives/keyed.js";
import { repeat } from "lit/directives/repeat.js";
import { createSortable, getLayerType } from "../helpers";
import "./layer";
import "./layerGroup";

import _debounce from "lodash.debounce";
/**
* Display of a list of layers
*
Expand Down Expand Up @@ -70,15 +70,28 @@ export class EOxLayerControlLayerList extends LitElement {
this.noShadow = true;
}

#handleLayersChangeLength = () => {
this.#debHandleLayersChangeLength();
};
#debHandleLayersChangeLength = _debounce(() => {
this.requestUpdate();
this.dispatchEvent(new CustomEvent("changed", { bubbles: true }));
}, 50);

firstUpdated() {
if (this.layers) {
createSortable(this.renderRoot.querySelector("ul"), this.layers, this);
}
}

updated() {
if (!this.layers) {
return;
}
this.layers.on("change:length", () => {
this.requestUpdate();
this.dispatchEvent(new CustomEvent("changed", { bubbles: true }));
});
createSortable(this.renderRoot.querySelector("ul"), this.layers, this);
if (this.layers.hasListener("change:length")) {
this.layers?.un("change:length", this.#handleLayersChangeLength);
}
this.layers.on("change:length", this.#handleLayersChangeLength);
}

createRenderRoot() {
Expand All @@ -95,57 +108,52 @@ export class EOxLayerControlLayerList extends LitElement {
${when(
this.layers,
() => html`
${this.layers
.getArray()
.filter(
(l) =>
!l.get("layerControlHide") && !l.get("layerControlOptional")
)
.reverse()
.map((layer) =>
keyed(
layer.get(this.idProperty),
html`
<li
data-layer="${layer.get(this.idProperty)}"
data-type="${getLayerType(layer, this.map)}"
>
${
/** @type {import("ol/layer").Group} */ (layer)
.getLayers
? html`
<eox-layercontrol-layer-group
.noShadow=${true}
.group=${layer}
.idProperty=${this.idProperty}
.map=${this.map}
.titleProperty=${this.titleProperty}
.showLayerZoomState=${this.showLayerZoomState}
.tools=${this.tools}
.unstyled=${this.unstyled}
@changed=${() => this.requestUpdate()}
>
</eox-layercontrol-layer-group>
`
: html`
<eox-layercontrol-layer
.noShadow=${true}
.layer=${layer}
.map=${this.map}
.titleProperty=${this.titleProperty}
.showLayerZoomState=${this.showLayerZoomState}
.tools=${this.tools}
.unstyled=${this.unstyled}
@changed=${() => {
this.requestUpdate();
}}
></eox-layercontrol-layer>
`
}
</li>
`
${repeat(
this.layers
.getArray()
.filter(
(l) =>
!l.get("layerControlHide") && !l.get("layerControlOptional")
)
)}
.reverse(),
(layer) => layer,
(layer) => html`
<li
data-layer="${layer.get(this.idProperty)}"
data-type="${getLayerType(layer, this.map)}"
>
${
/** @type {import("ol/layer").Group} */ (layer).getLayers
? html`
<eox-layercontrol-layer-group
.noShadow=${true}
.group=${layer}
.idProperty=${this.idProperty}
.map=${this.map}
.titleProperty=${this.titleProperty}
.showLayerZoomState=${this.showLayerZoomState}
.tools=${this.tools}
.unstyled=${this.unstyled}
@changed=${() => this.requestUpdate()}
>
</eox-layercontrol-layer-group>
`
: html`
<eox-layercontrol-layer
.noShadow=${true}
.layer=${layer}
.map=${this.map}
.titleProperty=${this.titleProperty}
.showLayerZoomState=${this.showLayerZoomState}
.tools=${this.tools}
.unstyled=${this.unstyled}
@changed=${() => this.requestUpdate()}
></eox-layercontrol-layer>
`
}
</li>
`
)}
`
)}
</ul>
Expand Down
35 changes: 30 additions & 5 deletions elements/layercontrol/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import Sortable from "sortablejs";
/**
*
* @param {HTMLElement} element
* @param {import("ol").Collection<import("ol/layer").Layer | import("ol/layer").Group>} layers
* @param {import("ol").Collection<import("ol/layer").Layer | import("ol/layer").Group>} collection
* @param {import("lit").LitElement} that
*/
export const createSortable = (element, layers, that) => {
export const createSortable = (element, collection, that) => {
/**
* @type {any[]}
*/
Expand Down Expand Up @@ -41,7 +41,8 @@ export const createSortable = (element, layers, that) => {
if (e.oldIndex == e.newIndex) return;
// Then move the element using your own logic.
// automatically dispatches "sort" event
const layer = layers.getArray().find(
const layers = collection.getArray();
const layer = layers.find(
(l) =>
// @ts-ignore
l.ol_uid ===
Expand All @@ -50,8 +51,32 @@ export const createSortable = (element, layers, that) => {
// @ts-ignore
).layer.ol_uid
);
layers.remove(layer);
layers.insertAt(layers.getLength() - e.newIndex, layer);
const numberOfHiddenLayers = layers.filter(
(l) => l.get("layerControlHide") || l.get("layerControlOptional")
).length;
const target =
layers[layers.length - 1 - e.newIndex - numberOfHiddenLayers];
let draggedIndex;
let dropIndex;
// remove dragged layer from collection
for (
draggedIndex = layers.length - 1;
draggedIndex > -1;
draggedIndex--
) {
if (layers[draggedIndex] == layer) {
collection.removeAt(draggedIndex);
break;
}
}
// re-add dragged layer at position of layer that has beend dropped on
for (dropIndex = layers.length - 1; dropIndex > -1; dropIndex--) {
if (layers[dropIndex] === target) {
if (draggedIndex > dropIndex) collection.insertAt(dropIndex, layer);
else collection.insertAt(dropIndex + 1, layer);
break;
}
}
that.requestUpdate();
},
});
Expand Down
2 changes: 1 addition & 1 deletion elements/layercontrol/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class EOxLayerControl extends LitElement {
* @type Element & { map: import("ol").Map }
*/
const foundElement = document.querySelector(this.for);
if (foundElement) {
if (foundElement && foundElement?.map !== this.map) {
this.map = foundElement.map;
}
}
Expand Down
42 changes: 42 additions & 0 deletions elements/layercontrol/test/_mockMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class MockLayer {
this.visible = visible;
}
visible = true;
events = {
["change"]: () => undefined,
};
on = (event, fun) => (this.events = { [event]: fun });
un = (event, fun) => {
if (this.events[event] == fun) {
delete this.events[event];
}
};
}

class MockCollection {
Expand All @@ -58,6 +67,12 @@ class MockCollection {
};
layers = [];
on = (event, fun) => (this.events = { [event]: fun });
un = (event, fun) => {
if (this.events[event] == fun) {
delete this.events[event];
}
};
hasListener = (event) => event in this.events;
pop() {
this.layers.pop();
this.events["change:length"]();
Expand All @@ -66,6 +81,33 @@ class MockCollection {
this.layers.push(new MockLayer(newLayer));
this.events["change:length"]();
}
remove(layer) {
layer = new MockLayer(layer);
const i = this.layers.indexOf(layer);
if (i) {
this.layers.splice(i, 1);
return layer;
} else {
return undefined;
}
}
removeAt(index) {
this.layers = [...this.layers.slice(index)];
}
insertAt(index, layer) {
layer = new MockLayer(layer);
this.layers = [
...this.layers.slice(0, index),
layer,
...this.layers.slice(index),
];
}
getLength() {
return this.layers.length;
}
forEach(func) {
this.layers.forEach(func);
}
}

export class MockMap extends HTMLElement {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading