Completed
Pull Request — develop (#161)
by Wachter
22:54 queued 09:56
created

main.js ➔ define   B

Complexity

Conditions 1
Paths 192

Size

Total Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 122
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 ➔ ... ➔ .initialize 0 11 1
A main.js ➔ ... ➔ trimSlash 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
/*
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
            return string.replace(/\/+$/g, '');
30
        },
31
32
        generatePath = function(pagePath, suffix) {
33
            return trimSlash(pagePath) + '/' + suffix;
34
        };
35
36
    return {
37
        defaults: defaults,
38
39
        initialize: function() {
40
            this.bindCustomEvents();
41
42
            this.render();
43
44
            var data = this.getData(),
45
                page = data.page || {};
46
47
            this.$prefix.val(page.path || '');
48
            this.$suffix.val(data.suffix || '');
49
        },
50
51
        bindCustomEvents: function() {
52
        },
53
54
        render: function() {
55
            this.html(this.templates.skeleton({translations: this.translations, options: this.options}));
56
57
            this.$prefix = this.$el.find('.prefix');
58
            this.$suffix = this.$el.find('.suffix');
59
            this.$choose = this.$el.find('.choose');
60
61
            this.bindDomEvents();
62
        },
63
64
        bindDomEvents: function() {
65
            this.$suffix.on('change', function(event) {
66
                this.setSuffix($(event.currentTarget).val());
67
            }.bind(this));
68
69
            this.$choose.on('click', function() {
70
                this.pageSelectClicked();
71
72
                return false;
73
            }.bind(this));
74
        },
75
76
        pageSelectClicked: function() {
77
            var $container = $('<div/>'),
78
                data = this.getData();
79
80
            this.$el.append($container);
81
82
            this.sandbox.start(
83
                [
84
                    {
85
                        name: 'page-tree-route/page-select@suluarticle',
86
                        options: {
87
                            el: $container,
88
                            selected: (data.page || {}).uuid || null,
89
                            locale: this.options.locale,
90
                            selectCallback: this.setParentPage.bind(this)
91
                        }
92
                    }
93
                ]
94
            );
95
        },
96
97
        setParentPage: function(item) {
98
            var data = this.getData();
99
            data.page = {uuid: item.id, path: (item.url || '/')};
100
101
            data.path = null;
102
            if (!!data.suffix && data.suffix.length > 0) {
103
                data.path = generatePath(data.page.path, data.suffix);
104
            }
105
106
            this.setData(data);
107
108
            this.$prefix.val(trimSlash(data.page.path));
109
        },
110
111
        setSuffix: function(suffix) {
112
            var data = this.getData();
113
            data.suffix = suffix;
114
115
            data.path = null;
116
            if (!!data.page) {
117
                data.path = generatePath(data.page.path, data.suffix);
118
            }
119
120
            this.setData(data);
121
        },
122
123
        setData: function(data) {
124
            this.$el.data('value', data);
125
        },
126
127
        getData: function() {
128
            return this.$el.data('value') || {};
129
        }
130
    };
131
});
132