summaryrefslogtreecommitdiff
path: root/app/client
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-05-31 23:15:32 +0200
committerJules Laplace <julescarbon@gmail.com>2018-05-31 23:15:32 +0200
commitdd31a7b9a3af167808b04ffe2af3a66af8b17c33 (patch)
treed99bc1be5d382d330a844a1c7bcb7b3fb44eda3e /app/client
parentfe399143527972050534b3262c94dfbf291ddb41 (diff)
nice size functions from bucky :)
Diffstat (limited to 'app/client')
-rw-r--r--app/client/api/index.js2
-rw-r--r--app/client/api/util.js15
-rw-r--r--app/client/common/fileList.component.js25
-rw-r--r--app/client/util.js121
4 files changed, 143 insertions, 20 deletions
diff --git a/app/client/api/index.js b/app/client/api/index.js
index 2fcd434..c19b78d 100644
--- a/app/client/api/index.js
+++ b/app/client/api/index.js
@@ -1,5 +1,5 @@
import { crud_actions } from './crud.actions'
-import * as util from './util'
+import * as util from '../util'
import * as parser from './parser'
/*
diff --git a/app/client/api/util.js b/app/client/api/util.js
deleted file mode 100644
index 6500979..0000000
--- a/app/client/api/util.js
+++ /dev/null
@@ -1,15 +0,0 @@
-export const is_iphone = !!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
-export const is_ipad = !!(navigator.userAgent.match(/iPad/i))
-export const is_android = !!(navigator.userAgent.match(/Android/i))
-export const is_mobile = is_iphone || is_ipad || is_android
-export const is_desktop = ! is_mobile;
-
-const htmlClassList = document.body.parentNode.classList
-htmlClassList.add(is_desktop ? 'desktop' : 'mobile')
-htmlClassList.remove('loading')
-
-// window.debug = false
-
-function randint(n) { return Math.floor(Math.random()*n) }
-
-document.body.style.backgroundImage = 'linear-gradient(' + (randint(40)+40) + 'deg, #fde, #ffe)'
diff --git a/app/client/common/fileList.component.js b/app/client/common/fileList.component.js
index b3bcd43..7d90045 100644
--- a/app/client/common/fileList.component.js
+++ b/app/client/common/fileList.component.js
@@ -5,13 +5,16 @@ import { Link } from 'react-router-dom';
import moment from 'moment'
import * as util from '../util'
+const defaultFields = new Set(['date', 'size'])
class FileList extends Component {
constructor(props){
super()
}
render(){
const { files, linkFiles, onClick } = this.props
+ let fields = this.props.fields || defaultFields
const fileList = files.map(file => {
+ const size = util.hush_size(file.size)
return (
<div class='row file' key={file.name}>
<div className="filename" title={file.name || file.url}>
@@ -20,10 +23,24 @@ class FileList extends Component {
: <span class='link' onClick={() => onClick(file)}>{file.name || file.url}</span>
}
</div>
- <div className="date">{moment(file.created_at).format("YYYY-MM-DD h:mm a")}</div>
- <div className="size">{file.size ? ((file.size) / 1024 / 1024).toFixed(1) + ' mb.' : ''}</div>
- <div className="epoch">{file.epoch > 0 ? 'epoch ' + file.epoch : ' '}</div>
- <div className='activity'>{file.activity || ''} {file.module || ''}</div>
+ {fields.has('date') &&
+ <div className={"date " + util.carbon_date(file.created_at)}>{moment(file.created_at).format("YYYY-MM-DD")}</div>
+ }
+ {fields.has('datetime') &&
+ <div className={"datetime " + util.carbon_date(file.created_at)}>{moment(file.created_at).format("YYYY-MM-DD h:mm a")}</div>
+ }
+ {fields.has('size') &&
+ <div className={"size " + size[0]}>{size[1]}</div>
+ }
+ {fields.has('epoch') &&
+ <div className="epoch">{file.epoch > 0 ? 'epoch ' + file.epoch : ' '}</div>
+ }
+ {(fields.has('activity') || fields.has('module')) &&
+ <div className='activity'>
+ {fields.has('activity') && file.activity}
+ {fields.has('module') && file.module}
+ </div>
+ }
{this.props.options && this.props.options(file)}
</div>
)
diff --git a/app/client/util.js b/app/client/util.js
index db9fa8c..04eb9c6 100644
--- a/app/client/util.js
+++ b/app/client/util.js
@@ -1,6 +1,127 @@
+export const is_iphone = !!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
+export const is_ipad = !!(navigator.userAgent.match(/iPad/i))
+export const is_android = !!(navigator.userAgent.match(/Android/i))
+export const is_mobile = is_iphone || is_ipad || is_android
+export const is_desktop = ! is_mobile;
+
+const htmlClassList = document.body.parentNode.classList
+htmlClassList.add(is_desktop ? 'desktop' : 'mobile')
+htmlClassList.remove('loading')
+
+// window.debug = false
+
+function randint(n) { return Math.floor(Math.random()*n) }
+
+document.body.style.backgroundImage = 'linear-gradient(' + (randint(40)+40) + 'deg, #fde, #ffe)'
+
export function timeInSeconds(n){
return (n / 10).toFixed(1) + ' s.'
}
export function gerund(s){
return s.replace(/e?$/, 'ing')
}
+export function commatize (n, radix) {
+ radix = radix || 1024
+ var nums = [], i, counter = 0, r = Math.floor
+ if (n > radix) {
+ n /= radix
+ 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("")
+}
+export function carbon_date (date, no_bold) {
+ var span = (+new Date() - new Date(date)) / 1000, color
+ 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
+}
+export function hush_views (n, bias, no_bold) {
+ var txt = commatize(n, 1000)
+ 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 (no_bold || n < 10000) { return ["recent", txt + " kv."] }
+ else { return ["new", txt + " kv."] }
+}
+export function hush_threads (n, bias, no_bold) {
+ var txt = commatize(n, 1000)
+ bias = bias || 1
+ n = n || 0
+ if (n < 10) { return["quiet", n + " t."] }
+ else if (n < 25) { return ["old", txt + " t."] }
+ else if (n < 50) { return ["med", txt + " t."] }
+ else if (no_bold || n < 100) { return ["recent", txt + " t."] }
+ else { return ["new", txt + " t."] }
+}
+export function hush_size (n, bias, no_bold) {
+ var txt = commatize(Math.floor(n / 1024))
+ bias = 1 || bias
+ n = n || 0
+ if (! n) { return ['', ''] }
+ 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 (no_bold || n < (170000000/bias)) {
+ return ["recent", txt + " mb."]
+ }
+ else {
+ return ["new", txt + " mb."]
+ }
+}
+export function hush_null (n, unit, no_bold) {
+ var 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]
+ }
+}
+export function courtesy_s (n, s) { return n == 1 ? "" : (s || "s") }