diff --git a/contribs/gmf/src/layertree/common.scss b/contribs/gmf/src/layertree/common.scss index 9605624b7c3..5074d62da99 100644 --- a/contribs/gmf/src/layertree/common.scss +++ b/contribs/gmf/src/layertree/common.scss @@ -194,6 +194,6 @@ } } .gmf-layer-being-swipe { - margin: 0 15px; + margin-left: 0.25rem; } } diff --git a/contribs/gmf/src/layertree/component.html b/contribs/gmf/src/layertree/component.html index 41112b4705c..d81e374da0d 100644 --- a/contribs/gmf/src/layertree/component.html +++ b/contribs/gmf/src/layertree/component.html @@ -79,7 +79,7 @@ -
- + +

This example shows how to use the ngeo-Mapswipe component to insert an input type range and swipe a map.

diff --git a/package.json b/package.json index 795535455c3..56d58e0ca6d 100644 --- a/package.json +++ b/package.json @@ -119,6 +119,7 @@ "popper.js": "~1.15.0", "proj4": "~2.5.0", "puppeteer": "~1.20.0", + "resize-observer-polyfill": "~1.5.1", "simple-html-tokenizer": "~0.5.7", "sinon": "~7.5.0", "svg-inline-loader": "~0.8.0", diff --git a/src/map/swipe.html b/src/map/swipe.html index b00f053f5aa..f5985bf9036 100644 --- a/src/map/swipe.html +++ b/src/map/swipe.html @@ -1,4 +1,4 @@ -
- - +
+
+
diff --git a/src/map/swipe.js b/src/map/swipe.js index 03014b62551..be892337dd6 100644 --- a/src/map/swipe.js +++ b/src/map/swipe.js @@ -1,6 +1,11 @@ import angular from 'angular'; import {listen, unlistenByKey} from 'ol/events.js'; import RenderEvent from 'ol/render/Event.js'; + +import ResizeObserver from 'resize-observer-polyfill'; +import 'jquery-ui/ui/widgets/draggable.js'; + + /** * @type {angular.IModule} * @hidden @@ -77,25 +82,26 @@ class SwipeController { /** * @type {number} */ - this.swipeValue = 50; + this.swipeValue = 0.5; /** * @type {JQuery} * @private */ - this.swipeInput_ = $element.find('.swipe-input'); + this.draggableElement_ = $element.find('.ngeo-swipe-line-draggable'); /** - * @type {JQuery} + * @type {import("ol/events.js").EventsKey[]} * @private */ - this.swipeOutput_ = $element.find('.swipe-output'); + this.layerKeys_ = []; /** - * @type {import("ol/events.js").EventsKey[]} + * @type {ResizeObserver} * @private */ - this.layerKeys_ = []; + this.resizeObserver_; + } /** @@ -105,17 +111,27 @@ class SwipeController { this.layerKeys_.push(listen(this.layer, 'prerender', this.handleLayerPrerender_, this)); this.layerKeys_.push(listen(this.layer, 'postrender', this.handleLayerPostrender_, this)); - this.swipeOutput_.css({ - 'left': `${this.swipeValue}%` + const halfDraggableWidth = this.draggableElement_.width() / 2; + + this.draggableElement_.draggable({ + axis: 'x', + containment: 'parent', + drag: () => { + const parentWidth = this.draggableElement_.parent().width(); + const position = this.draggableElement_.position().left + halfDraggableWidth; + + this.swipeValue = position / parentWidth; + + this.map.render(); + } }); - this.swipeInput_.on('input change', event => { - this.swipeValue = Number($(event.target).val()); - this.map.render(); - this.swipeOutput_.css({ - 'left': `${this.swipeValue}%` - }); + // keep the same percentage when the parent is resized + this.resizeObserver_ = new ResizeObserver(() => { + const parentWidth = this.draggableElement_.parent().width(); + this.draggableElement_.css('left', (parentWidth * this.swipeValue) - halfDraggableWidth); }); + this.resizeObserver_.observe(this.draggableElement_.parent().get(0)); } /** @@ -130,7 +146,7 @@ class SwipeController { if (!ctx) { return; } - const width = ctx.canvas.width * (this.swipeValue / 100); + const width = ctx.canvas.width * this.swipeValue; ctx.save(); ctx.beginPath(); ctx.rect(0, 0, width, ctx.canvas.height); @@ -154,7 +170,8 @@ class SwipeController { $onDestroy() { this.layerKeys_.forEach(unlistenByKey); this.layerKeys_.length = 0; - this.swipeInput_.off(); + this.draggableElement_.draggable('destroy'); + this.resizeObserver_.disconnect(); } } diff --git a/tsconfig.json b/tsconfig.json index 2d3df2e1ebe..c1c99728c01 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,6 +23,7 @@ "moment": ["node_modules/moment/moment.d.ts"], "jsts/*": ["node_modules/@types/jsts/index.d.ts"], "localforage/*": ["node_modules/localforage/*"], + "resize-observer-polyfill": ["node_modules/resize-observer-polyfill/src/index.d.ts"], } }, "include": [