From a00295a2b4202f47291c89e00e984338503a057c Mon Sep 17 00:00:00 2001 From: rottencandy Date: Fri, 5 Apr 2019 00:41:48 +0530 Subject: [PATCH 1/7] Add login.js login.js when used as a module works as a mithril component that renders a login page and communicates with RS Json API to faciliate validation. Also updated qmake file to include login.js in build. --- webui-src/app/login.js | 31 +++++++++++++++++++++++++++++++ webui.pro | 1 + 2 files changed, 32 insertions(+) create mode 100644 webui-src/app/login.js diff --git a/webui-src/app/login.js b/webui-src/app/login.js new file mode 100644 index 0000000..a58af9f --- /dev/null +++ b/webui-src/app/login.js @@ -0,0 +1,31 @@ +var m = require("mithril"); +var rs = require("rswebui"); + +function loginResponse(isSuccessful) { + if (isSuccessful) { + window.location.href = window.location.href.split("?")[0] + "?/downloads"; + } + else { + m.render(document.getElementById("warning"), "Incorrent login/password"); + } +} + +function startLogin() { + var uname = document.getElementById("uname").value; + var passwd = document.getElementById("passwd").value; + rs.validateLogin(uname, passwd, loginResponse); +} + +module.exports = { + view: function() { + return m("div[id=loginbox]", + [ + m("input", {type: "text", placeholder: "Username", id: "uname"}), + m("input", {type: "password", placeholder: "Password", id: "passwd"}), + m("button", {onclick: startLogin, style: "width:5em;height:2em;"}, "Login"), + m("p", {id: "warning", style: "color:red"}), + ] + ) + } +} + diff --git a/webui.pro b/webui.pro index 63090f5..d825cee 100644 --- a/webui.pro +++ b/webui.pro @@ -43,6 +43,7 @@ linux-* { WEBUI_SRC_JS += webui-src/app/mithril.js WEBUI_SRC_JS += webui-src/app/mithril.min.js WEBUI_SRC_JS += webui-src/app/downloads.js + WEBUI_SRC_JS += webui-src/app/login.js # WEBUI_SRC_JS += webui-src/app/peers.js # WEBUI_SRC_JS += webui-src/app/main.js From 5827932f252e76397250e1ee6fb0b2d8b3b23ac7 Mon Sep 17 00:00:00 2001 From: rottencandy Date: Fri, 5 Apr 2019 00:45:00 +0530 Subject: [PATCH 2/7] Update rswebui.js to use modifiable API keys Added extra function to verify API keys(to be used by login.js) and updated HTTP requests function to use a different callback on failiure. --- webui-src/app/rswebui.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/webui-src/app/rswebui.js b/webui-src/app/rswebui.js index 6185a3a..a9ae01a 100644 --- a/webui-src/app/rswebui.js +++ b/webui-src/app/rswebui.js @@ -3,7 +3,12 @@ var m = require("mithril"); var rsJsonApiUrl = "http://127.0.0.1:9092" -function rsJsonApiRequest(path, data, callback,async) +var loginKey = { + username: "", + passwd: "", + }; + +function rsJsonApiRequest(path, data, callback, async, failCallback) { console.log("rsJsonApiRequest(path, data, callback)", path, data, callback) var xhr = new XMLHttpRequest(); @@ -12,15 +17,35 @@ function rsJsonApiRequest(path, data, callback,async) if(xhr.readyState === 4) { console.log( path, "callback", xhr.status, xhr.responseText.replace(/\s+/g, " ").substr(0, 60).replace(/\r?\n|\r/g," ") ) - if(typeof(callback) === "function") callback(xhr.responseText); + if(xhr.status === 200) + { + if(typeof(callback) === "function") callback(xhr.responseText); + } + else + if(typeof(failCallback) === "function") failCallback(xhr.status); } } xhr.open('POST', rsJsonApiUrl + path, async); xhr.setRequestHeader("Accept", "application/json"); - xhr.setRequestHeader("Authorization", "Basic "+btoa("cyril"+":"+"proutprout233")); + xhr.setRequestHeader("Authorization", "Basic "+btoa(loginKey.username + ":" + loginKey.passwd)); xhr.send(data); } +function validateLogin(username, password, callback) { + loginKey.username = username; + loginKey.passwd = password; + rsJsonApiRequest("/rsPeers/GetRetroshareInvite", + "", + function() { + callback(true); + }, + true, + function() { + callback(false); + }, + ) +} + // These constants are the onces listed in retroshare/rsfiles.h. I would like to make them "members" of Downloads // but I dont know how to do this. @@ -119,6 +144,7 @@ module.exports = { } rsJsonApiRequest("/rsPeers/GetRetroshareInvite", "", setNodeCertificate,true) }, + validateLogin, Downloads, From 113f68ef3de144b80e01806eb01a0adba6b6b3dc Mon Sep 17 00:00:00 2001 From: rottencandy Date: Fri, 5 Apr 2019 00:49:12 +0530 Subject: [PATCH 3/7] Update index.html to use render login.js on start. index.html's initial startpoint now loads all pages using mithril's route functionality. And changed downloads.js to disable updating of downloads list before the keys have been verified. --- webui-src/app/assets/index.html | 6 +++++- webui-src/app/downloads.js | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/webui-src/app/assets/index.html b/webui-src/app/assets/index.html index 9e1e3fc..375a37b 100644 --- a/webui-src/app/assets/index.html +++ b/webui-src/app/assets/index.html @@ -25,12 +25,16 @@

//var ui = require("rswebui"); //ui.requestCertificate(load_cert); + var login = require("login"); var dl = require("downloads"); //dl.view(load_downloads); var m = require("mithril"); - m.mount(document.body,dl); + m.route(document.body, "/login", { + "/login": login, + "/downloads": dl, + }) } diff --git a/webui-src/app/downloads.js b/webui-src/app/downloads.js index 709d958..946b670 100644 --- a/webui-src/app/downloads.js +++ b/webui-src/app/downloads.js @@ -43,8 +43,8 @@ function cntrlBtn(file, act) { } module.exports = { - oninit: rs.Downloads.load(), // means we re-load the list everytime we render - onload: rs.Downloads.load(), // means we re-load the list everytime we render + //oninit: rs.Downloads.load(), // means we re-load the list everytime we render + //onload: rs.Downloads.load(), // means we re-load the list everytime we render view: function() { var filestreamer_url = "/fstream/"; From e35ce1d52b074429df68177ba12091e53fd38fe4 Mon Sep 17 00:00:00 2001 From: rottencandy Date: Fri, 5 Apr 2019 19:30:18 +0530 Subject: [PATCH 4/7] Update mithril from v0.2.3 to v1.1.6 --- webui-src/app/mithril.js | 3196 ++++++++++++---------------------- webui-src/app/mithril.min.js | 52 +- 2 files changed, 1200 insertions(+), 2048 deletions(-) diff --git a/webui-src/app/mithril.js b/webui-src/app/mithril.js index d18f818..a96b509 100644 --- a/webui-src/app/mithril.js +++ b/webui-src/app/mithril.js @@ -1,2141 +1,1257 @@ -;(function (global, factory) { // eslint-disable-line - "use strict" - /* eslint-disable no-undef */ - var m = factory(global) - if (typeof module === "object" && module != null && module.exports) { - module.exports = m - } else if (typeof define === "function" && define.amd) { - define(function () { return m }) - } else { - global.m = m - } - /* eslint-enable no-undef */ -})(typeof window !== "undefined" ? window : {}, function (global, undefined) { // eslint-disable-line - "use strict" - - m.version = function () { - return "v0.2.3" - } - - var hasOwn = {}.hasOwnProperty - var type = {}.toString - - function isFunction(object) { - return typeof object === "function" - } - - function isObject(object) { - return type.call(object) === "[object Object]" - } - - function isString(object) { - return type.call(object) === "[object String]" - } - - var isArray = Array.isArray || function (object) { - return type.call(object) === "[object Array]" - } - - function noop() {} - - var voidElements = { - AREA: 1, - BASE: 1, - BR: 1, - COL: 1, - COMMAND: 1, - EMBED: 1, - HR: 1, - IMG: 1, - INPUT: 1, - KEYGEN: 1, - LINK: 1, - META: 1, - PARAM: 1, - SOURCE: 1, - TRACK: 1, - WBR: 1 - } - - // caching commonly used variables - var $document, $location, $requestAnimationFrame, $cancelAnimationFrame - - // self invoking function needed because of the way mocks work - function initialize(mock) { - $document = mock.document - $location = mock.location - $cancelAnimationFrame = mock.cancelAnimationFrame || mock.clearTimeout - $requestAnimationFrame = mock.requestAnimationFrame || mock.setTimeout - } - - // testing API - m.deps = function (mock) { - initialize(global = mock || window) - return global - } - - m.deps(global) - - /** - * @typedef {String} Tag - * A string that looks like -> div.classname#id[param=one][param2=two] - * Which describes a DOM node - */ - - function parseTagAttrs(cell, tag) { - var classes = [] - var parser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])/g - var match - - while ((match = parser.exec(tag))) { - if (match[1] === "" && match[2]) { - cell.tag = match[2] - } else if (match[1] === "#") { - cell.attrs.id = match[2] - } else if (match[1] === ".") { - classes.push(match[2]) - } else if (match[3][0] === "[") { - var pair = /\[(.+?)(?:=("|'|)(.*?)\2)?\]/.exec(match[3]) - cell.attrs[pair[1]] = pair[3] || (pair[2] ? "" : true) +;(function() { +"use strict" +function Vnode(tag, key, attrs0, children, text, dom) { + return {tag: tag, key: key, attrs: attrs0, children: children, text: text, dom: dom, domSize: undefined, state: undefined, _state: undefined, events: undefined, instance: undefined, skip: false} +} +Vnode.normalize = function(node) { + if (Array.isArray(node)) return Vnode("[", undefined, undefined, Vnode.normalizeChildren(node), undefined, undefined) + if (node != null && typeof node !== "object") return Vnode("#", undefined, undefined, node === false ? "" : node, undefined, undefined) + return node +} +Vnode.normalizeChildren = function normalizeChildren(children) { + for (var i = 0; i < children.length; i++) { + children[i] = Vnode.normalize(children[i]) + } + return children +} +var selectorParser = /(?:(^|#|\.)([^#\.\[\]]+))|(\[(.+?)(?:\s*=\s*("|'|)((?:\\["'\]]|.)*?)\5)?\])/g +var selectorCache = {} +var hasOwn = {}.hasOwnProperty +function isEmpty(object) { + for (var key in object) if (hasOwn.call(object, key)) return false + return true +} +function compileSelector(selector) { + var match, tag = "div", classes = [], attrs = {} + while (match = selectorParser.exec(selector)) { + var type = match[1], value = match[2] + if (type === "" && value !== "") tag = value + else if (type === "#") attrs.id = value + else if (type === ".") classes.push(value) + else if (match[3][0] === "[") { + var attrValue = match[6] + if (attrValue) attrValue = attrValue.replace(/\\(["'])/g, "$1").replace(/\\\\/g, "\\") + if (match[4] === "class") classes.push(attrValue) + else attrs[match[4]] = attrValue === "" ? attrValue : attrValue || true + } + } + if (classes.length > 0) attrs.className = classes.join(" ") + return selectorCache[selector] = {tag: tag, attrs: attrs} +} +function execSelector(state, attrs, children) { + var hasAttrs = false, childList, text + var className = attrs.className || attrs.class + if (!isEmpty(state.attrs) && !isEmpty(attrs)) { + var newAttrs = {} + for(var key in attrs) { + if (hasOwn.call(attrs, key)) { + newAttrs[key] = attrs[key] } } - - return classes + attrs = newAttrs } - - function getVirtualChildren(args, hasAttrs) { - var children = hasAttrs ? args.slice(1) : args - - if (children.length === 1 && isArray(children[0])) { - return children[0] - } else { - return children - } - } - - function assignAttrs(target, attrs, classes) { - var classAttr = "class" in attrs ? "class" : "className" - - for (var attrName in attrs) { - if (hasOwn.call(attrs, attrName)) { - if (attrName === classAttr && - attrs[attrName] != null && - attrs[attrName] !== "") { - classes.push(attrs[attrName]) - // create key in correct iteration order - target[attrName] = "" - } else { - target[attrName] = attrs[attrName] + for (var key in state.attrs) { + if (hasOwn.call(state.attrs, key)) { + attrs[key] = state.attrs[key] + } + } + if (className !== undefined) { + if (attrs.class !== undefined) { + attrs.class = undefined + attrs.className = className + } + if (state.attrs.className != null) { + attrs.className = state.attrs.className + " " + className + } + } + for (var key in attrs) { + if (hasOwn.call(attrs, key) && key !== "key") { + hasAttrs = true + break + } + } + if (Array.isArray(children) && children.length === 1 && children[0] != null && children[0].tag === "#") { + text = children[0].children + } else { + childList = children + } + return Vnode(state.tag, attrs.key, hasAttrs ? attrs : undefined, childList, text) +} +function hyperscript(selector) { + // Because sloppy mode sucks + var attrs = arguments[1], start = 2, children + if (selector == null || typeof selector !== "string" && typeof selector !== "function" && typeof selector.view !== "function") { + throw Error("The selector must be either a string or a component."); + } + if (typeof selector === "string") { + var cached = selectorCache[selector] || compileSelector(selector) + } + if (attrs == null) { + attrs = {} + } else if (typeof attrs !== "object" || attrs.tag != null || Array.isArray(attrs)) { + attrs = {} + start = 1 + } + if (arguments.length === start + 1) { + children = arguments[start] + if (!Array.isArray(children)) children = [children] + } else { + children = [] + while (start < arguments.length) children.push(arguments[start++]) + } + var normalized = Vnode.normalizeChildren(children) + if (typeof selector === "string") { + return execSelector(cached, attrs, normalized) + } else { + return Vnode(selector, attrs.key, attrs, normalized) + } +} +hyperscript.trust = function(html) { + if (html == null) html = "" + return Vnode("<", undefined, undefined, html, undefined, undefined) +} +hyperscript.fragment = function(attrs1, children) { + return Vnode("[", attrs1.key, attrs1, Vnode.normalizeChildren(children), undefined, undefined) +} +var m = hyperscript +/** @constructor */ +var PromisePolyfill = function(executor) { + if (!(this instanceof PromisePolyfill)) throw new Error("Promise must be called with `new`") + if (typeof executor !== "function") throw new TypeError("executor must be a function") + var self = this, resolvers = [], rejectors = [], resolveCurrent = handler(resolvers, true), rejectCurrent = handler(rejectors, false) + var instance = self._instance = {resolvers: resolvers, rejectors: rejectors} + var callAsync = typeof setImmediate === "function" ? setImmediate : setTimeout + function handler(list, shouldAbsorb) { + return function execute(value) { + var then + try { + if (shouldAbsorb && value != null && (typeof value === "object" || typeof value === "function") && typeof (then = value.then) === "function") { + if (value === self) throw new TypeError("Promise can't be resolved w/ itself") + executeOnce(then.bind(value)) + } + else { + callAsync(function() { + if (!shouldAbsorb && list.length === 0) console.error("Possible unhandled promise rejection:", value) + for (var i = 0; i < list.length; i++) list[i](value) + resolvers.length = 0, rejectors.length = 0 + instance.state = shouldAbsorb + instance.retry = function() {execute(value)} + }) } } + catch (e) { + rejectCurrent(e) + } } - - if (classes.length) target[classAttr] = classes.join(" ") - } - - /** - * - * @param {Tag} The DOM node tag - * @param {Object=[]} optional key-value pairs to be mapped to DOM attrs - * @param {...mNode=[]} Zero or more Mithril child nodes. Can be an array, - * or splat (optional) - */ - function m(tag, pairs) { - var args = [].slice.call(arguments, 1) - - if (isObject(tag)) return parameterize(tag, args) - - if (!isString(tag)) { - throw new Error("selector in m(selector, attrs, children) should " + - "be a string") - } - - var hasAttrs = pairs != null && isObject(pairs) && - !("tag" in pairs || "view" in pairs || "subtree" in pairs) - - var attrs = hasAttrs ? pairs : {} - var cell = { - tag: "div", - attrs: {}, - children: getVirtualChildren(args, hasAttrs) - } - - assignAttrs(cell.attrs, attrs, parseTagAttrs(cell, tag)) - return cell - } - - function forEach(list, f) { - for (var i = 0; i < list.length && !f(list[i], i++);) { - // function called in condition - } - } - - function forKeys(list, f) { - forEach(list, function (attrs, i) { - return (attrs = attrs && attrs.attrs) && - attrs.key != null && - f(attrs, i) - }) } - // This function was causing deopts in Chrome. - function dataToString(data) { - // data.toString() might throw or return null if data is the return - // value of Console.log in some versions of Firefox (behavior depends on - // version) - try { - if (data != null && data.toString() != null) return data - } catch (e) { - // silently ignore errors - } - return "" - } - - // This function was causing deopts in Chrome. - function injectTextNode(parentElement, first, index, data) { - try { - insertNode(parentElement, first, index) - first.nodeValue = data - } catch (e) { - // IE erroneously throws error when appending an empty text node - // after a null - } - } - - function flatten(list) { - // recursively flatten array - for (var i = 0; i < list.length; i++) { - if (isArray(list[i])) { - list = list.concat.apply([], list) - // check current index again and flatten until there are no more - // nested arrays at that index - i-- + function executeOnce(then) { + var runs = 0 + function run(fn) { + return function(value) { + if (runs++ > 0) return + fn(value) } } - return list - } - - function insertNode(parentElement, node, index) { - parentElement.insertBefore(node, - parentElement.childNodes[index] || null) - } - - var DELETION = 1 - var INSERTION = 2 - var MOVE = 3 - - function handleKeysDiffer(data, existing, cached, parentElement) { - forKeys(data, function (key, i) { - existing[key = key.key] = existing[key] ? { - action: MOVE, - index: i, - from: existing[key].index, - element: cached.nodes[existing[key].index] || - $document.createElement("div") - } : {action: INSERTION, index: i} + var onerror = run(rejectCurrent) + try {then(run(resolveCurrent), onerror)} catch (e) {onerror(e)} + } + executeOnce(executor) +} +PromisePolyfill.prototype.then = function(onFulfilled, onRejection) { + var self = this, instance = self._instance + function handle(callback, list, next, state) { + list.push(function(value) { + if (typeof callback !== "function") next(value) + else try {resolveNext(callback(value))} catch (e) {if (rejectNext) rejectNext(e)} }) - - var actions = [] - for (var prop in existing) if (hasOwn.call(existing, prop)) { - actions.push(existing[prop]) - } - - var changes = actions.sort(sortChanges) - var newCached = new Array(cached.length) - - newCached.nodes = cached.nodes.slice() - - forEach(changes, function (change) { - var index = change.index - if (change.action === DELETION) { - clear(cached[index].nodes, cached[index]) - newCached.splice(index, 1) + if (typeof instance.retry === "function" && state === instance.state) instance.retry() + } + var resolveNext, rejectNext + var promise = new PromisePolyfill(function(resolve, reject) {resolveNext = resolve, rejectNext = reject}) + handle(onFulfilled, instance.resolvers, resolveNext, true), handle(onRejection, instance.rejectors, rejectNext, false) + return promise +} +PromisePolyfill.prototype.catch = function(onRejection) { + return this.then(null, onRejection) +} +PromisePolyfill.resolve = function(value) { + if (value instanceof PromisePolyfill) return value + return new PromisePolyfill(function(resolve) {resolve(value)}) +} +PromisePolyfill.reject = function(value) { + return new PromisePolyfill(function(resolve, reject) {reject(value)}) +} +PromisePolyfill.all = function(list) { + return new PromisePolyfill(function(resolve, reject) { + var total = list.length, count = 0, values = [] + if (list.length === 0) resolve([]) + else for (var i = 0; i < list.length; i++) { + (function(i) { + function consume(value) { + count++ + values[i] = value + if (count === total) resolve(values) + } + if (list[i] != null && (typeof list[i] === "object" || typeof list[i] === "function") && typeof list[i].then === "function") { + list[i].then(consume, reject) + } + else consume(list[i]) + })(i) + } + }) +} +PromisePolyfill.race = function(list) { + return new PromisePolyfill(function(resolve, reject) { + for (var i = 0; i < list.length; i++) { + list[i].then(resolve, reject) + } + }) +} +if (typeof window !== "undefined") { + if (typeof window.Promise === "undefined") window.Promise = PromisePolyfill + var PromisePolyfill = window.Promise +} else if (typeof global !== "undefined") { + if (typeof global.Promise === "undefined") global.Promise = PromisePolyfill + var PromisePolyfill = global.Promise +} else { +} +var buildQueryString = function(object) { + if (Object.prototype.toString.call(object) !== "[object Object]") return "" + var args = [] + for (var key0 in object) { + destructure(key0, object[key0]) + } + return args.join("&") + function destructure(key0, value) { + if (Array.isArray(value)) { + for (var i = 0; i < value.length; i++) { + destructure(key0 + "[" + i + "]", value[i]) + } + } + else if (Object.prototype.toString.call(value) === "[object Object]") { + for (var i in value) { + destructure(key0 + "[" + i + "]", value[i]) } - if (change.action === INSERTION) { - var dummy = $document.createElement("div") - dummy.key = data[index].attrs.key - insertNode(parentElement, dummy, index) - newCached.splice(index, 0, { - attrs: {key: data[index].attrs.key}, - nodes: [dummy] + } + else args.push(encodeURIComponent(key0) + (value != null && value !== "" ? "=" + encodeURIComponent(value) : "")) + } +} +var FILE_PROTOCOL_REGEX = new RegExp("^file://", "i") +var _8 = function($window, Promise) { + var callbackCount = 0 + var oncompletion + function setCompletionCallback(callback) {oncompletion = callback} + function finalizer() { + var count = 0 + function complete() {if (--count === 0 && typeof oncompletion === "function") oncompletion()} + return function finalize(promise0) { + var then0 = promise0.then + promise0.then = function() { + count++ + var next = then0.apply(promise0, arguments) + next.then(complete, function(e) { + complete() + if (count === 0) throw e }) - newCached.nodes[index] = dummy + return finalize(next) + } + return promise0 + } + } + function normalize(args, extra) { + if (typeof args === "string") { + var url = args + args = extra || {} + if (args.url == null) args.url = url + } + return args + } + function request(args, extra) { + var finalize = finalizer() + args = normalize(args, extra) + var promise0 = new Promise(function(resolve, reject) { + if (args.method == null) args.method = "GET" + args.method = args.method.toUpperCase() + var useBody = (args.method === "GET" || args.method === "TRACE") ? false : (typeof args.useBody === "boolean" ? args.useBody : true) + if (typeof args.serialize !== "function") args.serialize = typeof FormData !== "undefined" && args.data instanceof FormData ? function(value) {return value} : JSON.stringify + if (typeof args.deserialize !== "function") args.deserialize = deserialize + if (typeof args.extract !== "function") args.extract = extract + args.url = interpolate(args.url, args.data) + if (useBody) args.data = args.serialize(args.data) + else args.url = assemble(args.url, args.data) + var xhr = new $window.XMLHttpRequest(), + aborted = false, + _abort = xhr.abort + xhr.abort = function abort() { + aborted = true + _abort.call(xhr) + } + xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined) + if (args.serialize === JSON.stringify && useBody && !(args.headers && args.headers.hasOwnProperty("Content-Type"))) { + xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8") } - - if (change.action === MOVE) { - var changeElement = change.element - var maybeChanged = parentElement.childNodes[index] - if (maybeChanged !== changeElement && changeElement !== null) { - parentElement.insertBefore(changeElement, - maybeChanged || null) + if (args.deserialize === deserialize && !(args.headers && args.headers.hasOwnProperty("Accept"))) { + xhr.setRequestHeader("Accept", "application/json, text/*") + } + if (args.withCredentials) xhr.withCredentials = args.withCredentials + for (var key in args.headers) if ({}.hasOwnProperty.call(args.headers, key)) { + xhr.setRequestHeader(key, args.headers[key]) + } + if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr + xhr.onreadystatechange = function() { + // Don't throw errors on xhr.abort(). + if(aborted) return + if (xhr.readyState === 4) { + try { + var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args)) + if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304 || FILE_PROTOCOL_REGEX.test(args.url)) { + resolve(cast(args.type, response)) + } + else { + var error = new Error(xhr.responseText) + for (var key in response) error[key] = response[key] + reject(error) + } + } + catch (e) { + reject(e) + } } - newCached[index] = cached[change.from] - newCached.nodes[index] = changeElement } + if (useBody && (args.data != null)) xhr.send(args.data) + else xhr.send() }) - - return newCached - } - - function diffKeys(data, cached, existing, parentElement) { - var keysDiffer = data.length !== cached.length - - if (!keysDiffer) { - forKeys(data, function (attrs, i) { - var cachedCell = cached[i] - return keysDiffer = cachedCell && - cachedCell.attrs && - cachedCell.attrs.key !== attrs.key - }) - } - - if (keysDiffer) { - return handleKeysDiffer(data, existing, cached, parentElement) - } else { - return cached - } - } - - function diffArray(data, cached, nodes) { - // diff the array itself - - // update the list of DOM nodes by collecting the nodes from each item - forEach(data, function (_, i) { - if (cached[i] != null) nodes.push.apply(nodes, cached[i].nodes) - }) - // remove items from the end of the array if the new array is shorter - // than the old one. if errors ever happen here, the issue is most - // likely a bug in the construction of the `cached` data structure - // somewhere earlier in the program - forEach(cached.nodes, function (node, i) { - if (node.parentNode != null && nodes.indexOf(node) < 0) { - clear([node], [cached[i]]) + return args.background === true ? promise0 : finalize(promise0) + } + function jsonp(args, extra) { + var finalize = finalizer() + args = normalize(args, extra) + var promise0 = new Promise(function(resolve, reject) { + var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ + var script = $window.document.createElement("script") + $window[callbackName] = function(data) { + script.parentNode.removeChild(script) + resolve(cast(args.type, data)) + delete $window[callbackName] } + script.onerror = function() { + script.parentNode.removeChild(script) + reject(new Error("JSONP request failed")) + delete $window[callbackName] + } + if (args.data == null) args.data = {} + args.url = interpolate(args.url, args.data) + args.data[args.callbackKey || "callback"] = callbackName + script.src = assemble(args.url, args.data) + $window.document.documentElement.appendChild(script) }) - - if (data.length < cached.length) cached.length = data.length - cached.nodes = nodes - } - - function buildArrayKeys(data) { - var guid = 0 - forKeys(data, function () { - forEach(data, function (attrs) { - if ((attrs = attrs && attrs.attrs) && attrs.key == null) { - attrs.key = "__mithril__" + guid++ - } - }) - return 1 - }) - } - - function isDifferentEnough(data, cached, dataAttrKeys) { - if (data.tag !== cached.tag) return true - - if (dataAttrKeys.sort().join() !== - Object.keys(cached.attrs).sort().join()) { - return true - } - - if (data.attrs.id !== cached.attrs.id) { - return true + return args.background === true? promise0 : finalize(promise0) + } + function interpolate(url, data) { + if (data == null) return url + var tokens = url.match(/:[^\/]+/gi) || [] + for (var i = 0; i < tokens.length; i++) { + var key = tokens[i].slice(1) + if (data[key] != null) { + url = url.replace(tokens[i], data[key]) + } } - - if (data.attrs.key !== cached.attrs.key) { - return true + return url + } + function assemble(url, data) { + var querystring = buildQueryString(data) + if (querystring !== "") { + var prefix = url.indexOf("?") < 0 ? "?" : "&" + url += prefix + querystring } - - if (m.redraw.strategy() === "all") { - return !cached.configContext || cached.configContext.retain !== true + return url + } + function deserialize(data) { + try {return data !== "" ? JSON.parse(data) : null} + catch (e) {throw new Error(data)} + } + function extract(xhr) {return xhr.responseText} + function cast(type0, data) { + if (typeof type0 === "function") { + if (Array.isArray(data)) { + for (var i = 0; i < data.length; i++) { + data[i] = new type0(data[i]) + } + } + else return new type0(data) } - - if (m.redraw.strategy() === "diff") { - return cached.configContext && cached.configContext.retain === false + return data + } + return {request: request, jsonp: jsonp, setCompletionCallback: setCompletionCallback} +} +var requestService = _8(window, PromisePolyfill) +var coreRenderer = function($window) { + var $doc = $window.document + var $emptyFragment = $doc.createDocumentFragment() + var nameSpace = { + svg: "http://www.w3.org/2000/svg", + math: "http://www.w3.org/1998/Math/MathML" + } + var onevent + function setEventCallback(callback) {return onevent = callback} + function getNameSpace(vnode) { + return vnode.attrs && vnode.attrs.xmlns || nameSpace[vnode.tag] + } + //create + function createNodes(parent, vnodes, start, end, hooks, nextSibling, ns) { + for (var i = start; i < end; i++) { + var vnode = vnodes[i] + if (vnode != null) { + createNode(parent, vnode, hooks, ns, nextSibling) + } } - - return false } - - function maybeRecreateObject(data, cached, dataAttrKeys) { - // if an element is different enough from the one in cache, recreate it - if (isDifferentEnough(data, cached, dataAttrKeys)) { - if (cached.nodes.length) clear(cached.nodes) - - if (cached.configContext && - isFunction(cached.configContext.onunload)) { - cached.configContext.onunload() + function createNode(parent, vnode, hooks, ns, nextSibling) { + var tag = vnode.tag + if (typeof tag === "string") { + vnode.state = {} + if (vnode.attrs != null) initLifecycle(vnode.attrs, vnode, hooks) + switch (tag) { + case "#": return createText(parent, vnode, nextSibling) + case "<": return createHTML(parent, vnode, nextSibling) + case "[": return createFragment(parent, vnode, hooks, ns, nextSibling) + default: return createElement(parent, vnode, hooks, ns, nextSibling) + } + } + else return createComponent(parent, vnode, hooks, ns, nextSibling) + } + function createText(parent, vnode, nextSibling) { + vnode.dom = $doc.createTextNode(vnode.children) + insertNode(parent, vnode.dom, nextSibling) + return vnode.dom + } + function createHTML(parent, vnode, nextSibling) { + var match1 = vnode.children.match(/^\s*?<(\w+)/im) || [] + var parent1 = {caption: "table", thead: "table", tbody: "table", tfoot: "table", tr: "tbody", th: "tr", td: "tr", colgroup: "table", col: "colgroup"}[match1[1]] || "div" + var temp = $doc.createElement(parent1) + temp.innerHTML = vnode.children + vnode.dom = temp.firstChild + vnode.domSize = temp.childNodes.length + var fragment = $doc.createDocumentFragment() + var child + while (child = temp.firstChild) { + fragment.appendChild(child) + } + insertNode(parent, fragment, nextSibling) + return fragment + } + function createFragment(parent, vnode, hooks, ns, nextSibling) { + var fragment = $doc.createDocumentFragment() + if (vnode.children != null) { + var children = vnode.children + createNodes(fragment, children, 0, children.length, hooks, null, ns) + } + vnode.dom = fragment.firstChild + vnode.domSize = fragment.childNodes.length + insertNode(parent, fragment, nextSibling) + return fragment + } + function createElement(parent, vnode, hooks, ns, nextSibling) { + var tag = vnode.tag + var attrs2 = vnode.attrs + var is = attrs2 && attrs2.is + ns = getNameSpace(vnode) || ns + var element = ns ? + is ? $doc.createElementNS(ns, tag, {is: is}) : $doc.createElementNS(ns, tag) : + is ? $doc.createElement(tag, {is: is}) : $doc.createElement(tag) + vnode.dom = element + if (attrs2 != null) { + setAttrs(vnode, attrs2, ns) + } + insertNode(parent, element, nextSibling) + if (vnode.attrs != null && vnode.attrs.contenteditable != null) { + setContentEditable(vnode) + } + else { + if (vnode.text != null) { + if (vnode.text !== "") element.textContent = vnode.text + else vnode.children = [Vnode("#", undefined, undefined, vnode.text, undefined, undefined)] } - - if (cached.controllers) { - forEach(cached.controllers, function (controller) { - if (controller.onunload) controller.onunload({preventDefault: noop}); - }); + if (vnode.children != null) { + var children = vnode.children + createNodes(element, children, 0, children.length, hooks, null, ns) + setLateAttrs(vnode) } } + return element } - - function getObjectNamespace(data, namespace) { - if (data.attrs.xmlns) return data.attrs.xmlns - if (data.tag === "svg") return "http://www.w3.org/2000/svg" - if (data.tag === "math") return "http://www.w3.org/1998/Math/MathML" - return namespace - } - - var pendingRequests = 0 - m.startComputation = function () { pendingRequests++ } - m.endComputation = function () { - if (pendingRequests > 1) { - pendingRequests-- + function initComponent(vnode, hooks) { + var sentinel + if (typeof vnode.tag.view === "function") { + vnode.state = Object.create(vnode.tag) + sentinel = vnode.state.view + if (sentinel.$$reentrantLock$$ != null) return $emptyFragment + sentinel.$$reentrantLock$$ = true } else { - pendingRequests = 0 - m.redraw() - } - } - - function unloadCachedControllers(cached, views, controllers) { - if (controllers.length) { - cached.views = views - cached.controllers = controllers - forEach(controllers, function (controller) { - if (controller.onunload && controller.onunload.$old) { - controller.onunload = controller.onunload.$old + vnode.state = void 0 + sentinel = vnode.tag + if (sentinel.$$reentrantLock$$ != null) return $emptyFragment + sentinel.$$reentrantLock$$ = true + vnode.state = (vnode.tag.prototype != null && typeof vnode.tag.prototype.view === "function") ? new vnode.tag(vnode) : vnode.tag(vnode) + } + vnode._state = vnode.state + if (vnode.attrs != null) initLifecycle(vnode.attrs, vnode, hooks) + initLifecycle(vnode._state, vnode, hooks) + vnode.instance = Vnode.normalize(vnode._state.view.call(vnode.state, vnode)) + if (vnode.instance === vnode) throw Error("A view cannot return the vnode it received as argument") + sentinel.$$reentrantLock$$ = null + } + function createComponent(parent, vnode, hooks, ns, nextSibling) { + initComponent(vnode, hooks) + if (vnode.instance != null) { + var element = createNode(parent, vnode.instance, hooks, ns, nextSibling) + vnode.dom = vnode.instance.dom + vnode.domSize = vnode.dom != null ? vnode.instance.domSize : 0 + insertNode(parent, element, nextSibling) + return element + } + else { + vnode.domSize = 0 + return $emptyFragment + } + } + //update + function updateNodes(parent, old, vnodes, recycling, hooks, nextSibling, ns) { + if (old === vnodes || old == null && vnodes == null) return + else if (old == null) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, ns) + else if (vnodes == null) removeNodes(old, 0, old.length, vnodes) + else { + if (old.length === vnodes.length) { + var isUnkeyed = false + for (var i = 0; i < vnodes.length; i++) { + if (vnodes[i] != null && old[i] != null) { + isUnkeyed = vnodes[i].key == null && old[i].key == null + break + } } - - if (pendingRequests && controller.onunload) { - var onunload = controller.onunload - controller.onunload = noop - controller.onunload.$old = onunload + if (isUnkeyed) { + for (var i = 0; i < old.length; i++) { + if (old[i] === vnodes[i]) continue + else if (old[i] == null && vnodes[i] != null) createNode(parent, vnodes[i], hooks, ns, getNextSibling(old, i + 1, nextSibling)) + else if (vnodes[i] == null) removeNodes(old, i, i + 1, vnodes) + else updateNode(parent, old[i], vnodes[i], hooks, getNextSibling(old, i + 1, nextSibling), recycling, ns) + } + return } - }) + } + recycling = recycling || isRecyclable(old, vnodes) + if (recycling) { + var pool = old.pool + old = old.concat(old.pool) + } + var oldStart = 0, start = 0, oldEnd = old.length - 1, end = vnodes.length - 1, map + while (oldEnd >= oldStart && end >= start) { + var o = old[oldStart], v = vnodes[start] + if (o === v && !recycling) oldStart++, start++ + else if (o == null) oldStart++ + else if (v == null) start++ + else if (o.key === v.key) { + var shouldRecycle = (pool != null && oldStart >= old.length - pool.length) || ((pool == null) && recycling) + oldStart++, start++ + updateNode(parent, o, v, hooks, getNextSibling(old, oldStart, nextSibling), shouldRecycle, ns) + if (recycling && o.tag === v.tag) insertNode(parent, toFragment(o), nextSibling) + } + else { + var o = old[oldEnd] + if (o === v && !recycling) oldEnd--, start++ + else if (o == null) oldEnd-- + else if (v == null) start++ + else if (o.key === v.key) { + var shouldRecycle = (pool != null && oldEnd >= old.length - pool.length) || ((pool == null) && recycling) + updateNode(parent, o, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), shouldRecycle, ns) + if (recycling || start < end) insertNode(parent, toFragment(o), getNextSibling(old, oldStart, nextSibling)) + oldEnd--, start++ + } + else break + } + } + while (oldEnd >= oldStart && end >= start) { + var o = old[oldEnd], v = vnodes[end] + if (o === v && !recycling) oldEnd--, end-- + else if (o == null) oldEnd-- + else if (v == null) end-- + else if (o.key === v.key) { + var shouldRecycle = (pool != null && oldEnd >= old.length - pool.length) || ((pool == null) && recycling) + updateNode(parent, o, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), shouldRecycle, ns) + if (recycling && o.tag === v.tag) insertNode(parent, toFragment(o), nextSibling) + if (o.dom != null) nextSibling = o.dom + oldEnd--, end-- + } + else { + if (!map) map = getKeyMap(old, oldEnd) + if (v != null) { + var oldIndex = map[v.key] + if (oldIndex != null) { + var movable = old[oldIndex] + var shouldRecycle = (pool != null && oldIndex >= old.length - pool.length) || ((pool == null) && recycling) + updateNode(parent, movable, v, hooks, getNextSibling(old, oldEnd + 1, nextSibling), recycling, ns) + insertNode(parent, toFragment(movable), nextSibling) + old[oldIndex].skip = true + if (movable.dom != null) nextSibling = movable.dom + } + else { + var dom = createNode(parent, v, hooks, ns, nextSibling) + nextSibling = dom + } + } + end-- + } + if (end < start) break + } + createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, ns) + removeNodes(old, oldStart, oldEnd + 1, vnodes) + } + } + function updateNode(parent, old, vnode, hooks, nextSibling, recycling, ns) { + var oldTag = old.tag, tag = vnode.tag + if (oldTag === tag) { + vnode.state = old.state + vnode._state = old._state + vnode.events = old.events + if (!recycling && shouldNotUpdate(vnode, old)) return + if (typeof oldTag === "string") { + if (vnode.attrs != null) { + if (recycling) { + vnode.state = {} + initLifecycle(vnode.attrs, vnode, hooks) + } + else updateLifecycle(vnode.attrs, vnode, hooks) + } + switch (oldTag) { + case "#": updateText(old, vnode); break + case "<": updateHTML(parent, old, vnode, nextSibling); break + case "[": updateFragment(parent, old, vnode, recycling, hooks, nextSibling, ns); break + default: updateElement(old, vnode, recycling, hooks, ns) + } + } + else updateComponent(parent, old, vnode, hooks, nextSibling, recycling, ns) + } + else { + removeNode(old, null) + createNode(parent, vnode, hooks, ns, nextSibling) } } - - function scheduleConfigsToBeCalled(configs, data, node, isNew, cached) { - // schedule configs to be called. They are called after `build` finishes - // running - if (isFunction(data.attrs.config)) { - var context = cached.configContext = cached.configContext || {} - - // bind - configs.push(function () { - return data.attrs.config.call(data, node, !isNew, context, - cached) - }) + function updateText(old, vnode) { + if (old.children.toString() !== vnode.children.toString()) { + old.dom.nodeValue = vnode.children } + vnode.dom = old.dom } - - function buildUpdatedNode( - cached, - data, - editable, - hasKeys, - namespace, - views, - configs, - controllers - ) { - var node = cached.nodes[0] - - if (hasKeys) { - setAttributes(node, data.tag, data.attrs, cached.attrs, namespace) - } - - cached.children = build( - node, - data.tag, - undefined, - undefined, - data.children, - cached.children, - false, - 0, - data.attrs.contenteditable ? node : editable, - namespace, - configs - ) - - cached.nodes.intact = true - - if (controllers.length) { - cached.views = views - cached.controllers = controllers - } - - return node - } - - function handleNonexistentNodes(data, parentElement, index) { - var nodes - if (data.$trusted) { - nodes = injectHTML(parentElement, index, data) - } else { - nodes = [$document.createTextNode(data)] - if (!(parentElement.nodeName in voidElements)) { - insertNode(parentElement, nodes[0], index) - } + function updateHTML(parent, old, vnode, nextSibling) { + if (old.children !== vnode.children) { + toFragment(old) + createHTML(parent, vnode, nextSibling) } - - var cached - - if (typeof data === "string" || - typeof data === "number" || - typeof data === "boolean") { - cached = new data.constructor(data) - } else { - cached = data - } - - cached.nodes = nodes - return cached - } - - function reattachNodes( - data, - cached, - parentElement, - editable, - index, - parentTag - ) { - var nodes = cached.nodes - if (!editable || editable !== $document.activeElement) { - if (data.$trusted) { - clear(nodes, cached) - nodes = injectHTML(parentElement, index, data) - } else if (parentTag === "textarea") { - //