import $ from "jquery";
import utils from "base/js/utils";
import { URLs } from "./urls";
import { console } from "./console";
var url_parts = new RegExp("^((([^:/?#]+):)?(//([^/?#]*))?)?(.*)");
function strip_hostname(f) {
if (f._strip_hostname_applied) {
return f;
}
function wrapped() {
var hostname = "";
if (arguments.length > 0) {
var parts = arguments[0].match(url_parts);
hostname = parts[1];
arguments[0] = parts[6];
}
return hostname + f.apply(null, arguments);
}
wrapped._strip_hostname_applied = true;
return wrapped;
}
utils.url_join_encode = strip_hostname(utils.url_join_encode);
utils.url_path_join = strip_hostname(utils.url_path_join);
var ID;
function cellSessionID() {
return (ID = ID || utils.uuid());
}
function sendRequest(method, url, data, callback, files) {
var isXDomain =
URLs.root !==
window.location.protocol + "//" + window.location.host + "/";
method = method.toUpperCase();
var hasFiles = false;
var xhr = new XMLHttpRequest();
var fd = undefined;
if (method === "GET") {
data.rand = Math.random().toString();
}
if (method === "POST" && localStorage.accepted_tos) {
data.accepted_tos = "true";
}
if (window.FormData && method !== "GET") {
fd = new FormData();
for (var k in data) {
if (data.hasOwnProperty(k)) {
fd.append(k, data[k]);
}
}
} else {
fd = "";
for (var k in data) {
if (data.hasOwnProperty(k)) {
fd +=
"&" +
encodeURIComponent(k) +
"=" +
encodeURIComponent(data[k]);
}
}
fd = fd.substr(1);
if (fd.length > 0 && method === "GET") {
url += "?" + fd;
fd = undefined;
}
}
if (window.FormData || !(isXDomain )) {
xhr.open(method, url, true);
xhr.withCredentials = true;
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && callback) {
callback(xhr.responseText);
}
};
if (typeof fd === "string") {
xhr.setRequestHeader(
"Content-type",
"application/x-www-form-urlencoded"
);
}
xhr.send(fd);
} else if (method === "GET") {
url += (url.indexOf("?") === -1 ? "?" : "&") + "callback=?";
$.getJSON(url, callback);
} else {
var iframe = document.createElement("iframe");
iframe.name = utils.uuid();
var form = document.createElement("form", {
method: "POST",
action: url,
target: iframe.name,
});
if (data === undefined) {
data = {};
}
data.method = method;
for (var k in data) {
if (data.hasOwnProperty(k)) {
form.appendChild(
document.createElement("input", { name: k, value: data[k] })
);
}
}
form.appendChild(
document.createElement("input", { name: "frame", value: "on" })
);
form.style.display = iframe.style.display = "none";
document.body.appendChild(iframe);
document.body.appendChild(form);
var listen = function (evt) {
if (
evt.source === iframe.contentWindow &&
evt.origin + "/" === URLs.root
) {
if (window.removeEventListener) {
removeEventListener("message", listen);
} else {
detachEvent("onmessage", listen);
}
callback(evt.data);
document.body.removeChild(iframe);
}
};
if (window.addEventListener) {
window.addEventListener("message", listen);
} else {
window.attachEvent("onmessage", listen);
}
form.submit();
document.body.removeChild(form);
}
}
export default {
always_new: utils.always_new,
cellSessionID: cellSessionID,
createElement: function (type, attrs, children) {
var node = document.createElement(type);
for (var k in attrs) {
if (attrs.hasOwnProperty(k)) {
node.setAttribute(k, attrs[k]);
}
}
if (children) {
for (var i = 0; i < children.length; i++) {
if (typeof children[i] == "string") {
node.appendChild(document.createTextNode(children[i]));
} else {
node.appendChild(children[i]);
}
}
}
return node;
},
fixConsole: utils.fixConsole,
proxy: function (methods) {
var proxy = { _callbacks: [] };
$.each(methods, function (i, method) {
proxy[method] = function () {
proxy._callbacks.push([method, arguments]);
console.log("stored proxy for " + method);
};
});
proxy._run_callbacks = function (obj) {
$.each(proxy._callbacks, function (i, cb) {
obj[cb[0]].apply(obj, cb[1]);
});
};
return proxy;
},
sendRequest: sendRequest,
simpleTimer: function () {
var t = new Date().getTime();
console.debug("starting timer from " + t);
return function (reset) {
var old_t = t;
var new_t = new Date().getTime();
if (reset) {
t = new_t;
}
return new_t - old_t + " ms";
};
},
throttle: function (func, wait) {
var context, args, timeout, result;
var previous = 0;
var later = function () {
previous = new Date();
timeout = null;
result = func.apply(context, args);
};
return function () {
var now = new Date();
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
},
uuid: utils.uuid,
};