Completed
Push — master ( 1351fb...95f906 )
by Sander
50s
created

$(document).ready   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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