forked from xoxco/awarejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaware.js
More file actions
180 lines (135 loc) · 4.17 KB
/
aware.js
File metadata and controls
180 lines (135 loc) · 4.17 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(function($) {
/*
A Javascript library to help create dynamic
reader-aware interfaces to content.
by Ben Brown ben@xoxco.com
*/
var lastVisit = false;
/* Helpful little date helper here! */
Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}
function setLastVisit(date) {
if (window.localStorage) {
window.localStorage.setItem('lastVisit',date);
} else {
// fix this
// set a cookie
}
}
function getLastVisit() {
if (window.localStorage) {
return window.localStorage.getItem('lastVisit');
} else {
// fix this
// return from cookie
}
}
function pluralizeString(num,str) {
if (num==1) {
return str;
} else {
return str+'s';
}
}
function relativeTimestamp(ms) {
var seconds = Math.floor(ms / 1000);
if (seconds < 60) {
return seconds + pluralizeString(seconds,' second');
}
var minutes = Math.floor(seconds/60);
if (minutes < 60) {
return minutes + pluralizeString(minutes,' minute');
}
var hours = Math.floor(minutes/60);
if (hours < 24) {
return hours + pluralizeString(hours,' hour');
}
var days = Math.floor(hours/24);
if (days < 7) {
return days + pluralizeString(days,' day');
}
var weeks = Math.floor(days/7);
return weeks + pluralizeString(weeks,' week');
}
// insert a bookmark with a relative timestamp after the last new item on the page.
$.fn.shkmark = function(options) {
var settings = {
'className': 'shkmark',
'element': 'li',
'newIndicator':'.new'
}
$.extend(settings,options);
if (!$(this).length || !$(this).filter(settings.newIndicator).length) {
return;
}
if (!lastVisit) {
lastVisit = getLastVisit();
}
if (!lastVisit) { return; }
lastVisit = new Date(lastVisit);
var now = new Date();
var message = 'You started reading here ' + relativeTimestamp(now-lastVisit) + ' ago';
var bookmark = document.createElement(settings.element);
$(bookmark).addClass(settings.className);
$(bookmark).html(message);
if($(this).last()[0]!=$(this).filter(settings.newIndicator)[0]) {
$(this).filter(settings.newIndicator).last().after(bookmark);
}
}
$.fn.aware = function(options) {
var settings = {
dateAttribute: 'data-pubDate',
bufferTime: 60*60*1000 // by default, leave things new if they are an hour old or less
}
var reader = {};
$.extend(settings,options);
// retrieve user's last visit timestamp
// but make sure not to override it if already set once this session!
if (!lastVisit) {
lastVisit = getLastVisit();
}
var now = new Date();
if (!lastVisit) {
setLastVisit(now);
$('body').addClass('first-visit');
reader.lastVisit = now;
reader.firstVisit = true;
reader.secondsSinceLastVisit = 0;
window.reader = reader;
return;
} else {
lastVisit = new Date(lastVisit);
reader.lastVisit = lastVisit;
}
if (lastVisit.getDOY() < now.getDOY()) {
$('body').addClass('first-visit-of-day');
$('body').addClass('repeat-visitor');
reader.firstVisitOfDay = true;
reader.repeatVisitor = true;
} else {
if (!$('body').hasClass('first-visit')) {
$('body').addClass('repeat-visitor');
reader.repeatVisitor = true;
}
}
this.each(function() {
// find the date element
var postDate = $(this).attr(settings.dateAttribute);
if (postDate) {
var arr = postDate.split(/[- :]/);
postTimestamp = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
if (postTimestamp > lastVisit-settings.bufferTime) {
$(this).addClass('new');
} else {
$(this).addClass('seen');
}
}
});
reader.secondsSinceLastVisit = Math.floor((now-lastVisit)/1000);
reader.timeSinceLastVisit = relativeTimestamp(now-lastVisit);
window.reader = reader;
setLastVisit(now);
}
})(jQuery);