2020-11-30 15:05:26 -05:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 {isWebPlatform, Platform} from "./Platform.js";
|
|
|
|
import {ViewModel} from "../utils/ViewModel.js";
|
|
|
|
|
|
|
|
export class ClientViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-12-01 13:01:15 -05:00
|
|
|
const {client, link, pickClient} = options;
|
2020-11-30 15:05:26 -05:00
|
|
|
this._client = client;
|
2020-12-01 09:29:12 -05:00
|
|
|
this._link = link;
|
2020-12-01 13:01:15 -05:00
|
|
|
this._pickClient = pickClient;
|
2020-12-01 09:29:12 -05:00
|
|
|
|
2020-11-30 15:05:26 -05:00
|
|
|
const supportedPlatforms = client.platforms;
|
|
|
|
const matchingPlatforms = this.platforms.filter(p => {
|
|
|
|
return supportedPlatforms.includes(p);
|
|
|
|
});
|
|
|
|
const nativePlatform = matchingPlatforms.find(p => !isWebPlatform(p));
|
2020-12-01 13:01:15 -05:00
|
|
|
const webPlatform = matchingPlatforms.find(p => isWebPlatform(p));
|
2020-12-01 09:29:12 -05:00
|
|
|
|
|
|
|
this._proposedPlatform = this.preferences.platform || nativePlatform || webPlatform;
|
|
|
|
|
2020-11-30 15:05:26 -05:00
|
|
|
this.actions = this._createActions(client, link, nativePlatform, webPlatform);
|
2020-12-01 09:29:12 -05:00
|
|
|
this.name = this._client.getName(this._proposedPlatform);
|
|
|
|
this.deepLink = this._client.getDeepLink(this._proposedPlatform, this._link);
|
2020-12-01 13:01:15 -05:00
|
|
|
this._showOpen = this.deepLink && nativePlatform && !client.canInterceptMatrixToLinks(nativePlatform);
|
2020-11-30 15:05:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_createActions(client, link, nativePlatform, webPlatform) {
|
|
|
|
let actions = [];
|
|
|
|
if (nativePlatform) {
|
|
|
|
const nativeActions = client.getInstallLinks(nativePlatform).map(installLink => {
|
|
|
|
return {
|
|
|
|
label: installLink.description,
|
|
|
|
url: installLink.createInstallURL(link),
|
|
|
|
kind: installLink.channelId,
|
2020-12-01 06:04:11 -05:00
|
|
|
primary: true,
|
|
|
|
activated: () => this.preferences.setClient(client.id, nativePlatform),
|
2020-11-30 15:05:26 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
actions.push(...nativeActions);
|
|
|
|
}
|
|
|
|
if (webPlatform) {
|
|
|
|
actions.push({
|
|
|
|
label: `Or open in ${client.getName(webPlatform)}`,
|
|
|
|
url: client.getDeepLink(webPlatform, link),
|
|
|
|
kind: "open-in-web",
|
2020-12-01 06:04:11 -05:00
|
|
|
activated: () => this.preferences.setClient(client.id, webPlatform),
|
2020-11-30 15:05:26 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:01:15 -05:00
|
|
|
get identifier() {
|
|
|
|
return this._link.identifier;
|
|
|
|
}
|
|
|
|
|
2020-11-30 15:05:26 -05:00
|
|
|
get description() {
|
|
|
|
return this._client.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
get clientId() {
|
|
|
|
return this._client.id;
|
|
|
|
}
|
2020-12-01 09:29:12 -05:00
|
|
|
|
|
|
|
get stage() {
|
|
|
|
return this._showOpen ? "open" : "install";
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:01:15 -05:00
|
|
|
get textInstructions() {
|
|
|
|
return this._client.getLinkInstructions(this._proposedPlatform, this._link);
|
2020-12-01 09:29:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
deepLinkActivated() {
|
2020-12-01 13:01:15 -05:00
|
|
|
this._pickClient(this._client);
|
2020-12-01 09:29:12 -05:00
|
|
|
this.preferences.setClient(this._client.id, this._proposedPlatform);
|
|
|
|
if (this._showOpen) {
|
|
|
|
this._showOpen = false;
|
|
|
|
this.emitChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (this._preferredClient.getLinkSupport(this.preferences.platform, this._link)) {
|
|
|
|
const deepLink = this._preferredClient.getDeepLink(this.preferences.platform, this._link);
|
|
|
|
this.openLink(deepLink);
|
|
|
|
const protocol = new URL(deepLink).protocol;
|
|
|
|
const isWebProtocol = protocol === "http:" || protocol === "https:";
|
|
|
|
if (!isWebProtocol) {
|
|
|
|
this.missingClientViewModel = new ClientViewModel(this.childOptions({client: this._preferredClient, link: this._link}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.acceptInstructions = this._preferredClient.getLinkInstructions(this.preferences.platform, this._link);
|
|
|
|
}
|
|
|
|
*/
|