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,
|
2019-01-14 21:49:58 +00:00
|
|
|
JSX,
|
2019-01-15 03:20:45 +00:00
|
|
|
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;
|
2019-01-14 21:49:58 +00:00
|
|
|
this.JSX = JSX;
|
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
|
|
|
},
|
2019-05-16 22:32:11 +00:00
|
|
|
update(props, cb) {
|
2018-04-03 22:56:12 +00:00
|
|
|
const updatedProps = this.augmentProps(props);
|
2019-01-14 21:49:58 +00:00
|
|
|
const reactElement = this.JSX
|
|
|
|
? this.JSX
|
|
|
|
: React.createElement(this.Component, updatedProps);
|
2019-01-15 03:20:45 +00:00
|
|
|
ReactDOM.render(reactElement, this.el, () => {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (cb) {
|
|
|
|
try {
|
|
|
|
cb();
|
|
|
|
} catch (error) {
|
|
|
|
window.log.error(
|
|
|
|
'ReactWrapperView.update error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:20:45 +00:00
|
|
|
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: () => {
|
|
|
|
this.remove();
|
|
|
|
},
|
2018-04-15 03:27:38 +00:00
|
|
|
i18n,
|
2018-04-03 22:56:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
remove() {
|
2019-02-21 22:15:43 +00:00
|
|
|
if (this.onClose) {
|
|
|
|
this.onClose();
|
|
|
|
}
|
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
|
|
|
})();
|