-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.js
71 lines (68 loc) · 3.41 KB
/
plugin.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
;(function (tinymce, $) {
'use strict';
tinymce.PluginManager.requireLangPack('accordion', 'en,es,fr_FR');
tinymce.PluginManager.add('accordion', function(editor) {
// Add a button that opens a window
function showDialog() {
// Open window
var selection = editor.selection;
var selectedElm = selection.getNode();
var accordionElm = editor.dom.getParent(selectedElm, 'div[data-mce-type=accordion]');
var accordionTitle = "", accordionText="";
if (accordionElm != null) {
accordionTitle = $(accordionElm).find('h4.panel-title a').html();
accordionText = $(accordionElm).find('div.panel-body').html();
}
editor.windowManager.open({
title: 'Insert accordion',
body: [
{type: 'textbox', name: 'title', label: 'Title', size: 40, value : accordionTitle},
{type: 'textbox', multiline: true, name: 'text', label: 'Content', size: 40, minHeight: 150, value : accordionText}
],
onsubmit: function(e) {
// Insert content when the window form is submitted
if (accordionElm != null) {
$(accordionElm).find('h4.panel-title a').html(e.data.title);
$(accordionElm).find('div.panel-body').html(e.data.text);
} else {
var id= 'collapse-'+(new Date()).getTime();
var htmlAccordion = '<div data-mce-type="accordion" class="tiny-accordion panel-group" id="accordion">' +
'<div class="panel panel-default">' +
'<div class="panel-heading">' +
'<h4 class="panel-title">' +
'<a data-toggle="collapse" data-parent="#accordion" href="#' + id + '">' +
e.data.title +
'</a>' +
'</h4>' +
'</div>' +
'<div id="' + id + '" class="panel-collapse collapse">' +
'<div class="panel-body">' +
e.data.text +
'</div>'+
'</div>'+
'</div>'+
'</div>';
editor.insertContent(htmlAccordion);
var panelElt = editor.dom.get(id);
var accordionElt = editor.dom.getParent(panelElt, 'div[data-mce-type=accordion]');
var nextElt = editor.dom.getNext(accordionElt, '*');
if(nextElt==null) {
editor.dom.add(editor.getBody(), 'p','',' ');
nextElt = editor.dom.getNext(accordionElt, '*');
}
var range = document.createRange();
range.setStart(nextElt, 0);
range.setEnd(nextElt, 0);
selection.setRng(range);
}
}
});
}
editor.addButton('accordion', {
tooltip: 'Insert accordion',
icon: 'fa fa-list-alt',
stateSelector: 'div[data-mce-type=accordion]',
onclick: showDialog
});
});
})(tinymce, jQuery);