Merge remote-tracking branch 'origin/rewrite' into rewrite

This commit is contained in:
Leo Horie 2016-11-22 00:30:32 -05:00
commit 54ba4d945a
7 changed files with 369 additions and 52 deletions

View file

@ -26,6 +26,28 @@ m(Example)
---
### Passing data to components
Data can be passed to component instances through an `attrs` object as a parameter in the hyperscript function:
```javascript
m(Example, {name: "Floyd"})
```
`attrs` data can be accessed in the component's view or lifecycle methods via the `vnode`:
```javascript
var Example = {
view: function (vnode) {
return m("div", "Hello, " + vnode.attrs.name)
}
}
```
NOTE: Lifecycle methods can also be provided via attrs, so you should avoid using the lifecycle method names for your own callbacks as they will be invoked by Mithril. Use lifecycle methods in `attrs` only when you specifically wish to create lifecycle hooks.
---
### Lifecycle methods
Components can have the same [lifecycle methods](lifecycle-methods.md) as virtual DOM nodes: `oninit`, `oncreate`, `onupdate`, `onbeforeremove`, `onremove` and `onbeforeupdate`.
@ -69,6 +91,8 @@ m(ComponentWithHooks, {oninit: initialize})
Lifecycle methods in vnodes do not override component methods, nor vice versa. Component lifecycle methods are always run after the vnode's corresponding method.
Take care not to use lifecycle method names for your own callback function names in vnodes.
To learn more about lifecycle methods, [see the lifecycle methods page](lifecycle-methods.md).
---

289
docs/promise.md Normal file
View file

