nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

index.js (3462B)


      1 import L from 'leaflet';
      2 import './style.css';
      3 
      4 function getLayerHotkey(layer) {
      5     if (!layer) {
      6         return null;
      7     }
      8     if (typeof layer.hotkey !== 'undefined') {
      9         return layer.hotkey;
     10     }
     11     if (!layer.options) {
     12         return null;
     13     }
     14     if (layer.options.hotkey) {
     15         return layer.options.hotkey;
     16     }
     17     if (layer.options.code?.length === 1) {
     18         return layer.options.code;
     19     }
     20     return null;
     21 }
     22 
     23 function extendLayerName(name, layer) {
     24     if (layer.options) {
     25         const hotkey = getLayerHotkey(layer);
     26         if (hotkey) {
     27             name += `<span class="layers-control-hotkey">${hotkey}</span>`;
     28         }
     29     }
     30     return name;
     31 }
     32 
     33 function enableHotkeys(control) {
     34     if (control._hotkeysEnabled) {
     35         return control;
     36     }
     37 
     38     const originalOnAdd = control.onAdd;
     39     const originalOnRemove = control.onRemove;
     40     const originalAddItem = control._addItem;
     41 
     42     L.Util.extend(control, {
     43             _hotkeysEnabled: true,
     44 
     45             _addItem: function(obj) {
     46                 obj = L.Util.extend({}, obj);
     47                 obj.name = extendLayerName(obj.name, obj.layer);
     48                 return originalAddItem.call(this, obj);
     49             },
     50 
     51             onAdd: function(map) {
     52                 var result = originalOnAdd.call(this, map);
     53                 this._addHotkeyEvents();
     54                 return result;
     55             },
     56 
     57             onRemove: function(map) {
     58                 L.DomEvent.off(document, 'keyup', this._onHotkeyUp, this);
     59                 L.DomEvent.off(document, 'keydown', this.onKeyDown, this);
     60                 originalOnRemove.call(this, map);
     61             },
     62 
     63             _addHotkeyEvents: function() {
     64                 L.DomEvent.on(document, 'keyup', this._onHotkeyUp, this);
     65                 L.DomEvent.on(document, 'keydown', this.onKeyDown, this);
     66             },
     67 
     68             onKeyDown: function(e) {
     69                 if (e.altKey || e.ctrlKey || e.shiftKey) {
     70                     return;
     71                 }
     72                 this._keyDown = e.keyCode;
     73             },
     74 
     75             _onHotkeyUp: function(e) {
     76                 const pressedKey = this._keyDown;
     77                 this._keyDown = null;
     78                 const targetTag = e.target.tagName.toLowerCase();
     79                 if (
     80                     (targetTag === 'input' && ['text', 'search'].includes(e.target.type)) ||
     81                     targetTag === 'textarea' ||
     82                     pressedKey !== e.keyCode
     83                 ) {
     84                     return;
     85                 }
     86                 const key = String.fromCharCode(e.keyCode);
     87                 for (let layer of this._layers) {
     88                     let layerId = L.stamp(layer.layer);
     89                     const layerHotkey = getLayerHotkey(layer.layer);
     90                     if (layerHotkey === key) {
     91                         const inputs = this._form.getElementsByTagName('input');
     92                         for (let input of [...inputs]) {
     93                             if (input.layerId === layerId) {
     94                                 input.click();
     95                                 break;
     96                             }
     97                         }
     98                         break;
     99                     }
    100                 }
    101             }
    102         }
    103     );
    104     for (let layer of control._layers) {
    105         layer.name = extendLayerName(layer.name, layer.layer);
    106     }
    107     control._addHotkeyEvents();
    108     control._update();
    109     return control;
    110 }
    111 
    112 export default enableHotkeys;
    113