Completed
Pull Request — develop (#161)
by Wachter
15:19
created

main.js ➔ define   B

Complexity

Conditions 1
Paths 192

Size

Total Lines 126

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 126
rs 8.1463
cc 1
nc 192
nop 1

11 Functions

Rating   Name   Duplication   Size   Complexity  
A main.js ➔ ... ➔ .setSuffix 0 11 2
A main.js ➔ ... ➔ .render 0 9 1
A main.js ➔ ... ➔ generatePath 0 3 1
A main.js ➔ ... ➔ .pageSelectClicked 0 20 1
A main.js ➔ ... ➔ .getData 0 3 1
A main.js ➔ ... ➔ .bindCustomEvents 0 2 1
A main.js ➔ ... ➔ .setData 0 3 1
A main.js ➔ ... ➔ .bindDomEvents 0 11 1
A main.js ➔ ... ➔ .setParentPage 0 13 3
A main.js ➔ ... ➔ trimSlash 0 7 2
A main.js ➔ ... ➔ .initialize 0 11 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
define(['text!./skeleton.html'], function(skeletonTemplate) {
11
12
    'use strict';
13
14
    var defaults = {
15
            options: {
16
                historyApi: null,
17
                historyResultKey: 'routes',
18
                historyPathKey: 'path'
19
            },
20
            templates: {
21
                skeleton: skeletonTemplate
22
            },
23
            translations: {
24
                showHistory: 'public.show-history'
25
            }
26
        },
27
28
        trimSlash = function(string) {
29
            if (string === '/') {
30
                return string;
31
            }
32
33
            return string.replace(/\/+$/g, '');
34
        },
35
36
        generatePath = function(pagePath, suffix) {
37
            return trimSlash(pagePath) + '/' + suffix;
38
        };
39
40
    return {
41
        defaults: defaults,
42
43
        initialize: function() {
44
            this.bindCustomEvents();
45
46
            this.render();
47
48
            var data = this.getData(),
49
                page = data.page || {};
50
51
            this.$prefix.val(page.path || '');
52
            this.$suffix.val(data.suffix || '');
53
        },
54
55
        bindCustomEvents: function() {
56
        },
57
58
        render: function() {
59
            this.html(this.templates.skeleton({translations: this.translations, options: this.options}));
60
61
            this.$prefix = this.$el.find('.prefix');
62
            this.$suffix = this.$el.find('.suffix');
63
            this.$choose = this.$el.find('.choose');
64
65
            this.bindDomEvents();
66
        },
67
68
        bindDomEvents: function() {
69
            this.$suffix.on('change', function(event) {
70
                this.setSuffix($(event.currentTarget).val());
71
            }.bind(this));
72
73
            this.$choose.on('click', function() {
74
                this.pageSelectClicked();
75
76
                return false;
77
            }.bind(this));
78
        },
79
80
        pageSelectClicked: function() {
81
            var $container = $('<div/>'),
82
                data = this.getData();
83
84
            this.$el.append($container);
85
86
            this.sandbox.start(
87
                [
88
                    {
89
                        name: 'page-tree-route/page-select@suluarticle',
90
                        options: {
91
                            el: $container,
92
                            selected: (data.page || {}).uuid || null,
93
                            locale: this.options.locale,
94
                            selectCallback: this.setParentPage.bind(this)
95
                        }
96
                    }
97
                ]
98
            );
99
        },
100
101
        setParentPage: function(item) {
102
            var data = this.getData();
103
            data.page = {uuid: item.id, path: (item.url || '/')};
104
105
            data.path = null;
106
            if (!!data.suffix && data.suffix.length > 0) {
107
                data.path = generatePath(data.page.path, data.suffix);
108
            }
109
110
            this.setData(data);
111
112
            this.$prefix.val(trimSlash(data.page.path));
113
        },
114
115
        setSuffix: function(suffix) {
116
            var data = this.getData();
117
            data.suffix = suffix;
118
119
            data.path = null;
120
            if (!!data.page) {
121
                data.path = generatePath(data.page.path, data.suffix);
122
            }
123
124
            this.setData(data);
125
        },
126
127
        setData: function(data) {
128
            this.$el.data('value', data);
129
        },
130
131
        getData: function() {
132
            return this.$el.data('value') || {};
133
        }
134
    };
135
});
136