pict-section-video 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -2
- package/package.json +59 -5
- package/source/Pict-Section-Video-DefaultConfiguration.js +146 -0
- package/source/Pict-Section-Video.js +801 -0
package/README.md
CHANGED
|
@@ -1,3 +1,100 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pict-section-video
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A bespoke, responsive **video player** section for the Retold/Pict ecosystem. Point it at a video URL and it fits
|
|
4
|
+
the whole frame to its stage on phones, tablets, desktops, TVs, and projected displays — with its own control bar
|
|
5
|
+
(play/pause, a seek scrubber, a time readout, mute + volume, a playback-rate cycle, download, and maximize).
|
|
6
|
+
Seeking rides the browser's native HTTP **Range** requests, so a range-served file (an
|
|
7
|
+
[`orator-file-service`](https://github.com/fable-retold/orator-file-service) route, a Sluice depot, a bare 206 URL)
|
|
8
|
+
scrubs without downloading the whole clip.
|
|
9
|
+
|
|
10
|
+
It is a single [`pict-view`](https://github.com/fable-retold/pict-view) in the lightweight single-view shape
|
|
11
|
+
(like `pict-section-image`): a stub container template, with the player DOM built imperatively — a `<video>` is
|
|
12
|
+
inherently imperative (its `src`, buffered/seekable ranges, and the `loadedmetadata`/`timeupdate`/`play`/`pause`
|
|
13
|
+
events have no template equivalent).
|
|
14
|
+
|
|
15
|
+
Usable on its own, or driven as the video renderer inside
|
|
16
|
+
[`pict-section-fileview`](https://github.com/fable-retold/pict-section-fileview).
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install pict-section-video
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The only hard dependency is `pict-view`.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
const libPictSectionVideo = require('pict-section-video');
|
|
30
|
+
|
|
31
|
+
pict.addView('MyVideo', Object.assign({}, libPictSectionVideo.default_configuration,
|
|
32
|
+
{
|
|
33
|
+
DefaultDestinationAddress: '#SomeDiv',
|
|
34
|
+
TargetElementAddress: '#SomeDiv',
|
|
35
|
+
VideoURL: 'https://host/clip.mp4'
|
|
36
|
+
}), libPictSectionVideo);
|
|
37
|
+
|
|
38
|
+
pict.views.MyVideo.render();
|
|
39
|
+
|
|
40
|
+
// …or point it at a video later:
|
|
41
|
+
pict.views.MyVideo.setVideo('https://host/other.webm');
|
|
42
|
+
pict.views.MyVideo.setVideo({ URL: 'https://host/talk.mp4', Title: 'The Talk', PosterURL: 'https://host/art.jpg' });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Configuration
|
|
46
|
+
|
|
47
|
+
| Key | Meaning |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `VideoURL` | Initial video URL (or `false` for an empty player until `setVideo()`). |
|
|
50
|
+
| `PosterURL` | A poster image shown before playback (`<video poster>`). |
|
|
51
|
+
| `Title` | A human title (used as the download filename hint). |
|
|
52
|
+
| `DownloadURL` / `DownloadName` | Explicit download href + filename hint (`DownloadURL` defaults to the video URL). |
|
|
53
|
+
| `AutoPlay` / `Loop` / `Muted` | Mapped onto the `<video>` element (defaults off). |
|
|
54
|
+
| `Preload` | `'metadata'` (default), `'none'`, or `'auto'`. |
|
|
55
|
+
| `ShowToolbar` | Render the control bar (default on). Set `false` when a host owns the chrome. |
|
|
56
|
+
| `ShowDownload` | Show the download affordance in the control bar (default on). |
|
|
57
|
+
| `ShowFullscreen` | Show the maximize / restore button — fills the browser viewport and back; `Esc` exits (default on). |
|
|
58
|
+
| `ClickToPlay` | Clicking the video toggles play/pause (default on). |
|
|
59
|
+
| `PlaybackRates` | The rate cycle the speed button steps through (default `[0.5, 1, 1.25, 1.5, 2]`). |
|
|
60
|
+
| `Volume` | Initial volume `0..1` (default `1`). |
|
|
61
|
+
| `TargetElementAddress`, `DefaultDestinationAddress`, `RenderOnLoad` | Standard pict-view placement. |
|
|
62
|
+
|
|
63
|
+
### Public API
|
|
64
|
+
|
|
65
|
+
| Method | Does |
|
|
66
|
+
|---|---|
|
|
67
|
+
| `setVideo(urlOrDescriptor)` | Point the player at a video; resets to the start. |
|
|
68
|
+
| `play()` / `pause()` / `togglePlay()` | Playback control. |
|
|
69
|
+
| `seekTo(seconds)` | Seek to an absolute time (clamped to the duration). |
|
|
70
|
+
| `setVolume(0..1)` / `toggleMute()` | Volume control. |
|
|
71
|
+
| `setPlaybackRate(rate)` / `cyclePlaybackRate()` | Playback speed. |
|
|
72
|
+
| `toggleFullscreen()` / `enterFullscreen()` / `exitFullscreen()` / `isFullscreen()` | Fill the browser viewport, and back. `Esc` also exits. |
|
|
73
|
+
| `getState()` | `{ URL, Loaded, Error, Playing, Duration, CurrentTime, Muted, Volume, PlaybackRate, Fullscreen }`. |
|
|
74
|
+
| `destroy()` | Detach handlers and clear the player. |
|
|
75
|
+
|
|
76
|
+
### Formats
|
|
77
|
+
|
|
78
|
+
The player hands the URL straight to a `<video>` element, so it plays whatever the host browser plays — the safe
|
|
79
|
+
web set is MP4 (H.264/AAC), WebM and Ogg. A container a browser cannot decode (AVI, MKV, WMV, …) is the host's
|
|
80
|
+
problem to transcode; inside `pict-section-fileview` such a file degrades to the download card rather than a dead
|
|
81
|
+
player.
|
|
82
|
+
|
|
83
|
+
## Examples
|
|
84
|
+
|
|
85
|
+
`example_applications/video_player` is a runnable demo: standalone players for a couple of clips, each with the
|
|
86
|
+
full control bar, independent on one page.
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
cd example_applications/video_player && npm run build # build the example's browser bundle
|
|
90
|
+
npm run example-serve # (from the module root) serve at http://localhost:<port>/video_player/dist/index.html
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Development
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
npm test # Mocha TDD via quackage (real jsdom document)
|
|
97
|
+
npm run build # browser bundle via quackage
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
MIT © Steven Velozo
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "pict-section-video",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pict video player section — a bespoke, responsive video player (play/seek/time/volume/rate/fullscreen) that scrubs over native HTTP Range and renders well on phones, tablets, desktops and projected displays. Standalone, or driven as the video renderer inside pict-section-fileview.",
|
|
5
|
+
"main": "source/Pict-Section-Video.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node source/Pict-Section-Video.js",
|
|
8
|
+
"build": "npx quack build",
|
|
9
|
+
"test": "npx quack test",
|
|
10
|
+
"tests": "npx quack test -g",
|
|
11
|
+
"coverage": "npx quack coverage",
|
|
12
|
+
"docs": "npx quack prepare-docs ./docs -d ./modules",
|
|
13
|
+
"docs-serve": "npx quack docs-serve ./docs",
|
|
14
|
+
"example": "npx quack examples",
|
|
15
|
+
"example-serve": "npx quack examples-serve"
|
|
16
|
+
},
|
|
17
|
+
"quackageExamples": {
|
|
18
|
+
"mode": "static"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"source"
|
|
22
|
+
],
|
|
23
|
+
"directories": {
|
|
24
|
+
"test": "test"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/fable-retold/pict-section-video.git"
|
|
29
|
+
},
|
|
30
|
+
"author": "steven velozo <steven@velozo.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/fable-retold/pict-section-video/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/fable-retold/pict-section-video#readme",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"browser-env": "^3.3.0",
|
|
38
|
+
"chai": "^4.3.0",
|
|
39
|
+
"pict": "^1.0.372",
|
|
40
|
+
"quackage": "^1.3.0"
|
|
41
|
+
},
|
|
42
|
+
"mocha": {
|
|
43
|
+
"diff": true,
|
|
44
|
+
"extension": [
|
|
45
|
+
"js"
|
|
46
|
+
],
|
|
47
|
+
"package": "./package.json",
|
|
48
|
+
"reporter": "spec",
|
|
49
|
+
"slow": "75",
|
|
50
|
+
"timeout": "5000",
|
|
51
|
+
"ui": "tdd",
|
|
52
|
+
"watch-files": [
|
|
53
|
+
"source/**/*.js",
|
|
54
|
+
"test/**/*.js"
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"pict-view": "^1.0.68"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
module.exports = (
|
|
2
|
+
{
|
|
3
|
+
"RenderOnLoad": true,
|
|
4
|
+
|
|
5
|
+
"DefaultRenderable": "VideoPlayer-Wrap",
|
|
6
|
+
"DefaultDestinationAddress": "#VideoPlayer-Container-Div",
|
|
7
|
+
|
|
8
|
+
"Templates":
|
|
9
|
+
[
|
|
10
|
+
{
|
|
11
|
+
"Hash": "VideoPlayer-Container",
|
|
12
|
+
// A stub — the player DOM is built imperatively in onAfterInitialRender (a <video> is an inherently
|
|
13
|
+
// imperative element: its src, buffered/seekable ranges and the load/timeupdate/play/pause/error events
|
|
14
|
+
// have no template equivalent). This mirrors pict-section-image / pict-section-code.
|
|
15
|
+
"Template": "<!-- VideoPlayer-Container rendering soon -->"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
|
|
19
|
+
"Renderables":
|
|
20
|
+
[
|
|
21
|
+
{
|
|
22
|
+
"RenderableHash": "VideoPlayer-Wrap",
|
|
23
|
+
"TemplateHash": "VideoPlayer-Container",
|
|
24
|
+
"DestinationAddress": "#VideoPlayer-Container-Div"
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
|
|
28
|
+
// The element the player DOM is built into (defaults to the same div the stub renders into).
|
|
29
|
+
"TargetElementAddress": "#VideoPlayer-Container-Div",
|
|
30
|
+
|
|
31
|
+
// The initial video: a URL string, or false for an empty player until setVideo() is called.
|
|
32
|
+
"VideoURL": false,
|
|
33
|
+
// A poster image shown before playback (the <video poster> attribute).
|
|
34
|
+
"PosterURL": false,
|
|
35
|
+
// A human title shown by the player and used as the download filename hint.
|
|
36
|
+
"Title": "",
|
|
37
|
+
// An explicit download href + filename hint. DownloadURL defaults to VideoURL.
|
|
38
|
+
"DownloadURL": false,
|
|
39
|
+
"DownloadName": "",
|
|
40
|
+
|
|
41
|
+
// Playback toggles (mapped straight onto the <video> element).
|
|
42
|
+
"AutoPlay": false,
|
|
43
|
+
"Loop": false,
|
|
44
|
+
"Muted": false,
|
|
45
|
+
// 'metadata' fetches just enough to learn duration + dimensions (the right default for a range-served file);
|
|
46
|
+
// 'none' defers all network until play; 'auto' lets the browser buffer ahead.
|
|
47
|
+
"Preload": "metadata",
|
|
48
|
+
|
|
49
|
+
// Chrome + behavior toggles.
|
|
50
|
+
"ShowToolbar": true, // the bespoke control bar (play / seek / time / volume / rate / download)
|
|
51
|
+
"ShowDownload": true, // the download affordance in the control bar
|
|
52
|
+
"ShowFullscreen": true, // the maximize/restore button (fills the browser viewport and back; Esc exits)
|
|
53
|
+
"ClickToPlay": true, // clicking the video toggles play/pause
|
|
54
|
+
|
|
55
|
+
// The playback-rate cycle the rate button steps through (1x is the resting value).
|
|
56
|
+
"PlaybackRates": [ 0.5, 1, 1.25, 1.5, 2 ],
|
|
57
|
+
// Initial volume (0..1).
|
|
58
|
+
"Volume": 1,
|
|
59
|
+
|
|
60
|
+
"CSS": `.psv-root
|
|
61
|
+
{
|
|
62
|
+
display: flex;
|
|
63
|
+
flex-direction: column;
|
|
64
|
+
min-height: 0;
|
|
65
|
+
height: 100%;
|
|
66
|
+
width: 100%;
|
|
67
|
+
box-sizing: border-box;
|
|
68
|
+
background: var(--theme-color-background-panel, #ffffff);
|
|
69
|
+
color: var(--theme-color-text-primary, #1f2733);
|
|
70
|
+
}
|
|
71
|
+
/* Maximized: the player leaves its host box and fills the browser viewport. Esc or the toolbar button restores it. */
|
|
72
|
+
.psv-root.psv-fullscreen
|
|
73
|
+
{
|
|
74
|
+
position: fixed;
|
|
75
|
+
top: 0;
|
|
76
|
+
right: 0;
|
|
77
|
+
bottom: 0;
|
|
78
|
+
left: 0;
|
|
79
|
+
width: 100vw;
|
|
80
|
+
height: 100vh;
|
|
81
|
+
border-radius: 0;
|
|
82
|
+
z-index: 2147483000;
|
|
83
|
+
}
|
|
84
|
+
.psv-stage
|
|
85
|
+
{
|
|
86
|
+
position: relative;
|
|
87
|
+
flex: 1 1 auto;
|
|
88
|
+
min-height: 0;
|
|
89
|
+
display: flex;
|
|
90
|
+
align-items: center;
|
|
91
|
+
justify-content: center;
|
|
92
|
+
overflow: hidden;
|
|
93
|
+
background: #000;
|
|
94
|
+
}
|
|
95
|
+
.psv-video { display: block; max-width: 100%; max-height: 100%; width: auto; height: auto; background: #000; cursor: pointer; }
|
|
96
|
+
.psv-loading, .psv-error
|
|
97
|
+
{
|
|
98
|
+
position: absolute;
|
|
99
|
+
margin: auto;
|
|
100
|
+
text-align: center;
|
|
101
|
+
color: var(--theme-color-text-on-dark, #e8ebf0);
|
|
102
|
+
padding: 1.6rem 1rem;
|
|
103
|
+
font-size: 0.9rem;
|
|
104
|
+
display: flex;
|
|
105
|
+
flex-direction: column;
|
|
106
|
+
align-items: center;
|
|
107
|
+
gap: 0.55rem;
|
|
108
|
+
}
|
|
109
|
+
.psv-error-glyph { font-size: 1.8rem; opacity: 0.7; }
|
|
110
|
+
.psv-controls
|
|
111
|
+
{
|
|
112
|
+
display: flex;
|
|
113
|
+
align-items: center;
|
|
114
|
+
gap: 0.5rem;
|
|
115
|
+
flex: 0 0 auto;
|
|
116
|
+
padding: 0.45rem 0.7rem;
|
|
117
|
+
border-top: 1px solid var(--theme-color-border-light, #e8ebf0);
|
|
118
|
+
background: var(--theme-color-background-secondary, #f7f9fb);
|
|
119
|
+
}
|
|
120
|
+
.psv-spacer { flex: 1 1 auto; }
|
|
121
|
+
.psv-btn
|
|
122
|
+
{
|
|
123
|
+
font-size: 0.8rem;
|
|
124
|
+
font-weight: 600;
|
|
125
|
+
padding: 0.3rem 0.5rem;
|
|
126
|
+
min-width: 2rem;
|
|
127
|
+
border-radius: 6px;
|
|
128
|
+
border: 1px solid var(--theme-color-border-medium, #d5dbe3);
|
|
129
|
+
background: var(--theme-color-background-panel, #ffffff);
|
|
130
|
+
color: var(--theme-color-text-secondary, #45505f);
|
|
131
|
+
cursor: pointer;
|
|
132
|
+
display: inline-flex;
|
|
133
|
+
align-items: center;
|
|
134
|
+
justify-content: center;
|
|
135
|
+
gap: 0.3rem;
|
|
136
|
+
text-decoration: none;
|
|
137
|
+
line-height: 1;
|
|
138
|
+
}
|
|
139
|
+
.psv-btn:hover { border-color: var(--theme-color-brand-primary, #5650e6); color: var(--theme-color-text-primary, #1f2733); text-decoration: none; }
|
|
140
|
+
.psv-btn[disabled] { opacity: 0.45; cursor: default; }
|
|
141
|
+
.psv-rate { font-variant-numeric: tabular-nums; min-width: 2.6rem; }
|
|
142
|
+
.psv-time { font-size: 0.74rem; color: var(--theme-color-text-muted, #6b7686); font-variant-numeric: tabular-nums; min-width: 2.8rem; text-align: center; user-select: none; }
|
|
143
|
+
.psv-seek { flex: 1 1 auto; min-width: 4rem; accent-color: var(--theme-color-brand-primary, #5650e6); cursor: pointer; }
|
|
144
|
+
.psv-volume { width: 4.5rem; flex: 0 0 auto; accent-color: var(--theme-color-brand-primary, #5650e6); cursor: pointer; }
|
|
145
|
+
`
|
|
146
|
+
});
|
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
const libPictViewClass = require('pict-view');
|
|
2
|
+
const _DefaultConfiguration = require('./Pict-Section-Video-DefaultConfiguration.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* pict-section-video — a bespoke, responsive video player section.
|
|
6
|
+
*
|
|
7
|
+
* Drop it on a page, point it at a video URL, and it fits the whole frame to the stage on phones, tablets,
|
|
8
|
+
* desktops and projected displays, with its own control bar (play/pause, a seek scrubber, a time readout,
|
|
9
|
+
* mute + volume, a playback-rate cycle, download and maximize). Seeking rides the browser's native HTTP Range
|
|
10
|
+
* requests, so a range-served file (orator-file-service, a Sluice depot, a bare 206 URL) scrubs without
|
|
11
|
+
* downloading the whole clip.
|
|
12
|
+
*
|
|
13
|
+
* It is a single pict-view in the lightweight single-view shape (like pict-section-image / pict-section-code): a
|
|
14
|
+
* stub container template, with the player DOM built imperatively in onAfterInitialRender — a <video> is
|
|
15
|
+
* inherently imperative (its src, buffered/seekable ranges, and the load/timeupdate/play/pause events have no
|
|
16
|
+
* template equivalent).
|
|
17
|
+
*
|
|
18
|
+
* Standalone, or driven as the video renderer inside pict-section-fileview: the fileview delegate mounts it with
|
|
19
|
+
* ShowFullscreen:false and ShowDownload:false (fileview owns those) but keeps the control bar (ShowToolbar:true),
|
|
20
|
+
* and feeds files through setVideo().
|
|
21
|
+
*
|
|
22
|
+
* Event wiring note: the control-bar buttons, the seek/volume inputs and the <video>'s media events use
|
|
23
|
+
* element-property handlers (`el.onclick = ...`, `video.ontimeupdate = ...`), not addEventListener and not
|
|
24
|
+
* template inline handlers. The pict rule against addEventListener is about listeners silently lost on template
|
|
25
|
+
* re-render; this widget is built once imperatively and never re-rendered by the template engine, element-property
|
|
26
|
+
* handlers cannot duplicate, and the <video>'s media events have no template-handler equivalent — the same
|
|
27
|
+
* programmatic-element exception pict-section-code / pict-section-image document.
|
|
28
|
+
*/
|
|
29
|
+
class PictSectionVideo extends libPictViewClass
|
|
30
|
+
{
|
|
31
|
+
constructor(pFable, pOptions, pServiceHash)
|
|
32
|
+
{
|
|
33
|
+
let tmpOptions = Object.assign({}, _DefaultConfiguration, pOptions);
|
|
34
|
+
super(pFable, tmpOptions, pServiceHash);
|
|
35
|
+
|
|
36
|
+
this.initialRenderComplete = false;
|
|
37
|
+
|
|
38
|
+
// DOM handles — built once in onAfterInitialRender.
|
|
39
|
+
this.targetElement = false;
|
|
40
|
+
this._rootElement = null;
|
|
41
|
+
this._stageElement = null;
|
|
42
|
+
this._videoElement = null;
|
|
43
|
+
this._loadingElement = null;
|
|
44
|
+
this._controlsElement = null;
|
|
45
|
+
this._playButtonElement = null;
|
|
46
|
+
this._seekElement = null;
|
|
47
|
+
this._currentTimeElement = null;
|
|
48
|
+
this._durationElement = null;
|
|
49
|
+
this._muteButtonElement = null;
|
|
50
|
+
this._volumeElement = null;
|
|
51
|
+
this._rateButtonElement = null;
|
|
52
|
+
this._downloadElement = null;
|
|
53
|
+
this._fullscreenButtonElement = null;
|
|
54
|
+
|
|
55
|
+
// Fullscreen (fill-the-viewport) state + the document Escape handler installed only while maximized.
|
|
56
|
+
this._fullscreen = false;
|
|
57
|
+
this._keydownHandler = null;
|
|
58
|
+
|
|
59
|
+
// View state.
|
|
60
|
+
this._url = this.options.VideoURL || '';
|
|
61
|
+
this._posterURL = this.options.PosterURL || '';
|
|
62
|
+
this._title = this.options.Title || '';
|
|
63
|
+
this._downloadURL = this.options.DownloadURL || '';
|
|
64
|
+
this._downloadName = this.options.DownloadName || '';
|
|
65
|
+
|
|
66
|
+
this._loaded = false; // metadata (duration/dimensions) available
|
|
67
|
+
this._error = false;
|
|
68
|
+
this._playing = false;
|
|
69
|
+
this._duration = 0;
|
|
70
|
+
this._currentTime = 0;
|
|
71
|
+
this._muted = !!this.options.Muted;
|
|
72
|
+
this._volume = (typeof this.options.Volume === 'number') ? Math.min(1, Math.max(0, this.options.Volume)) : 1;
|
|
73
|
+
this._playbackRate = 1;
|
|
74
|
+
|
|
75
|
+
this._playbackRates = (Array.isArray(this.options.PlaybackRates) && this.options.PlaybackRates.length)
|
|
76
|
+
? this.options.PlaybackRates.slice() : [ 1 ];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
onBeforeInitialize()
|
|
80
|
+
{
|
|
81
|
+
super.onBeforeInitialize();
|
|
82
|
+
this.targetElement = false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
onAfterRender(pRenderable)
|
|
86
|
+
{
|
|
87
|
+
// Ensure the CSS from all registered views is injected (pict rule for lazy-rendered views).
|
|
88
|
+
this.pict.CSSMap.injectCSS();
|
|
89
|
+
|
|
90
|
+
// Build the player when it isn't present — the first render, or a host re-render that re-stamped the stub
|
|
91
|
+
// container template over our built DOM. Instance state (_url, _currentTime) persists, so a rebuild re-loads
|
|
92
|
+
// the current video; this makes a re-render self-healing rather than silently wiping the player.
|
|
93
|
+
if (!this._playerPresent())
|
|
94
|
+
{
|
|
95
|
+
this.onAfterInitialRender();
|
|
96
|
+
this.initialRenderComplete = true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return super.onAfterRender(pRenderable);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Is the player DOM currently present in the target (false after the first render or a stub re-stamp)? */
|
|
103
|
+
_playerPresent()
|
|
104
|
+
{
|
|
105
|
+
return !!this._queryOwned('#' + this.Hash + '-Stage');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
onAfterInitialRender()
|
|
109
|
+
{
|
|
110
|
+
let tmpTargetElementSet = this.services.ContentAssignment.getElement(this.options.TargetElementAddress);
|
|
111
|
+
if (!tmpTargetElementSet || tmpTargetElementSet.length < 1)
|
|
112
|
+
{
|
|
113
|
+
this.log.error(`PICT-Video Could not find target element [${this.options.TargetElementAddress}]!`);
|
|
114
|
+
this.targetElement = false;
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
this.targetElement = tmpTargetElementSet[0];
|
|
118
|
+
|
|
119
|
+
this._buildPlayerDOM();
|
|
120
|
+
|
|
121
|
+
if (this._url)
|
|
122
|
+
{
|
|
123
|
+
this._loadVideo(this._url);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
128
|
+
// DOM construction (once)
|
|
129
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Build the player chrome into the target element, then grab references and wire the element-property handlers.
|
|
133
|
+
* Called once from onAfterInitialRender.
|
|
134
|
+
*/
|
|
135
|
+
_buildPlayerDOM()
|
|
136
|
+
{
|
|
137
|
+
const tmpBase = this.Hash;
|
|
138
|
+
|
|
139
|
+
// Make sure the player glyphs exist in the icon registry before the control bar references them.
|
|
140
|
+
this._ensureIcons();
|
|
141
|
+
|
|
142
|
+
let tmpControls = '';
|
|
143
|
+
if (this.options.ShowToolbar)
|
|
144
|
+
{
|
|
145
|
+
let tmpFullscreen = '';
|
|
146
|
+
if (this.options.ShowFullscreen)
|
|
147
|
+
{
|
|
148
|
+
tmpFullscreen = '<button type="button" class="psv-btn psv-fullscreen-btn" id="' + tmpBase + '-FullscreenBtn" title="Full screen">'
|
|
149
|
+
+ this._icon(this._fullscreen ? 'Minimize' : 'Maximize') + '</button>';
|
|
150
|
+
}
|
|
151
|
+
let tmpDownload = '';
|
|
152
|
+
if (this.options.ShowDownload)
|
|
153
|
+
{
|
|
154
|
+
tmpDownload = '<a class="psv-btn psv-download" id="' + tmpBase + '-Download" style="display:none" href="#" download>'
|
|
155
|
+
+ this._icon('Download') + '</a>';
|
|
156
|
+
}
|
|
157
|
+
tmpControls = '<div class="psv-controls" id="' + tmpBase + '-Controls">'
|
|
158
|
+
+ '<button type="button" class="psv-btn psv-play-btn" id="' + tmpBase + '-PlayBtn" title="Play">' + this._icon('Play') + '</button>'
|
|
159
|
+
+ '<span class="psv-time psv-current" id="' + tmpBase + '-Current">0:00</span>'
|
|
160
|
+
+ '<input type="range" class="psv-seek" id="' + tmpBase + '-Seek" min="0" max="1000" value="0" step="1" disabled title="Seek" />'
|
|
161
|
+
+ '<span class="psv-time psv-duration" id="' + tmpBase + '-Duration">0:00</span>'
|
|
162
|
+
+ '<button type="button" class="psv-btn psv-mute-btn" id="' + tmpBase + '-MuteBtn" title="Mute">' + this._icon(this._muted ? 'VolumeMute' : 'VolumeHigh') + '</button>'
|
|
163
|
+
+ '<input type="range" class="psv-volume" id="' + tmpBase + '-Volume" min="0" max="100" value="' + Math.round(this._volume * 100) + '" step="1" title="Volume" />'
|
|
164
|
+
+ '<button type="button" class="psv-btn psv-rate" id="' + tmpBase + '-RateBtn" title="Playback speed">1×</button>'
|
|
165
|
+
+ '<span class="psv-spacer"></span>'
|
|
166
|
+
+ tmpFullscreen
|
|
167
|
+
+ tmpDownload
|
|
168
|
+
+ '</div>';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const tmpPosterAttribute = this._posterURL ? (' poster="' + this._esc(this._posterURL) + '"') : '';
|
|
172
|
+
const tmpPreload = this._esc(this.options.Preload || 'metadata');
|
|
173
|
+
const tmpVideoAttributes = ''
|
|
174
|
+
+ (this.options.AutoPlay ? ' autoplay' : '')
|
|
175
|
+
+ (this.options.Loop ? ' loop' : '')
|
|
176
|
+
+ (this._muted ? ' muted' : '')
|
|
177
|
+
+ ' playsinline preload="' + tmpPreload + '"' + tmpPosterAttribute;
|
|
178
|
+
|
|
179
|
+
const tmpHTML = '<div class="psv-root" id="' + tmpBase + '-Root">'
|
|
180
|
+
+ '<div class="psv-stage" id="' + tmpBase + '-Stage">'
|
|
181
|
+
+ '<video class="psv-video" id="' + tmpBase + '-Video"' + tmpVideoAttributes + '></video>'
|
|
182
|
+
+ '<div class="psv-loading" id="' + tmpBase + '-Loading" style="display:none">' + this._icon('Play') + ' Loading…</div>'
|
|
183
|
+
+ '</div>'
|
|
184
|
+
+ tmpControls
|
|
185
|
+
+ '</div>';
|
|
186
|
+
|
|
187
|
+
this.pict.ContentAssignment.assignContent(this.options.TargetElementAddress, tmpHTML);
|
|
188
|
+
|
|
189
|
+
// Reference grab (querySelector within the owned subtree is fine — these are nodes we just built).
|
|
190
|
+
this._rootElement = this._queryOwned('#' + tmpBase + '-Root');
|
|
191
|
+
this._stageElement = this._queryOwned('#' + tmpBase + '-Stage');
|
|
192
|
+
this._videoElement = this._queryOwned('#' + tmpBase + '-Video');
|
|
193
|
+
this._loadingElement = this._queryOwned('#' + tmpBase + '-Loading');
|
|
194
|
+
this._controlsElement = this._queryOwned('#' + tmpBase + '-Controls');
|
|
195
|
+
this._playButtonElement = this._queryOwned('#' + tmpBase + '-PlayBtn');
|
|
196
|
+
this._seekElement = this._queryOwned('#' + tmpBase + '-Seek');
|
|
197
|
+
this._currentTimeElement = this._queryOwned('#' + tmpBase + '-Current');
|
|
198
|
+
this._durationElement = this._queryOwned('#' + tmpBase + '-Duration');
|
|
199
|
+
this._muteButtonElement = this._queryOwned('#' + tmpBase + '-MuteBtn');
|
|
200
|
+
this._volumeElement = this._queryOwned('#' + tmpBase + '-Volume');
|
|
201
|
+
this._rateButtonElement = this._queryOwned('#' + tmpBase + '-RateBtn');
|
|
202
|
+
this._downloadElement = this._queryOwned('#' + tmpBase + '-Download');
|
|
203
|
+
this._fullscreenButtonElement = this._queryOwned('#' + tmpBase + '-FullscreenBtn');
|
|
204
|
+
|
|
205
|
+
this._wireMediaElement();
|
|
206
|
+
this._wireControls();
|
|
207
|
+
|
|
208
|
+
// Apply the initial volume/mute to the element so the control bar and the media agree from the first frame.
|
|
209
|
+
if (this._videoElement)
|
|
210
|
+
{
|
|
211
|
+
try { this._videoElement.volume = this._volume; } catch (pError) { /* headless */ }
|
|
212
|
+
this._videoElement.muted = this._muted;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// If a host re-render re-stamped the stub over a maximized player, the rebuilt root lost the class —
|
|
216
|
+
// re-apply it so the rebuild stays in fullscreen (instance state persists). The Escape handler lives on
|
|
217
|
+
// document, so it survives the rebuild and needs no re-attaching.
|
|
218
|
+
if (this._fullscreen && this._rootElement)
|
|
219
|
+
{
|
|
220
|
+
this._rootElement.classList.add('psv-fullscreen');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Wire the <video>'s media events (element-property handlers; see the class note). */
|
|
225
|
+
_wireMediaElement()
|
|
226
|
+
{
|
|
227
|
+
if (!this._videoElement) { return; }
|
|
228
|
+
let tmpSelf = this;
|
|
229
|
+
this._videoElement.onloadedmetadata = function () { tmpSelf._onLoadedMetadata(); };
|
|
230
|
+
this._videoElement.ontimeupdate = function () { tmpSelf._onTimeUpdate(); };
|
|
231
|
+
this._videoElement.onplay = function () { tmpSelf._onPlay(); };
|
|
232
|
+
this._videoElement.onpause = function () { tmpSelf._onPause(); };
|
|
233
|
+
this._videoElement.onended = function () { tmpSelf._onEnded(); };
|
|
234
|
+
this._videoElement.onvolumechange = function () { tmpSelf._onVolumeChange(); };
|
|
235
|
+
this._videoElement.onratechange = function () { tmpSelf._onRateChange(); };
|
|
236
|
+
this._videoElement.onerror = function () { tmpSelf._onError(); };
|
|
237
|
+
if (this.options.ClickToPlay)
|
|
238
|
+
{
|
|
239
|
+
this._videoElement.onclick = function () { tmpSelf.togglePlay(); };
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Wire the control-bar buttons + sliders (element-property handlers). */
|
|
244
|
+
_wireControls()
|
|
245
|
+
{
|
|
246
|
+
if (!this.options.ShowToolbar) { return; }
|
|
247
|
+
let tmpSelf = this;
|
|
248
|
+
if (this._playButtonElement) { this._playButtonElement.onclick = function () { tmpSelf.togglePlay(); }; }
|
|
249
|
+
if (this._muteButtonElement) { this._muteButtonElement.onclick = function () { tmpSelf.toggleMute(); }; }
|
|
250
|
+
if (this._rateButtonElement) { this._rateButtonElement.onclick = function () { tmpSelf.cyclePlaybackRate(); }; }
|
|
251
|
+
if (this._fullscreenButtonElement) { this._fullscreenButtonElement.onclick = function () { tmpSelf.toggleFullscreen(); }; }
|
|
252
|
+
if (this._seekElement) { this._seekElement.oninput = function () { tmpSelf._onSeekInput(); }; }
|
|
253
|
+
if (this._volumeElement) { this._volumeElement.oninput = function () { tmpSelf._onVolumeInput(); }; }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** querySelector scoped to the player subtree, via ContentAssignment, returning the element or null. */
|
|
257
|
+
_queryOwned(pSelector)
|
|
258
|
+
{
|
|
259
|
+
let tmpSet = this.services.ContentAssignment.getElement(pSelector);
|
|
260
|
+
return (tmpSet && tmpSet.length > 0) ? tmpSet[0] : null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
264
|
+
// Media loading
|
|
265
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
266
|
+
|
|
267
|
+
_loadVideo(pURL)
|
|
268
|
+
{
|
|
269
|
+
this._loaded = false;
|
|
270
|
+
this._error = false;
|
|
271
|
+
this._playing = false;
|
|
272
|
+
this._duration = 0;
|
|
273
|
+
this._currentTime = 0;
|
|
274
|
+
|
|
275
|
+
// Recover from a prior error: _paintErrorState replaced the stage contents and dropped the <video>, so
|
|
276
|
+
// rebuild the player DOM (which recreates a fresh <video> + loading element) before loading.
|
|
277
|
+
if (!this._videoElement && this.targetElement)
|
|
278
|
+
{
|
|
279
|
+
this._buildPlayerDOM();
|
|
280
|
+
}
|
|
281
|
+
if (!this._videoElement) { return; }
|
|
282
|
+
|
|
283
|
+
if (this._posterURL) { this._videoElement.setAttribute('poster', this._posterURL); }
|
|
284
|
+
else { this._videoElement.removeAttribute('poster'); }
|
|
285
|
+
|
|
286
|
+
if (!pURL)
|
|
287
|
+
{
|
|
288
|
+
this._videoElement.removeAttribute('src');
|
|
289
|
+
this._setLoadingVisible(false);
|
|
290
|
+
this._refreshControls();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
this._setLoadingVisible(true);
|
|
295
|
+
this._videoElement.setAttribute('src', pURL);
|
|
296
|
+
// Ask the element to (re)load the new source; harmless where load() is unavailable.
|
|
297
|
+
try { if (typeof this._videoElement.load === 'function') { this._videoElement.load(); } }
|
|
298
|
+
catch (pError) { /* headless / unsupported */ }
|
|
299
|
+
|
|
300
|
+
this._refreshControls();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
_onLoadedMetadata()
|
|
304
|
+
{
|
|
305
|
+
this._loaded = true;
|
|
306
|
+
this._error = false;
|
|
307
|
+
this._duration = this._readDuration();
|
|
308
|
+
this._setLoadingVisible(false);
|
|
309
|
+
this._refreshControls();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
_onTimeUpdate()
|
|
313
|
+
{
|
|
314
|
+
this._currentTime = this._videoElement ? (this._videoElement.currentTime || 0) : 0;
|
|
315
|
+
if (!this._duration) { this._duration = this._readDuration(); }
|
|
316
|
+
this._refreshTimeDisplay();
|
|
317
|
+
this._refreshSeek();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
_onPlay() { this._playing = true; this._refreshPlayButton(); }
|
|
321
|
+
_onPause() { this._playing = false; this._refreshPlayButton(); }
|
|
322
|
+
_onEnded() { this._playing = false; this._refreshPlayButton(); }
|
|
323
|
+
|
|
324
|
+
_onVolumeChange()
|
|
325
|
+
{
|
|
326
|
+
if (!this._videoElement) { return; }
|
|
327
|
+
this._muted = !!this._videoElement.muted;
|
|
328
|
+
this._volume = (typeof this._videoElement.volume === 'number') ? this._videoElement.volume : this._volume;
|
|
329
|
+
this._refreshVolume();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
_onRateChange()
|
|
333
|
+
{
|
|
334
|
+
if (!this._videoElement) { return; }
|
|
335
|
+
this._playbackRate = this._videoElement.playbackRate || 1;
|
|
336
|
+
this._refreshRate();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
_onError()
|
|
340
|
+
{
|
|
341
|
+
this._loaded = false;
|
|
342
|
+
this._error = true;
|
|
343
|
+
this._playing = false;
|
|
344
|
+
this._setLoadingVisible(false);
|
|
345
|
+
this._paintErrorState();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** The media element's duration, or 0 when it is not a finite positive number (unknown / headless). */
|
|
349
|
+
_readDuration()
|
|
350
|
+
{
|
|
351
|
+
if (!this._videoElement) { return 0; }
|
|
352
|
+
const tmpDuration = this._videoElement.duration;
|
|
353
|
+
return (typeof tmpDuration === 'number' && isFinite(tmpDuration) && tmpDuration > 0) ? tmpDuration : 0;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
_setLoadingVisible(pVisible)
|
|
357
|
+
{
|
|
358
|
+
if (this._loadingElement) { this._loadingElement.style.display = pVisible ? 'flex' : 'none'; }
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
_paintErrorState()
|
|
362
|
+
{
|
|
363
|
+
if (!this._stageElement) { return; }
|
|
364
|
+
let tmpDownload = '';
|
|
365
|
+
if (this._downloadURL || this._url)
|
|
366
|
+
{
|
|
367
|
+
tmpDownload = '<a class="psv-btn" href="' + this._esc(this._downloadURL || this._url) + '" download'
|
|
368
|
+
+ (this._downloadName ? ('="' + this._esc(this._downloadName) + '"') : '')
|
|
369
|
+
+ '>' + this._icon('Download') + ' Download</a>';
|
|
370
|
+
}
|
|
371
|
+
const tmpBase = this.Hash;
|
|
372
|
+
this.pict.ContentAssignment.assignContent('#' + tmpBase + '-Stage',
|
|
373
|
+
'<div class="psv-error"><span class="psv-error-glyph">' + this._icon('Play') + '</span>'
|
|
374
|
+
+ '<p>This video could not be played.</p>' + tmpDownload + '</div>');
|
|
375
|
+
// The <video> node was replaced by the error block; drop stale references so a later setVideo rebuilds.
|
|
376
|
+
this._videoElement = null;
|
|
377
|
+
this._loadingElement = null;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
381
|
+
// Control-bar input handlers
|
|
382
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
383
|
+
|
|
384
|
+
/** The seek slider moved — map its 0..1000 position onto currentTime. */
|
|
385
|
+
_onSeekInput()
|
|
386
|
+
{
|
|
387
|
+
if (!this._videoElement || !this._seekElement) { return; }
|
|
388
|
+
const tmpDuration = this._duration || this._readDuration();
|
|
389
|
+
if (!tmpDuration) { return; }
|
|
390
|
+
const tmpFraction = Number(this._seekElement.value) / 1000;
|
|
391
|
+
this.seekTo(tmpFraction * tmpDuration);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** The volume slider moved — map its 0..100 position onto the element volume (and unmute). */
|
|
395
|
+
_onVolumeInput()
|
|
396
|
+
{
|
|
397
|
+
if (!this._volumeElement) { return; }
|
|
398
|
+
this.setVolume(Number(this._volumeElement.value) / 100);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
402
|
+
// Control refresh
|
|
403
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
404
|
+
|
|
405
|
+
_refreshControls()
|
|
406
|
+
{
|
|
407
|
+
this._refreshPlayButton();
|
|
408
|
+
this._refreshTimeDisplay();
|
|
409
|
+
this._refreshSeek();
|
|
410
|
+
this._refreshVolume();
|
|
411
|
+
this._refreshRate();
|
|
412
|
+
this._refreshDownload();
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
_refreshPlayButton()
|
|
416
|
+
{
|
|
417
|
+
if (!this._playButtonElement) { return; }
|
|
418
|
+
this._playButtonElement.innerHTML = this._icon(this._playing ? 'Pause' : 'Play');
|
|
419
|
+
this._playButtonElement.setAttribute('title', this._playing ? 'Pause' : 'Play');
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
_refreshTimeDisplay()
|
|
423
|
+
{
|
|
424
|
+
if (this._currentTimeElement) { this._currentTimeElement.textContent = this._formatTime(this._currentTime); }
|
|
425
|
+
if (this._durationElement) { this._durationElement.textContent = this._formatTime(this._duration); }
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
_refreshSeek()
|
|
429
|
+
{
|
|
430
|
+
if (!this._seekElement) { return; }
|
|
431
|
+
const tmpDuration = this._duration || this._readDuration();
|
|
432
|
+
this._seekElement.disabled = !tmpDuration;
|
|
433
|
+
// Do not clobber the slider while the reader is scrubbing it (it owns the focus during a drag/keyboard seek).
|
|
434
|
+
if (this._isElementFocused(this._seekElement)) { return; }
|
|
435
|
+
const tmpValue = tmpDuration ? Math.round((this._currentTime / tmpDuration) * 1000) : 0;
|
|
436
|
+
this._seekElement.value = String(Math.max(0, Math.min(1000, tmpValue)));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
_refreshVolume()
|
|
440
|
+
{
|
|
441
|
+
if (this._muteButtonElement)
|
|
442
|
+
{
|
|
443
|
+
const tmpSilent = this._muted || this._volume <= 0;
|
|
444
|
+
this._muteButtonElement.innerHTML = this._icon(tmpSilent ? 'VolumeMute' : 'VolumeHigh');
|
|
445
|
+
this._muteButtonElement.setAttribute('title', tmpSilent ? 'Unmute' : 'Mute');
|
|
446
|
+
}
|
|
447
|
+
if (this._volumeElement && !this._isElementFocused(this._volumeElement))
|
|
448
|
+
{
|
|
449
|
+
this._volumeElement.value = String(Math.round((this._muted ? 0 : this._volume) * 100));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
_refreshRate()
|
|
454
|
+
{
|
|
455
|
+
if (!this._rateButtonElement) { return; }
|
|
456
|
+
const tmpRate = this._playbackRate || 1;
|
|
457
|
+
// Trim a trailing .0 (1.0 → "1", 1.25 → "1.25").
|
|
458
|
+
const tmpLabel = (Math.round(tmpRate * 100) / 100).toString();
|
|
459
|
+
this._rateButtonElement.innerHTML = tmpLabel + '×';
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
_refreshDownload()
|
|
463
|
+
{
|
|
464
|
+
if (!this._downloadElement) { return; }
|
|
465
|
+
const tmpHref = this._downloadURL || this._url;
|
|
466
|
+
if (tmpHref)
|
|
467
|
+
{
|
|
468
|
+
this._downloadElement.setAttribute('href', tmpHref);
|
|
469
|
+
if (this._downloadName) { this._downloadElement.setAttribute('download', this._downloadName); }
|
|
470
|
+
this._downloadElement.style.display = 'inline-flex';
|
|
471
|
+
}
|
|
472
|
+
else
|
|
473
|
+
{
|
|
474
|
+
this._downloadElement.style.display = 'none';
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
_isElementFocused(pElement)
|
|
479
|
+
{
|
|
480
|
+
return !!(pElement && (typeof document !== 'undefined') && document.activeElement === pElement);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
484
|
+
// Public API
|
|
485
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Point the player at a video. Accepts a URL string or a descriptor object
|
|
489
|
+
* ({ URL, PosterURL?, Title?, Name?, DownloadURL?, DownloadName? }). Resets playback to the start.
|
|
490
|
+
* @param {(string|Object)} pInput
|
|
491
|
+
*/
|
|
492
|
+
setVideo(pInput)
|
|
493
|
+
{
|
|
494
|
+
let tmpURL = '';
|
|
495
|
+
let tmpPoster = '';
|
|
496
|
+
let tmpTitle = this._title;
|
|
497
|
+
let tmpDownloadURL = '';
|
|
498
|
+
let tmpDownloadName = '';
|
|
499
|
+
|
|
500
|
+
if (typeof pInput === 'string')
|
|
501
|
+
{
|
|
502
|
+
tmpURL = pInput;
|
|
503
|
+
}
|
|
504
|
+
else if (pInput && typeof pInput === 'object')
|
|
505
|
+
{
|
|
506
|
+
tmpURL = pInput.URL || pInput.Url || pInput.VideoURL || '';
|
|
507
|
+
tmpPoster = pInput.PosterURL || pInput.Poster || '';
|
|
508
|
+
tmpTitle = pInput.Title || pInput.Name || this._title;
|
|
509
|
+
tmpDownloadURL = pInput.DownloadURL || '';
|
|
510
|
+
tmpDownloadName = pInput.DownloadName || pInput.Name || '';
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
this._url = tmpURL;
|
|
514
|
+
this._posterURL = tmpPoster || '';
|
|
515
|
+
this._title = tmpTitle || '';
|
|
516
|
+
this._downloadURL = tmpDownloadURL || tmpURL;
|
|
517
|
+
this._downloadName = tmpDownloadName || '';
|
|
518
|
+
|
|
519
|
+
if (this.targetElement)
|
|
520
|
+
{
|
|
521
|
+
this._loadVideo(tmpURL);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** Start playback (best-effort — a browser may reject autoplay; headless is a no-op). */
|
|
526
|
+
play()
|
|
527
|
+
{
|
|
528
|
+
if (!this._videoElement) { return; }
|
|
529
|
+
try
|
|
530
|
+
{
|
|
531
|
+
const tmpResult = this._videoElement.play();
|
|
532
|
+
if (tmpResult && (typeof tmpResult.catch === 'function')) { tmpResult.catch(() => { /* autoplay rejected */ }); }
|
|
533
|
+
}
|
|
534
|
+
catch (pError) { /* headless / unsupported */ }
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Pause playback. */
|
|
538
|
+
pause()
|
|
539
|
+
{
|
|
540
|
+
if (!this._videoElement) { return; }
|
|
541
|
+
try { this._videoElement.pause(); } catch (pError) { /* headless */ }
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Toggle play/pause. */
|
|
545
|
+
togglePlay()
|
|
546
|
+
{
|
|
547
|
+
if (this._playing) { this.pause(); } else { this.play(); }
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** Seek to an absolute time in seconds (clamped to the known duration). */
|
|
551
|
+
seekTo(pSeconds)
|
|
552
|
+
{
|
|
553
|
+
if (!this._videoElement) { return; }
|
|
554
|
+
let tmpSeconds = Number(pSeconds);
|
|
555
|
+
if (!isFinite(tmpSeconds) || tmpSeconds < 0) { tmpSeconds = 0; }
|
|
556
|
+
const tmpDuration = this._duration || this._readDuration();
|
|
557
|
+
if (tmpDuration && tmpSeconds > tmpDuration) { tmpSeconds = tmpDuration; }
|
|
558
|
+
try { this._videoElement.currentTime = tmpSeconds; } catch (pError) { /* headless */ }
|
|
559
|
+
this._currentTime = tmpSeconds;
|
|
560
|
+
this._refreshTimeDisplay();
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** Set the volume (0..1); any positive volume also unmutes. */
|
|
564
|
+
setVolume(pVolume)
|
|
565
|
+
{
|
|
566
|
+
let tmpVolume = Number(pVolume);
|
|
567
|
+
if (!isFinite(tmpVolume)) { tmpVolume = 0; }
|
|
568
|
+
tmpVolume = Math.min(1, Math.max(0, tmpVolume));
|
|
569
|
+
this._volume = tmpVolume;
|
|
570
|
+
if (this._videoElement)
|
|
571
|
+
{
|
|
572
|
+
try { this._videoElement.volume = tmpVolume; } catch (pError) { /* headless */ }
|
|
573
|
+
this._videoElement.muted = (tmpVolume <= 0);
|
|
574
|
+
}
|
|
575
|
+
this._muted = (tmpVolume <= 0);
|
|
576
|
+
this._refreshVolume();
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** Toggle mute. */
|
|
580
|
+
toggleMute()
|
|
581
|
+
{
|
|
582
|
+
this._muted = !this._muted;
|
|
583
|
+
if (this._videoElement) { this._videoElement.muted = this._muted; }
|
|
584
|
+
this._refreshVolume();
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/** Step to the next playback rate in the configured cycle. */
|
|
588
|
+
cyclePlaybackRate()
|
|
589
|
+
{
|
|
590
|
+
if (!this._playbackRates.length) { return; }
|
|
591
|
+
let tmpIndex = this._playbackRates.indexOf(this._playbackRate);
|
|
592
|
+
tmpIndex = (tmpIndex + 1) % this._playbackRates.length;
|
|
593
|
+
this.setPlaybackRate(this._playbackRates[tmpIndex]);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** Set the playback rate directly. */
|
|
597
|
+
setPlaybackRate(pRate)
|
|
598
|
+
{
|
|
599
|
+
let tmpRate = Number(pRate);
|
|
600
|
+
if (!isFinite(tmpRate) || tmpRate <= 0) { tmpRate = 1; }
|
|
601
|
+
this._playbackRate = tmpRate;
|
|
602
|
+
if (this._videoElement) { try { this._videoElement.playbackRate = tmpRate; } catch (pError) { /* headless */ } }
|
|
603
|
+
this._refreshRate();
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
607
|
+
// Fullscreen — fill the browser viewport, and back
|
|
608
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
609
|
+
|
|
610
|
+
/** Toggle the maximized (fill-the-viewport) presentation. */
|
|
611
|
+
toggleFullscreen()
|
|
612
|
+
{
|
|
613
|
+
if (this._fullscreen) { this.exitFullscreen(); } else { this.enterFullscreen(); }
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Maximize: lift the player out of its host box to fill the browser viewport (Esc or the button restores it). */
|
|
617
|
+
enterFullscreen()
|
|
618
|
+
{
|
|
619
|
+
if (this._fullscreen) { return; }
|
|
620
|
+
this._fullscreen = true;
|
|
621
|
+
if (this._rootElement) { this._rootElement.classList.add('psv-fullscreen'); }
|
|
622
|
+
this._installEscapeHandler();
|
|
623
|
+
this._refreshFullscreenButton();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/** Restore the player to its host box. */
|
|
627
|
+
exitFullscreen()
|
|
628
|
+
{
|
|
629
|
+
if (!this._fullscreen) { return; }
|
|
630
|
+
this._fullscreen = false;
|
|
631
|
+
if (this._rootElement) { this._rootElement.classList.remove('psv-fullscreen'); }
|
|
632
|
+
this._removeEscapeHandler();
|
|
633
|
+
this._refreshFullscreenButton();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/** Is the player currently maximized? */
|
|
637
|
+
isFullscreen()
|
|
638
|
+
{
|
|
639
|
+
return this._fullscreen;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/** Swap the maximize/restore glyph + title to match the current state. */
|
|
643
|
+
_refreshFullscreenButton()
|
|
644
|
+
{
|
|
645
|
+
if (!this._fullscreenButtonElement) { return; }
|
|
646
|
+
this._fullscreenButtonElement.innerHTML = this._icon(this._fullscreen ? 'Minimize' : 'Maximize');
|
|
647
|
+
this._fullscreenButtonElement.setAttribute('title', this._fullscreen ? 'Exit full screen' : 'Full screen');
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Install a document-level Escape handler while maximized. A document keydown listener is the documented pict
|
|
652
|
+
* exception (a browser-level event with no element-level inline equivalent). It is added only while fullscreen
|
|
653
|
+
* and removed on exit / destroy.
|
|
654
|
+
*/
|
|
655
|
+
_installEscapeHandler()
|
|
656
|
+
{
|
|
657
|
+
if (this._keydownHandler || (typeof document === 'undefined') || (typeof document.addEventListener !== 'function')) { return; }
|
|
658
|
+
let tmpSelf = this;
|
|
659
|
+
this._keydownHandler = function (pEvent)
|
|
660
|
+
{
|
|
661
|
+
if (pEvent && pEvent.key === 'Escape' && tmpSelf._fullscreen) { tmpSelf.exitFullscreen(); }
|
|
662
|
+
};
|
|
663
|
+
document.addEventListener('keydown', this._keydownHandler);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** Remove the document-level Escape handler. */
|
|
667
|
+
_removeEscapeHandler()
|
|
668
|
+
{
|
|
669
|
+
if (this._keydownHandler && (typeof document !== 'undefined') && (typeof document.removeEventListener === 'function'))
|
|
670
|
+
{
|
|
671
|
+
document.removeEventListener('keydown', this._keydownHandler);
|
|
672
|
+
}
|
|
673
|
+
this._keydownHandler = null;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** Register the player glyphs in the icon registry (idempotent; no-op without an Icon provider). */
|
|
677
|
+
_ensureIcons()
|
|
678
|
+
{
|
|
679
|
+
if (!this.pict || !this.pict.providers || !this.pict.providers.Icon || (typeof this.pict.providers.Icon.registerSet !== 'function')) { return; }
|
|
680
|
+
this.pict.providers.Icon.registerSet(
|
|
681
|
+
{
|
|
682
|
+
Outline:
|
|
683
|
+
{
|
|
684
|
+
Play: '<svg viewBox="0 0 24 24" fill="currentColor" stroke="none"><path d="M8 5v14l11-7z"/></svg>',
|
|
685
|
+
Pause: '<svg viewBox="0 0 24 24" fill="currentColor" stroke="none"><path d="M6 5h4v14H6zM14 5h4v14h-4z"/></svg>',
|
|
686
|
+
VolumeHigh: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 9v6h4l5 4V5L8 9H4z"/><path d="M16 8a5 5 0 0 1 0 8M18.5 5.5a9 9 0 0 1 0 13"/></svg>',
|
|
687
|
+
VolumeMute: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 9v6h4l5 4V5L8 9H4z"/><path d="M22 9l-6 6M16 9l6 6"/></svg>',
|
|
688
|
+
Maximize: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5"/></svg>',
|
|
689
|
+
Minimize: '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 4v5H4M15 4v5h5M9 20v-5H4M15 20v-5h5"/></svg>'
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** A snapshot of the player's current state (handy for hosts and tests). */
|
|
695
|
+
getState()
|
|
696
|
+
{
|
|
697
|
+
return (
|
|
698
|
+
{
|
|
699
|
+
URL: this._url,
|
|
700
|
+
Loaded: this._loaded,
|
|
701
|
+
Error: this._error,
|
|
702
|
+
Playing: this._playing,
|
|
703
|
+
Duration: this._duration,
|
|
704
|
+
CurrentTime: this._currentTime,
|
|
705
|
+
Muted: this._muted,
|
|
706
|
+
Volume: this._volume,
|
|
707
|
+
PlaybackRate: this._playbackRate,
|
|
708
|
+
Fullscreen: this._fullscreen
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/** Tear down handlers and clear the player. */
|
|
713
|
+
destroy()
|
|
714
|
+
{
|
|
715
|
+
this._removeEscapeHandler();
|
|
716
|
+
this._fullscreen = false;
|
|
717
|
+
|
|
718
|
+
if (this._videoElement)
|
|
719
|
+
{
|
|
720
|
+
try { this._videoElement.pause(); } catch (pError) { /* ok */ }
|
|
721
|
+
this._videoElement.onloadedmetadata = null;
|
|
722
|
+
this._videoElement.ontimeupdate = null;
|
|
723
|
+
this._videoElement.onplay = null;
|
|
724
|
+
this._videoElement.onpause = null;
|
|
725
|
+
this._videoElement.onended = null;
|
|
726
|
+
this._videoElement.onvolumechange = null;
|
|
727
|
+
this._videoElement.onratechange = null;
|
|
728
|
+
this._videoElement.onerror = null;
|
|
729
|
+
this._videoElement.onclick = null;
|
|
730
|
+
this._videoElement.removeAttribute('src');
|
|
731
|
+
try { if (typeof this._videoElement.load === 'function') { this._videoElement.load(); } } catch (pError) { /* ok */ }
|
|
732
|
+
}
|
|
733
|
+
if (this._playButtonElement) { this._playButtonElement.onclick = null; }
|
|
734
|
+
if (this._muteButtonElement) { this._muteButtonElement.onclick = null; }
|
|
735
|
+
if (this._rateButtonElement) { this._rateButtonElement.onclick = null; }
|
|
736
|
+
if (this._fullscreenButtonElement) { this._fullscreenButtonElement.onclick = null; }
|
|
737
|
+
if (this._seekElement) { this._seekElement.oninput = null; }
|
|
738
|
+
if (this._volumeElement) { this._volumeElement.oninput = null; }
|
|
739
|
+
|
|
740
|
+
if (this.targetElement && this.pict && this.pict.ContentAssignment)
|
|
741
|
+
{
|
|
742
|
+
this.pict.ContentAssignment.assignContent(this.options.TargetElementAddress, '');
|
|
743
|
+
}
|
|
744
|
+
this._rootElement = null;
|
|
745
|
+
this._stageElement = null;
|
|
746
|
+
this._videoElement = null;
|
|
747
|
+
this._loadingElement = null;
|
|
748
|
+
this._controlsElement = null;
|
|
749
|
+
this._playButtonElement = null;
|
|
750
|
+
this._seekElement = null;
|
|
751
|
+
this._currentTimeElement = null;
|
|
752
|
+
this._durationElement = null;
|
|
753
|
+
this._muteButtonElement = null;
|
|
754
|
+
this._volumeElement = null;
|
|
755
|
+
this._rateButtonElement = null;
|
|
756
|
+
this._downloadElement = null;
|
|
757
|
+
this._fullscreenButtonElement = null;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
761
|
+
// Small utilities
|
|
762
|
+
// -----------------------------------------------------------------------------------------------------------
|
|
763
|
+
|
|
764
|
+
/** A themable glyph from the icon registry; '' when no registry is present. */
|
|
765
|
+
_icon(pName)
|
|
766
|
+
{
|
|
767
|
+
if (!this.pict || (typeof this.pict.icon !== 'function')) { return ''; }
|
|
768
|
+
try { return this.pict.icon(pName); }
|
|
769
|
+
catch (pError) { return ''; }
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** Format seconds as m:ss (or h:mm:ss past an hour); '0:00' for an unknown/zero duration. */
|
|
773
|
+
_formatTime(pSeconds)
|
|
774
|
+
{
|
|
775
|
+
let tmpSeconds = Number(pSeconds);
|
|
776
|
+
if (!isFinite(tmpSeconds) || tmpSeconds < 0) { tmpSeconds = 0; }
|
|
777
|
+
tmpSeconds = Math.floor(tmpSeconds);
|
|
778
|
+
const tmpHours = Math.floor(tmpSeconds / 3600);
|
|
779
|
+
const tmpMinutes = Math.floor((tmpSeconds % 3600) / 60);
|
|
780
|
+
const tmpSecs = tmpSeconds % 60;
|
|
781
|
+
const tmpPad = function (pValue) { return (pValue < 10) ? ('0' + pValue) : String(pValue); };
|
|
782
|
+
if (tmpHours > 0) { return tmpHours + ':' + tmpPad(tmpMinutes) + ':' + tmpPad(tmpSecs); }
|
|
783
|
+
return tmpMinutes + ':' + tmpPad(tmpSecs);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/** Escape HTML metacharacters for safe interpolation into built markup. */
|
|
787
|
+
_esc(pText)
|
|
788
|
+
{
|
|
789
|
+
if (pText === null || typeof pText === 'undefined') { return ''; }
|
|
790
|
+
return String(pText)
|
|
791
|
+
.replace(/&/g, '&')
|
|
792
|
+
.replace(/</g, '<')
|
|
793
|
+
.replace(/>/g, '>')
|
|
794
|
+
.replace(/"/g, '"')
|
|
795
|
+
.replace(/'/g, ''');
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
module.exports = PictSectionVideo;
|
|
800
|
+
|
|
801
|
+
module.exports.default_configuration = _DefaultConfiguration;
|