commento/frontend/js/utils.js
Anton Linevych 9e3935b3b2 frontend: use gulp, eslint, code refactor
Apologies in advance for the insanely huge commit. This commit is
primarily based on Anton Linevych's amazing work found here [1]. While
he had gone through the pains of making small, atomic changes to each
file, I had put off reviewing the PR for a long time. By the time I
finally got around to it, the project had changed so much that it didn't
make sense to keep the commits the same way. So I've cherry-picked most
of his commits, with some changes here and there, and I've squashed them
into one commit.

[1] https://gitlab.com/linevych/commento-ce/tree/feature/frontend_building_improvements
2018-12-20 04:14:48 -05:00

129 lines
2.9 KiB
JavaScript

(function (global, document) {
"use strict";
// Gets a GET parameter in the current URL.
global.paramGet = function(param) {
var pageURL = decodeURIComponent(window.location.search.substring(1));
var urlVariables = pageURL.split("&");
for (var i = 0; i < urlVariables.length; i++) {
var paramURL = urlVariables[i].split("=");
if (paramURL[0] === param) {
return paramURL[1] === undefined ? true : paramURL[1];
}
}
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;
for (var i = 0; i < fields.length; i++) {
var el = $(fields[i]);
if (el.val() === "") {
callback(el);
}
}
return allOk;
}
// Gets the value of a cookie.
global.cookieGet = function(name) {
var c = "; " + document.cookie;
var x = c.split("; " + name + "=");
if (x.length === 2) {
return x.pop().split(";").shift();
}
};
// 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;
}
// Deletes a cookie.
global.cookieDelete = function(name) {
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
}
// 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) {
return interval + " years ago";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months ago";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days ago";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours ago";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes ago";
}
if (seconds > 5) {
return Math.floor(seconds) + " seconds ago";
} else {
return "just now";
}
}
} (window.commento, document));