Completed
Pull Request — master (#117)
by Sander
41s
created

js/ui/doorhanger/doorhanger.js   B

Complexity

Total Complexity 37
Complexity/F 1.48

Size

Lines of Code 165
Function Count 25

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 0
c 4
b 0
f 0
nc 32
dl 0
loc 165
rs 8.6
wmc 37
mnd 3
bc 33
fnc 25
bpm 1.32
cpm 1.48
noi 1
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') + '...');
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('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 = $('<button class="passman-btn passnman-btn-success btn-' + btn + '"></button>').text(button.text);
92
93
                if (btn === 'save') {
94
                    if (button.isCreate && accounts.length > 1) {
95
                        var caret = $('<span class="fa fa-caret-down" style="margin-left: 5px; cursor: pointer;"></span>');
96
                        var menu = $('<div class="select_account" style="display: none;"></div>');
97
                        html_button.append(caret);
98
                        for (var i = 0; i < accounts.length; i++) {
99
                            var a = accounts[i];
100
                            var item = $('<div class="account">Save to ' + a.vault.name + '</div>');
101
                            /* jshint ignore:start */
102
                            (function (account, item) {
103
                                item.click(function (e) {
104
                                    e.stopPropagation();
105
                                    e.preventDefault();
106
                                    button.onClickFn(account);
107
                                });
108
                            })(a, item);
109
                            /* jshint ignore:end */
110
                            menu.append(item);
111
                        }
112
                        caret.click(function (e) {
113
                            e.stopPropagation();
114
                            e.preventDefault();
115
                            var isVisible = ($('.select_account').is(':visible'));
116
                            var height = (isVisible) ? 0 : accounts.length * 29;
117
                            if (!isVisible) {
118
                                resizeIframe(height);
119
                            }
120
                            menu.slideToggle(function () {
121
                                if(isVisible){
122
                                    resizeIframe(height);
123
                                }
124
                            });
125
                        });
126
                        caret.after(menu);
127
                    }
128
                    html_button.click(function () {
129
                        button.onClickFn(default_account);
130
                    });
131
                } else {
132
                    html_button.click(function () {
133
                        button.onClickFn();
134
                    });
135
                }
136
137
                doorhanger_div.append(html_button);
138
            });
139
            dh.html(doorhanger_div);
140
            doorhanger_div.slideDown();
141
        });
142
    });
143
    var _this = {};
144
145
    function minedLoginSaved(args) {
146
        // If the login added by the user then this is true
147
148
        var saved = API.i18n.getMessage('credential_saved');
149
        var updated = API.i18n.getMessage('credential_updated');
150
        var action = (args.updated) ? updated : saved;
151
        $('#password-toolbar').html(action + '!');
152
        setTimeout(function () {
153
            closeDoorhanger();
154
        }, 2500);
155
156
    }
157
158
    _this.minedLoginSaved = minedLoginSaved;
159
    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...
160
        //console.log('Method call', msg.method);
161
        if (_this[msg.method]) {
162
            _this[msg.method](msg.args, sender);
163
        }
164
    });
165
});