2018-01-16 21:14:32 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
import React from 'react';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import Widgets from '../../../utils/widgets';
|
|
|
|
import AppTile from '../elements/AppTile';
|
|
|
|
import ContextualMenu from '../../structures/ContextualMenu';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2018-01-17 03:04:06 +03:00
|
|
|
import Modal from '../../../Modal';
|
2018-01-16 21:14:32 +03:00
|
|
|
import sdk from '../../../index';
|
2018-01-17 03:04:06 +03:00
|
|
|
import SdkConfig from '../../../SdkConfig';
|
|
|
|
import ScalarAuthClient from '../../../ScalarAuthClient';
|
2018-02-07 12:23:00 +03:00
|
|
|
import dis from '../../../dispatcher';
|
2018-01-16 21:14:32 +03:00
|
|
|
|
|
|
|
export default class Stickerpack extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.onShowStickersClick = this.onShowStickersClick.bind(this);
|
|
|
|
this.onHideStickersClick = this.onHideStickersClick.bind(this);
|
2018-01-17 00:28:15 +03:00
|
|
|
this.onFinished = this.onFinished.bind(this);
|
2018-01-17 03:04:06 +03:00
|
|
|
this._launchManageIntegrations = this._launchManageIntegrations.bind(this);
|
2018-02-05 14:49:44 +03:00
|
|
|
this._removeStickerpackWidgets = this._removeStickerpackWidgets.bind(this);
|
2018-02-07 12:23:00 +03:00
|
|
|
this._onWidgetAction = this._onWidgetAction.bind(this);
|
2018-01-16 21:14:32 +03:00
|
|
|
|
2018-01-17 00:06:57 +03:00
|
|
|
this.defaultStickersContent = (
|
2018-01-17 18:49:36 +03:00
|
|
|
<div className='mx_Stickers_contentPlaceholder'>
|
2018-02-07 12:23:00 +03:00
|
|
|
<p>{ _t("You don't currently have any stickerpacks enabled") }</p>
|
2018-02-22 18:20:51 +03:00
|
|
|
<p>{ _t("Click") } <span className='mx_Stickers_addLink' onClick={this._launchManageIntegrations} > { _t("here") }</span> { _t("to add some!") }</p>
|
2018-02-05 14:49:44 +03:00
|
|
|
<img src='img/stickerpack-placeholder.png' alt={_t('Add a stickerpack')} />
|
2018-01-17 00:06:57 +03:00
|
|
|
</div>
|
|
|
|
);
|
2018-01-16 21:14:32 +03:00
|
|
|
this.popoverWidth = 300;
|
|
|
|
this.popoverHeight = 300;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
stickersContent: this.defaultStickersContent,
|
|
|
|
showStickers: false,
|
2018-02-23 13:20:16 +03:00
|
|
|
imError: null,
|
2018-01-16 21:14:32 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:49:44 +03:00
|
|
|
_removeStickerpackWidgets() {
|
|
|
|
console.warn('Removing stickerpack widgets');
|
2018-02-24 03:10:28 +03:00
|
|
|
if (this.widgetId) {
|
|
|
|
this.scalarClient.disableWidgetAssets('stickerpack', this.widgetId).then(() => {
|
|
|
|
console.warn('Assets disabled');
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Failed to disable assets');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.warn('No widget ID specified, not disabling assets');
|
|
|
|
}
|
2018-02-07 12:23:00 +03:00
|
|
|
Widgets.removeStickerpackWidgets();
|
|
|
|
this._getStickerPickerWidget();
|
2018-02-23 13:20:16 +03:00
|
|
|
this.stickersMenu.close();
|
2018-02-05 14:49:44 +03:00
|
|
|
}
|
|
|
|
|
2018-01-17 00:35:25 +03:00
|
|
|
componentDidMount() {
|
2018-01-17 03:04:06 +03:00
|
|
|
this.scalarClient = null;
|
|
|
|
if (SdkConfig.get().integrations_ui_url && SdkConfig.get().integrations_rest_url) {
|
|
|
|
this.scalarClient = new ScalarAuthClient();
|
|
|
|
this.scalarClient.connect().then(() => {
|
|
|
|
this.forceUpdate();
|
|
|
|
}).catch((e) => {
|
2018-02-23 13:20:16 +03:00
|
|
|
this._imError("Failed to connect to integrations server", e);
|
2018-01-17 03:04:06 +03:00
|
|
|
});
|
|
|
|
}
|
2018-02-23 13:20:16 +03:00
|
|
|
|
|
|
|
if (!this.state.imError) {
|
|
|
|
this._getStickerPickerWidget();
|
|
|
|
this.dispatcherRef = dis.register(this._onWidgetAction);
|
|
|
|
}
|
2018-02-07 12:23:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
dis.unregister(this.dispatcherRef);
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:20:16 +03:00
|
|
|
_imError(errorMsg, e) {
|
|
|
|
console.error(errorMsg, e);
|
|
|
|
const imErrorContent = <div style={{"text-align": "center"}} className="error"><p> { errorMsg } </p></div>;
|
|
|
|
this.setState({
|
|
|
|
showStickers: false,
|
|
|
|
imError: errorMsg,
|
|
|
|
stickersContent: imErrorContent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-07 12:23:00 +03:00
|
|
|
_onWidgetAction(payload) {
|
|
|
|
if (payload.action === "user_widget_updated") {
|
|
|
|
this._getStickerPickerWidget();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 03:04:06 +03:00
|
|
|
|
2018-02-07 12:23:00 +03:00
|
|
|
_getStickerPickerWidget() {
|
2018-01-16 21:14:32 +03:00
|
|
|
// Stickers
|
|
|
|
// TODO - Add support for stickerpacks from multiple app stores.
|
|
|
|
// Render content from multiple stickerpack sources, each within their own iframe, within the stickerpack UI element.
|
|
|
|
const stickerpackWidget = Widgets.getStickerpackWidgets()[0];
|
|
|
|
let stickersContent;
|
|
|
|
|
|
|
|
// Load stickerpack content
|
|
|
|
if (stickerpackWidget && stickerpackWidget.content && stickerpackWidget.content.url) {
|
2018-02-07 12:23:00 +03:00
|
|
|
// Set default name
|
|
|
|
stickerpackWidget.content.name = stickerpackWidget.name || "Stickerpack";
|
2018-01-17 03:04:06 +03:00
|
|
|
this.widgetId = stickerpackWidget.id;
|
2018-02-07 17:44:01 +03:00
|
|
|
|
2018-01-17 02:25:07 +03:00
|
|
|
stickersContent = (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
overflow: 'hidden',
|
|
|
|
height: '300px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
id='stickersContent'
|
2018-01-17 18:49:36 +03:00
|
|
|
className='mx_Stickers_content'
|
2018-01-17 02:25:07 +03:00
|
|
|
style={{
|
|
|
|
border: 'none',
|
2018-02-07 17:44:01 +03:00
|
|
|
height: this.popoverHeight,
|
2018-01-17 02:25:07 +03:00
|
|
|
width: this.popoverWidth,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<AppTile
|
|
|
|
id={stickerpackWidget.id}
|
|
|
|
url={stickerpackWidget.content.url}
|
|
|
|
name={stickerpackWidget.content.name}
|
|
|
|
room={this.props.room}
|
|
|
|
type={stickerpackWidget.content.type}
|
|
|
|
fullWidth={true}
|
|
|
|
userId={stickerpackWidget.sender || MatrixClientPeg.get().credentials.userId}
|
|
|
|
creatorUserId={MatrixClientPeg.get().credentials.userId}
|
|
|
|
waitForIframeLoad={true}
|
|
|
|
show={true}
|
2018-02-07 17:44:01 +03:00
|
|
|
showMenubar={true}
|
|
|
|
onEditClick={this._launchManageIntegrations}
|
|
|
|
onDeleteClick={this._removeStickerpackWidgets}
|
|
|
|
showTitle={false}
|
|
|
|
showMinimise={false}
|
2018-01-17 02:25:07 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2018-01-16 21:14:32 +03:00
|
|
|
} else {
|
|
|
|
// Default content to show if stickerpack widget not added
|
2018-02-07 12:23:00 +03:00
|
|
|
console.warn("No available sticker picker widgets");
|
2018-02-05 14:49:44 +03:00
|
|
|
stickersContent = this.defaultStickersContent;
|
2018-02-24 03:10:28 +03:00
|
|
|
this.widgetId = null;
|
2018-02-07 12:23:00 +03:00
|
|
|
this.forceUpdate();
|
2018-01-16 21:14:32 +03:00
|
|
|
}
|
2018-02-07 12:23:00 +03:00
|
|
|
this.setState({
|
|
|
|
showStickers: false,
|
|
|
|
stickersContent: stickersContent,
|
|
|
|
});
|
2018-01-16 21:14:32 +03:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:00:50 +03:00
|
|
|
/**
|
|
|
|
* Show the sticker picker overlay
|
|
|
|
* If no stickerpacks have been added, show a link to the integration manager add sticker packs page.
|
|
|
|
* @param {Event} e Event that triggered the function
|
|
|
|
*/
|
2018-01-16 21:14:32 +03:00
|
|
|
onShowStickersClick(e) {
|
2018-02-07 12:23:00 +03:00
|
|
|
this._getStickerPickerWidget();
|
2018-01-16 21:14:32 +03:00
|
|
|
const GenericElementContextMenu = sdk.getComponent('context_menus.GenericElementContextMenu');
|
|
|
|
const buttonRect = e.target.getBoundingClientRect();
|
|
|
|
|
|
|
|
// The window X and Y offsets are to adjust position when zoomed in to page
|
2018-01-17 00:06:57 +03:00
|
|
|
const x = buttonRect.right + window.pageXOffset - 37;
|
2018-01-16 21:14:32 +03:00
|
|
|
const y = (buttonRect.top + (buttonRect.height / 2) + window.pageYOffset) - 19;
|
|
|
|
// const self = this;
|
2018-01-17 18:40:47 +03:00
|
|
|
this.stickersMenu = ContextualMenu.createMenu(GenericElementContextMenu, {
|
2018-01-16 21:14:32 +03:00
|
|
|
chevronOffset: 10,
|
|
|
|
chevronFace: 'bottom',
|
|
|
|
left: x,
|
|
|
|
top: y,
|
|
|
|
menuWidth: this.popoverWidth,
|
|
|
|
menuHeight: this.popoverHeight,
|
|
|
|
element: this.state.stickersContent,
|
2018-01-17 00:28:15 +03:00
|
|
|
onFinished: this.onFinished,
|
2018-02-08 20:50:30 +03:00
|
|
|
menuPaddingTop: 0,
|
2018-01-16 21:14:32 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.setState({showStickers: true});
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:00:50 +03:00
|
|
|
/**
|
|
|
|
* Trigger hiding of the sticker picker overlay
|
|
|
|
* @param {Event} ev Event that triggered the function call
|
|
|
|
*/
|
2018-01-16 21:14:32 +03:00
|
|
|
onHideStickersClick(ev) {
|
2018-01-17 18:40:47 +03:00
|
|
|
this.stickersMenu.close();
|
2018-01-16 21:14:32 +03:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:00:50 +03:00
|
|
|
/**
|
|
|
|
* The stickers picker was hidden
|
|
|
|
*/
|
2018-01-17 00:28:15 +03:00
|
|
|
onFinished() {
|
|
|
|
this.setState({showStickers: false});
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:00:50 +03:00
|
|
|
/**
|
|
|
|
* Launch the integrations manager on the stickers integration page
|
|
|
|
*/
|
2018-01-17 03:04:06 +03:00
|
|
|
_launchManageIntegrations() {
|
|
|
|
const IntegrationsManager = sdk.getComponent("views.settings.IntegrationsManager");
|
|
|
|
const src = (this.scalarClient !== null && this.scalarClient.hasCredentials()) ?
|
|
|
|
this.scalarClient.getScalarInterfaceUrlForRoom(
|
2018-02-09 14:44:27 +03:00
|
|
|
this.props.room,
|
2018-01-22 19:00:50 +03:00
|
|
|
'type_stickerpack',
|
|
|
|
this.widgetId,
|
2018-01-17 03:04:06 +03:00
|
|
|
) :
|
|
|
|
null;
|
|
|
|
Modal.createTrackedDialog('Integrations Manager', '', IntegrationsManager, {
|
|
|
|
src: src,
|
|
|
|
}, "mx_IntegrationsManager");
|
2018-02-22 18:36:01 +03:00
|
|
|
this.stickersMenu.close();
|
2018-01-17 03:04:06 +03:00
|
|
|
}
|
|
|
|
|
2018-01-16 21:14:32 +03:00
|
|
|
render() {
|
|
|
|
const TintableSvg = sdk.getComponent("elements.TintableSvg");
|
|
|
|
let stickersButton;
|
|
|
|
if (this.state.showStickers) {
|
|
|
|
// Show hide-stickers button
|
|
|
|
stickersButton =
|
|
|
|
<div
|
|
|
|
id='stickersButton'
|
|
|
|
key="controls_hide_stickers"
|
2018-01-17 18:49:36 +03:00
|
|
|
className="mx_MessageComposer_stickers mx_Stickers_hideStickers"
|
2018-01-16 21:14:32 +03:00
|
|
|
onClick={this.onHideStickersClick}
|
|
|
|
ref='target'
|
|
|
|
title={_t("Hide Stickers")}>
|
|
|
|
<TintableSvg src="img/icons-hide-stickers.svg" width="35" height="35" />
|
|
|
|
</div>;
|
|
|
|
} else {
|
|
|
|
// Show show-stickers button
|
|
|
|
stickersButton =
|
|
|
|
<div
|
|
|
|
id='stickersButton'
|
|
|
|
key="constrols_show_stickers"
|
|
|
|
className="mx_MessageComposer_stickers"
|
|
|
|
onClick={this.onShowStickersClick}
|
|
|
|
title={_t("Show Stickers")}>
|
|
|
|
<TintableSvg src="img/icons-show-stickers.svg" width="35" height="35" />
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
return stickersButton;
|
|
|
|
}
|
|
|
|
}
|