Skip to content

Commit

Permalink
feat(core/forms): add video url field type
Browse files Browse the repository at this point in the history
Add new video url field type and its corresponding base component and read only component
  • Loading branch information
trik committed May 25, 2020
1 parent 14dfa1c commit 5d84a7d
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/core/forms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ng_module(
":read-only-field.css",
":read-only-file-field.css",
":read-only-image-field.css",
":read-only-video-url-field.css",
":read-only-table-field.css",
] + glob(["**/*.html"]),
module_name = "@ajf/core/forms",
Expand Down Expand Up @@ -81,6 +82,12 @@ sass_binary(
deps = [],
)

sass_binary(
name = "read_only_video_url_field_scss",
src = "read-only-video-url-field.scss",
deps = [],
)

ng_test_library(
name = "unit_test_sources",
srcs = glob(
Expand Down
5 changes: 5 additions & 0 deletions src/core/forms/forms-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import {AjfCommonModule} from '@ajf/core/common';
import {AjfFileInputModule} from '@ajf/core/file-input';
import {CommonModule} from '@angular/common';
import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';

Expand All @@ -48,6 +49,7 @@ import {AjfReadOnlyFieldComponent} from './read-only-field';
import {AjfReadOnlyFileFieldComponent} from './read-only-file-field';
import {AjfReadOnlyImageFieldComponent} from './read-only-image-field';
import {AjfReadOnlyTableFieldComponent} from './read-only-table-field';
import {AjfReadOnlyVideoUrlFieldComponent} from './read-only-video-url-field';
import {AjfTableRowClass} from './table-row-class';
import {AjfTableVisibleColumnsPipe} from './table-visible-columns';
import {AjfValidSlidePipe} from './valid-slide';
Expand Down Expand Up @@ -76,6 +78,7 @@ import {AjfValidationService} from './validation-service';
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
AjfTableRowClass,
AjfTableVisibleColumnsPipe,
AjfValidSlidePipe,
Expand All @@ -84,6 +87,7 @@ import {AjfValidationService} from './validation-service';
AjfCommonModule,
AjfFileInputModule,
CommonModule,
HttpClientModule,
ReactiveFormsModule,
],
exports: [
Expand All @@ -108,6 +112,7 @@ import {AjfValidationService} from './validation-service';
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
AjfTableRowClass,
AjfTableVisibleColumnsPipe,
AjfValidSlidePipe,
Expand Down
1 change: 1 addition & 0 deletions src/core/forms/interface/fields/field-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ export enum AjfFieldType {
Barcode,
File,
Image,
VideoUrl,
LENGTH
}
2 changes: 2 additions & 0 deletions src/core/forms/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export * from './node-complete-name';
export * from './range';
export * from './read-only-file-field';
export * from './read-only-image-field';
export * from './read-only-video-url-field';
export * from './search-alert-threshold';
export * from './serializers/attachments-origin-serializer';
export * from './serializers/choices-origin-serializer';
Expand All @@ -63,6 +64,7 @@ export * from './table-row-class';
export * from './table-visible-columns';
export * from './valid-slide';
export * from './validation-service';
export * from './video-url-field';
export * from './warning-alert-service';

export * from './read-only-field';
Expand Down
7 changes: 7 additions & 0 deletions src/core/forms/read-only-video-url-field.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div *ngIf="control|async as ctrl" class="ajf-video-thumbnail">
<ng-container *ngIf="validUrl|async">
<a target="_blank" [href]="ctrl.value">
<img *ngIf="videoThumbnail|async as thumb" [src]="thumb" class="" alt="">
</a>
</ng-container>
</div>
Empty file.
51 changes: 51 additions & 0 deletions src/core/forms/read-only-video-url-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {HttpClient} from '@angular/common/http';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Inject,
ViewEncapsulation,
} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';

import {AjfFormRendererService} from './form-renderer';
import {AjfVideoUrlFieldComponent} from './video-url-field';
import {AJF_WARNING_ALERT_SERVICE, AjfWarningAlertService} from './warning-alert-service';

@Component({
selector: 'ajf-read-only-video-url-field',
templateUrl: 'read-only-video-url-field.html',
styleUrls: ['read-only-video-url-field.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class AjfReadOnlyVideoUrlFieldComponent extends AjfVideoUrlFieldComponent {
constructor(
cdr: ChangeDetectorRef, service: AjfFormRendererService,
@Inject(AJF_WARNING_ALERT_SERVICE) was: AjfWarningAlertService, domSanitizer: DomSanitizer,
httpClient: HttpClient) {
super(cdr, service, was, domSanitizer, httpClient);
}
}
208 changes: 208 additions & 0 deletions src/core/forms/video-url-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {HttpClient} from '@angular/common/http';
import {ChangeDetectorRef, Inject} from '@angular/core';
import {FormControl} from '@angular/forms';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {Observable, of as obsOf} from 'rxjs';
import {catchError, filter, map, startWith, switchMap} from 'rxjs/operators';

import {AjfBaseFieldComponent} from './base-field';
import {AjfFormRendererService} from './form-renderer';
import {AJF_WARNING_ALERT_SERVICE, AjfWarningAlertService} from './warning-alert-service';

export type AjfVideoProvider = 'youtube'|'vimeo';

interface VideoInfo {
provider: AjfVideoProvider;
id: string;
}

export class AjfVideoUrlFieldComponent extends AjfBaseFieldComponent {
readonly validUrl: Observable<boolean>;
readonly videoThumbnail: Observable<SafeResourceUrl>;

constructor(
cdr: ChangeDetectorRef, service: AjfFormRendererService,
@Inject(AJF_WARNING_ALERT_SERVICE) was: AjfWarningAlertService, domSanitizer: DomSanitizer,
httpClient: HttpClient) {
super(cdr, service, was);

const video = this.control.pipe(
filter(control => control != null),
switchMap(control => {
control = control as FormControl;
return control.valueChanges.pipe(
startWith(control.value),
);
}),
filter(value => value != null),
map(value => getVideoProviderAndId(value)),
);
this.validUrl = video.pipe(map(v => v != null));
this.videoThumbnail = video.pipe(
filter(info => info != null),
switchMap(info => videoPreviewUrl(httpClient, info as VideoInfo)),
filter(url => url != null),
map(url => domSanitizer.bypassSecurityTrustResourceUrl(url as string)),
);
}
}

function videoPreviewUrl(httpClient: HttpClient, video: VideoInfo): Observable<string|null> {
if (video.provider === 'youtube') {
return obsOf(`https://img.youtube.com/vi/${video.id}/default.jpg`);
}
if (video.provider === 'vimeo') {
return httpClient
.get<{thumbnail_url: string}>(
`https://vimeo.com/api/oembed.json?url=https://vimeo.com/${video.id}`)
.pipe(
map(response => response.thumbnail_url),
catchError(() => obsOf(null)),
);
}
return obsOf('');
}

function getVideoProviderAndId(url: string): VideoInfo|null {
let provider: AjfVideoProvider|null = null;
let id: string|null = null;
if (/youtube|youtu\.be|y2u\.be|i.ytimg\./.test(url)) {
provider = 'youtube';
id = getYouTubeVideoId(url);
} else if (/vimeo/.test(url)) {
provider = 'vimeo';
id = getVimeoVideoId(url);
}
if (provider == null || id == null) {
return null;
}
return {provider, id};
}

function getVimeoVideoId(url: string): string|null {
if (url.indexOf('#') > -1) {
url = url.split('#')[0];
}
if (url.indexOf('?') > -1 && url.indexOf('clip_id=') === -1) {
url = url.split('?')[0];
}

let id: string|null = null;
let arr: string[];

const vimeoPipe = [
'https?:\/\/vimeo\.com\/[0-9]+$', 'https?:\/\/player\.vimeo\.com\/video\/[0-9]+$',
'https?:\/\/vimeo\.com\/channels', 'groups', 'album'
].join('|');

const vimeoRegex = new RegExp(vimeoPipe, 'gim');

if (vimeoRegex.test(url)) {
arr = url.split('/');
if (arr && arr.length) {
id = arr.pop() as string;
}
} else if (/clip_id=/gim.test(url)) {
arr = url.split('clip_id=');
if (arr && arr.length) {
id = arr[1].split('&')[0];
}
}

return id;
}

function getYouTubeVideoId(url: string): string|null {
const shortcode = /youtube:\/\/|https?:\/\/youtu\.be\/|http:\/\/y2u\.be\//g;
if (shortcode.test(url)) {
const shortcodeId = url.split(shortcode)[1];
return stripParameters(shortcodeId);
}
// /v/ or /vi/
const inlinev = /\/v\/|\/vi\//g;

if (inlinev.test(url)) {
const inlineId = url.split(inlinev)[1];
return stripParameters(inlineId);
}

// v= or vi=
const parameterV = /v=|vi=/g;

if (parameterV.test(url)) {
const arr = url.split(parameterV);
return arr[1].split('&')[0];
}

// v= or vi=
const parameterWebp = /\/an_webp\//g;

if (parameterWebp.test(url)) {
const webp = url.split(parameterWebp)[1];
return stripParameters(webp);
}

// embed
const embedReg = /\/embed\//g;

if (embedReg.test(url)) {
const embedId = url.split(embedReg)[1];
return stripParameters(embedId);
}

// ignore /user/username pattern
const usernameReg = /\/user\/([a-zA-Z0-9]*)$/g;

if (usernameReg.test(url)) {
return null;
}

// user
const userReg = /\/user\/(?!.*videos)/g;

if (userReg.test(url)) {
const elements = url.split('/');
return stripParameters(elements.pop() as string);
}

// attribution_link
const attrReg = /\/attribution_link\?.*v%3D([^%&]*)(%26|&|$)/;

if (attrReg.test(url)) {
return (url.match(attrReg) as string[])[1];
}

return null;
}

function stripParameters(url: string): string {
// Split parameters or split folder separator
if (url.indexOf('?') > -1) {
return url.split('?')[0];
} else if (url.indexOf('/') > -1) {
return url.split('/')[0];
}
return url;
}
19 changes: 17 additions & 2 deletions tools/public_api_guard/core/forms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ export declare enum AjfFieldType {
Barcode = 13,
File = 14,
Image = 15,
LENGTH = 16
VideoUrl = 16,
LENGTH = 17
}

export interface AjfFieldWarningAlertResult {
Expand Down Expand Up @@ -441,7 +442,7 @@ export declare class AjfFormSerializer {

export declare class AjfFormsModule {
static ɵinj: i0.ɵɵInjectorDef<AjfFormsModule>;
static ɵmod: i0.ɵɵNgModuleDefWithMeta<AjfFormsModule, [typeof i1.AjfAsFieldInstancePipe, typeof i2.AjfAsRepeatingSlideInstancePipe, typeof i3.AjfBoolToIntPipe, typeof i4.AjfDateValuePipe, typeof i5.AjfDateValueStringPipe, typeof i6.AjfExpandFieldWithChoicesPipe, typeof i7.AjfFieldHost, typeof i8.AjfFieldIconPipe, typeof i9.AjfFieldIsValidPipe, typeof i10.AjfFileFieldComponent, typeof i11.AjfGetTableCellControlPipe, typeof i12.AjfImageFieldComponent, typeof i13.AjfIncrementPipe, typeof i14.AjfIsCellEditablePipe, typeof i15.AjfIsRepeatingSlideInstancePipe, typeof i16.AjfNodeCompleteNamePipe, typeof i17.AjfRangePipe, typeof i18.AjfReadOnlyFieldComponent, typeof i19.AjfReadOnlyFileFieldComponent, typeof i20.AjfReadOnlyImageFieldComponent, typeof i21.AjfReadOnlyTableFieldComponent, typeof i22.AjfTableRowClass, typeof i23.AjfTableVisibleColumnsPipe, typeof i24.AjfValidSlidePipe], [typeof i25.AjfCommonModule, typeof i26.AjfFileInputModule, typeof i27.CommonModule, typeof i28.ReactiveFormsModule], [typeof i1.AjfAsFieldInstancePipe, typeof i2.AjfAsRepeatingSlideInstancePipe, typeof i3.AjfBoolToIntPipe, typeof i4.AjfDateValuePipe, typeof i5.AjfDateValueStringPipe, typeof i6.AjfExpandFieldWithChoicesPipe, typeof i7.AjfFieldHost, typeof i8.AjfFieldIconPipe, typeof i9.AjfFieldIsValidPipe, typeof i10.AjfFileFieldComponent, typeof i11.AjfGetTableCellControlPipe, typeof i12.AjfImageFieldComponent, typeof i13.AjfIncrementPipe, typeof i14.AjfIsCellEditablePipe, typeof i15.AjfIsRepeatingSlideInstancePipe, typeof i16.AjfNodeCompleteNamePipe, typeof i17.AjfRangePipe, typeof i18.AjfReadOnlyFieldComponent, typeof i19.AjfReadOnlyFileFieldComponent, typeof i20.AjfReadOnlyImageFieldComponent, typeof i21.AjfReadOnlyTableFieldComponent, typeof i22.AjfTableRowClass, typeof i23.AjfTableVisibleColumnsPipe, typeof i24.AjfValidSlidePipe]>;
static ɵmod: i0.ɵɵNgModuleDefWithMeta<AjfFormsModule, [typeof i1.AjfAsFieldInstancePipe, typeof i2.AjfAsRepeatingSlideInstancePipe, typeof i3.AjfBoolToIntPipe, typeof i4.AjfDateValuePipe, typeof i5.AjfDateValueStringPipe, typeof i6.AjfExpandFieldWithChoicesPipe, typeof i7.AjfFieldHost, typeof i8.AjfFieldIconPipe, typeof i9.AjfFieldIsValidPipe, typeof i10.AjfFileFieldComponent, typeof i11.AjfGetTableCellControlPipe, typeof i12.AjfImageFieldComponent, typeof i13.AjfIncrementPipe, typeof i14.AjfIsCellEditablePipe, typeof i15.AjfIsRepeatingSlideInstancePipe, typeof i16.AjfNodeCompleteNamePipe, typeof i17.AjfRangePipe, typeof i18.AjfReadOnlyFieldComponent, typeof i19.AjfReadOnlyFileFieldComponent, typeof i20.AjfReadOnlyImageFieldComponent, typeof i21.AjfReadOnlyTableFieldComponent, typeof i22.AjfReadOnlyVideoUrlFieldComponent, typeof i23.AjfTableRowClass, typeof i24.AjfTableVisibleColumnsPipe, typeof i25.AjfValidSlidePipe], [typeof i26.AjfCommonModule, typeof i27.AjfFileInputModule, typeof i28.CommonModule, typeof i29.HttpClientModule, typeof i30.ReactiveFormsModule], [typeof i1.AjfAsFieldInstancePipe, typeof i2.AjfAsRepeatingSlideInstancePipe, typeof i3.AjfBoolToIntPipe, typeof i4.AjfDateValuePipe, typeof i5.AjfDateValueStringPipe, typeof i6.AjfExpandFieldWithChoicesPipe, typeof i7.AjfFieldHost, typeof i8.AjfFieldIconPipe, typeof i9.AjfFieldIsValidPipe, typeof i10.AjfFileFieldComponent, typeof i11.AjfGetTableCellControlPipe, typeof i12.AjfImageFieldComponent, typeof i13.AjfIncrementPipe, typeof i14.AjfIsCellEditablePipe, typeof i15.AjfIsRepeatingSlideInstancePipe, typeof i16.AjfNodeCompleteNamePipe, typeof i17.AjfRangePipe, typeof i18.AjfReadOnlyFieldComponent, typeof i19.AjfReadOnlyFileFieldComponent, typeof i20.AjfReadOnlyImageFieldComponent, typeof i21.AjfReadOnlyTableFieldComponent, typeof i22.AjfReadOnlyVideoUrlFieldComponent, typeof i23.AjfTableRowClass, typeof i24.AjfTableVisibleColumnsPipe, typeof i25.AjfValidSlidePipe]>;
}

export interface AjfFormStringIdentifier {
Expand Down Expand Up @@ -600,6 +601,12 @@ export declare class AjfReadOnlyTableFieldComponent extends AjfBaseFieldComponen
static ɵfac: i0.ɵɵFactoryDef<AjfReadOnlyTableFieldComponent, never>;
}

export declare class AjfReadOnlyVideoUrlFieldComponent extends AjfVideoUrlFieldComponent {
constructor(cdr: ChangeDetectorRef, service: AjfFormRendererService, was: AjfWarningAlertService, domSanitizer: DomSanitizer, httpClient: HttpClient);
static ɵcmp: i0.ɵɵComponentDefWithMeta<AjfReadOnlyVideoUrlFieldComponent, "ajf-read-only-video-url-field", never, {}, {}, never, never>;
static ɵfac: i0.ɵɵFactoryDef<AjfReadOnlyVideoUrlFieldComponent, never>;
}

export interface AjfRendererUpdateMap {
[prop: string]: AjfNodeInstance[];
}
Expand Down Expand Up @@ -738,6 +745,14 @@ export declare class AjfValidSlidePipe implements PipeTransform {
static ɵpipe: i0.ɵɵPipeDefWithMeta<AjfValidSlidePipe, "ajfValidSlide">;
}

export declare type AjfVideoProvider = 'youtube' | 'vimeo';

export declare class AjfVideoUrlFieldComponent extends AjfBaseFieldComponent {
readonly validUrl: Observable<boolean>;
readonly videoThumbnail: Observable<SafeResourceUrl>;
constructor(cdr: ChangeDetectorRef, service: AjfFormRendererService, was: AjfWarningAlertService, domSanitizer: DomSanitizer, httpClient: HttpClient);
}

export interface AjfWarning extends AjfCondition {
warningMessage: string;
}
Expand Down

0 comments on commit 5d84a7d

Please sign in to comment.