var $ = require("jquery"); var Key = { UP: 38, DOWN: 40, PAGE_UP: 33, PAGE_DOWN: 34, HOME: 36, END: 35, LEFT: 37, RIGHT: 39, ENTER: 13, ESC: 27, TAB: 9, SPACE: 32, BACKSPACE: 8, }; // Add A-Z for (var i = 65; i <= 90; i++) { Key[String.fromCharCode(i)] = i; } var formatSize = function (bytes) { if (bytes === 0) return "0"; var prefix = ["b", "kb", "mb", "gb", "tb"]; for (var i = 0; i < prefix.length; i++){ if (Math.pow(1024, i + 1) > bytes){ break; } } var precision; if (bytes%Math.pow(1024, i) === 0) precision = 0; else precision = 1; return (bytes/Math.pow(1024, i)).toFixed(precision) + prefix[i]; }; var formatTimeDelta = function (milliseconds) { var time = milliseconds; var prefix = ["ms", "s", "min", "h"]; var div = [1000, 60, 60]; var i = 0; while (Math.abs(time) >= div[i] && i < div.length) { time = time / div[i]; i++; } return Math.round(time) + prefix[i]; }; var formatTimeStamp = function (seconds) { var ts = (new Date(seconds * 1000)).toISOString(); return ts.replace("T", " ").replace("Z", ""); }; function EventEmitter() { this.listeners = {}; } EventEmitter.prototype.emit = function (event) { if (!(event in this.listeners)) { return; } var args = Array.prototype.slice.call(arguments, 1); this.listeners[event].forEach(function (listener) { listener.apply(this, args); }.bind(this)); }; EventEmitter.prototype.addListener = function (events, f) { events.split(" ").forEach(function (event) { this.listeners[event] = this.listeners[event] || []; this.listeners[event].push(f); }.bind(this)); }; EventEmitter.prototype.removeListener = function (events, f) { if (!(events in this.listeners)) { return false; } events.split(" ").forEach(function (event) { var index = this.listeners[event].indexOf(f); if (index >= 0) { this.listeners[event].splice(index, 1); } }.bind(this)); }; function getCookie(name) { var r = document.cookie.match("\\b" + name + "=([^;]*)\\b"); return r ? r[1] : undefined; } var xsrf = $.param({_xsrf: getCookie("_xsrf")}); //Tornado XSRF Protection. $.ajaxPrefilter(function (options) { if (["post", "put", "delete"].indexOf(options.type.toLowerCase()) >= 0 && options.url[0] === "/") { if (options.data) { options.data += ("&" + xsrf); } else { options.data = xsrf; } } }); // Log AJAX Errors $(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) { var message = jqXHR.responseText; console.error(message, arguments); EventLogActions.add_event(thrownError + ": " + message); window.alert(message); }); module.exports = { EventEmitter: EventEmitter, formatSize: formatSize, formatTimeDelta: formatTimeDelta, formatTimeStamp: formatTimeStamp, Key: Key };