-
Notifications
You must be signed in to change notification settings - Fork 1
/
media.js
192 lines (180 loc) · 7.43 KB
/
media.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
/**
* This file is part of the Media (Image,Video,Audio) Element for formBuilder.
* https://github.com/lucasnetau/formBuilder-plugin-media
*
* (c) James Lucas <james@lucas.net.au>
*
* @license MIT
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* To disable the default handler for files (convert to dataURI) configure this via controlConfig
*
* A custom handler can be
* ```
* var renderOpts = {
* controlConfig: {
* 'media.image': {
* default_change_handler: false,
* change_handler: (event) => {}
* }
* //Need to repeat config for media.video and media.image since Formbuilder does not load controlControl for parent type
* }
* };
*/
if (!window.fbControls) { window.fbControls = []; }
window.fbControls.push(function media(controlClass) {
class controlMedia extends controlClass {
/**
* Load embedded Javascript
*/
configure() {
if (this.preview) {
let changeHandler = this.defaultChangeHandler;
if ((this.classConfig.default_change_handler ?? true) === false) {
changeHandler = undefined;
}
if (typeof this.classConfig.change_handler === 'function') {
changeHandler = this.classConfig.change_handler
}
if (changeHandler) {
const fieldId = this.id.replace(/-preview$/, "")
const marker = `controlMediaEmbedded-${fieldId}`;
const cache = window.fbLoaded.js; //Reuse the FormBuilder cache to ensure we only load the media control JS once
if (!cache.includes(marker)) {
const input = $(`input[value="${fieldId}"]`).closest('.form-elements').find('input[name="media-file-upload"]');
$(input).on('change', changeHandler)
cache.push(marker);
}
}
}
}
defaultChangeHandler(event) {
const input = $(event.target);
let reader = new FileReader();
//Async read of the uploaded file and convert to a DataURI. Detect mimetype and adjust mimetype attribute and control Subtype
reader.addEventListener("load", function () {
const regexp = /^data:((?:\w+\/(?:(?!;).)+)?)/;
const elementContainer = input.closest('.form-elements'); //The container for an element's configuration fields
const srcElement = elementContainer.find('.fld-src');
const dataUri = this.result;
const mediatype = dataUri.match(regexp);
if (null !== mediatype) {
elementContainer.find('.fld-mimetype').val(mediatype[1]);
let pluginSubtype;
if (mediatype[1].startsWith('image/')) {
pluginSubtype = 'image';
} else if (mediatype[1].startsWith('video/')) {
pluginSubtype = 'video';
} else {
pluginSubtype = 'audio';
}
elementContainer.find('.fld-subtype').val(pluginSubtype);
}
srcElement.val(dataUri).trigger('change');
input.val("");
});
reader.readAsDataURL(input[0].files[0]);
}
/**
* Class configuration - return the icons & label related to this control
* @return {object} definition
*/
static get definition() {
return {
icon: '🖼️',
i18n: {
default: 'Media',
},
defaultAttrs: {
'className': {
label: "Class",
value: 'img-fluid',
type: 'text',
},
'description': {
label: "Help Text",
value: '',
type: 'text',
},
'src': {
label: "Src",
value: '',
type: 'textarea', //text inputs do not handle large data URI strings, need to use textarea to ensure the browser doesn't hang
},
'mimetype': {
label: "Mime Type",
value: '',
type: 'text',
description: 'Mimetype of Media',
//readonly: true,
},
'media-file-upload': {
label: "File",
value: '',
type: 'file',
description: 'Upload a media file (Image, Audio, Video)',
accept:"image/*,video/mp4,video/x-m4v,video/*,audio/x-m4a,audio/*",
},
'width': {
label: "Width",
value: '200',
type: 'text',
},
'height': {
label: "Height",
value: 'auto',
type: 'text',
},
'subtype': {
label: 'Media Type',
options: {
'image': 'Image',
'video': 'Video',
'audio': 'Audio',
},
},
},
};
}
/**
* Build the HTML5 attribute for the specified media type
* @return {Object} DOM Element to be injected into the form.
*/
build() {
const {...attrs} = this.config;
delete(attrs.type);
switch(this.subtype) {
case 'image':
let caption = this.markup('figcaption', this.label, {});
let img = this.markup('img', null, attrs);
this.field = this.markup('figure', [img, caption,], attrs);
return {
field: this.field,
layout: 'noLabel',
};
case 'video':
attrs.controls = true;
attrs.controlsList = "nodownload";
let videoSource = this.markup('source', null, {src: attrs.src, type: attrs.mimetype,});
this.field = this.markup('video', [videoSource, '<p>Your browser does not support HTML5 video</p>',], attrs);
return this.field;
case 'audio':
attrs.controls = true;
let audioSource = this.markup('source', null, {src: attrs.src, type: attrs.mimetype,});
this.field = this.markup('audio', [audioSource, '<p>Your browser does not support HTML5 audio</p>',], attrs);
return this.field;
}
}
/**
* onRender callback
*/
onRender() {
}
}
// register this control for the following types & text subtypes
controlClass.register('media', controlMedia);
controlClass.register(['image','video','audio',], controlMedia, 'media');
return controlMedia;
});