@ -0,0 +1,289 @@
# Promise(executor)
- [API](#api)
- [Static members](#static-members)
- [Promise.resolve](#promiseresolve)
- [Promise.reject](#promisereject)
- [Promise.all](#promiseall)
- [Promise.race](#promiserace)
- [Instance members](#static-members)
- [promise.then](#promisethen)
- [promise.catch](#promisecatch)
- [How it works](#how-it-works)
- [Promise chaining](#promise-chaining)
- [Promise absorption](#promise-absorption)
- [Error handling](#error-handling)
- [Shorthands](#shorthands)
- [Waiting for multiple promises](#waiting-for-multiple-promises)
---
### API
`promise = new Promise(executor)`
Argument | Type | Required | Description
----------- | ----------------------------- | -------- | ---
`executor` | `(Function, Function) -> any` | Yes | A function that determines how the promise will be resolved or rejected
**returns** | `Promise` | | Returns a promise
[How to read signatures](signatures.md)
---
##### executor
`executor(resolve, reject)`
Argument | Type | Required | Description
----------- | ----------------------------- | -------- | ---
`resolve` | `any -> any` | No | Call this function to resolve the promise
`reject` | `any -> any` | No | Call this function to reject the promise
**returns** | | | The return value is ignored
[How to read signatures](signatures.md)
---
#### Static members
##### Promise.resolve
`promise = Promise.resolve(value)`
Argument | Type | Required | Description
----------- | ----------------------------- | -------- | ---
`value` | `any` | No | A value to resolve to
**returns** | `Promise` | | A promise resolved to `value`
[How to read signatures](signatures.md)
---
##### Promise.reject
`promise = Promise.reject(value)`
Argument | Type | Required | Description
----------- | ----------------------------- | -------- | ---
`value` | `any` | No | A value to reject as
**returns** | `Promise` | | A rejected promise with `value` as its reason
[How to read signatures](signatures.md)
---
##### Promise.all
`promise = Promise.all(promises)`
Argument | Type | Required | Description
----------- | ----------------------------- | -------- | ---
`promises` | `Array<Promise|any>` | Yes | A list of promises to wait for. If an item is not a promise, it's equivalent to calling `Promise.resolve` on it
**returns** | `Promise` | | A promise that resolves only after all `promises` resolve, or rejects if any of them are rejected.
[How to read signatures](signatures.md)
---
##### Promise.race
`promise = Promise.race(promises)`
Argument | Type | Required | Description
----------- | ----------------------------- | -------- | ---
`promises` | `Array<Promise|any>` | Yes | A list of promises to wait for. If an item is not a promise, it's equivalent to calling `Promise.resolve` on it
**returns** | `Promise` | | A promise that resolves as soon as one of the `promises` is resolved or rejected.
[How to read signatures](signatures.md)
---
#### Instance members
##### promise.then
`nextPromise = promise.then(onFulfilled, onRejected)`
Argument | Type | Required | Description
------------- | ----------------------- | -------- | ---
`onFulfilled` | `any -> (any|Promise)` | No | A function that is called if the promise is resolved. The first parameter of this function is the value that this promise was resolved with. If the return value of this function is not a Promise, it is used as the value for resolving `nextPromise`. If the returned value is a Promise, the value of `nextPromise` depends on the inner Promise's status. If this function throws, `nextPromise` is rejected with the error as its reason. If `onFulfilled` is `null`, it's ignored
`onRejected` | `any -> (any|Promise)` | No | A function that is called when the promise is rejected. The first parameter of this function is a value that represents the reason why the promise was rejected. If the return value of this function is not a Promise, it is used as the value for resolving `nextPromise`. If the returned value is a Promise, then value of `nextPromise` depends on the inner Promise's status. If this function throws, `nextPromise` is rejected with the error as its reason. If `onRejected` is `null`, it's ignored
**returns** | `Promise` | | A promise whose value depends on the status of the current promise
[How to read signatures](signatures.md)
---
##### promise.catch
`nextPromise = promise.catch(onRejected)`
Argument | Type | Required | Description
------------- | ----------------------- | -------- | ---
`onRejected` | `any -> (any|Promise)` | No | A function that is called when the promise is rejected. The first parameter of this function is a value that represents the reason why the promise was rejected. If the return value of this function is not a Promise, it is used as the value for resolving `nextPromise`. If the returned value is a Promise, then value of `nextPromise` depends on the inner Promise's status. If this function throws, `nextPromise` is rejected with the error as its reason. If `onRejected` is `null`, it's ignored
**returns** | `Promise` | | A promise whose value depends on the status of the current promise
[How to read signatures](signatures.md)
---
### How it works
A Promise is an object that represents a value which may be available in the future
```javascript
// this promise resolves after one second
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("hello")
}, 1000)
})
promise.then(function(value) {
// logs "hello" after one second
console.log(value)
})
```
Promises are useful for working with [asynchronous](https://en.wikipedia.org/wiki/Asynchrony_(computer_programming)) APIs, such as [`m.request`](request.md)
Asynchronous APIs are those which typically take a long time to run, and therefore would take too long to return a value using the `return` statement of a function. Instead, they do their work in the background, allowing other Javascript code to run in the meantime. When they are done, they call a function with their results.
The `m.request` function takes time to run because it makes an HTTP request to a remote server and has to wait for a response, which may take several milliseconds due to network latency.
---
### Promise chaining
Promises can be chained. Returning a value from a `then` callback makes it available as the argument to the next `then` callback. This allows refactoring code into smaller functions
```javascript
function getUsers() {return m.request("/api/v1/users")}
// AVOID: hard to test god functions
getUsers().then(function(users) {
var firstTen = users.slice(0, 9)
var firstTenNames = firstTen.map(function(user) {return user.firstName + " " + user.lastName})
alert(firstTenNames)
})
// PREFER: easy to test small functions
function getFirstTen(items) {return items.slice(0, 9)}
function getUserName(user) {return user.firstName + " " + user.lastName}
function getUserNames(users) {return users.map(getUserName)}
getUsers()
.then(getFirstTen)
.then(getUserNames)
.then(alert)
```
In the refactored code, `getUsers()` returns a promise, and we chain three callbacks. When `getUsers()` resolves, the `getFirstTen` function is called with a list of users as its first argument. This function returns a list of ten items. `getUserNames` returns a list of names for the 10 items that were passed as the argument to it. Finally, the list of names is alerted.
In the original code above, it's very difficult to test the god function since you must make an HTTP request to run the code, and there's an `alert()` call at the end of the function
In the refactored version, it's trivial to test whether `getFirstTen` has any off-by-one errors, or whether we forgot to add a space between the first and last names in `getUserName`.
---
### Promise absorption
Promises absorb other promises. Basically, this means you can never receive a Promise as an argument to `onFulfilled` or `onRejected` callbacks for `then` and `catch` methods. This feature allows us to flatten nested promises to make code more manageable.
```Javascript
function searchUsers(q) {return m.request("/api/v1/users/search", {data: {q: q}})}
function getUserProjects() {return m.request("/api/v1/user/" + id + "/projects")}
// AVOID: pyramid of doom
searchUsers("John").then(function(users) {
getUserProjects(users[0].id).then(function(projects) {
var titles = projects.map(function(project) {return project.title})
alert(titles)
})
})
// PREFER: flat code flow
function getFirstId(items) {return items[0].id}
function getProjectTitles(projects) {return projects.map(getProjectTitle)}
function getProjectTitle(project) {return project.title}
searchUsers("John")
.then(getFirstId)
.then(getUserProjects)
.then(getProjectTitles)
.then(alert)
```
In the refactored code, `getFirstId` returns an id, which is passed as the first argument to `getUserProjects`. That, in turn, returns a promise that resolves to a list of projects. This promise is absorbed, so the first argument to `getProjectTitles` is not a promise, but the list of projects. `getProjectTitles` returns a list of titles, and that list is finally alerted.
---
### Error handling
Promises can propagate errors to appropriate handlers.
```javascript
searchUsers("John")
.then(getFirstId)
.then(getUserProjects)
.then(getProjectTitles)
.then(alert)
.catch(function(e) {
console.error(e)
})
```
Here's the previous example with error handling. The `searchUsers` function could fail if the network was offline, resulting in an error. In that case, none of the `.then` callbacks would be triggered, and the `.catch` callback would log the error to console.
If the request in `getUserProjects` failed, then similarly, `getProjectTitles` and `alert` would not be called. Again, the `.catch` callback would log the error.
The error handler would also catch a null reference exception if `searchUsers` returned no results, and `getFirstId` attempted to access the `id` property of a non-existent array item.
Thanks to these error propagation semantics, it's easy to keep each function small and testable without sprinkling `try`/`catch` blocks everywhere.
---
### Shorthands
Sometimes, you already have a value, but want to wrap it in a Promise. It's for this purpose that `Promise.resolve` and `Promise.reject` exist.
```javascript
// suppose this list came from localStorage
var users = [{id: 1, firstName: "John", lastName: "Doe"}]
// in that case, `users` may or may not exist depending on whether there was data in localStorage
var promise = users ? Promise.resolve(users) : getUsers()
promise
.then(getFirstTen)
.then(getUserNames)
.then(alert)
```
---
### Waiting for multiple promises
In some occasions, you may need to make HTTP requests in parallel, and run code after all requests complete. This can be accomplished by `Promise.all`
```javascript
Promise.all([
searchUsers("John"),
searchUsers("Mary"),
])
.then(function(data) {
// data[0] is an array of users whose names are John
// data[1] is an array of users whose names are Mary
//the returned value is equivalent to [
// getUserNames(data[0]),
// getUserNames(data[1]),
//]
return data.map(getUserNames)
})
.then(alert)
```
In the example above, there are two user searches happening in parallel. Once they both complete, we take the names of all the users and alert them.
This example also illustrates another benefit of smaller functions: we reused the `getUserNames` function we had created above.

View file

@ -32,7 +32,7 @@ Argument | Type | Required | Descript
`options.type` | `any = Function(any)` | No | A constructor to be applied to each object in the response. Defaults to the [identity function](https://en.wikipedia.org/wiki/Identity_function).
`options.serialize` | `string = Function(any)` | No | A serialization method to be applied to `data`. Defaults to `JSON.stringify`, or if `options.data` is an instance of [`FormData`](https://developer.mozilla.org/en/docs/Web/API/FormData), defaults to the [identity function](https://en.wikipedia.org/wiki/Identity_function) (i.e. `function(value) {return value}`).
`options.deserialize` | `any = Function(string)` | No | A deserialization method to be applied to the response. Defaults to a small wrapper around `JSON.parse` that returns `null` for empty responses.
`options.extract` | `string = Function(xhr, options)` | No | A hook to specify how the XMLHttpRequest response should be read. Useful for reading response headers and cookies. Defaults to a function that returns `xhr.responseText`. If defined, the `xhr` parameter is the XMLHttpRequest instance used for the request, and `options` is the object that was passed to the `m.request` call. If a custom `extract` callback is set, `options.deserialize` is ignored.
`options.extract` | `string = Function(xhr, options)` | No | A hook to specify how the XMLHttpRequest response should be read. Useful for reading response headers and cookies. Defaults to a function that returns `xhr.responseText`. If defined, the `xhr` parameter is the XMLHttpRequest instance used for the request, and `options` is the object that was passed to the `m.request` call. If a custom `extract` callback is set, `options.deserialize` is ignored and the string returned from the extract callback will not be parsed as JSON.
`options.useBody` | `Boolean` | No | Force the use of the HTTP body section for `data` in `GET` requests when set to `true`, or the use of querystring for other HTTP methods when set to `false`. Defaults to `false` for `GET` requests and `true` for other methods.
**returns** | `Promise` | | A promise that resolves to the response data, after it has been piped through the `extract`, `deserialize` and `type` methods
@ -186,7 +186,7 @@ Sometimes, it is desirable to abort a request. For example, in an autocompleter/
var searchXHR = null
function search() {
abortPreviousSearch()
m.request({
method: "GET",
url: "/api/v1/users",
@ -223,7 +223,7 @@ Next, you need to create a [`FormData`](https://developer.mozilla.org/en/docs/We
```javascript
function upload(e) {
var file = e.target.files[0]
var data = new FormData()
data.append("myfile", file)
}
@ -234,10 +234,10 @@ Next, you need to call `m.request` and set `options.method` to an HTTP method th
```javascript
function upload(e) {
var file = e.target.files[0]
var data = new FormData()
data.append("myfile", file)
m.request({
method: "POST",
url: "/api/v1/upload",
@ -261,12 +261,12 @@ m.render(document.body, [
function upload(e) {
var files = e.target.files
var data = new FormData()
for (var i = 0; i < files.length; i++) {
data.append("file" + i, file)
}
m.request({
method: "POST",
url: "/api/v1/upload",
@ -297,10 +297,10 @@ m.mount(document.body, {
function upload(e) {
var file = e.target.files[0]
var data = new FormData()
data.append("myfile", file)
m.request({
method: "POST",
url: "/api/v1/upload",
@ -308,7 +308,7 @@ function upload(e) {
config: function(xhr) {
xhr.addEventListener("progress", function(e) {
progress = e.loaded / e.total
m.redraw() // tell Mithril that data changed and a re-render is needed
})
}

View file

@ -757,6 +757,8 @@ var _13 = function($window) {
else if (key1 in element && !isAttribute(key1) && ns === undefined) {
//setting input[value] to same value by typing on focused element moves cursor to end in Chrome
if (vnode.tag === "input" && key1 === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return
//setting option[value] to same value while having select open blinks select dropdown in Chrome
if (vnode.tag === "option" && key1 === "value" && vnode.dom.value === value) return
element[key1] = value
}
else {

80
mithril.min.js vendored
View file

@ -1,40 +1,40 @@
new function(){function m(a,b,k,d,l,h){return{tag:a,key:b,attrs:k,children:d,text:l,dom:h,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function t(a){if(null==a||"string"!==typeof a&&null==a.view)throw Error("The selector must be either a string or a component.");if("string"===typeof a&&void 0===G[a]){for(var b,k,d=[],l={};b=N.exec(a);){var h=b[1],u=b[2];""===h&&""!==u?k=u:"#"===h?l.id=u:"."===h?d.push(u):"["===b[3][0]&&((h=b[6])&&(h=h.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")),
l[b[4]]=h||!0)}0<d.length&&(l.className=d.join(" "));G[a]=function(a,b){var d=!1,h,f,w=a.className||a["class"],p;for(p in l)a[p]=l[p];void 0!==w&&(void 0!==a["class"]&&(a["class"]=void 0,a.className=w),void 0!==l.className&&(a.className=l.className+" "+w));for(p in a)if("key"!==p){d=!0;break}b instanceof Array&&1==b.length&&null!=b[0]&&"#"===b[0].tag?f=b[0].children:h=b;return m(k||"div",a.key,d?a:void 0,h,f,void 0)}}var q;null!=arguments[1]&&("object"!==typeof arguments[1]||void 0!==arguments[1].tag||
arguments[1]instanceof Array)?d=1:(q=arguments[1],d=2);if(arguments.length===d+1)b=arguments[d]instanceof Array?arguments[d]:[arguments[d]];else for(b=[];d<arguments.length;d++)b.push(arguments[d]);return"string"===typeof a?G[a](q||{},m.normalizeChildren(b)):m(a,q&&q.key,q||{},m.normalizeChildren(b),void 0,void 0)}m.normalize=function(a){return a instanceof Array?m("[",void 0,void 0,m.normalizeChildren(a),void 0,void 0):null!=a&&"object"!==typeof a?m("#",void 0,void 0,a,void 0,void 0):a};m.normalizeChildren=
function(a){for(var b=0;b<a.length;b++)a[b]=m.normalize(a[b]);return a};var N=/(?:(^|#|\.)([^#\.\[\]]+))|(\[(.+?)(?:\s*=\s*("|'|)((?:\\["'\]]|.)*?)\5)?\])/g,G={};t.trust=function(a){null==a&&(a="");return m("<",void 0,void 0,a,void 0,void 0)};t.fragment=function(a,b){return m("[",a.key,a,m.normalizeChildren(b),void 0,void 0)};var x=function(a){function b(a,b){return function w(p){var n;try{if(!b||null==p||"object"!==typeof p&&"function"!==typeof p||"function"!==typeof(n=p.then))m(function(){b||0!==
a.length||console.error("Possible unhandled promise rejection:",p);for(var d=0;d<a.length;d++)a[d](p);l.length=0;h.length=0;r.state=b;r.retry=function(){w(p)}});else{if(p===d)throw new TypeError("Promise can't be resolved w/ itself");k(n.bind(p))}}catch(E){q(E)}}}function k(a){function b(a){return function(b){0<f++||a(b)}}var f=0,d=b(q);try{a(b(u),d)}catch(p){d(p)}}if(!(this instanceof x))throw Error("Promise must be called with `new`");if("function"!==typeof a)throw new TypeError("executor must be a function");
var d=this,l=[],h=[],u=b(l,!0),q=b(h,!1),r=d._instance={resolvers:l,rejectors:h},m="function"===typeof setImmediate?setImmediate:setTimeout;k(a)};x.prototype.then=function(a,b){function k(a,b,k,n){b.push(function(b){if("function"!==typeof a)k(b);else try{l(a(b))}catch(f){h&&h(f)}});"function"===typeof d.retry&&n===d.state&&d.retry()}var d=this._instance,l,h,u=new x(function(a,b){l=a;h=b});k(a,d.resolvers,l,!0);k(b,d.rejectors,h,!1);return u};x.prototype["catch"]=function(a){return this.then(null,
a)};x.resolve=function(a){return a instanceof x?a:new x(function(b){b(a)})};x.reject=function(a){return new x(function(b,k){k(a)})};x.all=function(a){return new x(function(b,k){var d=a.length,l=0,h=[];if(0===a.length)b([]);else for(var u=0;u<a.length;u++)(function(u){function r(a){l++;h[u]=a;l===d&&b(h)}null==a[u]||"object"!==typeof a[u]&&"function"!==typeof a[u]||"function"!==typeof a[u].then?r(a[u]):a[u].then(r,k)})(u)})};x.race=function(a){return new x(function(b,k){for(var d=0;d<a.length;d++)a[d].then(b,
k)})};"undefined"===typeof Promise&&("undefined"!==typeof window?window.Promise=x:"undefined"!==typeof global&&(global.Promise=x));var C=function(a){function b(a,d){if(d instanceof Array)for(var h=0;h<d.length;h++)b(a+"["+h+"]",d[h]);else if("[object Object]"===Object.prototype.toString.call(d))for(h in d)b(a+"["+h+"]",d[h]);else k.push(encodeURIComponent(a)+(null!=d&&""!==d?"="+encodeURIComponent(d):""))}if("[object Object]"!==Object.prototype.toString.call(a))return"";var k=[],d;for(d in a)b(d,
a[d]);return k.join("&")},H=function(a,b){function k(){0===--n&&"function"===typeof v&&v()}function d(a){var f=a.then;a.then=function(){n++;var b=f.apply(a,arguments);b.then(k,function(a){k();throw a;});return d(b)};return a}function l(a,b){if(null==b)return a;for(var f=a.match(/:[^\/]+/gi)||[],d=0;d<f.length;d++){var h=f[d].slice(1);null!=b[h]&&(a=a.replace(f[d],b[h]),delete b[h])}return a}function h(a,b){var f=C(b);if(""!==f){var d=0>a.indexOf("?")?"?":"&";a+=d+f}return a}function u(a){try{return""!==
a?JSON.parse(a):null}catch(w){throw Error(a);}}function q(a){return a.responseText}function r(a,b){if("function"===typeof a)if(b instanceof Array)for(var d=0;d<b.length;d++)b[d]=new a(b[d]);else return new a(b);return b}var m=0,n=0,v;return{request:function(f,k){return d(new b(function(b,d){if("string"===typeof f){var n=f;f=k||{};null==f.url&&(f.url=n)}null==f.method&&(f.method="GET");f.method=f.method.toUpperCase();n="boolean"===typeof f.useBody?f.useBody:"GET"!==f.method&&"TRACE"!==f.method;"function"!==
typeof f.serialize&&(f.serialize="undefined"!==typeof FormData&&f.data instanceof FormData?function(a){return a}:JSON.stringify);"function"!==typeof f.deserialize&&(f.deserialize=u);"function"!==typeof f.extract&&(f.extract=q);f.url=l(f.url,f.data);n?f.data=f.serialize(f.data):f.url=h(f.url,f.data);var p=new a.XMLHttpRequest;p.open(f.method,f.url,"boolean"===typeof f.async?f.async:!0,"string"===typeof f.user?f.user:void 0,"string"===typeof f.password?f.password:void 0);f.serialize===JSON.stringify&&
n&&p.setRequestHeader("Content-Type","application/json; charset=utf-8");f.deserialize===u&&p.setRequestHeader("Accept","application/json, text/*");"function"===typeof f.config&&(p=f.config(p,f)||p);p.onreadystatechange=function(){if(4===p.readyState)try{var a=f.extract!==q?f.extract(p,f):f.deserialize(f.extract(p,f));if(200<=p.status&&300>p.status||304===p.status)b(r(f.type,a));else{var h=Error(p.responseText),k;for(k in a)h[k]=a[k];d(h)}}catch(F){d(F)}};n&&null!=f.data?p.send(f.data):p.send()}))},
jsonp:function(f){return d(new b(function(b,d){var p=f.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+m++,k=a.document.createElement("script");a[p]=function(d){k.parentNode.removeChild(k);b(r(f.type,d));delete a[p]};k.onerror=function(){k.parentNode.removeChild(k);d(Error("JSONP request failed"));delete a[p]};null==f.data&&(f.data={});f.url=l(f.url,f.data);f.data[f.callbackKey||"callback"]=p;k.src=h(f.url,f.data);a.document.documentElement.appendChild(k)}))},setCompletionCallback:function(a){v=
a}}}(window,"undefined"!==typeof Promise?Promise:x),I=function(){var a=[];return{subscribe:a.push.bind(a),unsubscribe:function(b){b=a.indexOf(b);-1<b&&a.splice(b,1)},publish:function(){for(var b=0;b<a.length;b++)a[b].apply(this,arguments)}}}();H.setCompletionCallback(I.publish);var M=function(a){function b(c,g,a,b,d,f,h){for(;a<b;a++){var e=g[a];null!=e&&r(c,k(e,d,h),f)}}function k(c,g,a){var e=c.tag;null!=c.attrs&&t(c.attrs,c,g);if("string"===typeof e)switch(e){case "#":return c.dom=A.createTextNode(c.children);
case "<":return d(c);case "[":var f=A.createDocumentFragment();null!=c.children&&(e=c.children,b(f,e,0,e.length,g,null,a));c.dom=f.firstChild;c.domSize=f.childNodes.length;return f;default:var h=c.tag;switch(c.tag){case "svg":a="http://www.w3.org/2000/svg";break;case "math":a="http://www.w3.org/1998/Math/MathML"}var n=(e=c.attrs)&&e.is,h=a?n?A.createElementNS(a,h,{is:n}):A.createElementNS(a,h):n?A.createElement(h,{is:n}):A.createElement(h);c.dom=h;if(null!=e)for(f in n=a,e)p(c,f,null,e[f],n);null!=
c.attrs&&null!=c.attrs.contenteditable?x(c):(null!=c.text&&(""!==c.text?h.textContent=c.text:c.children=[m("#",void 0,void 0,c.text,void 0,void 0)]),null!=c.children&&(f=c.children,b(h,f,0,f.length,g,null,a),g=c.attrs,"select"===c.tag&&null!=g&&("value"in g&&p(c,"value",null,g.value,void 0),"selectedIndex"in g&&p(c,"selectedIndex",null,g.selectedIndex,void 0))));return h}else{c.state||(c.state={});O(c.state,c.tag);f=c.tag.view;if(null!=f.reentrantLock)c=F;else if(f.reentrantLock=!0,t(c.tag,c,g),c.instance=
m.normalize(f.call(c.state,c)),f.reentrantLock=null,null!=c.instance){if(c.instance===c)throw Error("A view cannot return the vnode it received as arguments");g=k(c.instance,g,a);c.dom=c.instance.dom;c.domSize=null!=c.dom?c.instance.domSize:0;c=g}else c.domSize=0,c=F;return c}}function d(c){var g={caption:"table",thead:"table",tbody:"table",tfoot:"table",tr:"tbody",th:"tr",td:"tr",colgroup:"table",col:"colgroup"}[(c.children.match(/^\s*?<(\w+)/im)||[])[1]]||"div",g=A.createElement(g);g.innerHTML=
c.children;c.dom=g.firstChild;c.domSize=g.childNodes.length;c=A.createDocumentFragment();for(var a;a=g.firstChild;)c.appendChild(a);return c}function l(c,a,e,f,d,p){if(a!==e&&(null!=a||null!=e))if(null==a)b(c,e,0,e.length,f,d,void 0);else if(null==e)n(a,0,a.length,e);else{var g;a:{if(null!=a.pool&&Math.abs(a.pool.length-e.length)<=Math.abs(a.length-e.length)&&(g=e[0]&&e[0].children&&e[0].children.length||0,Math.abs((a.pool[0]&&a.pool[0].children&&a.pool[0].children.length||0)-g)<=Math.abs((a[0]&&
a[0].children&&a[0].children.length||0)-g))){g=!0;break a}g=!1}g&&(a=a.concat(a.pool));if(a.length===e.length&&null!=e[0]&&null==e[0].key)for(var l=0;l<a.length;l++)a[l]===e[l]||null==a[l]&&null==e[l]||(null==a[l]?r(c,k(e[l],f,p),q(a,l+1,d)):null==e[l]?n(a,l,l+1,e):h(c,a[l],e[l],f,q(a,l+1,d),g,p),g&&a[l].tag===e[l].tag&&r(c,u(a[l]),q(a,l+1,d)));else{for(var z=l=0,v=a.length-1,B=e.length-1,w;v>=l&&B>=z;){var y=a[l],m=e[z];if(y!==m||g)if(null!=y&&null!=m&&y.key===m.key)l++,z++,h(c,y,m,f,q(a,l,d),g,
p),g&&y.tag===m.tag&&r(c,u(y),d);else if(y=a[v],y!==m||g)if(null!=y&&null!=m&&y.key===m.key)h(c,y,m,f,q(a,v+1,d),g,p),(g||z<B)&&r(c,u(y),q(a,l,d)),v--,z++;else break;else v--,z++;else l++,z++}for(;v>=l&&B>=z;){y=a[v];m=e[B];if(y!==m||g)if(null!=y&&null!=m&&y.key===m.key)h(c,y,m,f,q(a,v+1,d),g,p),g&&y.tag===m.tag&&r(c,u(y),d),null!=y.dom&&(d=y.dom),v--;else{if(!w){w=a;var y=v,D={},t;for(t=0;t<y;t++){var x=w[t];null!=x&&(x=x.key,null!=x&&(D[x]=t))}w=D}null!=m&&(y=w[m.key],null!=y?(D=a[y],h(c,D,m,f,
q(a,v+1,d),g,p),r(c,u(D),d),a[y].skip=!0,null!=D.dom&&(d=D.dom)):(m=k(m,f,void 0),r(c,m,d),d=m))}else v--;B--;if(B<z)break}b(c,e,z,B+1,f,d,p);n(a,l,v+1,e)}}}function h(a,g,e,b,n,v,w){var c=g.tag;if(c===e.tag){e.state=g.state;e.events=g.events;var z;var B;null!=e.attrs&&"function"===typeof e.attrs.onbeforeupdate&&(z=e.attrs.onbeforeupdate.call(e.state,e,g));"string"!==typeof e.tag&&"function"===typeof e.tag.onbeforeupdate&&(B=e.tag.onbeforeupdate.call(e.state,e,g));void 0===z&&void 0===B||z||B?z=!1:
(e.dom=g.dom,e.domSize=g.domSize,e.instance=g.instance,z=!0);if(!z)if(null!=e.attrs&&L(e.attrs,e,b,v),"string"===typeof c)switch(c){case "#":g.children.toString()!==e.children.toString()&&(g.dom.nodeValue=e.children);e.dom=g.dom;break;case "<":g.children!==e.children?(u(g),r(a,d(e),n)):(e.dom=g.dom,e.domSize=g.domSize);break;case "[":l(a,g.children,e.children,b,n,w);g=0;b=e.children;e.dom=null;if(null!=b){for(var q=0;q<b.length;q++)a=b[q],null!=a&&null!=a.dom&&(null==e.dom&&(e.dom=a.dom),g+=a.domSize||
1);1!==g&&(e.domSize=g)}break;default:a=w;n=e.dom=g.dom;switch(e.tag){case "svg":a="http://www.w3.org/2000/svg";break;case "math":a="http://www.w3.org/1998/Math/MathML"}"textarea"===e.tag&&(null==e.attrs&&(e.attrs={}),null!=e.text&&(e.attrs.value=e.text,e.text=void 0));v=g.attrs;w=e.attrs;c=a;if(null!=w)for(q in w)p(e,q,v&&v[q],w[q],c);if(null!=v)for(q in v)null!=w&&q in w||("className"===q&&(q="class"),"o"!==q[0]||"n"!==q[1]||K(q)?"key"!==q&&e.dom.removeAttribute(q):E(e,q,void 0));null!=e.attrs&&
null!=e.attrs.contenteditable?x(e):null!=g.text&&null!=e.text&&""!==e.text?g.text.toString()!==e.text.toString()&&(g.dom.firstChild.nodeValue=e.text):(null!=g.text&&(g.children=[m("#",void 0,void 0,g.text,void 0,g.dom.firstChild)]),null!=e.text&&(e.children=[m("#",void 0,void 0,e.text,void 0,void 0)]),l(n,g.children,e.children,b,null,a))}else e.instance=m.normalize(e.tag.view.call(e.state,e)),L(e.tag,e,b,v),null!=e.instance?(null==g.instance?r(a,k(e.instance,b,w),n):h(a,g.instance,e.instance,b,n,
v,w),e.dom=e.instance.dom,e.domSize=e.instance.domSize):null!=g.instance?(f(g.instance,null),e.dom=void 0,e.domSize=0):(e.dom=g.dom,e.domSize=g.domSize)}else f(g,null),r(a,k(e,b,w),n)}function u(a){var c=a.domSize;if(null!=c||null==a.dom){var e=A.createDocumentFragment();if(0<c){for(a=a.dom;--c;)e.appendChild(a.nextSibling);e.insertBefore(a,e.firstChild)}return e}return a.dom}function q(a,g,e){for(;g<a.length;g++)if(null!=a[g]&&null!=a[g].dom)return a[g].dom;return e}function r(a,g,e){e&&e.parentNode?
a.insertBefore(g,e):a.appendChild(g)}function x(a){var c=a.children;if(null!=c&&1===c.length&&"<"===c[0].tag)c=c[0].children,a.dom.innerHTML!==c&&(a.dom.innerHTML=c);else if(null!=c||null!=a.text)throw Error("Child node of a contenteditable must be trusted");}function n(a,g,e,b){for(;g<e;g++){var c=a[g];null!=c&&(c.skip?c.skip=!1:f(c,b))}}function v(a){var c=!1;return function(){c||(c=!0,a())}}function f(a,g){function c(){if(++d===b&&(w(a),a.dom)){var c=a.domSize||1;if(1<c)for(var e=a.dom;--c;){var f=
e.nextSibling,h=f.parentNode;null!=h&&h.removeChild(f)}c=a.dom;e=c.parentNode;null!=e&&e.removeChild(c);if(c=null!=g&&null==a.domSize)c=a.attrs,c=!(null!=c&&(c.oncreate||c.onupdate||c.onbeforeremove||c.onremove));c&&"string"===typeof a.tag&&(g.pool?g.pool.push(a):g.pool=[a])}}var b=1,d=0;a.attrs&&a.attrs.onbeforeremove&&(b++,a.attrs.onbeforeremove.call(a.state,a,v(c)));"string"!==typeof a.tag&&a.tag.onbeforeremove&&(b++,a.tag.onbeforeremove.call(a.state,a,v(c)));c()}function w(a){a.attrs&&a.attrs.onremove&&
a.attrs.onremove.call(a.state,a);"string"!==typeof a.tag&&a.tag.onremove&&a.tag.onremove.call(a.state,a);if(null!=a.instance)w(a.instance);else if(a=a.children,a instanceof Array)for(var c=0;c<a.length;c++){var e=a[c];null!=e&&w(e)}}function p(a,g,e,b,d){var c=a.dom;if("key"!==g&&(e!==b||"value"===g||"checked"===g||"selectedIndex"===g||"selected"===g&&a.dom===A.activeElement||"object"===typeof b)&&"undefined"!==typeof b&&!K(g)){var f=g.indexOf(":");if(-1<f&&"xlink"===g.substr(0,f))c.setAttributeNS("http://www.w3.org/1999/xlink",
g.slice(f+1),b);else if("o"===g[0]&&"n"===g[1]&&"function"===typeof b)E(a,g,b);else if("style"===g)if(a=e,a===b&&(c.style.cssText="",a=null),null==b)c.style.cssText="";else if("string"===typeof b)c.style.cssText=b;else{"string"===typeof a&&(c.style.cssText="");for(var h in b)c.style[h]=b[h];if(null!=a&&"string"!==typeof a)for(h in a)h in b||(c.style[h]="")}else if(g in c&&"href"!==g&&"list"!==g&&"form"!==g&&"width"!==g&&"height"!==g&&void 0===d){if("input"!==a.tag||"value"!==g||a.dom.value!==b||a.dom!==
A.activeElement)c[g]=b}else"boolean"===typeof b?b?c.setAttribute(g,""):c.removeAttribute(g):c.setAttribute("className"===g?"class":g,b)}}function K(a){return"oninit"===a||"oncreate"===a||"onupdate"===a||"onremove"===a||"onbeforeremove"===a||"onbeforeupdate"===a}function E(a,b,e){var c=a.dom,g=function(a){var b=e.call(c,a);"function"===typeof C&&C.call(c,a);return b};if(b in c)c[b]="function"===typeof e?g:null;else{var d=b.slice(2);void 0===a.events&&(a.events={});null!=a.events[b]&&c.removeEventListener(d,
a.events[b],!1);"function"===typeof e&&(a.events[b]=g,c.addEventListener(d,a.events[b],!1))}}function t(a,b,e){"function"===typeof a.oninit&&a.oninit.call(b.state,b);"function"===typeof a.oncreate&&e.push(a.oncreate.bind(b.state,b))}function L(a,b,e,d){d?t(a,b,e):"function"===typeof a.onupdate&&e.push(a.onupdate.bind(b.state,b))}function O(a,b){Object.keys(b).forEach(function(c){a[c]=b[c]})}var A=a.document,F=A.createDocumentFragment(),C;return{render:function(a,b){if(!a)throw Error("Ensure the DOM element being passed to m.route/m.mount/m.render is not undefined.");
var c=[],d=A.activeElement;null==a.vnodes&&(a.textContent="");b instanceof Array||(b=[b]);l(a,a.vnodes,m.normalizeChildren(b),c,null,void 0);a.vnodes=b;for(var f=0;f<c.length;f++)c[f]();A.activeElement!==d&&d.focus()},setEventCallback:function(a){return C=a}}}(window),P=function(a){var b=0,k=null,d="function"===typeof requestAnimationFrame?requestAnimationFrame:setTimeout;return function(l){var h=Date.now();!0===l||0===b||16<=h-b?(b=h,a()):null===k&&(k=d(function(){k=null;a();b=Date.now()},16-(h-
b)))}},Q=function(a,b,k,d){d=P(d);null!=b&&b.setEventCallback(function(a){!1!==a.redraw&&k.publish()});null!=k&&(a.redraw&&k.unsubscribe(a.redraw),k.subscribe(d));return a.redraw=d};t.mount=function(a,b){return function(k,d){if(null===d)a.render(k,[]),b.unsubscribe(k.redraw),delete k.redraw;else{if(null==d.view)throw Error("m.mount(element, component) expects a component, not a vnode");Q(k,a,b,function(){a.render(k,m(d,void 0,void 0,void 0,void 0,void 0))})()}}}(M,I);var J=function(a){if(""===a||
null==a)return{};"?"===a.charAt(0)&&(a=a.slice(1));a=a.split("&");for(var b={},k={},d=0;d<a.length;d++){var l=a[d].split("="),h=decodeURIComponent(l[0]),l=2===l.length?decodeURIComponent(l[1]):"";"true"===l?l=!0:"false"===l&&(l=!1);var m=h.split(/\]\[?|\[/),q=b;-1<h.indexOf("[")&&m.pop();for(var r=0;r<m.length;r++){var h=m[r],t=m[r+1],t=""==t||!isNaN(parseInt(t,10)),n=r===m.length-1;""===h&&(h=m.slice(0,r).join(),null==k[h]&&(k[h]=0),h=k[h]++);null==q[h]&&(q[h]=n?l:t?[]:{});q=q[h]}}return b},R=function(a){function b(b){var d=
a.location[b].replace(/(?:%[a-f89][a-f0-9])+/gim,decodeURIComponent);"pathname"===b&&"/"!==d[0]&&(d="/"+d);return d}function k(a){return function(){null==t&&(t=q(function(){t=null;a()}))}}function d(a,b,d){var f=a.indexOf("?"),h=a.indexOf("#"),l=-1<f?f:-1<h?h:a.length;if(-1<f){var f=J(a.slice(f+1,-1<h?h:a.length)),k;for(k in f)b[k]=f[k]}if(-1<h)for(k in b=J(a.slice(h+1)),b)d[k]=b[k];return a.slice(0,l)}function l(){switch(r.charAt(0)){case "#":return b("hash").slice(r.length);case "?":return b("search").slice(r.length)+
b("hash");default:return b("pathname").slice(r.length)+b("search")+b("hash")}}function h(b,h,f){var k={},l={};b=d(b,k,l);if(null!=h){for(var n in h)k[n]=h[n];b=b.replace(/:([^\/]+)/g,function(a,b){delete k[b];return h[b]})}(n=C(k))&&(b+="?"+n);(l=C(l))&&(b+="#"+l);m?(f&&f.replace?a.history.replaceState(null,null,r+b):a.history.pushState(null,null,r+b),a.onpopstate()):a.location.href=r+b}var m="function"===typeof a.history.pushState,q="function"===typeof setImmediate?setImmediate:setTimeout,r="#!",
t;return{setPrefix:function(a){r=a},getPath:l,setPath:h,defineRoutes:function(b,h,f){function n(){var a=l(),k={},m=d(a,k,k),n;for(n in b){var q=new RegExp("^"+n.replace(/:[^\/]+?\.{3}/g,"(.*?)").replace(/:[^\/]+/g,"([^\\/]+)")+"/?$");if(q.test(m)){m.replace(q,function(){for(var d=n.match(/:[^\/]+/g)||[],f=[].slice.call(arguments,1,-2),l=0;l<d.length;l++)k[d[l].replace(/:|\./g,"")]=decodeURIComponent(f[l]);h(b[n],k,a,n)});return}}f(a,k)}m?a.onpopstate=k(n):"#"===r.charAt(0)&&(a.onhashchange=n);n();
return n},link:function(a){a.dom.setAttribute("href",r+a.attrs.href);a.dom.onclick=function(a){a.ctrlKey||a.metaKey||a.shiftKey||2===a.which||(a.preventDefault(),a.redraw=!1,a=this.getAttribute("href"),0===a.indexOf(r)&&(a=a.slice(r.length)),h(a,void 0,void 0))}}}};t.route=function(a,b){function k(a){return a}var d=R(a),l,h,t,q,r,x={view:function(){return[t(m(h,null,q,void 0,void 0,void 0))]}},n=function(a,f,m){h="div";t=k;q=null;b(a,x);d.defineRoutes(m,function(b,d,f){var m="function"!==typeof b.view,
n=k,p=l=function(k){p===l&&(l=null,h=null!=k?k:m?"div":b,t=n,q=d,r=f,a.redraw(!0))},u=function(){p()};m&&("function"===typeof b.render&&(n=b.render.bind(b)),"function"===typeof b.onmatch&&(u=b.onmatch));u.call(b,p,d,f)},function(){d.setPath(f,null,{replace:!0})})};n.link=d.link;n.prefix=d.setPrefix;n.set=d.setPath;n.get=function(){return r};return n}(window,t.mount);t.withAttr=function(a,b,k){return function(d){return b.call(k||this,a in d.currentTarget?d.currentTarget[a]:d.currentTarget.getAttribute(a))}};
t.render=M.render;t.redraw=I.publish;t.request=H.request;t.jsonp=H.jsonp;t.parseQueryString=J;t.buildQueryString=C;t.version="1.0.0-rc.4";"undefined"!==typeof module?module.exports=t:window.m=t};
new function(){function l(a,b,k,d,n,h){return{tag:a,key:b,attrs:k,children:d,text:n,dom:h,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function t(a){if(null==a||"string"!==typeof a&&null==a.view)throw Error("The selector must be either a string or a component.");if("string"===typeof a&&void 0===G[a]){for(var b,k,d=[],n={};b=N.exec(a);){var h=b[1],v=b[2];""===h&&""!==v?k=v:"#"===h?n.id=v:"."===h?d.push(v):"["===b[3][0]&&((h=b[6])&&(h=h.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")),
n[b[4]]=h||!0)}0<d.length&&(n.className=d.join(" "));G[a]=function(a,b){var d=!1,h,f,x=a.className||a["class"],m;for(m in n)a[m]=n[m];void 0!==x&&(void 0!==a["class"]&&(a["class"]=void 0,a.className=x),void 0!==n.className&&(a.className=n.className+" "+x));for(m in a)if("key"!==m){d=!0;break}b instanceof Array&&1==b.length&&null!=b[0]&&"#"===b[0].tag?f=b[0].children:h=b;return l(k||"div",a.key,d?a:void 0,h,f,void 0)}}var q;null!=arguments[1]&&("object"!==typeof arguments[1]||void 0!==arguments[1].tag||
arguments[1]instanceof Array)?d=1:(q=arguments[1],d=2);if(arguments.length===d+1)b=arguments[d]instanceof Array?arguments[d]:[arguments[d]];else for(b=[];d<arguments.length;d++)b.push(arguments[d]);return"string"===typeof a?G[a](q||{},l.normalizeChildren(b)):l(a,q&&q.key,q||{},l.normalizeChildren(b),void 0,void 0)}l.normalize=function(a){return a instanceof Array?l("[",void 0,void 0,l.normalizeChildren(a),void 0,void 0):null!=a&&"object"!==typeof a?l("#",void 0,void 0,a,void 0,void 0):a};l.normalizeChildren=
function(a){for(var b=0;b<a.length;b++)a[b]=l.normalize(a[b]);return a};var N=/(?:(^|#|\.)([^#\.\[\]]+))|(\[(.+?)(?:\s*=\s*("|'|)((?:\\["'\]]|.)*?)\5)?\])/g,G={};t.trust=function(a){null==a&&(a="");return l("<",void 0,void 0,a,void 0,void 0)};t.fragment=function(a,b){return l("[",a.key,a,l.normalizeChildren(b),void 0,void 0)};var y=function(a){function b(a,b){return function x(m){var p;try{if(!b||null==m||"object"!==typeof m&&"function"!==typeof m||"function"!==typeof(p=m.then))l(function(){b||0!==
a.length||console.error("Possible unhandled promise rejection:",m);for(var d=0;d<a.length;d++)a[d](m);n.length=0;h.length=0;r.state=b;r.retry=function(){x(m)}});else{if(m===d)throw new TypeError("Promise can't be resolved w/ itself");k(p.bind(m))}}catch(E){q(E)}}}function k(a){function b(a){return function(b){0<f++||a(b)}}var f=0,d=b(q);try{a(b(v),d)}catch(m){d(m)}}if(!(this instanceof y))throw Error("Promise must be called with `new`");if("function"!==typeof a)throw new TypeError("executor must be a function");
var d=this,n=[],h=[],v=b(n,!0),q=b(h,!1),r=d._instance={resolvers:n,rejectors:h},l="function"===typeof setImmediate?setImmediate:setTimeout;k(a)};y.prototype.then=function(a,b){function k(a,b,k,p){b.push(function(b){if("function"!==typeof a)k(b);else try{n(a(b))}catch(f){h&&h(f)}});"function"===typeof d.retry&&p===d.state&&d.retry()}var d=this._instance,n,h,v=new y(function(a,b){n=a;h=b});k(a,d.resolvers,n,!0);k(b,d.rejectors,h,!1);return v};y.prototype["catch"]=function(a){return this.then(null,
a)};y.resolve=function(a){return a instanceof y?a:new y(function(b){b(a)})};y.reject=function(a){return new y(function(b,k){k(a)})};y.all=function(a){return new y(function(b,k){var d=a.length,n=0,h=[];if(0===a.length)b([]);else for(var v=0;v<a.length;v++)(function(v){function r(a){n++;h[v]=a;n===d&&b(h)}null==a[v]||"object"!==typeof a[v]&&"function"!==typeof a[v]||"function"!==typeof a[v].then?r(a[v]):a[v].then(r,k)})(v)})};y.race=function(a){return new y(function(b,k){for(var d=0;d<a.length;d++)a[d].then(b,
k)})};"undefined"===typeof Promise&&("undefined"!==typeof window?window.Promise=y:"undefined"!==typeof global&&(global.Promise=y));var C=function(a){function b(a,d){if(d instanceof Array)for(var h=0;h<d.length;h++)b(a+"["+h+"]",d[h]);else if("[object Object]"===Object.prototype.toString.call(d))for(h in d)b(a+"["+h+"]",d[h]);else k.push(encodeURIComponent(a)+(null!=d&&""!==d?"="+encodeURIComponent(d):""))}if("[object Object]"!==Object.prototype.toString.call(a))return"";var k=[],d;for(d in a)b(d,
a[d]);return k.join("&")},H=function(a,b){function k(){0===--p&&"function"===typeof w&&w()}function d(a){var f=a.then;a.then=function(){p++;var b=f.apply(a,arguments);b.then(k,function(a){k();throw a;});return d(b)};return a}function n(a,b){if(null==b)return a;for(var f=a.match(/:[^\/]+/gi)||[],d=0;d<f.length;d++){var h=f[d].slice(1);null!=b[h]&&(a=a.replace(f[d],b[h]),delete b[h])}return a}function h(a,b){var f=C(b);if(""!==f){var d=0>a.indexOf("?")?"?":"&";a+=d+f}return a}function v(a){try{return""!==
a?JSON.parse(a):null}catch(x){throw Error(a);}}function q(a){return a.responseText}function r(a,b){if("function"===typeof a)if(b instanceof Array)for(var d=0;d<b.length;d++)b[d]=new a(b[d]);else return new a(b);return b}var l=0,p=0,w;return{request:function(f,k){return d(new b(function(b,d){if("string"===typeof f){var p=f;f=k||{};null==f.url&&(f.url=p)}null==f.method&&(f.method="GET");f.method=f.method.toUpperCase();p="boolean"===typeof f.useBody?f.useBody:"GET"!==f.method&&"TRACE"!==f.method;"function"!==
typeof f.serialize&&(f.serialize="undefined"!==typeof FormData&&f.data instanceof FormData?function(a){return a}:JSON.stringify);"function"!==typeof f.deserialize&&(f.deserialize=v);"function"!==typeof f.extract&&(f.extract=q);f.url=n(f.url,f.data);p?f.data=f.serialize(f.data):f.url=h(f.url,f.data);var m=new a.XMLHttpRequest;m.open(f.method,f.url,"boolean"===typeof f.async?f.async:!0,"string"===typeof f.user?f.user:void 0,"string"===typeof f.password?f.password:void 0);f.serialize===JSON.stringify&&
p&&m.setRequestHeader("Content-Type","application/json; charset=utf-8");f.deserialize===v&&m.setRequestHeader("Accept","application/json, text/*");"function"===typeof f.config&&(m=f.config(m,f)||m);m.onreadystatechange=function(){if(4===m.readyState)try{var a=f.extract!==q?f.extract(m,f):f.deserialize(f.extract(m,f));if(200<=m.status&&300>m.status||304===m.status)b(r(f.type,a));else{var h=Error(m.responseText),k;for(k in a)h[k]=a[k];d(h)}}catch(F){d(F)}};p&&null!=f.data?m.send(f.data):m.send()}))},
jsonp:function(f){return d(new b(function(b,d){var m=f.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+l++,k=a.document.createElement("script");a[m]=function(d){k.parentNode.removeChild(k);b(r(f.type,d));delete a[m]};k.onerror=function(){k.parentNode.removeChild(k);d(Error("JSONP request failed"));delete a[m]};null==f.data&&(f.data={});f.url=n(f.url,f.data);f.data[f.callbackKey||"callback"]=m;k.src=h(f.url,f.data);a.document.documentElement.appendChild(k)}))},setCompletionCallback:function(a){w=
a}}}(window,"undefined"!==typeof Promise?Promise:y),I=function(){var a=[];return{subscribe:a.push.bind(a),unsubscribe:function(b){b=a.indexOf(b);-1<b&&a.splice(b,1)},publish:function(){for(var b=0;b<a.length;b++)a[b].apply(this,arguments)}}}();H.setCompletionCallback(I.publish);var M=function(a){function b(c,g,a,b,d,f,h){for(;a<b;a++){var e=g[a];null!=e&&r(c,k(e,d,h),f)}}function k(c,g,a){var e=c.tag;null!=c.attrs&&t(c.attrs,c,g);if("string"===typeof e)switch(e){case "#":return c.dom=A.createTextNode(c.children);
case "<":return d(c);case "[":var f=A.createDocumentFragment();null!=c.children&&(e=c.children,b(f,e,0,e.length,g,null,a));c.dom=f.firstChild;c.domSize=f.childNodes.length;return f;default:var h=c.tag;switch(c.tag){case "svg":a="http://www.w3.org/2000/svg";break;case "math":a="http://www.w3.org/1998/Math/MathML"}var p=(e=c.attrs)&&e.is,h=a?p?A.createElementNS(a,h,{is:p}):A.createElementNS(a,h):p?A.createElement(h,{is:p}):A.createElement(h);c.dom=h;if(null!=e)for(f in p=a,e)m(c,f,null,e[f],p);null!=
c.attrs&&null!=c.attrs.contenteditable?y(c):(null!=c.text&&(""!==c.text?h.textContent=c.text:c.children=[l("#",void 0,void 0,c.text,void 0,void 0)]),null!=c.children&&(f=c.children,b(h,f,0,f.length,g,null,a),g=c.attrs,"select"===c.tag&&null!=g&&("value"in g&&m(c,"value",null,g.value,void 0),"selectedIndex"in g&&m(c,"selectedIndex",null,g.selectedIndex,void 0))));return h}else{c.state||(c.state={});O(c.state,c.tag);f=c.tag.view;if(null!=f.reentrantLock)c=F;else if(f.reentrantLock=!0,t(c.tag,c,g),c.instance=
l.normalize(f.call(c.state,c)),f.reentrantLock=null,null!=c.instance){if(c.instance===c)throw Error("A view cannot return the vnode it received as arguments");g=k(c.instance,g,a);c.dom=c.instance.dom;c.domSize=null!=c.dom?c.instance.domSize:0;c=g}else c.domSize=0,c=F;return c}}function d(c){var g={caption:"table",thead:"table",tbody:"table",tfoot:"table",tr:"tbody",th:"tr",td:"tr",colgroup:"table",col:"colgroup"}[(c.children.match(/^\s*?<(\w+)/im)||[])[1]]||"div",g=A.createElement(g);g.innerHTML=
c.children;c.dom=g.firstChild;c.domSize=g.childNodes.length;c=A.createDocumentFragment();for(var a;a=g.firstChild;)c.appendChild(a);return c}function n(c,a,e,f,d,m){if(a!==e&&(null!=a||null!=e))if(null==a)b(c,e,0,e.length,f,d,void 0);else if(null==e)p(a,0,a.length,e);else{var g;a:{if(null!=a.pool&&Math.abs(a.pool.length-e.length)<=Math.abs(a.length-e.length)&&(g=e[0]&&e[0].children&&e[0].children.length||0,Math.abs((a.pool[0]&&a.pool[0].children&&a.pool[0].children.length||0)-g)<=Math.abs((a[0]&&
a[0].children&&a[0].children.length||0)-g))){g=!0;break a}g=!1}g&&(a=a.concat(a.pool));if(a.length===e.length&&null!=e[0]&&null==e[0].key)for(var u=0;u<a.length;u++)a[u]===e[u]||null==a[u]&&null==e[u]||(null==a[u]?r(c,k(e[u],f,m),q(a,u+1,d)):null==e[u]?p(a,u,u+1,e):h(c,a[u],e[u],f,q(a,u+1,d),g,m),g&&a[u].tag===e[u].tag&&r(c,v(a[u]),q(a,u+1,d)));else{for(var n=u=0,w=a.length-1,B=e.length-1,x;w>=u&&B>=n;){var z=a[u],l=e[n];if(z!==l||g)if(null!=z&&null!=l&&z.key===l.key)u++,n++,h(c,z,l,f,q(a,u,d),g,
m),g&&z.tag===l.tag&&r(c,v(z),d);else if(z=a[w],z!==l||g)if(null!=z&&null!=l&&z.key===l.key)h(c,z,l,f,q(a,w+1,d),g,m),(g||n<B)&&r(c,v(z),q(a,u,d)),w--,n++;else break;else w--,n++;else u++,n++}for(;w>=u&&B>=n;){z=a[w];l=e[B];if(z!==l||g)if(null!=z&&null!=l&&z.key===l.key)h(c,z,l,f,q(a,w+1,d),g,m),g&&z.tag===l.tag&&r(c,v(z),d),null!=z.dom&&(d=z.dom),w--;else{if(!x){x=a;var z=w,D={},t;for(t=0;t<z;t++){var y=x[t];null!=y&&(y=y.key,null!=y&&(D[y]=t))}x=D}null!=l&&(z=x[l.key],null!=z?(D=a[z],h(c,D,l,f,
q(a,w+1,d),g,m),r(c,v(D),d),a[z].skip=!0,null!=D.dom&&(d=D.dom)):(l=k(l,f,void 0),r(c,l,d),d=l))}else w--;B--;if(B<n)break}b(c,e,n,B+1,f,d,m);p(a,u,w+1,e)}}}function h(a,g,e,b,p,w,x){var c=g.tag;if(c===e.tag){e.state=g.state;e.events=g.events;var u;var B;null!=e.attrs&&"function"===typeof e.attrs.onbeforeupdate&&(u=e.attrs.onbeforeupdate.call(e.state,e,g));"string"!==typeof e.tag&&"function"===typeof e.tag.onbeforeupdate&&(B=e.tag.onbeforeupdate.call(e.state,e,g));void 0===u&&void 0===B||u||B?u=!1:
(e.dom=g.dom,e.domSize=g.domSize,e.instance=g.instance,u=!0);if(!u)if(null!=e.attrs&&L(e.attrs,e,b,w),"string"===typeof c)switch(c){case "#":g.children.toString()!==e.children.toString()&&(g.dom.nodeValue=e.children);e.dom=g.dom;break;case "<":g.children!==e.children?(v(g),r(a,d(e),p)):(e.dom=g.dom,e.domSize=g.domSize);break;case "[":n(a,g.children,e.children,b,p,x);g=0;b=e.children;e.dom=null;if(null!=b){for(var q=0;q<b.length;q++)a=b[q],null!=a&&null!=a.dom&&(null==e.dom&&(e.dom=a.dom),g+=a.domSize||
1);1!==g&&(e.domSize=g)}break;default:a=x;p=e.dom=g.dom;switch(e.tag){case "svg":a="http://www.w3.org/2000/svg";break;case "math":a="http://www.w3.org/1998/Math/MathML"}"textarea"===e.tag&&(null==e.attrs&&(e.attrs={}),null!=e.text&&(e.attrs.value=e.text,e.text=void 0));w=g.attrs;x=e.attrs;c=a;if(null!=x)for(q in x)m(e,q,w&&w[q],x[q],c);if(null!=w)for(q in w)null!=x&&q in x||("className"===q&&(q="class"),"o"!==q[0]||"n"!==q[1]||K(q)?"key"!==q&&e.dom.removeAttribute(q):E(e,q,void 0));null!=e.attrs&&
null!=e.attrs.contenteditable?y(e):null!=g.text&&null!=e.text&&""!==e.text?g.text.toString()!==e.text.toString()&&(g.dom.firstChild.nodeValue=e.text):(null!=g.text&&(g.children=[l("#",void 0,void 0,g.text,void 0,g.dom.firstChild)]),null!=e.text&&(e.children=[l("#",void 0,void 0,e.text,void 0,void 0)]),n(p,g.children,e.children,b,null,a))}else e.instance=l.normalize(e.tag.view.call(e.state,e)),L(e.tag,e,b,w),null!=e.instance?(null==g.instance?r(a,k(e.instance,b,x),p):h(a,g.instance,e.instance,b,p,
w,x),e.dom=e.instance.dom,e.domSize=e.instance.domSize):null!=g.instance?(f(g.instance,null),e.dom=void 0,e.domSize=0):(e.dom=g.dom,e.domSize=g.domSize)}else f(g,null),r(a,k(e,b,x),p)}function v(a){var c=a.domSize;if(null!=c||null==a.dom){var e=A.createDocumentFragment();if(0<c){for(a=a.dom;--c;)e.appendChild(a.nextSibling);e.insertBefore(a,e.firstChild)}return e}return a.dom}function q(a,g,e){for(;g<a.length;g++)if(null!=a[g]&&null!=a[g].dom)return a[g].dom;return e}function r(a,g,e){e&&e.parentNode?
a.insertBefore(g,e):a.appendChild(g)}function y(a){var c=a.children;if(null!=c&&1===c.length&&"<"===c[0].tag)c=c[0].children,a.dom.innerHTML!==c&&(a.dom.innerHTML=c);else if(null!=c||null!=a.text)throw Error("Child node of a contenteditable must be trusted");}function p(a,g,e,b){for(;g<e;g++){var c=a[g];null!=c&&(c.skip?c.skip=!1:f(c,b))}}function w(a){var c=!1;return function(){c||(c=!0,a())}}function f(a,g){function c(){if(++d===b&&(x(a),a.dom)){var c=a.domSize||1;if(1<c)for(var e=a.dom;--c;){var f=
e.nextSibling,h=f.parentNode;null!=h&&h.removeChild(f)}c=a.dom;e=c.parentNode;null!=e&&e.removeChild(c);if(c=null!=g&&null==a.domSize)c=a.attrs,c=!(null!=c&&(c.oncreate||c.onupdate||c.onbeforeremove||c.onremove));c&&"string"===typeof a.tag&&(g.pool?g.pool.push(a):g.pool=[a])}}var b=1,d=0;a.attrs&&a.attrs.onbeforeremove&&(b++,a.attrs.onbeforeremove.call(a.state,a,w(c)));"string"!==typeof a.tag&&a.tag.onbeforeremove&&(b++,a.tag.onbeforeremove.call(a.state,a,w(c)));c()}function x(a){a.attrs&&a.attrs.onremove&&
a.attrs.onremove.call(a.state,a);"string"!==typeof a.tag&&a.tag.onremove&&a.tag.onremove.call(a.state,a);if(null!=a.instance)x(a.instance);else if(a=a.children,a instanceof Array)for(var c=0;c<a.length;c++){var e=a[c];null!=e&&x(e)}}function m(a,g,e,b,d){var c=a.dom;if("key"!==g&&(e!==b||"value"===g||"checked"===g||"selectedIndex"===g||"selected"===g&&a.dom===A.activeElement||"object"===typeof b)&&"undefined"!==typeof b&&!K(g)){var f=g.indexOf(":");if(-1<f&&"xlink"===g.substr(0,f))c.setAttributeNS("http://www.w3.org/1999/xlink",
g.slice(f+1),b);else if("o"===g[0]&&"n"===g[1]&&"function"===typeof b)E(a,g,b);else if("style"===g)if(a=e,a===b&&(c.style.cssText="",a=null),null==b)c.style.cssText="";else if("string"===typeof b)c.style.cssText=b;else{"string"===typeof a&&(c.style.cssText="");for(var h in b)c.style[h]=b[h];if(null!=a&&"string"!==typeof a)for(h in a)h in b||(c.style[h]="")}else g in c&&"href"!==g&&"list"!==g&&"form"!==g&&"width"!==g&&"height"!==g&&void 0===d?"input"===a.tag&&"value"===g&&a.dom.value===b&&a.dom===
A.activeElement||"option"===a.tag&&"value"===g&&a.dom.value===b||(c[g]=b):"boolean"===typeof b?b?c.setAttribute(g,""):c.removeAttribute(g):c.setAttribute("className"===g?"class":g,b)}}function K(a){return"oninit"===a||"oncreate"===a||"onupdate"===a||"onremove"===a||"onbeforeremove"===a||"onbeforeupdate"===a}function E(a,b,e){var c=a.dom,g=function(a){var b=e.call(c,a);"function"===typeof C&&C.call(c,a);return b};if(b in c)c[b]="function"===typeof e?g:null;else{var d=b.slice(2);void 0===a.events&&
(a.events={});null!=a.events[b]&&c.removeEventListener(d,a.events[b],!1);"function"===typeof e&&(a.events[b]=g,c.addEventListener(d,a.events[b],!1))}}function t(a,b,e){"function"===typeof a.oninit&&a.oninit.call(b.state,b);"function"===typeof a.oncreate&&e.push(a.oncreate.bind(b.state,b))}function L(a,b,e,d){d?t(a,b,e):"function"===typeof a.onupdate&&e.push(a.onupdate.bind(b.state,b))}function O(a,b){Object.keys(b).forEach(function(c){a[c]=b[c]})}var A=a.document,F=A.createDocumentFragment(),C;return{render:function(a,
b){if(!a)throw Error("Ensure the DOM element being passed to m.route/m.mount/m.render is not undefined.");var c=[],d=A.activeElement;null==a.vnodes&&(a.textContent="");b instanceof Array||(b=[b]);n(a,a.vnodes,l.normalizeChildren(b),c,null,void 0);a.vnodes=b;for(var f=0;f<c.length;f++)c[f]();A.activeElement!==d&&d.focus()},setEventCallback:function(a){return C=a}}}(window),P=function(a){var b=0,k=null,d="function"===typeof requestAnimationFrame?requestAnimationFrame:setTimeout;return function(n){var h=
Date.now();!0===n||0===b||16<=h-b?(b=h,a()):null===k&&(k=d(function(){k=null;a();b=Date.now()},16-(h-b)))}},Q=function(a,b,k,d){d=P(d);null!=b&&b.setEventCallback(function(a){!1!==a.redraw&&k.publish()});null!=k&&(a.redraw&&k.unsubscribe(a.redraw),k.subscribe(d));return a.redraw=d};t.mount=function(a,b){return function(k,d){if(null===d)a.render(k,[]),b.unsubscribe(k.redraw),delete k.redraw;else{if(null==d.view)throw Error("m.mount(element, component) expects a component, not a vnode");Q(k,a,b,function(){a.render(k,
l(d,void 0,void 0,void 0,void 0,void 0))})()}}}(M,I);var J=function(a){if(""===a||null==a)return{};"?"===a.charAt(0)&&(a=a.slice(1));a=a.split("&");for(var b={},k={},d=0;d<a.length;d++){var n=a[d].split("="),h=decodeURIComponent(n[0]),n=2===n.length?decodeURIComponent(n[1]):"";"true"===n?n=!0:"false"===n&&(n=!1);var l=h.split(/\]\[?|\[/),q=b;-1<h.indexOf("[")&&l.pop();for(var r=0;r<l.length;r++){var h=l[r],t=l[r+1],t=""==t||!isNaN(parseInt(t,10)),p=r===l.length-1;""===h&&(h=l.slice(0,r).join(),null==
k[h]&&(k[h]=0),h=k[h]++);null==q[h]&&(q[h]=p?n:t?[]:{});q=q[h]}}return b},R=function(a){function b(b){var d=a.location[b].replace(/(?:%[a-f89][a-f0-9])+/gim,decodeURIComponent);"pathname"===b&&"/"!==d[0]&&(d="/"+d);return d}function k(a){return function(){null==t&&(t=q(function(){t=null;a()}))}}function d(a,b,d){var f=a.indexOf("?"),h=a.indexOf("#"),k=-1<f?f:-1<h?h:a.length;if(-1<f){var f=J(a.slice(f+1,-1<h?h:a.length)),p;for(p in f)b[p]=f[p]}if(-1<h)for(p in b=J(a.slice(h+1)),b)d[p]=b[p];return a.slice(0,
k)}function n(){switch(r.charAt(0)){case "#":return b("hash").slice(r.length);case "?":return b("search").slice(r.length)+b("hash");default:return b("pathname").slice(r.length)+b("search")+b("hash")}}function h(b,h,f){var k={},m={};b=d(b,k,m);if(null!=h){for(var n in h)k[n]=h[n];b=b.replace(/:([^\/]+)/g,function(a,b){delete k[b];return h[b]})}(n=C(k))&&(b+="?"+n);(m=C(m))&&(b+="#"+m);l?(f&&f.replace?a.history.replaceState(null,null,r+b):a.history.pushState(null,null,r+b),a.onpopstate()):a.location.href=
r+b}var l="function"===typeof a.history.pushState,q="function"===typeof setImmediate?setImmediate:setTimeout,r="#!",t;return{setPrefix:function(a){r=a},getPath:n,setPath:h,defineRoutes:function(b,h,f){function p(){var a=n(),k={},l=d(a,k,k),p;for(p in b){var q=new RegExp("^"+p.replace(/:[^\/]+?\.{3}/g,"(.*?)").replace(/:[^\/]+/g,"([^\\/]+)")+"/?$");if(q.test(l)){l.replace(q,function(){for(var d=p.match(/:[^\/]+/g)||[],f=[].slice.call(arguments,1,-2),m=0;m<d.length;m++)k[d[m].replace(/:|\./g,"")]=decodeURIComponent(f[m]);
h(b[p],k,a,p)});return}}f(a,k)}l?a.onpopstate=k(p):"#"===r.charAt(0)&&(a.onhashchange=p);p();return p},link:function(a){a.dom.setAttribute("href",r+a.attrs.href);a.dom.onclick=function(a){a.ctrlKey||a.metaKey||a.shiftKey||2===a.which||(a.preventDefault(),a.redraw=!1,a=this.getAttribute("href"),0===a.indexOf(r)&&(a=a.slice(r.length)),h(a,void 0,void 0))}}}};t.route=function(a,b){function k(a){return a}var d=R(a),n,h,t,q,r,y={view:function(){return[t(l(h,null,q,void 0,void 0,void 0))]}},p=function(a,
f,l){h="div";t=k;q=null;b(a,y);d.defineRoutes(l,function(b,d,f){var l="function"!==typeof b.view,m=k,p=n=function(k){p===n&&(n=null,h=null!=k?k:l?"div":b,t=m,q=d,r=f,a.redraw(!0))},v=function(){p()};l&&("function"===typeof b.render&&(m=b.render.bind(b)),"function"===typeof b.onmatch&&(v=b.onmatch));v.call(b,p,d,f)},function(){d.setPath(f,null,{replace:!0})})};p.link=d.link;p.prefix=d.setPrefix;p.set=d.setPath;p.get=function(){return r};return p}(window,t.mount);t.withAttr=function(a,b,k){return function(d){return b.call(k||
this,a in d.currentTarget?d.currentTarget[a]:d.currentTarget.getAttribute(a))}};t.render=M.render;t.redraw=I.publish;t.request=H.request;t.jsonp=H.jsonp;t.parseQueryString=J;t.buildQueryString=C;t.version="1.0.0-rc.5";"undefined"!==typeof module?module.exports=t:window.m=t};

View file

@ -432,6 +432,8 @@ module.exports = function($window) {
else if (key in element && !isAttribute(key) && ns === undefined) {
//setting input[value] to same value by typing on focused element moves cursor to end in Chrome
if (vnode.tag === "input" && key === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return
//setting option[value] to same value while having select open blinks select dropdown in Chrome
if (vnode.tag === "option" && key === "value" && vnode.dom.value === value) return
element[key] = value
}
else {

View file

@ -44,7 +44,7 @@ function updateState(stream, value) {
}
function updateDependency(stream, mustSync) {
var state = stream._state, parents = state.parents
if (parents.length > 0 && parents.filter(active).length === parents.length && (mustSync || parents.filter(changed).length > 0)) {
if (parents.length > 0 && parents.every(active) && (mustSync || parents.some(changed))) {
var value = stream._state.derive()
if (value === HALT) return false
updateState(stream, value)
@ -56,7 +56,7 @@ function finalize(stream) {
}
function combine(fn, streams) {
if (streams.length > streams.filter(valid).length) throw new Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream")
if (!streams.every(valid)) throw new Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream")
return initDependency(createStream(), streams, function() {
return fn.apply(this, streams.concat([streams.filter(changed)]))
})