-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkr-list.html
74 lines (72 loc) · 2.07 KB
/
linkr-list.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<link rel="import" href="./bower_components/polymer/polymer.html">
<dom-module name="linkr-list" attributes="links">
<style>
:host { list-style: none; padding: 0px; margin: 0px;}
li:first-child { border-top: 1px solid; }
li { border: 1px solid; border-top: 0px; }
h3, p { margin: 0px; }
h3 { padding: 4px; padding-bottom: 0px; }
p { padding: 4px; }
</style>
<template>
<template is="dom-repeat" items="{{links}}">
<li>
<h3>
<a href="{{item.url}}">{{item.title}}</a>
<small>{{item.domain}}</small>
</h3>
<p>Posted <span>{{item.dateStr}}</span> in <a href="{{item.categoryUrl}}" title="View all in posts in this category">{{item.category}}</a></p>
</li>
</template>
</template>
</dom-module>
<script>
var getDomain = function (url) {
// strip protocol:// and everything after domain (slash)
var reg = /^[\w]*\:\/\/([^\/]*)/;
var stripped = url.match(reg)[1] || '';
// strip subdomains
var reg2 = /([\w]*\.[\w]*$)/;
var domainOnly = stripped.match(reg2)[1] || '';
return domainOnly;
};
var timeAgo = function (date) {
var unit = Math.floor((Date.now() - date)/1000);
var uNames = ['second', 'minute', 'hour', 'day', 'year'];
var uLens = [60, 60, 24, 365, Infinity];
for (var i = 0; i < uNames.length; i += 1) {
var div = uLens[i];
if (unit < div) {
var pl = (unit !== 1) ? 's' : '';
return unit + " " + uNames[i] + pl + ' ago';
}
unit = Math.floor(unit/div);
}
};
Polymer({
is: 'linkr-list',
extends: "ul",
properties: {
links: {
type: Array,
value: [],
observer: 'update'
},
baseUrl: {
type: String,
value: '/c/'
}
},
update: function () {
var base = this.baseUrl;
this.links.forEach(function (link) {
var d = getDomain(link.url);
if (d) {
link.domain = '(' + d + ')';
}
link.categoryUrl = base + link.category;
link.dateStr = timeAgo(link.date);
});
}
});
</script>