Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / redactor / plugins / wbutton.js
1 if (!RedactorPlugins) var RedactorPlugins = {};
2
3 /**
4 * Provides custom BBCode buttons for Redactor.
5 *
6 * @author Alexander Ebert
7 * @copyright 2001-2014 WoltLab GmbH
8 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 */
10 RedactorPlugins.wbutton = {
11 /**
12 * list of button names and their associated bbcode tag
13 * @var object<string>
14 */
15 _bbcodes: { },
16
17 /**
18 * Initializes the RedactorPlugins.wbutton plugin.
19 */
20 init: function() {
21 this._bbcodes = { };
22
23 for (var $i = 0, $length = __REDACTOR_BUTTONS.length; $i < $length; $i++) {
24 this._addBBCodeButton(__REDACTOR_BUTTONS[$i]);
25 }
26
27 // this list contains overrides for built-in buttons, if a button is not present
28 // Redactor's own icon will be used instead. This solves the problem of FontAwesome
29 // not providing an icon for everything we need (especially the core stuff)
30 var $faIcons = {
31 'html': 'fa-square-o',
32 'bold': 'fa-bold',
33 'italic': 'fa-italic',
34 'underline': 'fa-underline',
35 'deleted': 'fa-strikethrough',
36 'subscript': 'fa-subscript',
37 'superscript': 'fa-superscript',
38 'orderedlist': 'fa-list-ol',
39 'unorderedlist': 'fa-list-ul',
40 'outdent': 'fa-outdent',
41 'indent': 'fa-indent',
42 'link': 'fa-link',
43 'alignment': 'fa-align-left',
44 'table': 'fa-table'
45 };
46
47 var $buttons = this.getOption('buttons');
48 var $lastButton = '';
49 for (var $i = 0, $length = $buttons.length; $i < $length; $i++) {
50 var $button = $buttons[$i];
51
52 if ($button == 'separator') {
53 this.buttonGet($lastButton).parent().addClass('separator');
54
55 continue;
56 }
57
58 // check if button does not exist
59 var $buttonObj = this.buttonGet($button);
60 if ($buttonObj.length) {
61 if ($faIcons[$button]) {
62 this.buttonAwesome($button, $faIcons[$button]);
63 }
64 }
65 else {
66 this._addCoreButton($button, ($faIcons[$button] ? $faIcons[$button] : null), $lastButton);
67 }
68
69 $lastButton = $button;
70 }
71 },
72
73 _addCoreButton: function(buttonName, faIcon, insertAfter) {
74 var $button = this.buttonBuild(buttonName, {
75 title: buttonName,
76 exec: buttonName
77 }, false);
78 $('<li />').append($button).insertAfter(this.buttonGet(insertAfter).parent());
79
80 if (faIcon !== null) {
81 this.buttonAwesome(buttonName, faIcon);
82 }
83 },
84
85 /**
86 * Adds a custom button.
87 *
88 * @param object<string> data
89 */
90 _addBBCodeButton: function(data) {
91 var $buttonName = '__wcf_' + data.name;
92 var $button = this.buttonAdd($buttonName, data.label, this._insertBBCode);
93 this._bbcodes[$buttonName] = data.name;
94
95 // FontAwesome class name
96 if (data.icon.match(/^fa\-[a-z\-]+$/)) {
97 this.buttonAwesome($buttonName, data.icon);
98 }
99 else {
100 // image reference
101 $button.css('background-image', 'url(' + __REDACTOR_ICON_PATH + data.icon + ')');
102 }
103 },
104
105 /**
106 * Inserts the specified BBCode.
107 *
108 * @param string buttonName
109 * @param jQuery buttonDOM
110 * @param object buttonObj
111 * @param object event
112 */
113 _insertBBCode: function(buttonName, buttonDOM, buttonObj, event) {
114 var $bbcode = this._bbcodes[buttonName];
115 var $selectedHtml = this.getSelectionHtml();
116 this.insertHtml('[' + $bbcode + ']' + $selectedHtml + '[/' + $bbcode + ']');
117
118 this.sync();
119 }
120 };