hal/web-resource/js/hal.js
Ziver Koc fc41b60386 Added relative timestamps
Former-commit-id: 5ea7e7519cbe1059d9595a82229b7c7de3ef17bf
2016-01-24 17:37:32 +01:00

43 lines
1.1 KiB
JavaScript
Executable file

///////////////////////////////// Autostart
$(function(){
$(".toggle-switch").bootstrapSwitch();
$(".timestamp").relTimestamp();
});
////////////////////////////////////// JQuery functions
// $.attr() # returns all attributes of an element
(function(old) {
$.fn.attr = function() {
if(arguments.length === 0) {
if(this.length === 0) {
return null;
}
var obj = {};
$.each(this[0].attributes, function() {
if(this.specified) {
obj[this.name] = this.value;
}
});
return obj;
}
return old.apply(this, arguments);
};
})($.fn.attr);
$.fn.relTimestamp = function() {
return this.each(function() {
var timestamp = parseInt($(this).text());
var timestampNow = Date.now();
var timeDiff = timestampNow - timestamp;
if(timeDiff < 24 * 60 * 60 * 1000) // less than 24 hours
$(this).text(moment(timestamp).fromNow());
else
$(this).text(moment(timestamp).format("YYYY-MM-DD HH:mm"));
return this;
});
};