Completed
Branch master (30706c)
by Ron
57:09 queued 06:53
created

plugin.js ➔ Plugin   F

Complexity

Conditions 43

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 43
eloc 2
c 0
b 0
f 0
dl 0
loc 2
rs 0

How to fix   Complexity   

Complexity

Complex classes like plugin.js ➔ Plugin 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 () {
2
var autoresize = (function () {
0 ignored issues
show
Unused Code introduced by
The variable autoresize seems to be never used. Consider removing it.
Loading history...
3
    'use strict';
4
5
    var Cell = function (initial) {
6
      var value = initial;
7
      var get = function () {
8
        return value;
9
      };
10
      var set = function (v) {
11
        value = v;
12
      };
13
      var clone = function () {
14
        return Cell(get());
15
      };
16
      return {
17
        get: get,
18
        set: set,
19
        clone: clone
20
      };
21
    };
22
23
    var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
0 ignored issues
show
Bug introduced by
The variable tinymce seems to be never declared. If this is a global, consider adding a /** global: tinymce */ 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...
24
25
    var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
26
27
    var global$2 = tinymce.util.Tools.resolve('tinymce.util.Delay');
28
29
    var getAutoResizeMinHeight = function (editor) {
30
      return parseInt(editor.getParam('autoresize_min_height', editor.getElement().offsetHeight), 10);
31
    };
32
    var getAutoResizeMaxHeight = function (editor) {
33
      return parseInt(editor.getParam('autoresize_max_height', 0), 10);
34
    };
35
    var getAutoResizeOverflowPadding = function (editor) {
36
      return editor.getParam('autoresize_overflow_padding', 1);
37
    };
38
    var getAutoResizeBottomMargin = function (editor) {
39
      return editor.getParam('autoresize_bottom_margin', 50);
40
    };
41
    var shouldAutoResizeOnInit = function (editor) {
42
      return editor.getParam('autoresize_on_init', true);
43
    };
44
    var Settings = {
45
      getAutoResizeMinHeight: getAutoResizeMinHeight,
46
      getAutoResizeMaxHeight: getAutoResizeMaxHeight,
47
      getAutoResizeOverflowPadding: getAutoResizeOverflowPadding,
48
      getAutoResizeBottomMargin: getAutoResizeBottomMargin,
49
      shouldAutoResizeOnInit: shouldAutoResizeOnInit
50
    };
51
52
    var isFullscreen = function (editor) {
53
      return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
54
    };
55
    var wait = function (editor, oldSize, times, interval, callback) {
56
      global$2.setEditorTimeout(editor, function () {
57
        resize(editor, oldSize);
58
        if (times--) {
59
          wait(editor, oldSize, times, interval, callback);
60
        } else if (callback) {
61
          callback();
62
        }
63
      }, interval);
64
    };
65
    var toggleScrolling = function (editor, state) {
66
      var body = editor.getBody();
67
      if (body) {
68
        body.style.overflowY = state ? '' : 'hidden';
69
        if (!state) {
70
          body.scrollTop = 0;
71
        }
72
      }
73
    };
74
    var resize = function (editor, oldSize) {
75
      var deltaSize, doc, body, resizeHeight, myHeight;
76
      var marginTop, marginBottom, paddingTop, paddingBottom, borderTop, borderBottom;
77
      var dom = editor.dom;
78
      doc = editor.getDoc();
79
      if (!doc) {
80
        return;
81
      }
82
      if (isFullscreen(editor)) {
83
        toggleScrolling(editor, true);
84
        return;
85
      }
86
      body = doc.body;
87
      resizeHeight = Settings.getAutoResizeMinHeight(editor);
88
      marginTop = dom.getStyle(body, 'margin-top', true);
89
      marginBottom = dom.getStyle(body, 'margin-bottom', true);
90
      paddingTop = dom.getStyle(body, 'padding-top', true);
91
      paddingBottom = dom.getStyle(body, 'padding-bottom', true);
92
      borderTop = dom.getStyle(body, 'border-top-width', true);
93
      borderBottom = dom.getStyle(body, 'border-bottom-width', true);
94
      myHeight = body.offsetHeight + parseInt(marginTop, 10) + parseInt(marginBottom, 10) + parseInt(paddingTop, 10) + parseInt(paddingBottom, 10) + parseInt(borderTop, 10) + parseInt(borderBottom, 10);
95
      if (isNaN(myHeight) || myHeight <= 0) {
96
        myHeight = global$1.ie ? body.scrollHeight : global$1.webkit && body.clientHeight === 0 ? 0 : body.offsetHeight;
97
      }
98
      if (myHeight > Settings.getAutoResizeMinHeight(editor)) {
99
        resizeHeight = myHeight;
100
      }
101
      var maxHeight = Settings.getAutoResizeMaxHeight(editor);
102
      if (maxHeight && myHeight > maxHeight) {
103
        resizeHeight = maxHeight;
104
        toggleScrolling(editor, true);
105
      } else {
106
        toggleScrolling(editor, false);
107
      }
108
      if (resizeHeight !== oldSize.get()) {
109
        deltaSize = resizeHeight - oldSize.get();
110
        dom.setStyle(editor.iframeElement, 'height', resizeHeight + 'px');
111
        oldSize.set(resizeHeight);
112
        if (global$1.webkit && deltaSize < 0) {
113
          resize(editor, oldSize);
114
        }
115
      }
116
    };
117
    var setup = function (editor, oldSize) {
118
      editor.on('init', function () {
119
        var overflowPadding, bottomMargin;
120
        var dom = editor.dom;
121
        overflowPadding = Settings.getAutoResizeOverflowPadding(editor);
122
        bottomMargin = Settings.getAutoResizeBottomMargin(editor);
123
        if (overflowPadding !== false) {
124
          dom.setStyles(editor.getBody(), {
125
            paddingLeft: overflowPadding,
126
            paddingRight: overflowPadding
127
          });
128
        }
129
        if (bottomMargin !== false) {
130
          dom.setStyles(editor.getBody(), { paddingBottom: bottomMargin });
131
        }
132
      });
133
      editor.on('nodechange setcontent keyup FullscreenStateChanged', function (e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
134
        resize(editor, oldSize);
135
      });
136
      if (Settings.shouldAutoResizeOnInit(editor)) {
137
        editor.on('init', function () {
138
          wait(editor, oldSize, 20, 100, function () {
139
            wait(editor, oldSize, 5, 1000);
140
          });
141
        });
142
      }
143
    };
144
    var Resize = {
145
      setup: setup,
146
      resize: resize
147
    };
148
149
    var register = function (editor, oldSize) {
150
      editor.addCommand('mceAutoResize', function () {
151
        Resize.resize(editor, oldSize);
152
      });
153
    };
154
    var Commands = { register: register };
155
156
    global.add('autoresize', function (editor) {
157
      if (!editor.inline) {
158
        var oldSize = Cell(0);
159
        Commands.register(editor, oldSize);
160
        Resize.setup(editor, oldSize);
161
      }
162
    });
163
    function Plugin () {
164
    }
165
166
    return Plugin;
167
168
}());
169
})();
170