Passed
Push — master ( fe7b3f...22651e )
by Andrey
08:30
created

web/plugins/pbckcode/plugin.js   A

Complexity

Total Complexity 9
Complexity/F 1.5

Size

Lines of Code 107
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 57
c 0
b 0
f 0
dl 0
loc 107
rs 10
wmc 9
mnd 3
bc 3
fnc 6
bpm 0.5
cpm 1.5
noi 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
A plugin.js ➔ getScriptUrl 0 3 1
B plugin.js ➔ normalizeJsUrl 0 4 8
1
// needed js files
2
var js = {
3
  ace: 'ace.js',
4
  aceExtWhitespace: 'ext-whitespace.js',
5
  pbSyntaxHighlighter: CKEDITOR.plugins.getPath('pbckcode') + 'dialogs/PBSyntaxHighlighter.js'
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
};
7
8
var commandName = 'pbckcode';
9
10
/**
11
 * Plugin definition
12
 */
13
CKEDITOR.plugins.add('pbckcode', {
14
  icons: 'pbckcode',
15
  hidpi: true,
16
  lang: ['fr', 'en', 'ru'],
17
  init: function(editor) {
18
    // if there is no user settings
19
    // create an empty object
20
    if (editor.config.pbckcode === undefined) {
21
      editor.config.pbckcode = {};
22
    }
23
24
    // default settings object
25
    var DEFAULT_SETTINGS = {
26
      cls: '',
27
      modes: [
28
        ['HTML', 'html'],
29
        ['CSS', 'css'],
30
        ['PHP', 'php'],
31
        ['JS', 'javascript']
32
      ],
33
      theme: 'textmate',
34
      tab_size: 4,
35
      js: '//cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/'
36
    };
37
38
    // merge user settings with default settings
39
    editor.settings = CKEDITOR.tools.extend(DEFAULT_SETTINGS, editor.config.pbckcode, true);
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
    editor.settings.js = normalizeJsUrl(editor.settings.js);
41
42
    // load CSS for the dialog
43
    editor.on('instanceReady', function() {
44
      CKEDITOR.document.appendStyleSheet(this.path + 'dialogs/style.css');
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
45
    }.bind(this));
46
47
    // add the button in the toolbar
48
    editor.ui.addButton('pbckcode', {
49
      label: editor.lang.pbckcode.addCode,
50
      command: commandName,
51
      toolbar: 'pbckcode'
52
    });
53
54
    // link the button to the command
55
    editor.addCommand(commandName, new CKEDITOR.dialogCommand('pbckcodeDialog', {
56
        allowedContent: 'pre[*]{*}(*)'
57
      })
58
    );
59
60
    // disable the button while the required js files are not loaded
61
    editor.getCommand(commandName).disable();
62
63
    // add the plugin dialog element to the plugin
64
    CKEDITOR.dialog.add('pbckcodeDialog', this.path + 'dialogs/pbckcode.js');
65
66
    // add the context menu
67
    if (editor.contextMenu) {
68
      editor.addMenuGroup('pbckcodeGroup');
69
      editor.addMenuItem('pbckcodeItem', {
70
        label: editor.lang.pbckcode.editCode,
71
        icon: this.path + 'icons/pbckcode.png',
72
        command: commandName,
73
        group: 'pbckcodeGroup'
74
      });
75
76
      editor.contextMenu.addListener(function(element) {
77
        if (element.getAscendant('pre', true)) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if element.getAscendant("pre", true) is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
78
          return {pbckcodeItem: CKEDITOR.TRISTATE_OFF};
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
79
        }
80
      });
81
    }
82
83
    var scripts = [
84
      getScriptUrl(editor.settings.js, js.ace),
85
      js.pbSyntaxHighlighter
86
    ];
87
88
    // Load the required js files
89
    // enable the button when loaded
90
    CKEDITOR.scriptLoader.load(scripts, function() {
91
      editor.getCommand(commandName).enable();
92
93
      // need ace to be loaded
94
      CKEDITOR.scriptLoader.load([
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
95
        getScriptUrl(editor.settings.js, js.aceExtWhitespace)
96
      ]);
97
    });
98
  }
99
});
100
101
function normalizeJsUrl(js) {
102
  return js.concat('/')
103
    .replace(new RegExp('([^:]\/)\/+', 'g'), '$1');
104
}
105
106
function getScriptUrl(prefix, scriptName) {
107
  return prefix + scriptName;
108
}
109
110