2018-04-03 22:56:12 +00:00
|
|
|
/* global Backbone: false */
|
2018-04-15 03:27:38 +00:00
|
|
|
/* global i18n: false */
|
|
|
|
/* global React: false */
|
|
|
|
/* global ReactDOM: false */
|
2018-04-03 22:56:12 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
2018-04-03 22:56:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2018-04-11 16:30:28 +00:00
|
|
|
window.Whisper.ReactWrapperView = Backbone.View.extend({
|
2018-04-03 22:56:12 +00:00
|
|
|
className: 'react-wrapper',
|
|
|
|
initialize(options) {
|
2019-01-15 03:20:45 +00:00
|
|
|
const {
|
|
|
|
Component,
|
|
|
|
props,
|
|
|
|
onClose,
|
|
|
|
tagName,
|
|
|
|
className,
|
|
|
|
onInitialRender,
|
|
|
|
elCallback,
|
|
|
|
} = options;
|
2018-04-03 22:56:12 +00:00
|
|
|
this.render();
|
2019-01-15 03:20:45 +00:00
|
|
|
if (elCallback) {
|
|
|
|
elCallback(this.el);
|
|
|
|
}
|
2018-04-03 22:56:12 +00:00
|
|
|
|
2019-01-15 03:20:45 +00:00
|
|
|
this.tagName = tagName;
|
|
|
|
this.className = className;
|
2018-04-03 22:56:12 +00:00
|
|
|
this.Component = Component;
|
|
|
|
this.onClose = onClose;
|
2019-01-15 03:20:45 +00:00
|
|
|
this.onInitialRender = onInitialRender;
|
2018-04-03 22:56:12 +00:00
|
|
|
|
|
|
|
this.update(props);
|
2019-01-15 03:20:45 +00:00
|
|
|
|
|
|
|
this.hasRendered = false;
|
2018-04-03 22:56:12 +00:00
|
|
|
},
|
|
|
|
update(props) {
|
|
|
|
const updatedProps = this.augmentProps(props);
|
2018-04-15 03:27:38 +00:00
|
|
|
const reactElement = React.createElement(this.Component, updatedProps);
|
2019-01-15 03:20:45 +00:00
|
|
|
ReactDOM.render(reactElement, this.el, () => {
|
|
|
|
if (this.hasRendered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hasRendered = true;
|
|
|
|
if (this.onInitialRender) {
|
|
|
|
this.onInitialRender();
|
|
|
|
}
|
|
|
|
});
|
2018-04-03 22:56:12 +00:00
|
|
|
},
|
|
|
|
augmentProps(props) {
|
|
|
|
return Object.assign({}, props, {
|
|
|
|
close: () => {
|
|
|
|
if (this.onClose) {
|
|
|
|
this.onClose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.remove();
|
|
|
|
},
|
2018-04-15 03:27:38 +00:00
|
|
|
i18n,
|
2018-04-03 22:56:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
remove() {
|
2018-04-15 03:27:38 +00:00
|
|
|
ReactDOM.unmountComponentAtNode(this.el);
|
2018-04-03 22:56:12 +00:00
|
|
|
Backbone.View.prototype.remove.call(this);
|
|
|
|
},
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
})();
|