commento/frontend/js/utils.js

129 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-06-04 00:06:17 +08:00
(function (global, document) {
"use strict";
2018-06-04 00:06:17 +08:00
// Gets a GET parameter in the current URL.
global.paramGet = function(param) {
var pageURL = decodeURIComponent(window.location.search.substring(1));
var urlVariables = pageURL.split("&");
2018-06-04 00:06:17 +08:00
for (var i = 0; i < urlVariables.length; i++) {
var paramURL = urlVariables[i].split("=");
if (paramURL[0] === param) {
2018-06-04 00:06:17 +08:00
return paramURL[1] === undefined ? true : paramURL[1];
}
2018-06-04 00:06:17 +08:00
}
return null;
}
// Sets the disabled attribute in a button.
global.buttonDisable = function(id) {
var el = $(id);
el.attr("disabled", true);
}
// Unsets the disabled attribute in a button.
global.buttonEnable = function(id) {
var el = $(id);
el.attr("disabled", false);
}
// Sets the text on the given label ID.
global.textSet = function(id, text) {
var el = $(id);
el.show();
el.text(text);
}
// Given an array of input IDs, this function calls a callback function with
// the first unfilled ID.
global.unfilledMark = function(fields, callback) {
var allOk = true;
2018-06-04 00:06:17 +08:00
for (var i = 0; i < fields.length; i++) {
var el = $(fields[i]);
if (el.val() === "") {
2018-06-04 00:06:17 +08:00
callback(el);
}
}
return allOk;
2018-06-04 00:06:17 +08:00
}
// Gets the value of a cookie.
global.cookieGet = function(name) {
var c = "; " + document.cookie;
var x = c.split("; " + name + "=");
if (x.length === 2) {
2018-06-04 00:06:17 +08:00
return x.pop().split(";").shift();
}
2018-06-04 00:06:17 +08:00
};
// Sets the value of a cookie.
global.cookieSet = function(name, value) {
var expires = "";
var date = new Date();
date.setTime(date.getTime() + (365*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
var cookieString = name + "=" + value + expires + "; path=/";
if (/^https:\/\//i.test(origin)) {
cookieString += "; secure";
}
document.cookie = cookieString;
2018-06-04 00:06:17 +08:00
}
// Deletes a cookie.
global.cookieDelete = function(name) {
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
}
2018-06-04 00:06:17 +08:00
// Converts a date in the past to a human-friendly duration relative to now.
global.timeSince = function(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
2018-06-04 00:06:17 +08:00
return interval + " years ago";
}
2018-06-04 00:06:17 +08:00
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
2018-06-04 00:06:17 +08:00
return interval + " months ago";
}
2018-06-04 00:06:17 +08:00
interval = Math.floor(seconds / 86400);
if (interval > 1) {
2018-06-04 00:06:17 +08:00
return interval + " days ago";
}
2018-06-04 00:06:17 +08:00
interval = Math.floor(seconds / 3600);
if (interval > 1) {
2018-06-04 00:06:17 +08:00
return interval + " hours ago";
}
2018-06-04 00:06:17 +08:00
interval = Math.floor(seconds / 60);
if (interval > 1) {
2018-06-04 00:06:17 +08:00
return interval + " minutes ago";
}
2018-06-04 00:06:17 +08:00
if (seconds > 5) {
2018-06-04 00:06:17 +08:00
return Math.floor(seconds) + " seconds ago";
} else {
2018-06-04 00:06:17 +08:00
return "just now";
}
2018-06-04 00:06:17 +08:00
}
} (window.commento, document));