-
Notifications
You must be signed in to change notification settings - Fork 24
/
summernote-video-attributes.js
executable file
·328 lines (308 loc) · 16.5 KB
/
summernote-video-attributes.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* https://github.com/DiemenDesign/summernote-video-attributes */
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'],factory);
} else if(typeof module === 'object' && module.exports) {
module.exports = factory(require('jquery'));
} else {
factory(window.jQuery);
}
}(function ($) {
$.extend(true,$.summernote.lang, {
'en-US': { /* English */
videoAttributes: {
dialogTitle: 'Video Attributes',
tooltip: 'Video Attributes',
pluginTitle: 'Video Attributes',
href: 'URL',
videoSize: 'Video size',
videoOption0: 'Responsive',
videoOption1: '1280x720',
videoOption2: '853x480',
videoOption3: '640x360',
videoOption4: '560x315',
alignment: 'Alignment',
alignmentOption0: 'None',
alignmentOption1: 'Left',
alignmentOption2: 'Right',
alignmentOption3: 'Initial',
alignmentOption4: 'Inherit',
suggested: 'Show Suggested videos when the video finishes',
controls: 'Show player controls',
autoplay: 'Autoplay',
loop: 'Loop',
note: 'Note: Not all options are available with all services...',
ok: 'OK'
}
}
});
var defaults = {
showVideoSize: true,
showVideoAlignment: true,
showSuggestedVideos: true,
showPlayerControls: true,
showAutoplay: true,
showLoop: true,
icon: '<i class="note-icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 14" width="14" height="14"><path d="m 12.503106,4.03105 -3.087752,0 -0.09237,-0.72049 c 0,-0.41163 -0.333714,-0.74534 -0.745341,-0.74534 l -3.180124,0 c -0.411628,0 -0.745342,0.33372 -0.745342,0.74534 l -0.09237,0.72049 -3.062907,0 C 1.22246,4.03105 1,4.24109 1,4.51553 l 0,6.40993 c 0,0.27444 0.22246,0.50932 0.496894,0.50932 l 11.006212,0 C 12.77754,11.43478 13,11.1999 13,10.92546 L 13,4.51553 C 13,4.24109 12.77754,4.03105 12.503106,4.03105 Z M 7.00236,10.77794 c -1.687652,0 -3.055751,-1.3681 -3.055751,-3.05573 0,-1.68765 1.368099,-3.05574 3.055751,-3.05574 1.687652,0 3.055752,1.3681 3.055752,3.05574 0,1.6876 -1.3681,3.05573 -3.055752,3.05573 z m 5.426211,-5.43012 -2.608695,0 0,-0.77017 2.608695,0 0,0.77017 z M 8.876472,7.71575 A 1.8687454,1.8687454 0 0 1 7.007727,9.58449 1.8687454,1.8687454 0 0 1 5.138981,7.71575 1.8687454,1.8687454 0 0 1 7.007727,5.84701 1.8687454,1.8687454 0 0 1 8.876472,7.71575 Z"/></svg></i>'
};
$.extend($.summernote.plugins, {
'videoAttributes': function (context) {
var self = this,
ui = $.summernote.ui,
$editor = context.layoutInfo.editor,
$editable = context.layoutInfo.editable,
options = $.extend({}, defaults, context.options),
lang = options.langInfo;
context.memo('button.videoAttributes', function() {
var button = ui.button({
contents: options.icon,
container: false,
tooltip: lang.videoAttributes.tooltip,
click: function (e) {
context.invoke('saveRange');
context.invoke('videoAttributes.show');
}
});
return button.render();
});
this.initialize = function () {
var $container = options.dialogsInBody ? $(document.body) : $editor;
var body =
'<div class="form-group">' +
' <div class="col-xs-3"></div>' +
' <div class="col-xs-9 help-block">' + lang.videoAttributes.note + '</div>' +
'</div>' +
'<div class="form-group">' +
' <label for="note-video-attributes-href" class="control-label col-xs-3">' + lang.videoAttributes.href + '</label>' +
' <div class="input-group col-xs-9">';
var a = options.videoAttributes.videoUrls;
var b = Array.isArray(options.videoAttributes.videoUrls);
if ((options.videoAttributes.videoUrls !== undefined) && Array.isArray(options.videoAttributes.videoUrls)) {
body += ' <select id="note-video-attributes-href" class="note-video-attributes-href form-control">';
options.videoAttributes.videoUrls.forEach(element => body += ' <option value="' + element + '">' + element + '</option>');
body += ' </select>';
}
else {
body += '<input type="text" id="note-video-attributes-href" class="note-video-attributes-href form-control">';
}
body += ' </div>' +
'</div>';
if (options.videoAttributes.showVideoSize === true) {
body += '<div class="form-group">' +
' <label for="note-video-attributes-video-size" class="control-label col-xs-3">' + lang.videoAttributes.videoSize + '</label>' +
' <div class="input-group col-xs-9">' +
' <select id="note-video-attributes-size" class="note-video-attributes-size form-control col-xs-6" readonly="readonly">' +
' <option value="0">' + lang.videoAttributes.videoOption0 + '</option>' +
' <option value="1">' + lang.videoAttributes.videoOption1 + '</option>' +
' <option value="2">' + lang.videoAttributes.videoOption2 + '</option>' +
' <option value="3" selected>' + lang.videoAttributes.videoOption3 + '</option>' +
' <option value="4">' + lang.videoAttributes.videoOption4 + '</option>' +
' </select>' +
' </div>' +
'</div>';
}
if (options.videoAttributes.showVideoAlignment === true) {
body += '<div class="form-group">' +
' <label for="note-video-attributes-video-alignment" class="control-label col-xs-3">' + lang.videoAttributes.alignment + '</label>' +
' <div class="input-group col-xs-9">' +
' <select id="note-video-attributes-alignment" class="note-video-attributes-alignment form-control col-xs-6">' +
' <option value="none" selected>' + lang.videoAttributes.alignmentOption0 + '</option>' +
' <option value="left">' + lang.videoAttributes.alignmentOption1 + '</option>' +
' <option value="right">' + lang.videoAttributes.alignmentOption2 + '</option>' +
' <option value="initial">' + lang.videoAttributes.alignmentOption3 + '</option>' +
' <option value="inherit">' + lang.videoAttributes.alignmentOption4 + '</option>' +
' </select>' +
' </div>' +
'</div>';
}
if (options.videoAttributes.showSuggestedVideos === true) {
body += '<div class="form-group clearfix">' +
' <div class="control-label col-xs-3"></div>' +
' <div class="input-group col-xs-9">' +
' <div class="checkbox checkbox-success">' +
' <input type="checkbox" id="note-video-attributes-suggested-checkbox" class="note-video-attributes-suggested-checkbox" checked>' +
' <label for="note-video-attributes-suggested-checkbox">' + lang.videoAttributes.suggested + '</label>' +
' </div>' +
' </div>' +
'</div>';
}
if (options.videoAttributes.showPlayerControls === true) {
body += '<div class="form-group clearfix">' +
' <div class="control-label col-xs-3"></div>' +
' <div class="input-group col-xs-9">' +
' <div class="checkbox checkbox-success">' +
' <input type="checkbox" id="note-video-attributes-controls-checkbox" class="note-video-attributes-controls-checkbox" checked>' +
' <label for="note-video-attributes-controls-checkbox">' + lang.videoAttributes.controls + '</label>' +
' </div>' +
' </div>' +
'</div>';
}
if (options.videoAttributes.showAutoplay === true) {
body += '<div class="form-group clearfix">' +
' <div class="control-label col-xs-3"></div>' +
' <div class="input-group col-xs-9">' +
' <div class="checkbox checkbox-success">' +
' <input type="checkbox" id="note-video-attributes-autoplay-checkbox" class="note-video-attributes-autoplay-checkbox">' +
' <label for="note-video-attributes-autoplay-checkbox">' + lang.videoAttributes.autoplay + '</label>' +
' </div>' +
' </div>' +
'</div>';
}
if (options.videoAttributes.showLoop === true) {
body += '<div class="form-group clearfix">' +
' <div class="control-label col-xs-3"></div>' +
' <div class="input-group col-xs-9">' +
' <div class="checkbox checkbox-success">' +
' <input type="checkbox" id="note-video-attributes-loop-checkbox" class="note-video-attributes-loop-checkbox">' +
' <label for="note-video-attributes-loop-checkbox">' + lang.videoAttributes.loop + '</label>' +
' </div>' +
' </div>' +
'</div>';
}
this.$dialog = ui.dialog({
title: lang.videoAttributes.dialogTitle,
body: body,
footer: '<button href="#" class="btn btn-primary note-video-attributes-btn">' + lang.videoAttributes.ok + '</button>'
}).render().appendTo($container);
};
this.destroy = function () {
ui.hideDialog(this.$dialog);
this.$dialog.remove();
};
this.bindEnterKey = function ($input,$btn) {
$input.on('keypress', function (e) {
if(e.keyCode === 13) $btn.trigger('click');
});
};
this.bindLabels = function () {
self.$dialog.find('.form-control:first').focus().select();
self.$dialog.find('label').on('click', function() {
$(this).parent().find('.form-control:first').focus();
});
};
this.show = function () {
var $vid = $($editable.data('target'));
var vidInfo = {
vidDom: $vid,
href: $vid.attr('href')
};
this.showLinkDialog(vidInfo).then(function (vidInfo) {
ui.hideDialog(self.$dialog);
var $vid = vidInfo.vidDom,
$videoHref = self.$dialog.find('.note-video-attributes-href'),
$videoSize = self.$dialog.find('.note-video-attributes-size'),
$videoAlignment = self.$dialog.find('.note-video-attributes-alignment'),
$videoSuggested = self.$dialog.find('.note-video-attributes-suggested-checkbox'),
$videoControls = self.$dialog.find('.note-video-attributes-controls-checkbox'),
$videoAutoplay = self.$dialog.find('.note-video-attributes-autoplay-checkbox'),
$videoLoop = self.$dialog.find('.note-video-attributes-loop-checkbox'),
url = $videoHref.val(),
$videoHTML = $('<div/>');
if ($videoSize.val() === 0) {
$videoHTML.addClass('embed-responsive embed-responsive-16by9');
$videoHTML.css({'float': $videoAlignment.val()});
}
var videoWidth = 'auto', videoHeight = 'auto';
if ($videoSize.val() === "1") videoWidth = '1280', videoHeight = '720';
if ($videoSize.val() === "2") videoWidth = '853', videoHeight = '480';
if ($videoSize.val() === "3") videoWidth = '640', videoHeight = '360';
if ($videoSize.val() === "4") videoWidth = '560', videoHeight = '315';
var ytMatch = url.match(/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/);
var igMatch = url.match(/(?:www\.|\/\/)instagram\.com\/p\/(.[a-zA-Z0-9_-]*)/);
var vMatch = url.match(/\/\/vine\.co\/v\/([a-zA-Z0-9]+)/);
var vimMatch = url.match(/\/\/(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/);
var dmMatch = url.match(/.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/);
var youkuMatch = url.match(/\/\/v\.youku\.com\/v_show\/id_(\w+)=*\.html/);
var mp4Match = url.match(/^.+.(mp4|m4v)$/);
var oggMatch = url.match(/^.+.(ogg|ogv)$/);
var webmMatch = url.match(/^.+.(webm)$/);
var $video;
var urlVars = '';
if (ytMatch && ytMatch[1].length === 11) {
if (!$videoSuggested.is(':checked')) urlVars += 'rel=0';
if (!$videoControls.is(':checked')) urlVars += '&controls=0';
if ($videoAutoplay.is(':checked')) urlVars += '&autoplay=1';
if (!$videoLoop.is(':checked')) urlVars += '&loop=0';
var youtubeId = ytMatch[1];
$video = $('<iframe>')
.attr('frameborder', 0)
.attr('src', '//www.youtube.com/embed/' + youtubeId + '?' + urlVars)
.attr('width', videoWidth)
.attr('height', videoHeight);
} else if (igMatch && igMatch[0].length) {
$video = $('<iframe>')
.attr('frameborder', 0)
.attr('src', 'https://instagram.com/p/' + igMatch[1] + '/embed/')
.attr('width', videoWidth)
.attr('height', videoHeight)
.attr('scrolling', 'no')
.attr('allowtransparency', 'true');
} else if (vMatch && vMatch[0].length) {
$video = $('<iframe>')
.attr('frameborder', 0)
.attr('src', vMatch[0] + '/embed/simple')
.attr('width', videoWidth)
.attr('height', videoHeight)
.attr('class', 'vine-embed');
} else if (vimMatch && vimMatch[3].length) {
if ($videoAutoplay.is(':checked')) urlVars += '&autoplay=1';
if ($videoLoop.is(':checked')) urlVars += '&loop=1';
$video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>')
.attr('frameborder', 0)
.attr('src', '//player.vimeo.com/video/' + vimMatch[3] + '?' + urlVars)
.attr('width', videoWidth)
.attr('height', videoHeight);
} else if(dmMatch && dmMatch[2].length) {
if(!$videoSuggested.is(':checked')) urlVars += 'related=1';
if(!$videoAutoplay.is(':checked')) urlVars += 'autoplay=1'; else urlVars += 'autoplay=0';
$video = $('<iframe>')
.attr('frameborder', 0)
.attr('src', '//www.dailymotion.com/embed/video/' + dmMatch[2])
.attr('width', videoWidth)
.attr('height', videoHeight);
} else if (youkuMatch && youkuMatch[1].length) {
$video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>')
.attr('frameborder', 0)
.attr('width', videoWidth)
.attr('height', videoHeight)
.attr('src', '//player.youku.com/embed/' + youkuMatch[1]);
} else if(mp4Match || oggMatch || webmMatch) {
$video = $('<video controls>')
.attr('src', url)
.attr('width', videoWidth)
.attr('height', videoHeight);
}
if ($videoSize.val() === 0) $video.addClass('embed-responsive'); else $video.css({'float': $videoAlignment.val()});
$video.addClass('note-video-clip');
$videoHTML.html($video);
context.invoke('restoreRange');
context.invoke('editor.insertNode', $videoHTML[0]);
});
};
this.showLinkDialog = function (vidInfo) {
return $.Deferred(function (deferred) {
var $videoHref = self.$dialog.find('.note-video-attributes-href');
$editBtn = self.$dialog.find('.note-video-attributes-btn');
ui.onDialogShown(self.$dialog, function () {
context.triggerEvent('dialog.shown');
$editBtn.click(function (e) {
e.preventDefault();
deferred.resolve({
vidDom: vidInfo.vidDom,
href: $videoHref.val()
});
});
$videoHref.val(vidInfo.href).focus;
self.bindEnterKey($editBtn);
self.bindLabels();
});
ui.onDialogHidden(self.$dialog, function () {
$editBtn.off('click');
if(deferred.state() === 'pending') deferred.reject();
});
ui.showDialog(self.$dialog);
});
};
}
});
}));