Completed
Pull Request — develop (#161)
by Wachter
14:20
created

main.js ➔ define   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
c 3
b 1
f 1
nc 1
nop 0
dl 0
loc 96
rs 8.3859

5 Functions

Rating   Name   Duplication   Size   Complexity  
A main.js ➔ ... ➔ .defaults.options.selectCallback 0 2 1
A main.js ➔ ... ➔ .defaults.options.cancelCallback 0 2 1
B main.js ➔ ... ➔ .initialize 0 36 1
A main.js ➔ ... ➔ .setItem 0 11 2
B main.js ➔ ... ➔ .startContentDatasource 0 28 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
/*
2
 * This file is part of the Sulu CMS.
3
 *
4
 * (c) MASSIVE ART WebServices GmbH
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
/**
11
 * Overlay to select parent page.
12
 *
13
 * @class page-tree-route/page-select
14
 * @constructor
15
 */
16
define(function() {
17
    return {
18
19
        defaults: {
20
            options: {
21
                selected: null,
22
                locale: null,
23
                selectCallback: function(item) {
0 ignored issues
show
Unused Code introduced by
The parameter item 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...
24
                },
25
                cancelCallback: function() {
26
                }
27
            },
28
            translations: {
29
                overlayTitle: 'public.choose'
30
            }
31
        },
32
33
        initialize: function() {
34
            this.id = this.options.selected;
35
36
            var $container = $('<div/>');
37
            var $componentContainer = $('<div/>');
38
            this.$el.append($container);
39
40
            this.sandbox.start([
41
                {
42
                    name: 'overlay@husky',
43
                    options: {
44
                        el: $container,
45
                        openOnStart: true,
46
                        removeOnClose: true,
47
                        skin: 'medium',
48
                        instanceName: 'page-select',
49
                        slides: [
50
                            {
51
                                title: this.translations.overlayTitle,
52
                                data: $componentContainer,
53
                                cssClass: 'data-source-slide',
54
                                contentSpacing: false,
55
                                okCallback: function() {
56
                                    if (this.item) {
57
                                        this.options.selectCallback(this.item);
58
                                    }
59
                                }.bind(this),
60
                                cancelCallback: function() {
61
                                    this.options.cancelCallback();
62
                                }.bind(this)
63
                            }
64
                        ]
65
                    }
66
                }
67
            ]).then(this.startContentDatasource.bind(this, $componentContainer));
68
        },
69
70
        startContentDatasource: function($componentContainer) {
71
            this.sandbox.start(
72
                [
73
                    {
74
                        name: 'content-datasource@sulucontent',
75
                        options: {
76
                            el: $componentContainer,
77
                            singleMarkable: true,
78
                            selected: this.options.selected,
79
                            locale: this.options.locale,
80
                            selectedUrl: '/admin/api/nodes/{datasource}?tree=true&language={locale}&fields=title,order,published,url&webspace-nodes=all',
81
                            rootUrl: '/admin/api/nodes?language={locale}&fields=title,order,published&webspace-nodes=all',
82
                            resultKey: 'nodes',
83
                            instanceName: 'internal-link',
84
                            instanceNamePrefix: '',
85
                            showStatus: true,
86
                            selectCallback: function(id, path, title, item) {
87
                                if (this.id === id) {
88
                                    return this.setItem(null, null);
89
                                }
90
91
                                this.setItem(item, id);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
92
                            }.bind(this)
93
                        }
94
                    }
95
                ]
96
            );
97
        },
98
99
        setItem: function(item, id) {
100
            this.item = item;
101
            this.id = id;
102
103
            var action = 'activate';
104
            if (!item) {
105
                action = 'deactivate';
106
            }
107
108
            this.sandbox.emit('husky.overlay.page-select.okbutton.' + action);
109
        }
110
    };
111
});
112