-
Notifications
You must be signed in to change notification settings - Fork 1
/
bivrost-tinymce.js
242 lines (202 loc) · 6.58 KB
/
bivrost-tinymce.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
/* global tinymce, wp, tinyMCE */
"use strict";
(function() {
// default values for attributes
var defaults={
loop: false,
autoplay: true,
source: "autodetect",
stereoscopy: "autodetect",
projection: "equirectangular",
theme: "default",
width: "",
height: ""
};
var __=function(key) {
return tinymce.activeEditor.getLang("bivrost-360webplayer."+key);
};
// media selector
function get_media(on_got_media) {
// TODO: add preselected
var custom_uploader = (wp.media({
title: __("custom_uploader_select_files"),
button: {
text: "OK"
},
multiple: true
})
.on("select", function() {
var selection=custom_uploader.state().get("selection");
on_got_media(selection.models);
})
.open());
}
function shortcode_editor(shortcode, on_submit) {
function listbox_helper(name, label, value, values) {
return {
type: "listbox",
name: name,
label: label,
value: value,
values: values.map(function(v) { return { text: v, value: v }; })
};
};
function checkbox_helper(name, label, value) {
return {
type: "checkbox",
name: name,
label: label,
checked: value,
text: ""
};
};
var id="placeholder-"+(+new Date());
var srcs=(shortcode.content || "").replace(/<[^>]+>/g, " ").split(/\s+/).map(function(s) { return s.trim(); }).filter(function(s) { return s !== ""; });
var values=shortcode.attrs.named;
var available_projections=["equirectangular", "cubemap:horizontal", "cubemap:two-by-three", "cubemap:horizontal-cross", "cubemap:vertical-cross"];
if(values.projection && available_projections.indexOf(values.projection) === -1 && /^cubemap:.+/.test(values.projection)) // custom cubemap projection - add it to list
available_projections.push(values.projection);
// http://stackoverflow.com/questions/24871792/tinymce-api-v4-windowmanager-open-what-widgets-can-i-configure-for-the-body-op
tinyMCE.activeEditor.windowManager.open( {
title: __("title_360webplayer_configuration"),
body: [
checkbox_helper("loop", __("label_loop"), !!JSON.parse(values.loop || JSON.stringify(defaults.loop))),
checkbox_helper("autoplay", __("label_autoplay"), !!JSON.parse(values.autoplay || JSON.stringify(defaults.autoplay))),
listbox_helper("source", __("label_source"), values.source || defaults.source, ["autodetect", "video", "picture", "stream-hls"]),
listbox_helper("stereoscopy", __("label_stereoscopy"), values.stereoscopy || defaults.stereoscopy, ["autodetect", "mono", "side-by-side", "top-and-bottom", "top-and-bottom-reversed"]),
listbox_helper("projection", __("label_projection"), values.projection || defaults.projection, available_projections),
listbox_helper("theme", __("label_theme"), values.theme || defaults.theme, ["default", "spring", "autumn"]),
{
type: "textbox",
name: "width",
label: __("label_width"),
value: values.width || defaults.width
},
{
type: "textbox",
name: "height",
label: __("label_height"),
value: values.height || defaults.height
},
{
type: "container",
label: "sources",
html: "<div id=\""+id+"\"></div>",
minHeight: 150
},
{
type: "button",
text: __("button_add_gallery"),
classes: "add-button"
},
{
type: "button",
text: __("button_add_url"),
classes: "add-url-button"
}
],
onsubmit: function(values) {
for(var key in values.data)
if(values.data.hasOwnProperty(key)) {
var val=values.data[key];
delete shortcode.attrs.named[key];
if(defaults[key] !== val) {
shortcode.attrs.named[key]=values.data[key];
}
}
shortcode.content="\n"+srcs.join("<br/>\n")+"\n";
on_submit();
}
} );
// modify container
var media=document.getElementById(id);
// while(!(/mce-formitem/.test(media.className)))
media=media.parentNode.parentNode.parentNode.parentNode;
function reset_srcs() {
media.innerHTML="<p>"+ (srcs.length
? __("sources_list")
: __("sources_empty")
)+"</p>\n";
var ul=document.createElement("ul");
media.appendChild(ul);
media.style.overflow="auto";
// unique
var usedSrc={};
srcs=srcs.filter(function(src) {
if(usedSrc[src])
return false;
usedSrc[src]=true;
return true;
});
// add labels and remove buttons
srcs.forEach(function(src) {
var li=document.createElement("li");
var span=document.createElement("span");
span.style.verticalAlign="middle";
span.style.lineHeight="30px";
span.appendChild(document.createTextNode(" • "+src.split("/").pop()));
span.className="mce-container";
var remove=document.createElement("button");
remove.appendChild(document.createTextNode(__("button_remove")));
remove.addEventListener("click", function() {
srcs=srcs.filter(function(s) { return s !== src; } );
reset_srcs();
});
var removeHolder=document.createElement("span");
removeHolder.className="mce-btn";
removeHolder.style.float="right";
removeHolder.appendChild(remove);
li.appendChild(removeHolder);
li.appendChild(span);
li.style.break="both";
li.style.marginTop="0.5em";
ul.appendChild(li);
});
}
var button=media.parentNode.parentNode.querySelector(".mce-add-button button");
button.addEventListener("click", function() {
get_media(function(m) {
m.forEach(function(mm) {
srcs.push(mm.attributes.url);
});
reset_srcs();
});
});
var buttonUrl=media.parentNode.parentNode.querySelector(".mce-add-url-button button");
buttonUrl.addEventListener("click", function() {
var url=prompt(__("prompt_url"), "");
if(url)
srcs.push(url);
reset_srcs();
});
reset_srcs(media);
}
var plugin_url;
// shortcode preview
wp.mce.views.register('bivrost-player', _.extend({}, {
state: [],
shortcode: "bivrost-player",
initialize: function() {
this.render("<img src=\""+plugin_url+"/preview.png\">");
},
edit: function(text, update) {
var shortcode=this.shortcode;
shortcode_editor(shortcode, function() { update(shortcode.string()); } );
}
}));
// tinymce button
tinymce.create("tinymce.plugins.bivrost", {
init: function(editor, url) {
plugin_url=url;
editor.addButton("bivrost_button", {
title: __("button_add_360webplayer"),
image: url+"/bivrost-button.png",
onclick: function() {
var shortcode=new wp.shortcode({tag: "bivrost-player"});
shortcode_editor(shortcode, function() { editor.insertContent(shortcode.string()); });
}
});
}
});
tinymce.PluginManager.add("bivrost", tinymce.plugins.bivrost);
})();