all files / src/olHelpers/ layerSwipe.ts

15.28% Statements 11/72
0% Branches 0/16
12.5% Functions 2/16
15.49% Lines 11/71
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                                                                                                                                                                                                                                                           
/**
 * Created by gavorhes on 6/1/2016.
 */
 
 
import provide from '../util/provide';
import {LayerBase} from "../layers/LayerBase";
import ol = require('custom-ol');
import $ = require('jquery');
 
let nm = provide('collections.layerSwipe');
 
 
class LayerSwipe {
    leftLayers: Array<LayerBase>;
    rightLayers: Array<LayerBase>;
    _percentRight: number;
    _map: ol.Map;
    $mapElement: JQuery;
    $swiper: JQuery;
    dragging: boolean;
    offset: number;
    /**
     *
     * @param {ol.Map} map - the map
     * @param {string} [sliderContent=''] - additional html to be added inside the slider div
     */
    constructor(map: ol.Map, sliderContent: string = '') {
 
        sliderContent = sliderContent || '';
        /**
         *
         * @type {Array<LayerBase>}
         */
        this.leftLayers = [];
 
        /**
         *
         * @type {Array<LayerBase>}
         */
        this.rightLayers = [];
 
        this._percentRight = 50;
        this.offset = null;
 
        this._map = map;
        this.$mapElement = $(map.getTargetElement());
        this.$mapElement.append(`<div class="layer-swiper">${sliderContent}</div>`);
 
 
        this.$swiper = this.$mapElement.find('.layer-swiper');
        this.percentRight = this.percentRight;
 
        this.dragging = false;
 
        this.$mapElement.mouseleave(() => {
            this.dragging = false;
        });
 
        this.$swiper.bind('mousewheel DOMMouseScroll', function(evt){
            evt.preventDefault();
        });
 
        this.$swiper.mousedown((evt) => {
            this.dragging = true;
            this.offset = evt.offsetX;
        });
 
        $(window).mouseup(() => {
            this.dragging = false;
        });
 
        this.$mapElement.mousemove((evt) => {
            if (this.dragging) {
                let mapLeft = this.$mapElement.position().left;
                let mapWidth = this.$mapElement.width();
 
                this.percentRight = 100 * (evt.pageX - this.offset - mapLeft) / mapWidth;
            }
        });
    }
 
    /**
     *
     * @param {LayerBase|*} lyr - layer to be added to left side
     */
    addLeftLayer(lyr) {
 
        if (this.leftLayers.indexOf(lyr) != -1){
            return;
        }
 
        lyr.olLayer.on('precompose', (event) => {
            let ctx = event['context'];
            let width = ctx.canvas.width * (this.percentRight / 100);
 
            ctx.save();
            ctx.beginPath();
            ctx.rect(0, 0, width, ctx.canvas.height);
            ctx.clip();
        });
 
        lyr.olLayer.on('postcompose', function (event) {
            let ctx = event['context'];
            ctx.restore();
        });
 
 
        this.leftLayers.push(lyr);
    }
 
    /**
     *
     * @param {LayerBase|*} lyr - layer to be added to right side
     */
    addRightLayer(lyr) {
 
        if (this.rightLayers.indexOf(lyr) != -1){
            return;
        }
 
        lyr.olLayer.on('precompose', (event) => {
            let ctx = event['context'];
            let width = ctx.canvas.width * (this.percentRight / 100);
 
            ctx.save();
            ctx.beginPath();
            ctx.rect(width, 0, ctx.canvas.width - width, ctx.canvas.height);
            ctx.clip();
        });
 
        lyr.olLayer.on('postcompose', function (event) {
            let ctx = event['context'];
            ctx.restore();
        });
 
        this.rightLayers.push(lyr);
    }
 
    get percentRight() : number{
        return this._percentRight;
    }
 
    set percentRight(percent: number) {
        let maxed = this.$swiper.position().left + this.$swiper.width() > this.$mapElement.width();
 
        if (percent < 0) {
            return;
        } else if (maxed && percent > this.percentRight) {
            return;
        }
 
        this._percentRight = percent;
        this.$swiper.css('left', `${this._percentRight.toFixed(2)}%`);
        this._map.render();
    }
}
 
nm.LayerSwipe = LayerSwipe;
export default LayerSwipe;