Completed
Pull Request — master (#9)
by Flo
02:37
created

docs/assets/js/routing/dispatcher.js   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 141

Duplication

Lines 0
Ratio 0 %

Importance

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

8 Functions

Rating   Name   Duplication   Size   Complexity  
A ns.dispatcher.init 0 21 3
A _helpers.detectPathChanges 0 10 2
A _events.onDocumentLoad 0 3 1
A _helpers.getPath 0 3 1
A _private.getAction 0 19 3
A _private.run 0 18 3
A _events.onLinkClick 0 7 1
A _helpers.addPath 0 6 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
(function(controller) {
2
3
    'use strict';
4
5
    /** @namespace faulancer.routing */
6
    var ns = faulancer.namespace('docs.routing');
0 ignored issues
show
Bug introduced by
The variable faulancer seems to be never declared. If this is a global, consider adding a /** global: faulancer */ 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...
7
8
    var _fields = {
9
        paths: [],
10
        basePath: 'faulancer',
11
        currentPath: '/'
12
    };
13
14
    var _private = {
15
16
        /**
17
         * Get the action
18
         *
19
         * @param {String} path
20
         * @returns {Function}
21
         */
22
        getAction: function(path) {
23
24
            if (controller.hasOwnProperty(path + 'Action')) {
25
26
                var ctrl   = 'controller';
27
                var method = path + 'Action';
28
                var target = faulancer['docs'][ctrl][method];
0 ignored issues
show
Bug introduced by
The variable faulancer seems to be never declared. If this is a global, consider adding a /** global: faulancer */ 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...
29
30
                if (typeof target === 'function') {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if typeof target === "function" is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
31
                    return target();
32
                }
33
34
            } else {
35
36
                return controller.errorAction();
37
38
            }
39
40
        },
41
42
        /**
43
         * Run dispatcher
44
         *
45
         * @param {String} path
46
         * @returns {Function}
47
         */
48
        run: function(path) {
49
50
            if (path.substr(0,1) === '/') {
51
                path = path.substr(1, path.length);
52
            }
53
54
            if (document.querySelector('a.selected')) {
55
                document.querySelector('a.selected').classList.remove('selected');
56
            }
57
58
            document.querySelector('[href="/' + path + '"]').classList.add('selected');
59
60
61
            return this.getAction(path);
62
63
64
65
        }
66
67
    };
68
69
    var _events = {
70
71
        onDocumentLoad: function() {
72
            _private.run('/documentation');
73
        },
74
75
        onLinkClick: function(e) {
76
            e.preventDefault();
77
            _helpers.addPath(e.target);
78
            _private.run(_helpers.getPath());
79
            _fields.currentPath = _helpers.getPath();
80
            return false;
81
        }
82
83
    };
84
85
    var _helpers = {
86
87
        addPath: function(item) {
88
89
            var path = item.getAttribute('href');
90
            window.history.pushState(null, 'Site', _fields.basePath + path);
91
92
        },
93
94
        getPath: function() {
95
            return window.location.pathname.replace(_fields.basePath, '');
96
        },
97
98
        detectPathChanges: function() {
99
100
            if (_fields.currentPath !== _helpers.getPath()) {
101
102
                _fields.currentPath = _helpers.getPath();
103
                _private.run(_helpers.getPath());
104
105
            }
106
107
        }
108
109
    };
110
111
    ns.dispatcher = {
112
113
        init: function() {
114
115
            _fields.basePath = document.querySelector('base').getAttribute('href');
116
117
            var links = document.querySelectorAll('a');
118
119
            for (var link in links) {
120
121
                if (!links.hasOwnProperty(link)) {
122
                    continue;
123
                }
124
125
                links[link].addEventListener('click', _events.onLinkClick);
126
127
            }
128
129
            _events.onDocumentLoad();
130
131
            //setInterval(_helpers.detectPathChanges, 500);
132
133
        }
134
135
    };
136
137
    window.addEventListener('load', ns.dispatcher.init);
138
139
140
141
})(faulancer.namespace('docs.controller'));
0 ignored issues
show
Bug introduced by
The variable faulancer seems to be never declared. If this is a global, consider adding a /** global: faulancer */ 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...