From 080e2f4a56cee54728462f27ede7f81605f5ad4f Mon Sep 17 00:00:00 2001 From: Chris Bowdon Date: Sun, 14 Sep 2014 20:37:49 +0100 Subject: [PATCH] Included Mithril definitions. --- CONTRIBUTORS.md | 1 + mithril/mithril-tests.ts | 55 ++++++++++++++++++++++++++++++++ mithril/mithril.d.ts | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 mithril/mithril-tests.ts create mode 100644 mithril/mithril.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ec2eec5d8..0eca0fa13 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -253,6 +253,7 @@ All definitions files include a header with the author and editors, so at some p * [Microsoft Live Connect](http://msdn.microsoft.com/en-us/library/live/hh243643.aspx) (by [John Vilk](https://github.com/jvilk)) * [Minimatch](https://github.com/isaacs/minimatch) (by [vvakame](https://github.com/vvakame)) * [minimist](https://github.com/substack/minimist) (by [Bart van der Schoor](https://github.com/Bartvds)) +* [Mithril](http://lhorie.github.io/mithril) (by [Leo Horie](https://github.com/lhorie) and [Chris Bowdon](https://github.com/cbowdon)) * [Mixpanel](https://github.com/mixpanel/mixpanel-js) (by [Knut Eirik Leira Hjelle](https://github.com/hjellek)) * [mixto](https://github.com/atom/mixto) (by [vvakame](https://github.com/vvakame)) * [Modernizr](http://modernizr.com/) (by [Boris Yankov](https://github.com/borisyankov) and [Theodore Brown](https://github.com/theodorejb/)) diff --git a/mithril/mithril-tests.ts b/mithril/mithril-tests.ts new file mode 100644 index 000000000..5ca26702c --- /dev/null +++ b/mithril/mithril-tests.ts @@ -0,0 +1,55 @@ +/// +// This is the todolist example from http://lhorie.github.io/mithril/getting-started.html + +var todo = { + + //the Todo class has two properties + Todo: function(data: any) { + this.description = m.prop(data.description); + this.done = m.prop(false); + }, + + //the TodoList class is a list of Todo's + TodoList: Array, + + //the controller uses three model-level entities, of which one is a custom defined class: + //`Todo` is the central class in this application + //`list` is merely a generic array, with standard array methods + //`description` is a temporary storage box that holds a string + // + //the `add` method simply adds a new todo to the list + controller: function() { + this.list = new todo.TodoList(); + this.description = m.prop(""); + + this.add = function() { + if (this.description()) { + this.list.push(new todo.Todo({description: this.description()})); + this.description(""); + } + }.bind(this); + }, + + //here's the view + view: function(ctrl: any) { + return m("html", [ + m("body", [ + m("input", {onchange: m.withAttr("value", ctrl.description), value: ctrl.description()}), + m("button", {onclick: ctrl.add}, "Add"), + m("table", [ + ctrl.list.map(function(task: any) { + return m("tr", [ + m("td", [ + m("input[type=checkbox]", {onclick: m.withAttr("checked", task.done), checked: task.done()}) + ]), + m("td", {style: {textDecoration: task.done() ? "line-through" : "none"}}, task.description()), + ]) + }) + ]) + ]) + ]); + }, +}; + +//initialize the application +m.module(document, todo); diff --git a/mithril/mithril.d.ts b/mithril/mithril.d.ts new file mode 100644 index 000000000..39c9539f8 --- /dev/null +++ b/mithril/mithril.d.ts @@ -0,0 +1,69 @@ +// Type definitions for Mithril +// Project: http://lhorie.github.io/mithril/ +// Definitions by: Leo Horie , Chris Bowdon +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +//Mithril type definitions for Typescript + +interface MithrilStatic { + (selector: string, attributes: Object, children?: any): MithrilVirtualElement; + (selector: string, children?: any): MithrilVirtualElement; + prop(value?: any): (value?: any) => any; + withAttr(property: string, callback: (value: any) => void): (e: Event) => any; + module(rootElement: Node, module: MithrilModule): void; + trust(html: string): String; + render(rootElement: Element, children?: any): void; + render(rootElement: HTMLDocument, children?: any): void; + redraw(): void; + route(rootElement: Element, defaultRoute: string, routes: { [key: string]: MithrilModule }): void; + route(rootElement: HTMLDocument, defaultRoute: string, routes: { [key: string]: MithrilModule }): void; + route(path: string, params?: any, shouldReplaceHistory?: boolean): void; + route(): string; + route(element: Element, isInitialized: boolean): void; + request(options: MithrilXHROptions): MithrilPromise; + deferred(): MithrilDeferred; + sync(promises: MithrilPromise[]): MithrilPromise; + startComputation(): void; + endComputation(): void; +} + +interface MithrilVirtualElement { + tag: string; + attrs: Object; + children: any; +} + +interface MithrilModule { + controller: Function; + view: Function; +} + +interface MithrilDeferred { + resolve(value?: any): void; + reject(value?: any): void; + promise: MithrilPromise; +} + +interface MithrilPromise { + (value?: any): any; + then(successCallback?: (value: any) => any, errorCallback?: (value: any) => any): MithrilPromise; +} + +interface MithrilXHROptions { + method: string; + url: string; + user?: string; + password?: string; + data?: any; + background?: boolean; + unwrapSuccess?(data: any): any; + unwrapError?(data: any): any; + serialize?(dataToSerialize: any): string; + deserialize?(dataToDeserialize: string): any; + extract?(xhr: XMLHttpRequest, options: MithrilXHROptions): string; + type?(data: Object): void; + config?(xhr: XMLHttpRequest, options: MithrilXHROptions): XMLHttpRequest; +} + +declare var Mithril: MithrilStatic; +declare var m: MithrilStatic;