2017-08-29 00:08:32 -04:00
|
|
|
import { ScalarService } from "../../shared/scalar.service";
|
2017-12-09 18:34:59 -05:00
|
|
|
import { ScalarToWidgets, Widget } from "../../shared/models/widget";
|
2017-10-10 22:44:09 -04:00
|
|
|
import { ToasterService } from "angular2-toaster";
|
2017-12-11 17:39:15 -05:00
|
|
|
import { Integration } from "../../shared/models/integration";
|
2017-08-29 00:08:32 -04:00
|
|
|
|
2017-10-10 22:44:09 -04:00
|
|
|
const SCALAR_WIDGET_LINKS = [
|
|
|
|
"https://scalar-staging.riot.im/scalar/api/widgets/__TYPE__.html?url=",
|
|
|
|
"https://scalar-staging.vector.im/scalar/api/widgets/__TYPE__.html?url=",
|
|
|
|
"https://scalar-develop.riot.im/scalar/api/widgets/__TYPE__.html?url=",
|
|
|
|
"https://demo.riot.im/scalar/api/widgets/__TYPE__.html?url=",
|
2017-10-09 22:26:46 -04:00
|
|
|
];
|
|
|
|
|
2017-08-29 00:08:32 -04:00
|
|
|
export class WidgetComponent {
|
|
|
|
|
2017-10-10 22:44:09 -04:00
|
|
|
public isLoading = true;
|
|
|
|
public isUpdating = false;
|
|
|
|
public widgets: Widget[];
|
|
|
|
public newWidgetUrl: string = "";
|
|
|
|
public newWidgetName: string = "";
|
|
|
|
|
|
|
|
private toggledWidgetIds: string[] = [];
|
2017-11-19 06:34:41 -05:00
|
|
|
private wrapperUrl = "";
|
2017-10-10 22:44:09 -04:00
|
|
|
private scalarWrapperUrls: string[] = [];
|
|
|
|
|
|
|
|
constructor(protected toaster: ToasterService,
|
|
|
|
protected scalarApi: ScalarService,
|
|
|
|
protected roomId: string,
|
|
|
|
window: Window,
|
|
|
|
private primaryWidgetType: string,
|
|
|
|
alternateWidgetType: string,
|
2017-12-11 17:39:15 -05:00
|
|
|
public integration: Integration,
|
2017-10-10 22:44:09 -04:00
|
|
|
requestedEditId: string,
|
|
|
|
private defaultName: string,
|
|
|
|
wrapperId = "generic",
|
|
|
|
scalarWrapperId = null) {
|
|
|
|
this.isLoading = true;
|
|
|
|
this.isUpdating = false;
|
|
|
|
|
2017-12-10 02:59:36 -05:00
|
|
|
if (wrapperId) {
|
|
|
|
this.wrapperUrl = window.location.origin + "/widgets/" + wrapperId + "?url=";
|
|
|
|
|
|
|
|
if (!scalarWrapperId) scalarWrapperId = wrapperId;
|
|
|
|
for (let widgetLink of SCALAR_WIDGET_LINKS) {
|
|
|
|
this.scalarWrapperUrls.push(widgetLink.replace("__TYPE__", scalarWrapperId));
|
|
|
|
}
|
2017-10-10 22:44:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.getWidgetsOfType(primaryWidgetType, alternateWidgetType).then(widgets => {
|
|
|
|
this.widgets = widgets;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.isUpdating = false;
|
|
|
|
|
|
|
|
// Unwrap URLs for easy-editing
|
|
|
|
for (let widget of this.widgets) {
|
|
|
|
this.setWidgetUrl(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we should request editing a particular widget
|
|
|
|
if (requestedEditId) {
|
|
|
|
for (let widget of this.widgets) {
|
|
|
|
if (widget.id === requestedEditId) {
|
|
|
|
console.log("Requesting edit for " + widget.id);
|
|
|
|
this.editWidget(widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-08-29 00:08:32 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 02:59:36 -05:00
|
|
|
protected finishParsing(widget: Widget) {
|
|
|
|
// We don't actually need to do anything
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected widgetAdded() {
|
|
|
|
// Meant to be overridden
|
|
|
|
}
|
|
|
|
|
2017-12-11 19:29:57 -05:00
|
|
|
protected beforeEdit(widget: Widget) {
|
|
|
|
// Meant to be overridden - we do some silly logic here to make typescript happy
|
|
|
|
// noinspection SillyAssignmentJS
|
|
|
|
widget.url = widget.url;
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:44:09 -04:00
|
|
|
private getWidgetsOfType(type: string, altType: string): Promise<Widget[]> {
|
2017-08-29 00:08:32 -04:00
|
|
|
return this.scalarApi.getWidgets(this.roomId)
|
|
|
|
.then(resp => ScalarToWidgets(resp))
|
|
|
|
.then(widgets => {
|
|
|
|
let filtered: Widget[] = [];
|
|
|
|
|
|
|
|
for (let widget of widgets) {
|
|
|
|
if (widget.type === type || (altType && widget.type === altType))
|
|
|
|
filtered.push(widget);
|
|
|
|
}
|
|
|
|
|
2017-12-10 02:59:36 -05:00
|
|
|
return filtered.map(w => this.finishParsing(w));
|
2017-08-29 00:08:32 -04:00
|
|
|
});
|
|
|
|
}
|
2017-10-10 22:44:09 -04:00
|
|
|
|
|
|
|
private getWrappedUrl(url: string): string {
|
2017-12-10 02:59:36 -05:00
|
|
|
if (!this.wrapperUrl) return url;
|
|
|
|
|
2017-10-10 22:44:09 -04:00
|
|
|
const urls = [this.wrapperUrl].concat(this.scalarWrapperUrls);
|
|
|
|
for (let scalarUrl of urls) {
|
|
|
|
if (url.startsWith(scalarUrl)) {
|
|
|
|
return decodeURIComponent(url.substring(scalarUrl.length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2017-11-19 06:34:41 -05:00
|
|
|
private wrapUrl(url: string): string {
|
2017-12-10 02:59:36 -05:00
|
|
|
if (!this.wrapperUrl) return url;
|
|
|
|
|
2017-11-19 06:34:41 -05:00
|
|
|
let encodedURL = this.wrapperUrl + encodeURIComponent(url);
|
2017-12-09 18:34:59 -05:00
|
|
|
|
2017-12-10 02:59:36 -05:00
|
|
|
// TODO: Decode data parameters
|
|
|
|
encodedURL = this.unformatParams(encodedURL);
|
2017-11-19 06:34:41 -05:00
|
|
|
|
|
|
|
return encodedURL;
|
2017-10-10 22:44:09 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 02:59:36 -05:00
|
|
|
protected unformatParams(encodedUrl: string, additionalData: any = {}):string {
|
|
|
|
encodedUrl = encodedUrl.replace(encodeURIComponent("$matrix_user_id"), "$matrix_user_id");
|
|
|
|
encodedUrl = encodedUrl.replace(encodeURIComponent("$matrix_room_id"), "$matrix_room_id");
|
|
|
|
encodedUrl = encodedUrl.replace(encodeURIComponent("$matrix_display_name"), "$matrix_display_name");
|
|
|
|
encodedUrl = encodedUrl.replace(encodeURIComponent("$matrix_avatar_url"), "$matrix_avatar_url");
|
|
|
|
|
|
|
|
for (const key of Object.keys(additionalData)) {
|
|
|
|
encodedUrl = encodedUrl.replace(encodeURIComponent("$" + key), "$" + key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodedUrl;
|
|
|
|
}
|
|
|
|
|
2017-10-10 22:44:09 -04:00
|
|
|
private setWidgetUrl(widget: Widget) {
|
|
|
|
widget.url = this.getWrappedUrl(widget.url);
|
|
|
|
|
|
|
|
// Use the Dimension-specific URL override if one is present
|
|
|
|
if (widget.data && widget.data.dimOriginalUrl) {
|
|
|
|
widget.url = widget.data.dimOriginalUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public addWidget(data: any = null) {
|
|
|
|
let constructedWidget: Widget = {
|
|
|
|
id: "dimension-" + this.primaryWidgetType + "-" + (new Date().getTime()),
|
|
|
|
url: this.wrapUrl(this.newWidgetUrl),
|
|
|
|
type: this.primaryWidgetType,
|
|
|
|
name: this.newWidgetName || this.defaultName,
|
|
|
|
};
|
2017-10-11 00:32:08 -04:00
|
|
|
if (data) constructedWidget.data = data;
|
2017-10-10 22:44:09 -04:00
|
|
|
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.scalarApi.setWidget(this.roomId, constructedWidget)
|
|
|
|
.then(() => this.widgets.push(constructedWidget))
|
|
|
|
.then(() => this.setWidgetUrl(constructedWidget))
|
|
|
|
.then(() => {
|
|
|
|
this.isUpdating = false;
|
|
|
|
this.newWidgetUrl = "";
|
|
|
|
this.newWidgetName = "";
|
|
|
|
this.toaster.pop("success", "Widget added!");
|
2017-12-10 02:59:36 -05:00
|
|
|
this.widgetAdded();
|
2017-10-10 22:44:09 -04:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.toaster.pop("error", err.json().error);
|
|
|
|
console.error(err);
|
|
|
|
this.isUpdating = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public saveWidget(widget: Widget) {
|
|
|
|
if (widget.newUrl.trim().length === 0) {
|
|
|
|
this.toaster.pop("warning", "Please enter a URL for the widget");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.name = widget.newName || this.defaultName;
|
|
|
|
widget.url = this.wrapUrl(widget.newUrl);
|
|
|
|
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.scalarApi.setWidget(this.roomId, widget)
|
|
|
|
.then(() => this.toggleWidget(widget))
|
|
|
|
.then(() => {
|
|
|
|
this.isUpdating = false;
|
2017-12-09 18:35:40 -05:00
|
|
|
widget.url = this.getWrappedUrl(widget.url); // for easier editing
|
2017-10-10 22:44:09 -04:00
|
|
|
this.toaster.pop("success", "Widget updated!");
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.toaster.pop("error", err.json().error);
|
|
|
|
console.error(err);
|
|
|
|
this.isUpdating = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeWidget(widget: Widget) {
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.scalarApi.deleteWidget(this.roomId, widget)
|
|
|
|
.then(() => this.widgets.splice(this.widgets.indexOf(widget), 1))
|
|
|
|
.then(() => {
|
|
|
|
this.isUpdating = false;
|
|
|
|
this.toaster.pop("success", "Widget deleted!");
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.toaster.pop("error", err.json().error);
|
|
|
|
console.error(err);
|
|
|
|
this.isUpdating = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public editWidget(widget: Widget) {
|
|
|
|
widget.newName = widget.name || this.defaultName;
|
|
|
|
widget.newUrl = widget.url;
|
2017-12-11 19:29:57 -05:00
|
|
|
this.beforeEdit(widget);
|
2017-10-10 22:44:09 -04:00
|
|
|
this.toggleWidget(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggleWidget(widget: Widget) {
|
|
|
|
let idx = this.toggledWidgetIds.indexOf(widget.id);
|
|
|
|
if (idx === -1) this.toggledWidgetIds.push(widget.id);
|
|
|
|
else this.toggledWidgetIds.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public isWidgetToggled(widget: Widget) {
|
|
|
|
return this.toggledWidgetIds.indexOf(widget.id) !== -1;
|
|
|
|
}
|
2017-08-29 00:08:32 -04:00
|
|
|
}
|