|
| 1 | +/** |
| 2 | + * The footnotes dialog definition. |
| 3 | + * |
| 4 | + * Version 1.0.9 |
| 5 | + * https://github.com/andykirk/CKEditorFootnotes |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +(function() { |
| 10 | + "use strict"; |
| 11 | + |
| 12 | + // Dialog definition. |
| 13 | + CKEDITOR.dialog.add( 'footnotesDialog', function( editor ) { |
| 14 | + |
| 15 | + return { |
| 16 | + editor_name: false, |
| 17 | + footnotes_editor: false, |
| 18 | + dialog_dom_id: false, |
| 19 | + // Basic properties of the dialog window: title, minimum size. |
| 20 | + title: 'Manage Footnotes', |
| 21 | + minWidth: 400, |
| 22 | + minHeight: 200, |
| 23 | + |
| 24 | + // Dialog window contents definition. |
| 25 | + contents: [ |
| 26 | + { |
| 27 | + // Definition of the Basic Settings dialog tab (page). |
| 28 | + id: 'tab-basic', |
| 29 | + label: 'Basic Settings', |
| 30 | + |
| 31 | + // The tab contents. |
| 32 | + elements: [ |
| 33 | + { |
| 34 | + // Text input field for the footnotes text. |
| 35 | + type: 'textarea', |
| 36 | + id: 'new_footnote', |
| 37 | + 'class': 'footnote_text', |
| 38 | + label: 'New footnote:', |
| 39 | + inputStyle: 'height: 100px', |
| 40 | + }, |
| 41 | + { |
| 42 | + // Text input field for the footnotes title (explanation). |
| 43 | + type: 'text', |
| 44 | + id: 'footnote_id', |
| 45 | + name: 'footnote_id', |
| 46 | + label: 'No existing footnotes', |
| 47 | + |
| 48 | + // Called by the main setupContent call on dialog initialization. |
| 49 | + setup: function( element ) { |
| 50 | + |
| 51 | + var dialog = this.getDialog(), |
| 52 | + editor = dialog.getParentEditor(), |
| 53 | + el = dialog.getElement().findOne('#' + this.domId), |
| 54 | + footnotes = editor.editable().findOne('.footnotes ol'); |
| 55 | + |
| 56 | + dialog.dialog_dom_id = this.domId; |
| 57 | + |
| 58 | + if (footnotes !== null) { |
| 59 | + |
| 60 | + if (el.findOne('p') === null) { |
| 61 | + el.appendHtml('<p style="margin-bottom: 10px;"><strong>OR:</strong> Choose footnote:</p><ol class="footnotes_list"></ol>'); |
| 62 | + } else { |
| 63 | + el.findOne('ol').getChildren().toArray().forEach(function(item){ |
| 64 | + item.remove(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + var radios = ''; |
| 69 | + |
| 70 | + footnotes.find('li').toArray().forEach(function(item){ |
| 71 | + |
| 72 | + var footnote_id = item.getAttribute('data-footnote-id'); |
| 73 | + var cite_text = item.findOne('cite').getText(); |
| 74 | + |
| 75 | + radios += '<li style="margin-left: 15px;"><input type="radio" name="footnote_id" value="' + footnote_id + '" id="fn_' + footnote_id + '" /> <label for="fn_' + footnote_id + '" style="white-space: normal; display: inline-block; padding: 0 25px 0 5px; vertical-align: top; margin-bottom: 10px;">' + cite_text + '</label></li>'; |
| 76 | + }); |
| 77 | + |
| 78 | + el.find('label,div').toArray().forEach(function(item){ |
| 79 | + item.setStyle('display', 'none'); |
| 80 | + }); |
| 81 | + el.findOne('ol').appendHtml(radios); |
| 82 | + |
| 83 | + el.find('input[type="radio"]').toArray().forEach(function(item){ |
| 84 | + item.on('change', function(){ |
| 85 | + |
| 86 | + // Set the hidden input with the radio ident for the |
| 87 | + // footnote links to use: |
| 88 | + el.findOne('input[type="text"]').setValue(item.getValue()); |
| 89 | + |
| 90 | + // Also clear the editor to avoid any confusion: |
| 91 | + dialog.footnotes_editor.setData(''); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + } else { |
| 96 | + el.find('div').toArray().forEach(function(item){ |
| 97 | + item.setStyle('display', 'none'); |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + ] |
| 103 | + }, |
| 104 | + ], |
| 105 | + |
| 106 | + // Invoked when the dialog is loaded. |
| 107 | + onShow: function() { |
| 108 | + this.setupContent(); |
| 109 | + |
| 110 | + var dialog = this; |
| 111 | + CKEDITOR.on( 'instanceLoaded', function( evt ) { |
| 112 | + dialog.editor_name = evt.editor.name; |
| 113 | + dialog.footnotes_editor = evt.editor; |
| 114 | + } ); |
| 115 | + |
| 116 | + // Allow page to scroll with dialog to allow for many/long footnotes |
| 117 | + // (https://github.com/andykirk/CKEditorFootnotes/issues/12) |
| 118 | + /*this.getElement().findOne('.cke_dialog').setStyles({ |
| 119 | + 'position': 'absolute', |
| 120 | + 'top': '2%' |
| 121 | + });*/ |
| 122 | + // Note that it seems core CKEditor Dialog CSS now solves this for me so I don't |
| 123 | + // need the above code. I'll keep it here for reference for now though. |
| 124 | + |
| 125 | + var current_editor_id = dialog.getParentEditor().id; |
| 126 | + |
| 127 | + CKEDITOR.replaceAll( function( textarea, config ) { |
| 128 | + // Make sure the textarea has the correct class: |
| 129 | + if (!textarea.className.match(/footnote_text/)) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + // Make sure we only instantiate the relevant editor: |
| 134 | + var el = textarea; |
| 135 | + while ((el = el.parentElement) && !el.classList.contains(current_editor_id)); |
| 136 | + if (!el) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + config.toolbarGroups = [ |
| 141 | + { name: 'editing', groups: [ 'undo', 'find', 'selection', 'spellchecker' ] }, |
| 142 | + { name: 'clipboard', groups: [ 'clipboard' ] }, |
| 143 | + { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] }, |
| 144 | + ] |
| 145 | + config.allowedContent = 'br em strong; a[!href]'; |
| 146 | + config.enterMode = CKEDITOR.ENTER_BR; |
| 147 | + config.autoParagraph = false; |
| 148 | + config.height = 80; |
| 149 | + config.resize_enabled = false; |
| 150 | + config.autoGrow_minHeight = 80; |
| 151 | + config.removePlugins = 'footnotes'; |
| 152 | + |
| 153 | + var extra_config = editor.config.footnotesDialogEditorExtraConfig; |
| 154 | + if (extra_config) { |
| 155 | + for (var attribute in extra_config) { |
| 156 | + config[attribute] = extra_config[attribute]; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // If we focus on the dialog editor we should clear the radios to avoid any |
| 161 | + // confusion. Similarly, if we focus on a radio, we should clear the editor |
| 162 | + // (see setup above for radio change event handler for that) |
| 163 | + config.on = { |
| 164 | + focus: function( evt ){ |
| 165 | + var form_row = evt.editor.element.getAscendant('tr').getNext(); |
| 166 | + form_row.find('input[type="radio"]').toArray().forEach(function(item){ |
| 167 | + item.$.checked = false; |
| 168 | + }); |
| 169 | + form_row.findOne('input[type="text"]').setValue(''); |
| 170 | + } |
| 171 | + }; |
| 172 | + return true; |
| 173 | + }); |
| 174 | + |
| 175 | + }, |
| 176 | + |
| 177 | + // This method is invoked once a user clicks the OK button, confirming the dialog. |
| 178 | + onOk: function() { |
| 179 | + var dialog = this; |
| 180 | + var footnote_editor = CKEDITOR.instances[dialog.editor_name]; |
| 181 | + var footnote_id = dialog.getValueOf('tab-basic', 'footnote_id'); |
| 182 | + var footnote_data = footnote_editor.getData(); |
| 183 | + |
| 184 | + |
| 185 | + if (footnote_id == '') { |
| 186 | + // No existing id selected, check for new footnote: |
| 187 | + if (footnote_data == '') { |
| 188 | + // Nothing entered, so quit: |
| 189 | + return; |
| 190 | + } else { |
| 191 | + // Insert new footnote: |
| 192 | + editor.plugins.footnotes.build(footnote_data, true, editor); |
| 193 | + } |
| 194 | + } else { |
| 195 | + // Insert existing footnote: |
| 196 | + editor.plugins.footnotes.build(footnote_id, false, editor); |
| 197 | + } |
| 198 | + // Destroy the editor so it's rebuilt properly next time: |
| 199 | + footnote_editor.destroy(); |
| 200 | + // Destroy the list of footnotes so it's rebuilt properly next time: |
| 201 | + var list = dialog.getElement().findOne('#' + dialog.dialog_dom_id).findOne('ol'); |
| 202 | + if (list) { |
| 203 | + list.getChildren().toArray().forEach(function(item){ |
| 204 | + item.remove(); |
| 205 | + }); |
| 206 | + } |
| 207 | + return; |
| 208 | + }, |
| 209 | + |
| 210 | + onCancel: function() { |
| 211 | + var dialog = this; |
| 212 | + var footnote_editor = CKEDITOR.instances[dialog.editor_name]; |
| 213 | + footnote_editor.destroy(); |
| 214 | + } |
| 215 | + }; |
| 216 | + }); |
| 217 | +}()); |
0 commit comments