Code coverage report for lib/mixins/element.js

Statements: 17.07% (7 / 41)      Branches: 12.5% (3 / 24)      Functions: 7.14% (1 / 14)      Lines: 15.38% (6 / 39)      Ignored: none     

All files » lib/mixins/ » element.js
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    3   1   1   1   1   1                                                                                                                                        
"use strict";
 
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };
 
var clone = _interopRequire(require("lodash/lang/clone"));
 
var forEach = _interopRequire(require("lodash/collection/forEach"));
 
var reduce = _interopRequire(require("lodash/collection/reduce"));
 
var EVENTS_RE = /on(?:Leaflet)?(.+)/i;
 
module.exports = {
  getLeafletElement: function getLeafletElement() {
    return this._leafletElement;
  },
 
  extractEvents: function extractEvents(props) {
    return reduce(props, function (res, cb, ev) {
      if (EVENTS_RE.test(ev)) {
        var key = ev.replace(EVENTS_RE, function (match, p) {
          return p.toLowerCase();
        });
        res[key] = cb;
      }
      return res;
    }, {});
  },
 
  bindEvents: function bindEvents() {
    var next = arguments[0] === undefined ? {} : arguments[0];
    var prev = arguments[1] === undefined ? {} : arguments[1];
 
    var el = this.getLeafletElement();
    if (!el) {
      return;
    }var diff = clone(prev);
    forEach(prev, function (cb, ev) {
      if (!next[ev] || cb !== next[ev]) {
        delete diff[ev];
        el.off(ev, cb);
      }
    });
 
    forEach(next, function (cb, ev) {
      if (!prev[ev] || cb !== prev[ev]) {
        diff[ev] = cb;
        el.on(ev, cb);
      }
    });
 
    return diff;
  },
 
  fireEvent: function fireEvent(type, data) {
    var el = this.getLeafletElement();
    if (el) el.fire(type, data);
  },
 
  componentWillMount: function componentWillMount() {
    this._leafletEvents = this.extractEvents(this.props);
  },
 
  componentDidMount: function componentDidMount() {
    this.bindEvents(this._leafletEvents);
  },
 
  componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
    var next = this.extractEvents(nextProps);
    this._leafletEvents = this.bindEvents(next, this._leafletEvents);
  },
 
  componentWillUnmount: function componentWillUnmount() {
    var el = this.getLeafletElement();
    if (!el) {
      return;
    }forEach(this._leafletEvents, function (cb, ev) {
      el.off(ev, cb);
    });
  }
};