UNPKG

6.93 kBJavaScriptView Raw
1import { defineComponent, ref, onMounted, onBeforeUnmount } from '@vue/composition-api';
2
3//
4var script = defineComponent({
5 name: 'VPip',
6 props: {
7 // Video related options
8 videoOptions: {
9 type: Object,
10 required: false,
11 default: function _default() {
12 return {
13 wrapper: '',
14 src: '',
15 poster: '',
16 class: '',
17 height: '100%',
18 width: '100%'
19 };
20 }
21 },
22 // button related options
23 buttonOptions: {
24 type: Object,
25 required: false,
26 default: function _default() {
27 return {
28 wrapper: '',
29 type: 'button',
30 class: '',
31 label: 'Toggle picture-in-picture'
32 };
33 }
34 },
35 wrapper: {
36 type: String,
37 default: '',
38 required: false
39 }
40 },
41
42 setup(props, _ref) {
43 var emit = _ref.emit;
44 // State
45 var video = ref(null);
46 var isPipSupported = ref(false); // Lifecycle Hooks
47
48 onMounted(function () {
49 isPipSupported.value = 'pictureInPictureEnabled' in document;
50 video.value.addEventListener('enterpictureinpicture', function () {
51 emit('video-in-pip', true);
52 });
53 video.value.addEventListener('leavepictureinpicture', function () {
54 emit('video-in-pip', false);
55 });
56 });
57 onBeforeUnmount(function () {
58 video.value.removeEventListener('enterpictureinpicture');
59 video.value.removeEventListener('leavepictureinpicture');
60 }); // Methods
61
62 var togglePip = function togglePip() {
63 // If there is no element in Picture-in-Picture yet, let’s request
64 // Picture-in-Picture for the video, otherwise leave it.
65 if (!document.pictureInPictureElement) {
66 video.value.requestPictureInPicture().catch(function (error) {
67 // Video failed to enter Picture-in-Picture mode.
68 emit('requesting-pip-failure', error);
69 });
70 } else {
71 document.exitPictureInPicture().catch(function (error) {
72 // Video failed to leave Picture-in-Picture mode.
73 emit('exiting-pip-failure', error);
74 });
75 }
76 };
77
78 return {
79 video,
80 isPipSupported,
81 togglePip
82 };
83 }
84
85});
86
87function normalizeComponent(template, style, script, scopeId, isFunctionalTemplate, moduleIdentifier /* server only */, shadowMode, createInjector, createInjectorSSR, createInjectorShadow) {
88 if (typeof shadowMode !== 'boolean') {
89 createInjectorSSR = createInjector;
90 createInjector = shadowMode;
91 shadowMode = false;
92 }
93 // Vue.extend constructor export interop.
94 const options = typeof script === 'function' ? script.options : script;
95 // render functions
96 if (template && template.render) {
97 options.render = template.render;
98 options.staticRenderFns = template.staticRenderFns;
99 options._compiled = true;
100 // functional template
101 if (isFunctionalTemplate) {
102 options.functional = true;
103 }
104 }
105 // scopedId
106 if (scopeId) {
107 options._scopeId = scopeId;
108 }
109 let hook;
110 if (moduleIdentifier) {
111 // server build
112 hook = function (context) {
113 // 2.3 injection
114 context =
115 context || // cached call
116 (this.$vnode && this.$vnode.ssrContext) || // stateful
117 (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext); // functional
118 // 2.2 with runInNewContext: true
119 if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
120 context = __VUE_SSR_CONTEXT__;
121 }
122 // inject component styles
123 if (style) {
124 style.call(this, createInjectorSSR(context));
125 }
126 // register component module identifier for async chunk inference
127 if (context && context._registeredComponents) {
128 context._registeredComponents.add(moduleIdentifier);
129 }
130 };
131 // used by ssr in case component is cached and beforeCreate
132 // never gets called
133 options._ssrRegister = hook;
134 }
135 else if (style) {
136 hook = shadowMode
137 ? function (context) {
138 style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot));
139 }
140 : function (context) {
141 style.call(this, createInjector(context));
142 };
143 }
144 if (hook) {
145 if (options.functional) {
146 // register for functional component in vue file
147 const originalRender = options.render;
148 options.render = function renderWithStyleInjection(h, context) {
149 hook.call(context);
150 return originalRender(h, context);
151 };
152 }
153 else {
154 // inject component registration as beforeCreate hook
155 const existing = options.beforeCreate;
156 options.beforeCreate = existing ? [].concat(existing, hook) : [hook];
157 }
158 }
159 return script;
160}
161
162/* script */
163const __vue_script__ = script;
164
165/* template */
166var __vue_render__ = function() {
167 var _vm = this;
168 var _h = _vm.$createElement;
169 var _c = _vm._self._c || _h;
170 return _c("div", { class: _vm.wrapper }, [
171 _c("div", { class: _vm.videoOptions.wrapper }, [
172 _c("video", {
173 ref: "video",
174 class: _vm.videoOptions.class,
175 attrs: {
176 src: _vm.videoOptions.src,
177 poster: _vm.videoOptions.poster,
178 height: _vm.videoOptions.height,
179 width: _vm.videoOptions.width,
180 controls: ""
181 }
182 })
183 ]),
184 _vm._v(" "),
185 _c("div", { class: _vm.buttonOptions.wrapper }, [
186 _vm.isPipSupported
187 ? _c(
188 "button",
189 {
190 class: _vm.buttonOptions.class,
191 attrs: { type: _vm.buttonOptions.type },
192 on: { click: _vm.togglePip }
193 },
194 [_vm._v("\n " + _vm._s(_vm.buttonOptions.label) + "\n ")]
195 )
196 : _vm._e()
197 ])
198 ])
199};
200var __vue_staticRenderFns__ = [];
201__vue_render__._withStripped = true;
202
203 /* style */
204 const __vue_inject_styles__ = undefined;
205 /* scoped */
206 const __vue_scope_id__ = undefined;
207 /* module identifier */
208 const __vue_module_identifier__ = undefined;
209 /* functional template */
210 const __vue_is_functional_template__ = false;
211 /* style inject */
212
213 /* style inject SSR */
214
215 /* style inject shadow dom */
216
217
218
219 const __vue_component__ = normalizeComponent(
220 { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
221 __vue_inject_styles__,
222 __vue_script__,
223 __vue_scope_id__,
224 __vue_is_functional_template__,
225 __vue_module_identifier__,
226 false,
227 undefined,
228 undefined,
229 undefined
230 );
231
232export default __vue_component__;