| 1 |
module.exports = DetailsInterface; |
| 2 |
|
| 3 |
const m = require("mithril"); |
| 4 |
const Moment = require("moment"); |
| 5 |
const helpers = require("../helpers"); |
| 6 |
|
| 7 |
/** |
| 8 |
* Login details interface |
| 9 |
* |
| 10 |
* @since 3.6.0 |
| 11 |
* |
| 12 |
* @param object settings Settings object |
| 13 |
* @param array login Target login object |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
function DetailsInterface(settings, login) { |
| 17 |
// public methods |
| 18 |
this.attach = attach; |
| 19 |
this.view = view; |
| 20 |
|
| 21 |
//fields |
| 22 |
this.settings = settings; |
| 23 |
this.login = login; |
| 24 |
|
| 25 |
// get basename & dirname of entry |
| 26 |
this.login.basename = this.login.login.substr(this.login.login.lastIndexOf("/") + 1); |
| 27 |
this.login.dirname = this.login.login.substr(0, this.login.login.lastIndexOf("/")) + "/"; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Attach the interface on the given element |
| 32 |
* |
| 33 |
* @since 3.6.0 |
| 34 |
* |
| 35 |
* @param DOMElement element Target element |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
function attach(element) { |
| 39 |
m.mount(element, this); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Generates vnodes for render |
| 44 |
* |
| 45 |
* @since 3.6.0 |
| 46 |
* |
| 47 |
* @param function ctl Controller |
| 48 |
* @param object params Runtime params |
| 49 |
* @return []Vnode |
| 50 |
*/ |
| 51 |
function view(ctl, params) { |
| 52 |
const login = this.login; |
| 53 |
const storeBgColor = login.store.bgColor || login.store.settings.bgColor; |
| 54 |
const storeColor = login.store.color || login.store.settings.color; |
| 55 |
const passChars = login.fields.secret.split("").map((c) => { |
| 56 |
if (c.match(/[0-9]/)) { |
| 57 |
return m("span.char.num", c); |
| 58 |
} else if (c.match(/[^\w\s]/)) { |
| 59 |
return m("span.char.punct", c); |
| 60 |
} |
| 61 |
return m("span.char", c); |
| 62 |
}); |
| 63 |
|
| 64 |
var nodes = []; |
| 65 |
nodes.push( |
| 66 |
m("div.part.login.details-header", [ |
| 67 |
m("div.name", [ |
| 68 |
m("div.line1", [ |
| 69 |
m( |
| 70 |
"div.store.badge", |
| 71 |
{ |
| 72 |
style: `background-color: ${storeBgColor}; |
| 73 |
color: ${storeColor}`, |
| 74 |
}, |
| 75 |
login.store.name |
| 76 |
), |
| 77 |
m("div.path", [m.trust(login.dirname)]), |
| 78 |
login.recent.when > 0 |
| 79 |
? m("div.recent", { |
| 80 |
title: |
| 81 |
"Used here " + |
| 82 |
login.recent.count + |
| 83 |
" time" + |
| 84 |
(login.recent.count > 1 ? "s" : "") + |
| 85 |
", last " + |
| 86 |
Moment(new Date(login.recent.when)).fromNow(), |
| 87 |
}) |
| 88 |
: null, |
| 89 |
]), |
| 90 |
m("div.line2", [m.trust(login.basename)]), |
| 91 |
]), |
| 92 |
]), |
| 93 |
m("div.part.details", [ |
| 94 |
m("div.part.snack.line-secret", [ |
| 95 |
m("div.label", "Secret"), |
| 96 |
m("div.chars", passChars), |
| 97 |
m("div.action.copy", { onclick: () => login.doAction("copyPassword") }), |
| 98 |
]), |
| 99 |
m("div.part.snack.line-login", [ |
| 100 |
m("div.label", "Login"), |
| 101 |
m("div", login.fields.login), |
| 102 |
m("div.action.copy", { onclick: () => login.doAction("copyUsername") }), |
| 103 |
]), |
| 104 |
(() => { |
| 105 |
if ( |
| 106 |
this.settings.enableOTP && |
| 107 |
login.fields.otp && |
| 108 |
login.fields.otp.params.type === "totp" |
| 109 |
) { |
| 110 |
// update progress |
| 111 |
let progress = this.progress; |
| 112 |
let updateProgress = (vnode) => { |
| 113 |
let period = login.fields.otp.params.period; |
| 114 |
let remaining = period - ((Date.now() / 1000) % period); |
| 115 |
vnode.dom.style.transition = "none"; |
| 116 |
vnode.dom.style.width = `${(remaining / period) * 100}%`; |
| 117 |
setTimeout(function () { |
| 118 |
vnode.dom.style.transition = `width linear ${remaining}s`; |
| 119 |
vnode.dom.style.width = "0%"; |
| 120 |
}, 100); |
| 121 |
setTimeout(function () { |
| 122 |
m.redraw(); |
| 123 |
}, remaining); |
| 124 |
}; |
| 125 |
let progressNode = m("div.progress", { |
| 126 |
oncreate: updateProgress, |
| 127 |
onupdate: updateProgress, |
| 128 |
}); |
| 129 |
|
| 130 |
// display otp snack |
| 131 |
return m("div.part.snack.line-otp", [ |
| 132 |
m("div.label", "Token"), |
| 133 |
m("div.progress-container", progressNode), |
| 134 |
m("div", helpers.makeTOTP(login.fields.otp.params)), |
| 135 |
m("div.action.copy", { onclick: () => login.doAction("copyOTP") }), |
| 136 |
]); |
| 137 |
} |
| 138 |
})(), |
| 139 |
m("div.part.raw", m("textarea", login.raw.trim())), |
| 140 |
]) |
| 141 |
); |
| 142 |
|
| 143 |
return nodes; |
| 144 |
} |