2017-12-23 17:28:42 -05:00
|
|
|
import { WidgetComponent } from "../widget.component";
|
|
|
|
import { EditableWidget, WIDGET_YOUTUBE } from "../../../shared/models/widget";
|
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import * as embed from "embed-video";
|
|
|
|
import * as $ from "jquery";
|
|
|
|
import * as url from "url";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2017-12-23 17:28:42 -05:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "youtube.widget.component.html",
|
|
|
|
styleUrls: ["youtube.widget.component.scss"],
|
|
|
|
})
|
|
|
|
export class YoutubeWidgetConfigComponent extends WidgetComponent {
|
2020-10-23 07:30:20 -04:00
|
|
|
constructor(public translate: TranslateService) {
|
|
|
|
super(WIDGET_YOUTUBE, "Video", "video", translate,"youtube");
|
|
|
|
this.translate = translate;
|
2017-12-23 17:28:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
protected OnNewWidgetPrepared(widget: EditableWidget) {
|
|
|
|
widget.dimension.newData.videoUrl = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected OnWidgetsDiscovered(widgets: EditableWidget[]) {
|
|
|
|
for (const widget of widgets) {
|
|
|
|
if (widget.data.videoUrl) continue; // Dimension widget
|
|
|
|
|
|
|
|
if (widget.data.cUrl) {
|
|
|
|
// Scalar widget
|
|
|
|
widget.data.videoUrl = this.parseVideoUrl(widget.data.cUrl);
|
|
|
|
} else {
|
|
|
|
// Older Dimension widget - infer link
|
|
|
|
const parsedUrl = url.parse(widget.url, true);
|
|
|
|
if (parsedUrl.query["url"]) {
|
2018-03-22 14:55:50 -04:00
|
|
|
widget.data.videoUrl = this.parseVideoUrl(decodeURIComponent(<string>parsedUrl.query["url"]));
|
2017-12-23 17:28:42 -05:00
|
|
|
} else console.warn("Cannot parse URL for " + widget.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected OnWidgetBeforeAdd(widget: EditableWidget) {
|
|
|
|
this.setVideoUrl(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected OnWidgetBeforeEdit(widget: EditableWidget) {
|
|
|
|
this.setVideoUrl(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
private setVideoUrl(widget: EditableWidget) {
|
|
|
|
if (!widget.dimension.newData.videoUrl || widget.dimension.newData.videoUrl.trim().length === 0) {
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Please enter a video URL').subscribe((res: string) => {throw new Error(res); });
|
2017-12-23 17:28:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const videoUrl = this.getRealVideoUrl(widget.dimension.newData.videoUrl);
|
|
|
|
if (!videoUrl) {
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate.get('Please enter a YouTube, Vimeo, or DailyMotion video URL').subscribe((res: string) => {throw new Error(res); });
|
2017-12-23 17:28:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
widget.dimension.newUrl = videoUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getRealVideoUrl(videoUrl): string {
|
|
|
|
const embedCode = embed(videoUrl);
|
|
|
|
if (!embedCode) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: Grab the video URL from the iframe embed code
|
|
|
|
videoUrl = $(embedCode).attr("src");
|
|
|
|
if (videoUrl.startsWith("//")) videoUrl = "https:" + videoUrl;
|
|
|
|
|
|
|
|
return videoUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private parseVideoUrl(videoUrl: string): string {
|
|
|
|
const parsed = url.parse(videoUrl);
|
|
|
|
|
|
|
|
const realPath = parsed.path.split("?")[0];
|
|
|
|
if (parsed.host.indexOf("youtube.com") !== -1) {
|
|
|
|
return "https://youtube.com/watch?v=" + realPath.substring("/embed/".length);
|
|
|
|
} else if (parsed.host.indexOf("vimeo.com") !== -1) {
|
|
|
|
return "https://vimeo.com/" + realPath.substring("/video/".length);
|
|
|
|
} else if (parsed.host.indexOf("dailymotion.com") !== -1) {
|
|
|
|
return "https://dailymotion.com/" + realPath.substring("/embed/".length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return videoUrl;
|
|
|
|
}
|
2020-10-23 07:30:20 -04:00
|
|
|
}
|