diff options
Diffstat (limited to 'client/vendor')
| -rw-r--r-- | client/vendor/format.js | 210 | ||||
| -rw-r--r-- | client/vendor/paths.js | 18 |
2 files changed, 228 insertions, 0 deletions
diff --git a/client/vendor/format.js b/client/vendor/format.js new file mode 100644 index 0000000..1e5836e --- /dev/null +++ b/client/vendor/format.js @@ -0,0 +1,210 @@ +export default { + commatize, + privacyDot, + shortMonths, + verboseDate, + carbonDate, + hushViews, + hushSize, + hushNull, + courtesyS, + getRevision, + getAge, + tidyURLs, + getDomain, +} + +function commatize (n) { + var nums = [], i, counter = 0, r = Math.floor + if (n > 1024) { + n /= 1024 + nums.unshift(r((n * 10) % 10)) + nums.unshift(".") + } + do { + i = n % 10 + n = r(n / 10) + if (n && ! (++counter % 3)) + { i = ' ' + r(i) } + nums.unshift(r(i)) + } + while (n) + return nums.join("") +} + +function privacyDot (p) { + if (! p) return "·" + else return "·:" +} + +const shortMonths = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ") +function verboseDate (dateStr, pad_hours) { + const dateObj = new Date(dateStr) + let d = dateObj.getDate() + let m = dateObj.getMinutes() + let h = dateObj.getHours() + let meridian + + if (h == 0) { + h = 12 + meridian = " am" + } + else if (h == 12) { + meridian = " pm" + } + else if (h > 12) { + h -= 12 + meridian = " pm" + } + else { + meridian = " am" + } + + if (d < 10) d = "0" + d + if (m < 10) m = "0" + m + if (pad_hours && h < 10) h = "0" + h + + const date = d + '-' + shortMonths[dateObj.getMonth()] + '-' + dateObj.getFullYear() + const time = h + ':' + m + meridian + + return { date, time } +} +function carbonDate (date, no_bold) { + const span = (+new Date() - new Date(date)) / 1000 + if (! no_bold && span < 86400) // modified today + { color = "new" } + else if (span < 604800) // modifed this week + { color = "recent" } + else if (span < 1209600) // modifed 2 weeks ago + { color = "med" } + else if (span < 3024000) // modifed 5 weeks ago + { color = "old" } + else if (span < 12315200) // modifed 6 months ago + { color = "older" } + else + { color = "quiet" } + return color +} + +function hushViews (n, bias, no_bold) { + const txt = commatize(n) + bias = bias || 1 + n = n || 0 + if (n < 30) { return["quiet", n + " v."] } + if (n < 200) { return ["quiet", txt + " v."] } + else if (n < 500) { return ["quiet", txt + " v."] } + else if (n < 1000) { return ["old", txt + " v."] } + else if (n < 5000) { return ["med", txt + " kv."] } + else if (nobold || n < 10000) { return ["recent", txt + " kv."] } + else { return ["new", txt + " kv."] } +} + +function hushSize (n, bias, nobold) { + const txt = commatize(Math.floor(n / 1024)) + bias = 1 || bias + n = n || 0 + if (n < 1024) { + return ["quiet", n + " b."] + } + if (n < 1024*1024) { + return ["quiet", txt + " kb."] + } + else if (n < (20000000/bias)) { + return ["quiet", txt + " mb."] + } + else if (n < (50000000/bias)) { + return ["old", txt + " mb."] + } + else if (n < (80000000/bias)) { + return ["med", txt + " mb."] + } + else if (nobold || n < (170000000/bias)) { + return ["recent", txt + " mb."] + } + else { + return ["new", txt + " mb."] + } +} + +function hushNull (n, unit, no_bold) { + const s = unit ? n + " " + unit + "." : n + if (n < 3) { + return ["quiet", s] + } + else if (n < 6) { + return ["older", s] + } + else if (n < 10) { + return ["old", s] + } + else if (n < 16) { + return ["med", s] + } + else if (no_bold || n < 21) { + return ["recent", s] + } + else { + return ["new", s] + } +} + +function courtesyS (n, s) { return v == 1 ? "" : (s || "s") } + +const revisionLetters = "z a b c d f g h j k l m n p q r s t v w x y".split(" ") +function getRevision (thread) { + if (! thread.revision) return "" + let rev = thread.revision + let n = 0 + let digits = "" + do { + n = rev % 21 + rev = Math.floor(rev / 21) + digits = revisionLetters[n] + digits + } + while (rev !== 0) + return digits +} + +function getAge (t) { + let age = Math.abs( Date.now()/1000 - t) + const r = Math.floor + let m + if (age < 5) { return "now" } + if (age < 60) { return r(age) + "s" } + age /= 60 + if (age < 60) { return r(age) + "m" } + m = r(age % 60) + age /= 60 + if (m > 0 && age < 2) { return m + "m" + r(age) + "h" } + if (age < 24) { return r(age) + "h" } + age /= 24 + if (age < 7) { return r(age) + "d" } + age /= 7 + if (age < 12) { return r(age) + "w" } + age /= 4 + if (age < 12) { return r(age) + "m" } + age /= 12 + return r(age) + "y" +} + +function tidyURLs (s, short_urls) { + return s.split("\n").map(function(line){ + if (line.indexOf("<") !== -1) { + return line + } + return line.replace(/https?:\/\/[^ ]+/g, function(url){ + if (is_image(url)) { + return '<a href="' + url + '" target="_blank"><img src="' + url + '"></a>' + } + else if (short_urls) { + return '<a href="' + url + '" target="_blank">[' + get_domain(url) + ']</a>' + } + else { + return '<a href="' + url + '" target="_blank">' + url + '</a>' + } + }) + }).join("<br>\n") +} +function getDomain(url){ + return url.replace(/https?:\/\//,"").replace(/\/.*/,"").replace(/www\./, "") +} diff --git a/client/vendor/paths.js b/client/vendor/paths.js new file mode 100644 index 0000000..14c35b1 --- /dev/null +++ b/client/vendor/paths.js @@ -0,0 +1,18 @@ +function filepath (file) { + return '/data/' + file.folder_id + '/' + encodeURIComponent(file.name) +} +function mp3path (file) { + if (file.mime !== 'audio/mp3') { + return filepath(file) + '.mp3' + } + return filepath(file) +} +function pngpath (file) { + return filepath(file) + '.png' +} + +export { + filepath, + mp3path, + pngpath, +}
\ No newline at end of file |
