| Conditions | 18 |
| Total Lines | 91 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like FormCKEdit.js ➔ loadEditor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | function loadEditor() |
||
| 2 | { |
||
| 3 | var oEditor = null; |
||
|
|
|||
| 4 | |||
| 5 | var oTA = document.getElementById(g_oConfigFromPHP.CKEditor.editorID); |
||
| 6 | if (!oTA) { |
||
| 7 | displayJSError('Element [' + g_oConfigFromPHP.CKEditor.editorID + 'to be replaced by CKEditor not exists!', 'error'); |
||
| 8 | return; |
||
| 9 | } |
||
| 10 | // get initial size of textarea to replace |
||
| 11 | var iHeight = oTA.offsetHeight; |
||
| 12 | var iWidth = oTA.offsetWidth; |
||
| 13 | oEditor = CKEDITOR.replace(g_oConfigFromPHP.CKEditor.editorID, g_oConfigFromPHP.CKEditor.editorOptions); |
||
| 14 | |||
| 15 | // custom buttons |
||
| 16 | if (Array.isArray(g_oConfigFromPHP.CKEditor.customButtons)) { |
||
| 17 | let length = g_oConfigFromPHP.CKEditor.customButtons.length; |
||
| 18 | for (let i = 0; i < length; i++) { |
||
| 19 | let btn = g_oConfigFromPHP.CKEditor.customButtons[i]; |
||
| 20 | |||
| 21 | var exec = window[btn.func]; |
||
| 22 | if (typeof exec === "function") { |
||
| 23 | oEditor.addCommand('cmd_' + btn.func, {exec: function(oEditor) {window[btn.func](oEditor);}}); |
||
| 24 | oEditor.ui.addButton(btn.func, {label: btn.name, command: 'cmd_' + btn.func, icon: btn.icon}); |
||
| 25 | } else { |
||
| 26 | displayJSError('Handler for Custom Button [' + btn.func + '] is not defined!', 'error'); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | // resize to desired size |
||
| 32 | CKEDITOR.on('instanceReady', function(event) {event.editor.resize(iWidth, iHeight);}); |
||
| 33 | |||
| 34 | if (g_oConfigFromPHP.CKEditor.editorData !== undefined) { |
||
| 35 | oEditor.setData(g_oConfigFromPHP.CKEditor.editorData); |
||
| 36 | } |
||
| 37 | |||
| 38 | if (g_oConfigFromPHP.RichFilemanager === undefined) { |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | // check filemanager... |
||
| 42 | CKEDITOR.on('dialogDefinition', function (event) { |
||
| 43 | var editor = event.editor; |
||
| 44 | var dialogDefinition = event.data.definition; |
||
| 45 | var dialogName = event.data.name; |
||
| 46 | var cleanUpFuncRef = CKEDITOR.tools.addFunction(function () { |
||
| 47 | let oIFrame = document.getElementById('fm-iframeCKE'); |
||
| 48 | if (oIFrame) { |
||
| 49 | oIFrame.remove(); |
||
| 50 | } |
||
| 51 | document.body.style.overflowY = 'scroll'; |
||
| 52 | }); |
||
| 53 | |||
| 54 | var tabCount = dialogDefinition.contents.length; |
||
| 55 | for (var i = 0; i < tabCount; i++) { |
||
| 56 | var dialogTab = dialogDefinition.contents[i]; |
||
| 57 | if (!(dialogTab && typeof dialogTab.get === 'function')) { |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | |||
| 61 | var browseButton = dialogTab.get('browse'); |
||
| 62 | if (browseButton !== null) { |
||
| 63 | browseButton.hidden = false; |
||
| 64 | var params = |
||
| 65 | '?CKEditorFuncNum=' + CKEDITOR.instances[event.editor.name]._.filebrowserFn + |
||
| 66 | '&CKEditorCleanUpFuncNum=' + cleanUpFuncRef + |
||
| 67 | '&langCode=' + g_oConfigFromPHP.RichFilemanager.language + |
||
| 68 | '&CKEditor=' + event.editor.name; |
||
| 69 | |||
| 70 | if (dialogName == 'link') { |
||
| 71 | params += '&expandedFolder=' + g_oConfigFromPHP.RichFilemanager.expandFolder.browseLinkURL; |
||
| 72 | } else if (dialogTab.id == 'info') { |
||
| 73 | params += '&expandedFolder=' + g_oConfigFromPHP.RichFilemanager.expandFolder.browseImageURL; |
||
| 74 | } else { |
||
| 75 | params += '&expandedFolder=' + g_oConfigFromPHP.RichFilemanager.expandFolder.browseImageLinkURL; |
||
| 76 | } |
||
| 77 | browseButton.filebrowser.params = params; |
||
| 78 | browseButton.onClick = function (dialog, i) { |
||
| 79 | editor._.filebrowserSe = this; |
||
| 80 | |||
| 81 | let oIFrame = document.createElement('iframe'); |
||
| 82 | oIFrame.id = 'fm-iframeCKE'; |
||
| 83 | oIFrame.className = 'fm-modal'; |
||
| 84 | oIFrame.src = g_oConfigFromPHP.RichFilemanager.Path + dialog.sender.filebrowser.params; |
||
| 85 | document.body.append(oIFrame); |
||
| 86 | document.body.style.overflowY = 'hidden'; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | }); |
||
| 91 | } |
||
| 92 |