Test Setup Failed
Push — master ( c4f98f...f73258 )
by
unknown
08:22 queued 04:20
created

plugin-resizable-area.js ➔ define   B

Complexity

Conditions 1
Paths 8

Size

Total Lines 211

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 8
dl 0
loc 211
rs 8.2857
nop 1

12 Functions

Rating   Name   Duplication   Size   Complexity  
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.enable 0 13 3
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.dispose 0 7 2
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.removePreviousState 0 13 4
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend._onResizeEnd 0 3 1
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend._applyResizable 0 16 2
A plugin-resizable-area.js ➔ ... ➔ ResizableArea.setPreviousState 0 13 2
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.disable 0 12 3
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend._destroyResizable 0 5 1
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.initialize 0 17 4
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend._savePreviousSize 0 12 2
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.removeCalculatedSize 0 3 1
A plugin-resizable-area.js ➔ ... ➔ BasePlugin.extend.setPreviousState 0 3 1

How to fix   Long Method   

Long Method

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:

1
define(function(require) {
2
    'use strict';
3
4
    var ResizableArea;
5
    var persistentStorage = require('oroui/js/persistent-storage');
6
    var BasePlugin = require('oroui/js/app/plugins/base/plugin');
7
    var _ = require('underscore');
8
    var $ = require('jquery');
9
    require('jquery-ui');
10
11
    ResizableArea = BasePlugin.extend({
12
        /**
13
         * @property {Options}
14
         */
15
        defaults: {
16
            useResizable: true
17
        },
18
19
        /**
20
         * @property {Object}
21
         */
22
        resizableOptions: {
23
            // 'n, e, s, w, ne, se, sw, nw, all' */
24
            handles: 'e',
25
            zIndex: null,
26
            maxWidth: 600,
27
            minWidth: 320,
28
            // Selector or Element or String
29
            containment: 'parent',
30
            classes: {
31
                'ui-resizable': 'resizable',
32
                'ui-resizable-e': 'resizable-area'
33
            }
34
        },
35
36
        /**
37
         * @property {jQuery}
38
         */
39
        $resizableEl: null,
40
41
        /**
42
         * @inheritDoc
43
         */
44
        initialize: function(main, options) {
45
            this.options = _.extend(this.defaults, options);
46
47
            if (!this.options.useResizable) {
48
                return;
49
            }
50
51
            if (_.isObject(this.options.resizableOptions)) {
52
                this.resizableOptions = _.defaults({}, this.options.resizableOptions, this.resizableOptions);
53
            }
54
55
            if (this.main.$(this.options.$resizableEl).length) {
56
                this.$resizableEl = this.main.$(this.options.$resizableEl);
57
58
                this._applyResizable();
59
            }
60
        },
61
62
        /**
63
         * @inheritDoc
64
         */
65
        dispose: function() {
66
            if (this.disposed) {
67
                return;
68
            }
69
70
            ResizableArea.__super__.dispose.apply(this, arguments);
71
        },
72
73
        /**
74
         * Apply the resizable functionality
75
         * @private
76
         */
77
        _applyResizable: function() {
78
            if (this.$resizableEl.data('uiResizable')) {
79
                this._destroyResizable();
80
            }
81
82
            this.$resizableEl
83
                .resizable(
84
                    _.extend(
85
                        {},
86
                        this.resizableOptions,
87
                        {
88
                            stop: this._onResizeEnd.bind(this)
89
                        }
90
                    )
91
                );
92
        },
93
94
        /**
95
         * Remove the resizable functionality
96
         * @private
97
         */
98
        _destroyResizable: function() {
99
            this.$resizableEl
100
                .removeClass('resizable-enable')
101
                .resizable('destroy');
102
        },
103
104
        /**
105
         * {Boolean} [removeSize]
106
         * Disable the resizable functionality
107
         */
108
        disable: function(removeSize) {
109
            var restore = _.isUndefined(removeSize) ? true : removeSize;
110
111
            this.$resizableEl
112
                .removeClass('resizable-enable')
113
                .resizable('disable');
114
115
            if (_.isBoolean(restore)) {
116
                this.removeCalculatedSize();
117
            }
118
            ResizableArea.__super__.disable.call(this);
119
        },
120
121
        /**
122
         * {Boolean} [restoreSize]
123
         * Enable the resizable functionality
124
         */
125
        enable: function(restoreSize) {
126
            var restore = _.isUndefined(restoreSize) ? true : restoreSize;
127
128
            this.$resizableEl
129
                .addClass('resizable-enable')
130
                .resizable('enable');
131
132
            if (_.isBoolean(restore)) {
133
                this.setPreviousState();
134
            }
135
136
            ResizableArea.__super__.enable.call(this);
137
        },
138
139
        /**
140
         * @param {Event} event
141
         * @param {Object} ui
142
         * @private
143
         */
144
        _onResizeEnd: function(event, ui) {
145
            this._savePreviousSize(ui.size.width);
146
        },
147
148
        /**
149
         * @param {Number} size
150
         * @private
151
         */
152
        _savePreviousSize: function(size) {
153
            var oldValue = persistentStorage.getItem(ResizableArea.STORAGE_KEY);
154
            var newValue = {};
155
156
            oldValue = oldValue ? JSON.parse(oldValue) : {};
157
158
            newValue[this.options.$resizableEl] = {width: size};
159
160
            persistentStorage.setItem(ResizableArea.STORAGE_KEY,
161
                JSON.stringify(_.extend({}, oldValue, newValue))
162
            );
163
        },
164
165
        setPreviousState: function() {
166
            ResizableArea.setPreviousState(this.main.$el);
167
        },
168
169
        removePreviousState: function() {
170
            var state = JSON.parse(persistentStorage.getItem(ResizableArea.STORAGE_KEY));
171
172
            if (_.isObject(state)) {
173
                if (_.has(state, this.options.$resizableEl)) {
174
                    delete state[this.options.$resizableEl];
175
                }
176
177
                if (_.isEmpty(state)) {
178
                    persistentStorage.removeItem(ResizableArea.STORAGE_KEY);
179
                }
180
            }
181
        },
182
183
        removeCalculatedSize: function() {
184
            this.$resizableEl.css({width: ''});
185
        }
186
    });
187
188
    /**
189
     * @static
190
     */
191
    ResizableArea.STORAGE_KEY = 'custom-style-elements-cache';
192
193
    /**
194
     * @static
195
     */
196
    ResizableArea.setPreviousState = function($container) {
197
        var state = JSON.parse(persistentStorage.getItem(ResizableArea.STORAGE_KEY));
198
199
        if (_.isObject(state)) {
200
            _.each(state, function(value, key) {
201
                var $el = $container.find(key);
202
203
                if ($.contains($container[0], $el[0])) {
204
                    $el.css(value);
205
                }
206
            }, this);
207
        }
208
    };
209
210
    return ResizableArea;
211
});
212