Skip to content

Commit

Permalink
feat: cleanup, refactor & improve the code for PHP cxl/jw-player
Browse files Browse the repository at this point in the history
* jw-player height tweaks
* [jw-player] add support for sources
	cherry-pick: #236
* [cxl-jw-player] set up api endpoint
	cherry-pick: #234

https://app.clickup.com/t/3rgx82p
  • Loading branch information
saas786 committed Nov 30, 2022
1 parent 4c9f0cc commit 08b3cf5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 21 deletions.
4 changes: 0 additions & 4 deletions packages/cxl-ui/scss/jw-player/jw-player.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
}
}

:host([captions]) #container {
grid-template-rows: 1fr max-content 1fr;
}

.captions {
h2,
span {
Expand Down
2 changes: 1 addition & 1 deletion packages/cxl-ui/src/components/jw-player/index.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@vaadin/text-field';
// eslint-disable-next-line func-names
export const template = function () {
return html`
<div class="grid height-100" id="container">
<div class="cxl-jw-player-container" id="cxl-jw-player-container">
<slot></slot>
${this.captions
? html`
Expand Down
2 changes: 1 addition & 1 deletion packages/cxl-ui/src/components/jw-player/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export class JWPlayerElement extends mixin(LitElement, [
]) {
config = {
width: '100%',
height: '100%',
playbackRateControls: [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2],
plugins: {
// 'http://192.168.0.101:8080/telemetry-8.20.0.js': {},
},
skin: {
name: 'cxl-institute',
},
stretching: 'uniform',
};

static get styles() {
Expand Down
60 changes: 48 additions & 12 deletions packages/cxl-ui/src/components/jw-player/mixins/BaseMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ export function BaseMixin(BaseClass) {

@property({ attribute: 'media-id', type: String }) mediaId;

@property({ attribute: 'player-id', type: String }) playerId;
@property({ attribute: 'media-source', type: String }) mediaSource;

@property({ attribute: 'is-public', type: String }) isPublic;

@property({ attribute: 'library-id', type: String }) libraryId;

@property({ attribute: 'player-source', type: String }) playerSource;

@property({ attribute: 'playlist-id', type: String }) playlistId;

@property({ attribute: 'playlist-source', type: String }) playlistSource;

firstUpdated(_changedProperties) {
super.firstUpdated(_changedProperties);

Expand All @@ -34,7 +42,17 @@ export function BaseMixin(BaseClass) {
}

get __scriptUrl() {
return `https://content.jwplatform.com/libraries/${this.playerId}.js`;
let scriptUrl;

if (this.libraryId && this.isPublic) {
scriptUrl = `https://content.jwplatform.com/libraries/${this.libraryId}.js`;
} else if (this.playerSource) {
scriptUrl = this.playerSource;
} else {
return false;
}

return scriptUrl;
}

async __getChapters() {
Expand All @@ -46,21 +64,31 @@ export function BaseMixin(BaseClass) {
}

async __getMedia() {
if (!this.mediaId) return false;

const response = await fetch(`https://cdn.jwplayer.com/v2/media/${this.mediaId}`);
const result = await response.json();
let response;

if (this.mediaId && this.isPublic) {
response = await fetch(`https://cdn.jwplayer.com/v2/media/${this.mediaId}`);
} else if (this.mediaSource) {
response = await fetch(this.mediaSource);
} else {
return false;
}

return result;
return response.json();
}

async __getPlaylist() {
if (!this.playlistId) return false;

const response = await fetch(`https://cdn.jwplayer.com/v2/playlists/${this.playlistId}`);
const result = await response.json();
let response;

if (this.playlistId) {
response = await fetch(`https://cdn.jwplayer.com/v2/playlists/${this.playlistId}`);
} else if (this.playlistSource) {
response = await fetch(`https://cdn.jwplayer.com/v2/playlists/${this.playlistId}`);
} else {
return false;
}

return result;
return response.json();
}

async __loadScript() {
Expand Down Expand Up @@ -90,6 +118,14 @@ export function BaseMixin(BaseClass) {
* Each mixin has the ability to hook onto this method.
*/
async __setup() {
// Merge configs from `cxlJWPlayerData`.
if (typeof window.cxlJWPlayerData !== 'undefined') {
// eslint-disable-next-line camelcase
const { media_config } = window.cxlJWPlayerData[this.mediaId];
// eslint-disable-next-line camelcase
this.config = { ...this.config, ...media_config };
}

const jwPlayer = await this.__loadScript();

const el = document.createElement('div');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function SavePositionMixin(BaseClass) {
class Mixin extends BaseClass {
__endpoint = '';
__endpoint;

__nonce;

Expand All @@ -9,6 +9,9 @@ export function SavePositionMixin(BaseClass) {
async __setup() {
await super.__setup();

this.__endpoint = `${window.ajaxurl}?action=jwplayer_save_position`;
this.__nonce = window.cxl_pum_vars.nonce;

this.__loadPosition();
}

Expand Down
21 changes: 19 additions & 2 deletions packages/storybook/cxl-ui/jw-player/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ export default {
title: 'JW Player/JW Player',
};

const Template = ({ captions, mediaId, minimumSearchLength, playerId, playlistId, pluginPath }) =>
const Template = ({
captions,
mediaId,
mediaSource,
minimumSearchLength,
playerId,
playerSource,
playlistId,
playlistSource,
pluginPath,
}) =>
html`
<style>
#root-inner {
Expand All @@ -22,11 +32,15 @@ const Template = ({ captions, mediaId, minimumSearchLength, playerId, playlistId
}
</style>
<jw-player
is-public="true"
?captions=${captions}
media-id=${mediaId}
media-source=${mediaSource}
minimum-search-length=${minimumSearchLength}
player-id=${playerId}
library-id=${playerId}
library-source=${playerSource}
playlist-id=${playlistId}
playlist-source=${playlistSource}
plugin-path="${pluginPath}"
></jw-player>
`;
Expand All @@ -37,9 +51,12 @@ Object.assign(Default, {
args: {
captions: true,
mediaId: 'fZ0XiGdb',
mediaSource: '',
minimumSearchLength: 3,
playerId: '5CFJNXKb',
playerSource: '',
playlistId: '',
playlistSource: '',
pluginPath: 'https://cxl.com/institute/wp-content/plugins/cxl-jwplayer/',
},
});

0 comments on commit 08b3cf5

Please sign in to comment.