Skip to content

Instantly share code, notes, and snippets.

@rarous
Created March 23, 2010 14:08
Show Gist options
  • Save rarous/341201 to your computer and use it in GitHub Desktop.
Save rarous/341201 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="cs">
<head>
<title></title>
<script src="scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="scripts/rx.js" type="text/javascript"></script>
<script type="text/javascript">
Array.prototype.toObservable = function () {
return Rx.Observable.FromArray(this);
};
jQuery.fn.toObservable = function (eventType, eventData) {
if (arguments.length == 0) {
return Rx.Observable.FromArray(this.toArray());
}
return Rx.Observable.FromJQuery(this, eventType, eventData);
}
Date.prototype.setISO8601 = function(dString) {
var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
if (dString.toString().match(regexp)) {
var d = dString.match(regexp);
var offset = 0;
this.setUTCDate(1);
this.setUTCFullYear(parseInt(d[1], 10));
this.setUTCMonth(parseInt(d[3], 10) - 1);
this.setUTCDate(parseInt(d[5], 10));
this.setUTCHours(parseInt(d[7], 10));
this.setUTCMinutes(parseInt(d[9], 10));
this.setUTCSeconds(parseInt(d[11], 10));
if (d[12])
this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
else
this.setUTCMilliseconds(0);
if (d[13] != 'Z') {
offset = (d[15] * 60) + parseInt(d[17], 10);
offset *= ((d[14] == '-') ? -1 : 1);
this.setTime(this.getTime() - offset * 60 * 1000);
}
}
else {
this.setTime(Date.parse(dString));
}
return this;
};
var DateCutizer = function(options) {
var defaults = {
lang: document.documentElement.getAttribute('lang') || 'en',
texts: {
'cs': ["v budoucnosti", "právě teď", "před minutou", "před {0} minutami", "před hodinou", "před {0} hodinami"],
'en': ["in the future", "right now", "a minute ago", "{0} minutes ago", "an hour ago", "{0} hours ago"]
}
};
this.options = options || defaults;
}
DateCutizer.prototype.formatString = function(str) {
var result = str;
for (var i = 1, length = arguments.length; i < length; i++) {
var index = i - 1;
var stringToReplace = '{' + index + '}';
result = result.replace(stringToReplace, arguments[i]);
}
return result;
}
DateCutizer.prototype.makeCute = function (date) {
var MINUTE = 60 * 1000;
var TWO_MINUTES = 2 * MINUTE;
var HOUR = 60 * MINUTE;
var TWO_HOURS = 2 * HOUR;
var DAY = 24 * HOUR;
var now = new Date();
var delta = now - date;
if (delta < 0) {
return this.options.texts[this.options.lang][0];
}
else if (delta < MINUTE) {
return this.options.texts[this.options.lang][1];
}
else if (delta < TWO_MINUTES) {
return this.options.texts[this.options.lang][2];
}
else if (delta < HOUR) {
return this.formatString(this.options.texts[this.options.lang][3], new Date(delta).getMinutes());
}
else if (delta < TWO_HOURS) {
return this.options.texts[this.options.lang][4];
}
else if (delta < DAY) {
return this.formatString(this.options.texts[this.options.lang][5], new Date(delta).getHours());
}
return date.toLocaleDateString();
};
function createCuteText(date) {
var dateCutizer = new DateCutizer();
return dateCutizer.makeCute(date);
}
function changeElementText(entry) {
var cuteText = createCuteText(entry.value);
$('.text', entry.element).text(cuteText);
}
function parseDate(el) {
var value = $('.value-title,.value', el);
if (!value.size()) value = el;
return value.attr('title') || value.text();
}
function mapToElementValuePair(element) {
var el = $(element);
var date = parseDate(el);
return {
element: el,
value: new Date().setISO8601(date)
};
}
function observeTimeChage(observable) {
observable.
Select(mapToElementValuePair).
Subscribe(changeElementText);
}
$(function () {
var hAtomDates = $('.hentry .updated,.hentry .published');
var interval = Rx.Observable.Interval(10000).Select(function () {
return hAtomDates.toObservable();
}).Switch();
observeTimeChage(hAtomDates.toObservable());
observeTimeChage(interval);
});
</script>
</head>
<body>
<ul class="null list10 list10b hfeed">
<li class="hentry" id="activity-2330">
<div class="cont">
<p class="entry-title"><a class="vcard author" href="/lide/zbiejczuk"><span class="admin" title="Trop Administrator"><span class="power" title="Znalec"><span class="fn">zbiejczuk</span></span></span></a>
napsal recenzi pro <a href="/mista/808/ku-bar-praha#review-1096" rel="Bookmark">K.U. Bar</a></p>
<p class="entry-content">
Hoďte na sebe nevyzývavě značkové oblečení, kupte si drahý cigára,
popíjejte koktejly a konverzujte markeťáckým…
</p>
<p class="meta">přidáno <span class="updated"><span class="text">22. března 2010 ve 12.24</span><span class="value-title" title="2010-03-24T13:24:19Z"></span></span></p>
</div>
<div class="cleaner"><hr /></div>
</li>
<li class="hentry" id="activity-2329">
<div class="cont">
<p class="entry-title"><a class="vcard author" href="/lide/zbiejczuk"><span class="admin" title="Trop Administrator"><span class="power" title="Znalec"><span class="fn">zbiejczuk</span></span></span></a>
vytvořil nové místo <a href="/mista/808/ku-bar-praha" rel="Bookmark">K.U. Bar</a></p>
<p class="entry-content">
</p>
<p class="meta">přidáno <span class="published"><span class="text">22. března 2010 ve 12.15</span><span class="value-title" title="2010-03-23T12:15:19Z"></span></span></p>
</div>
<div class="cleaner"><hr /></div>
</li>
<li class="hentry" id="activity-2328">
<div class="cont">
<p class="entry-title"><a class="vcard author" href="/lide/kolman"><span class="fn">kolman</span></a>
napsal recenzi pro <a href="/mista/651/potrefena-husa-hradec-kralove#review-1095" rel="Bookmark">Potrefen&#225; Husa</a></p>
<p class="entry-content">
Na Huse se mi nejvíc líbí, že je tu vždycky perfektní obsluha.
</p>
<p class="meta">přidáno <abbr class="updated" title="2010-03-22T20:54:19Z"><span class="text">21. března 2010 ve 22.54</span></span></p>
</div>
<div class="cleaner"><hr /></div>
</li>
<li class="hentry" id="activity-2328">
<div class="cont">
<p class="entry-title"><a class="vcard author" href="/lide/kolman"><span class="fn">kolman</span></a>
napsal recenzi pro <a href="/mista/651/potrefena-husa-hradec-kralove#review-1095" rel="Bookmark">Potrefen&#225; Husa</a></p>
<p class="entry-content">
Na Huse se mi nejvíc líbí, že je tu vždycky perfektní obsluha.
</p>
<p class="meta">přidáno <span class="updated"><span class="text">21. března 2010 ve 22.54</span><span class="value" title="2010-03-15T20:54:19Z"></span></span></p>
</div>
<div class="cleaner"><hr /></div>
</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment