Completed
Push — master ( 95f906...7adf3a )
by Sander
41s
created

$(document).ready   B

Complexity

Conditions 3
Paths 33

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 33
nop 1
dl 0
loc 82
rs 8.7769

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
$(document).ready(function () {
2
    function closeDoorhanger() {
3
        $('#password-doorhanger').slideUp(function () {
4
            API.runtime.sendMessage(API.runtime.id, {
5
                method: "passToParent",
6
                args: {'injectMethod': 'closeDoorhanger'}
7
            });
8
        });
9
    }
10
11
    function resizeIframe(height) {
12
        API.runtime.sendMessage(API.runtime.id, {
13
            method: "passToParent",
14
            args: {'injectMethod': 'resizeIframe', args: height}
15
        });
16
    }
17
18
    var default_account;
19
    var dh = $('#password-doorhanger');
20
    var btn_config = {
21
        'cancel': function () {
22
            return {
23
                text: API.i18n.getMessage('cancel'),
24
                onClickFn: function () {
25
                    closeDoorhanger();
26
                    API.runtime.sendMessage(API.runtime.id, {method: "clearMined"});
27
                }
28
            };
29
        },
30
        'save': function (data) {
31
            var save = API.i18n.getMessage('save');
32
            var update = API.i18n.getMessage('update');
33
            var btnText = (data.guid === null) ? save : update;
34
            return {
35
                text: btnText,
36
                onClickFn: function (account) {
37
                    API.runtime.sendMessage(API.runtime.id, {method: "saveMined", args: {account: account}});
38
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving_to', [account.vault.name]) + '...');
39
                    dh.find('.passman-btn').hide();
40
                },
41
                isCreate: (data.guid === null)
42
            };
43
        },
44
        'updateUrl': function (data) {
45
            return {
46
                text: 'Update',
47
                onClickFn: function () {
48
                    API.runtime.sendMessage(API.runtime.id, {method: "updateCredentialUrl", args: data});
49
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving'));
50
                    dh.find('.passman-btn').hide();
51
                }
52
            };
53
        },
54
        'ignore': function (data) {
55
            return {
56
                text: API.i18n.getMessage('ignore_site'),
57
                onClickFn: function () {
58
                    //closeToolbar();
59
                    API.runtime.sendMessage(API.runtime.id, {method: "ignoreSite", args: data.currentLocation});
60
                    dh.find('.toolbar-text').text(API.i18n.getMessage('site_ignored'));
61
                    dh.find('.passman-btn').hide();
62
                    setTimeout(function () {
63
                        closeDoorhanger();
64
                    }, 3000);
65
                }
66
            };
67
        }
68
    };
69
70
    API.runtime.sendMessage(API.runtime.id, {method: "getRuntimeSettings"}).then(function (settings) {
71
        var accounts = settings.accounts;
72
        default_account = accounts[0];
73
        API.runtime.sendMessage(API.runtime.id, {method: "getDoorhangerData"}).then(function (data) {
74
            if (!data) {
75
                return;
76
            }
77
            var buttons = data.buttons;
78
            data = data.data;
79
            var username = (data.username) ? data.username : data.email;
80
            var doorhanger_div = $('<div id="password-toolbar" style="display: none;">');
81
            $('<span>', {
82
                class: 'toolbar-text',
83
                text: data.title + ' ' + username + ' at ' + data.url
84
            }).appendTo(doorhanger_div);
85
86
87
            $.each(buttons, function (k, button) {
88
                var btn = button;
89
90
                button = btn_config[btn](data);
91
                var html_button;
92
93
                if (btn === 'save') {
94
                    var btn_text = (button.isCreate && accounts.length > 1) ? API.i18n.getMessage('save_to','') : API.i18n.getMessage('save');
95
                    btn_text = (!button.isCreate) ? API.i18n.getMessage('update') : btn_text;
96
                    html_button = $('<button class="passman-btn btn-save btn-success"></button>').append('<span class="btn-txt"></span>');
97
                    html_button.find('.btn-txt').text(btn_text);
98
                    if (button.isCreate && accounts.length > 1) {
99
                        var caret_container =  $('<span class="caret-container"></span>').append('<span class="caret-container-txt"></span>');
100
                        caret_container.find('.caret-container-txt').text(default_account.vault.name);
101
                        var caret = $('<span class="fa fa-caret-down" style="margin-left: 5px; cursor: pointer;"></span>');
102
                        var menu = $('<div class="select_account" style="display: none;"></div>');
103
                        caret_container.append(caret);
104
                        html_button.append(caret_container);
105
                        for (var i = 1; i < accounts.length; i++) {
106
                            var a = accounts[i];
107
                            var item = $('<div class="account"></div>').text(API.i18n.getMessage('save_to', [a.vault.name]));
108
                            /* jshint ignore:start */
109
                            (function (account, item) {
110
                                item.click(function (e) {
111
                                    e.stopPropagation();
112
                                    e.preventDefault();
113
                                    button.onClickFn(account);
114
                                });
115
                            })(a, item);
116
                            /* jshint ignore:end */
117
                            menu.append(item);
118
                        }
119
                        caret_container.click(function (e) {
120
                            e.stopPropagation();
121
                            e.preventDefault();
122
                            var isVisible = ($('.select_account').is(':visible'));
123
                            var height = (isVisible) ? 0 : accounts.length * 29;
124
                            if (!isVisible) {
125
                                resizeIframe(height);
126
                            }
127
                            menu.slideToggle(function () {
128
                                if(isVisible){
129
                                    resizeIframe(height);
130
                                }
131
                            });
132
                        });
133
                        caret.after(menu);
134
                    }
135
                    html_button.click(function () {
136
                        button.onClickFn(default_account);
137
                    });
138
                } else {
139
                    html_button = $('<button></button>',
140
                        {
141
                            class: 'passman-btn btn-'+ btn
142
                        }
143
                    ).append('<span class="btn-text"></span>');
144
                    html_button.find('.btn-text').text(button.text);
145
                    html_button.click(function () {
146
                        button.onClickFn();
147
                    });
148
                }
149
150
                doorhanger_div.append(html_button);
151
            });
152
            dh.html(doorhanger_div);
153
            doorhanger_div.slideDown();
154
        });
155
    });
156
    var _this = {};
157
158
    function minedLoginSaved(args) {
159
        // If the login added by the user then this is true
160
161
        var saved = API.i18n.getMessage('credential_saved');
162
        var updated = API.i18n.getMessage('credential_updated');
163
        var action = (args.updated) ? updated : saved;
164
        $('#password-toolbar').html(action + '!');
165
        setTimeout(function () {
166
            closeDoorhanger();
167
        }, 2500);
168
169
    }
170
171
    _this.minedLoginSaved = minedLoginSaved;
172
    API.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
0 ignored issues
show
Unused Code introduced by
The parameter sendResponse 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...
173
        //console.log('Method call', msg.method);
174
        if (_this[msg.method]) {
175
            _this[msg.method](msg.args, sender);
176
        }
177
    });
178
});