nakarte

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

index.js (3461B)


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