rc-tile-map
Version:
194 lines (172 loc) • 7.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports['default'] = exports.EVENTS_RE = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _lodash = require('lodash');
var _react = require('react');
var _reactDom = require('react-dom');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var EVENTS_RE = exports.EVENTS_RE = /^on(.+)$/i;
var MapComponent = function (_Component) {
_inherits(MapComponent, _Component);
function MapComponent(props, context) {
_classCallCheck(this, MapComponent);
var _this = _possibleConstructorReturn(this, (MapComponent.__proto__ || Object.getPrototypeOf(MapComponent)).call(this, props, context));
_this._mapEvents = {};
return _this;
}
_createClass(MapComponent, [{
key: 'componentWillMount',
value: function componentWillMount() {
this._mapEvents = this.extractEvents(this.props);
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
this.bindEvents(this._mapEvents, {});
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var next = this.extractEvents(nextProps);
this._mapEvents = this.bindEvents(next, this._mapEvents);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var el = this.componentInstance;
if (!el) return;
(0, _lodash.forEach)(this._mapEvents, function (cb, ev) {
el.removeEventListener(ev, cb);
});
}
}, {
key: 'extractEvents',
value: function extractEvents(props) {
return (0, _lodash.reduce)((0, _lodash.keys)(props), function (res, prop) {
if (EVENTS_RE.test(prop)) {
var key = prop.replace(EVENTS_RE, function (match, p) {
return p.substr(0, 1).toLowerCase() + p.substr(1, p.length - 1);
});
if (props[prop] != null) {
res[key] = props[prop];
}
}
return res;
}, {});
}
}, {
key: 'bindEvents',
value: function bindEvents(next, prev) {
var el = this.componentInstance;
if (el == null || el.addEventListener == null) return {};
var diff = (0, _lodash.clone)(prev);
(0, _lodash.forEach)(prev, function (cb, ev) {
if (!next[ev] || cb !== next[ev]) {
delete diff[ev];
el.removeEventListener(ev, cb);
}
});
(0, _lodash.forEach)(next, function (cb, ev) {
if (!prev[ev] || cb !== prev[ev]) {
diff[ev] = cb;
el.addEventListener(ev, cb);
}
});
return diff;
}
}, {
key: 'getElementsByClassName',
value: function getElementsByClassName(dom, className) {
if (!dom.getElementsByTagName) {
return dom;
}
// 解决IE8之类不支持getElementsByClassName
if (!dom.getElementsByClassName) {
var children = dom.getElementsByTagName('*');
var elements = [];
for (var i = 0; i < children.length; i++) {
var child = children[i];
var classNames = child.className.split(' ');
for (var j = 0; j < classNames.length; j++) {
if (classNames[j] === className) {
elements.push(child);
break;
}
}
}
return elements;
} else {
return dom.getElementsByClassName(className);
}
}
}, {
key: 'fireEvent',
value: function fireEvent(type, data) {
var el = this.componentInstance;
if (el) el.dispatchEvent(type, data);
}
}, {
key: 'getOptions',
value: function getOptions(props) {
var pane = props.pane == null ? this.context.pane : props.pane;
return pane ? _extends({}, props, { pane: pane }) : props;
}
}, {
key: 'getHtmlDomByReactDom',
value: function getHtmlDomByReactDom(reactDom) {
if ((0, _lodash.isString)(reactDom)) {
return reactDom;
} else {
var section = document.createElement('section');
(0, _reactDom.render)(reactDom, section);
return section;
}
}
}, {
key: 'bindContentEvents',
value: function bindContentEvents(contentEvents, dom, markerInstance, componentInstance) {
var _this2 = this;
if (contentEvents) {
(0, _lodash.forEach)(contentEvents, function (evtFun, evtName) {
var domNow = evtName.indexOf('.') > 0 ? _this2.getElementsByClassName(dom, evtName.split('.')[0])[0] : dom;
var evtNameNow = evtName.indexOf('.') > 0 ? evtName.split('.')[1] : evtName;
if (domNow.addEventListener) {
domNow.addEventListener(evtNameNow, function (evt) {
evtFun(evt, markerInstance, _this2.componentInstance);
});
} else if (domNow.attachEvent) {
domNow.attachEvent('on' + evtNameNow, function (evt) {
evtFun(evt, markerInstance, _this2.componentInstance);
});
}
});
}
}
}, {
key: 'updatePropsBySetFun',
value: function updatePropsBySetFun(funcName, fromProp, toProp) {
if (!(0, _lodash.isEqual)(fromProp, toProp)) {
return this.componentInstance[funcName](toProp);
}
}
}, {
key: 'updatePropsByBoolFun',
value: function updatePropsByBoolFun(funcNameTrue, funcNameFalse, fromProp, toProp) {
if (fromProp !== toProp) {
if (toProp === true) {
this.componentInstance[funcNameTrue]();
} else {
this.componentInstance[funcNameFalse]();
}
}
}
}]);
return MapComponent;
}(_react.Component);
exports['default'] = MapComponent;