/
video.js
153 lines (138 loc) · 4.29 KB
/
video.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
(function($R)
{
$R.add('plugin', 'video', {
translations: {
en: {
"video": "Video",
"video-html-code": "Video Embed Code or Youtube/Vimeo Link"
}
},
modals: {
'video':
'<form action=""> \
<div class="form-item"> \
<label for="modal-video-input">## video-html-code ##</label> \
<textarea id="modal-video-input" name="video" style="height: 160px;"></textarea> \
</div> \
</form>'
},
init: function(app)
{
this.app = app;
this.lang = app.lang;
this.opts = app.opts;
this.toolbar = app.toolbar;
this.component = app.component;
this.insertion = app.insertion;
this.inspector = app.inspector;
this.selection = app.selection;
},
// messages
onmodal: {
video: {
opened: function($modal, $form)
{
$video = $form.getField('video');
$video.focus();
},
insert: function($modal, $form)
{
var data = $form.getData();
this._insert(data);
}
}
},
oncontextbar: function(e, contextbar)
{
var data = this.inspector.parse(e.target)
if (data.isComponentType('video'))
{
var node = data.getComponent();
var buttons = {
"remove": {
title: this.lang.get('delete'),
api: 'plugin.video.remove',
args: node
}
};
contextbar.set(e, node, buttons, 'bottom');
}
},
// public
start: function()
{
var obj = {
title: this.lang.get('video'),
api: 'plugin.video.open'
};
var $button = this.toolbar.addButtonAfter('image', 'video', obj);
$button.setIcon('<i class="re-icon-video"></i>');
},
open: function()
{
var options = {
title: this.lang.get('video'),
width: '600px',
name: 'video',
handle: 'insert',
commands: {
insert: { title: this.lang.get('insert') },
cancel: { title: this.lang.get('cancel') }
}
};
this.app.api('module.modal.build', options);
},
remove: function(node)
{
this.component.remove(node);
},
// private
_insert: function(data)
{
this.app.api('module.modal.close');
if (data.video.trim() === '')
{
return;
}
// parsing
data.video = this._matchData(data.video);
// inserting
if (this._isVideoIframe(data.video))
{
var $video = this.component.create('video', data.video);
this.insertion.insertHtml($video);
}
},
_isVideoIframe: function(data)
{
return (data.match(/<iframe|<video/gi) !== null);
},
_matchData: function(data)
{
var iframeStart = '<iframe style="width: 500px; height: 281px;" src="';
var iframeEnd = '" frameborder="0" allowfullscreen></iframe>';
if (this._isVideoIframe(data))
{
var allowed = ['iframe', 'video', 'source'];
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
data = data.replace(/<p(.*?[^>]?)>([\w\W]*?)<\/p>/gi, '');
data = data.replace(tags, function ($0, $1)
{
return (allowed.indexOf($1.toLowerCase()) === -1) ? '' : $0;
});
}
else
{
if (data.match(this.opts.regex.youtube))
{
data = data.replace(this.opts.regex.youtube, iframeStart + '//www.youtube.com/embed/$1' + iframeEnd);
}
else if (data.match(this.opts.regex.vimeo))
{
data = data.replace(this.opts.regex.vimeo, iframeStart + '//player.vimeo.com/video/$2' + iframeEnd);
}
}
return data;
}
});
})(Redactor);