| 1 |
module.exports = SearchInterface; |
| 2 |
|
| 3 |
const BrowserpassURL = require("@browserpass/url"); |
| 4 |
const m = require("mithril"); |
| 5 |
|
| 6 |
/** |
| 7 |
* Search interface |
| 8 |
* |
| 9 |
* @since 3.0.0 |
| 10 |
* |
| 11 |
* @param object interface Popup main interface |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
function SearchInterface(popup) { |
| 15 |
// public methods |
| 16 |
this.view = view; |
| 17 |
|
| 18 |
// fields |
| 19 |
this.popup = popup; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Generates vnodes for render |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
* |
| 27 |
* @param function ctl Controller |
| 28 |
* @param object params Runtime params |
| 29 |
* @return []Vnode |
| 30 |
*/ |
| 31 |
function view(ctl, params) { |
| 32 |
var self = this; |
| 33 |
var host = new BrowserpassURL(this.popup.settings.origin).host; |
| 34 |
return m( |
| 35 |
"form.part.search", |
| 36 |
{ |
| 37 |
onkeydown: function (e) { |
| 38 |
switch (e.code) { |
| 39 |
case "Tab": |
| 40 |
e.preventDefault(); |
| 41 |
if (e.shiftKey) { |
| 42 |
document.querySelector(".part.login:last-child").focus(); |
| 43 |
break; |
| 44 |
} |
| 45 |
// fall through to ArrowDown |
| 46 |
case "ArrowDown": |
| 47 |
e.preventDefault(); |
| 48 |
if (self.popup.results.length) { |
| 49 |
document.querySelector("*[tabindex]").focus(); |
| 50 |
} |
| 51 |
break; |
| 52 |
case "Enter": |
| 53 |
e.preventDefault(); |
| 54 |
if (self.popup.results.length) { |
| 55 |
self.popup.results[0].doAction("fill"); |
| 56 |
} |
| 57 |
break; |
| 58 |
} |
| 59 |
}, |
| 60 |
}, |
| 61 |
[ |
| 62 |
this.popup.currentDomainOnly |
| 63 |
? m("div.hint.badge", [ |
| 64 |
host, |
| 65 |
m("div.remove-hint", { |
| 66 |
title: "Clear domain filter | <Backspace>", |
| 67 |
onclick: function (e) { |
| 68 |
var target = document.querySelector( |
| 69 |
".part.search > input[type=text]" |
| 70 |
); |
| 71 |
target.focus(); |
| 72 |
self.popup.currentDomainOnly = false; |
| 73 |
self.popup.search(target.value); |
| 74 |
}, |
| 75 |
}), |
| 76 |
]) |
| 77 |
: null, |
| 78 |
m("input[type=text]", { |
| 79 |
focused: true, |
| 80 |
placeholder: "Search logins...", |
| 81 |
oncreate: function (e) { |
| 82 |
e.dom.focus(); |
| 83 |
}, |
| 84 |
oninput: function (e) { |
| 85 |
self.popup.search(e.target.value); |
| 86 |
}, |
| 87 |
onkeydown: function (e) { |
| 88 |
switch (e.code) { |
| 89 |
case "Backspace": |
| 90 |
if (self.popup.currentDomainOnly) { |
| 91 |
if (e.target.value.length == 0) { |
| 92 |
self.popup.currentDomainOnly = false; |
| 93 |
self.popup.search(""); |
| 94 |
} else if ( |
| 95 |
e.target.selectionStart == 0 && |
| 96 |
e.target.selectionEnd == 0 |
| 97 |
) { |
| 98 |
self.popup.currentDomainOnly = false; |
| 99 |
self.popup.search(e.target.value); |
| 100 |
} |
| 101 |
} |
| 102 |
break; |
| 103 |
case "KeyC": |
| 104 |
if (e.ctrlKey && e.target.selectionStart == e.target.selectionEnd) { |
| 105 |
e.preventDefault(); |
| 106 |
self.popup.results[0].doAction( |
| 107 |
e.shiftKey ? "copyUsername" : "copyPassword" |
| 108 |
); |
| 109 |
} |
| 110 |
break; |
| 111 |
case "KeyG": |
| 112 |
if (e.ctrlKey && e.target.selectionStart == e.target.selectionEnd) { |
| 113 |
e.preventDefault(); |
| 114 |
self.popup.results[0].doAction( |
| 115 |
e.shiftKey ? "launchInNewTab" : "launch" |
| 116 |
); |
| 117 |
} |
| 118 |
break; |
| 119 |
case "KeyO": |
| 120 |
if (e.ctrlKey && e.target.selectionStart == e.target.selectionEnd) { |
| 121 |
e.preventDefault(); |
| 122 |
self.popup.results[0].doAction("getDetails"); |
| 123 |
} |
| 124 |
break; |
| 125 |
case "End": { |
| 126 |
if (e.target.selectionStart === e.target.value.length) { |
| 127 |
let logins = document.querySelectorAll(".login"); |
| 128 |
if (logins.length) { |
| 129 |
let target = logins.item(logins.length - 1); |
| 130 |
target.focus(); |
| 131 |
target.scrollIntoView(); |
| 132 |
} |
| 133 |
} |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
}, |
| 138 |
}), |
| 139 |
] |
| 140 |
); |
| 141 |
} |