From 88cac1b55b32dc86630964a1ae91ed6e9996e00b Mon Sep 17 00:00:00 2001 From: borislavjivkov Date: Sun, 7 Feb 2016 23:03:19 +0200 Subject: [PATCH 01/69] Added definitions for pace(https://github.com/HubSpot/pace) --- pace/pace-tests.ts | 61 ++++++++++++++++++++++++ pace/pace.d.ts | 113 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 pace/pace-tests.ts create mode 100644 pace/pace.d.ts diff --git a/pace/pace-tests.ts b/pace/pace-tests.ts new file mode 100644 index 000000000..c4a6f4cec --- /dev/null +++ b/pace/pace-tests.ts @@ -0,0 +1,61 @@ +/// + +pace.start({ + document: false +}); + +pace.start(); + +pace.restart(); + +pace.stop(); + +var paceOptions: PaceOptions; + +paceOptions = { + // Disable the 'elements' source + elements: false, + + // Only show the progress on regular and ajax-y page navigation, + // not every request + restartOnRequestAfter: false +} + +paceOptions = { + ajax: false, // disabled + document: false, // disabled + eventLag: false, // disabled + elements: { + selectors: ['.my-page'] + } +}; + +paceOptions = { + elements: { + selectors: ['.timeline,.timeline-error', '.user-profile,.profile-error'] + } +} + +paceOptions = { + restartOnPushState: false +} + +paceOptions = { + restartOnRequestAfter: false +} + +pace.options = { + restartOnRequestAfter: false +} + +pace.ignore(function(){ +}); + +pace.track(function(){ +}); + +pace.options = { + ajax: { + ignoreURLs: ['some-substring', /some-regexp/] + } +}; diff --git a/pace/pace.d.ts b/pace/pace.d.ts new file mode 100644 index 000000000..8eaa8c564 --- /dev/null +++ b/pace/pace.d.ts @@ -0,0 +1,113 @@ +// Type definitions for pace v0.7.5 +// Project: https://github.com/HubSpot/pace +// Definitions by: Borislav Zhivkov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface PaceOptions { + /** + * How long should it take for the bar to animate to a new point after receiving it + */ + catchupTime?: number; + /** + * How quickly should the bar be moving before it has any progress info from a new source in %/ms + */ + initialRate?: number; + /** + * What is the minimum amount of time the bar should be on the screen. Irrespective of this number, the bar will always be on screen for 33 * (100 / maxProgressPerFrame) + ghostTime ms. + */ + minTime?: number; + /** + * What is the minimum amount of time the bar should sit after the last update before disappearing + */ + ghostTime?: number; + /** + * Its easy for a bunch of the bar to be eaten in the first few frames before we know how much there is to load. This limits how much of the bar can be used per frame + */ + maxProgressPerFrame?: number; + /** + * This tweaks the animation easing + */ + easeFactor?: number; + /** + * Should pace automatically start when the page is loaded, or should it wait for `start` to be called? Always false if pace is loaded with AMD or CommonJS. + */ + startOnPageLoad?: boolean; + /** + * Should we restart the browser when pushState or replaceState is called? (Generally means ajax navigation has occured) + */ + restartOnPushState?: boolean; + /** + * Should we show the progress bar for every ajax request (not just regular or ajax-y page navigation)? Set to false to disable. If so, how many ms does the request have to be running for before we show the progress? + */ + restartOnRequestAfter?: boolean | number; + /** + * What element should the pace element be appended to on the page? + */ + target?: string; + document?: boolean | string; + elements?: boolean | PaceElementsOptions; + eventLag?: boolean | PaceEventLagOptions; + ajax?: boolean | PaceAjaxOptions; +} + +interface PaceElementsOptions { + /** + * How frequently in ms should we check for the elements being tested for using the element monitor? + */ + checkInterval?: number; + /** + * What elements should we wait for before deciding the page is fully loaded (not required) + */ + selectors?: string[]; +} + +interface PaceEventLagOptions { + /** + * When we first start measuring event lag, not much is going on in the browser yet, so it's not uncommon for the numbers to be abnormally low for the first few samples. This configures how many samples we need before we consider a low number to mean completion. + */ + minSamples?: number; + /** + * How many samples should we average to decide what the current lag is? + */ + sampleCount?: number; + /** + * Above how many ms of lag is the CPU considered busy? + */ + lagThreshold?: number; +} + +interface PaceAjaxOptions { + /** + * Which HTTP methods should we track? + */ + trackMethods?: string[]; + /** + * Should we track web socket connections? + */ + trackWebSockets?: boolean; + /** + * A list of regular expressions or substrings of URLS we should ignore (for both tracking and restarting) + */ + ignoreURLs?: (string | RegExp)[]; +} + +interface Pace { + options: PaceOptions; + + start(options?: PaceOptions): void; + restart(): void; + stop(): void; + track(fn: () => void, ...args: any[]): void; + ignore(fn: () => void, ...args: any[]): void; + + on(event: string, handler: (...args: any[]) => void, context?: any): void; + off(event: string, handler?: (...args: any[]) => void): void; + once(event: string, handler: (...args: any[]) => void, context?: any): void; +} + +declare enum PaceEvent { start, stop, restart, done, hide } + +declare var pace: Pace; +declare module "pace" { + export = pace; +} From f387303f41af1dfd6868ed0291f1522b264fa1f8 Mon Sep 17 00:00:00 2001 From: borislavjivkov Date: Thu, 11 Feb 2016 21:44:24 +0200 Subject: [PATCH 02/69] Moved pace interfaces to separate module --- pace/pace-tests.ts | 2 +- pace/pace.d.ts | 208 +++++++++++++++++++++++---------------------- 2 files changed, 106 insertions(+), 104 deletions(-) diff --git a/pace/pace-tests.ts b/pace/pace-tests.ts index c4a6f4cec..cecb55ca2 100644 --- a/pace/pace-tests.ts +++ b/pace/pace-tests.ts @@ -10,7 +10,7 @@ pace.restart(); pace.stop(); -var paceOptions: PaceOptions; +var paceOptions: PaceInterfaces.PaceOptions; paceOptions = { // Disable the 'elements' source diff --git a/pace/pace.d.ts b/pace/pace.d.ts index 8eaa8c564..3565ed6cd 100644 --- a/pace/pace.d.ts +++ b/pace/pace.d.ts @@ -3,111 +3,113 @@ // Definitions by: Borislav Zhivkov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -interface PaceOptions { - /** - * How long should it take for the bar to animate to a new point after receiving it - */ - catchupTime?: number; - /** - * How quickly should the bar be moving before it has any progress info from a new source in %/ms - */ - initialRate?: number; - /** - * What is the minimum amount of time the bar should be on the screen. Irrespective of this number, the bar will always be on screen for 33 * (100 / maxProgressPerFrame) + ghostTime ms. - */ - minTime?: number; - /** - * What is the minimum amount of time the bar should sit after the last update before disappearing - */ - ghostTime?: number; - /** - * Its easy for a bunch of the bar to be eaten in the first few frames before we know how much there is to load. This limits how much of the bar can be used per frame - */ - maxProgressPerFrame?: number; - /** - * This tweaks the animation easing - */ - easeFactor?: number; - /** - * Should pace automatically start when the page is loaded, or should it wait for `start` to be called? Always false if pace is loaded with AMD or CommonJS. - */ - startOnPageLoad?: boolean; - /** - * Should we restart the browser when pushState or replaceState is called? (Generally means ajax navigation has occured) - */ - restartOnPushState?: boolean; - /** - * Should we show the progress bar for every ajax request (not just regular or ajax-y page navigation)? Set to false to disable. If so, how many ms does the request have to be running for before we show the progress? - */ - restartOnRequestAfter?: boolean | number; - /** - * What element should the pace element be appended to on the page? - */ - target?: string; - document?: boolean | string; - elements?: boolean | PaceElementsOptions; - eventLag?: boolean | PaceEventLagOptions; - ajax?: boolean | PaceAjaxOptions; +declare module PaceInterfaces { + interface PaceOptions { + /** + * How long should it take for the bar to animate to a new point after receiving it + */ + catchupTime?: number; + /** + * How quickly should the bar be moving before it has any progress info from a new source in %/ms + */ + initialRate?: number; + /** + * What is the minimum amount of time the bar should be on the screen. Irrespective of this number, the bar will always be on screen for 33 * (100 / maxProgressPerFrame) + ghostTime ms. + */ + minTime?: number; + /** + * What is the minimum amount of time the bar should sit after the last update before disappearing + */ + ghostTime?: number; + /** + * Its easy for a bunch of the bar to be eaten in the first few frames before we know how much there is to load. This limits how much of the bar can be used per frame + */ + maxProgressPerFrame?: number; + /** + * This tweaks the animation easing + */ + easeFactor?: number; + /** + * Should pace automatically start when the page is loaded, or should it wait for `start` to be called? Always false if pace is loaded with AMD or CommonJS. + */ + startOnPageLoad?: boolean; + /** + * Should we restart the browser when pushState or replaceState is called? (Generally means ajax navigation has occured) + */ + restartOnPushState?: boolean; + /** + * Should we show the progress bar for every ajax request (not just regular or ajax-y page navigation)? Set to false to disable. If so, how many ms does the request have to be running for before we show the progress? + */ + restartOnRequestAfter?: boolean | number; + /** + * What element should the pace element be appended to on the page? + */ + target?: string; + document?: boolean | string; + elements?: boolean | PaceElementsOptions; + eventLag?: boolean | PaceEventLagOptions; + ajax?: boolean | PaceAjaxOptions; + } + + interface PaceElementsOptions { + /** + * How frequently in ms should we check for the elements being tested for using the element monitor? + */ + checkInterval?: number; + /** + * What elements should we wait for before deciding the page is fully loaded (not required) + */ + selectors?: string[]; + } + + interface PaceEventLagOptions { + /** + * When we first start measuring event lag, not much is going on in the browser yet, so it's not uncommon for the numbers to be abnormally low for the first few samples. This configures how many samples we need before we consider a low number to mean completion. + */ + minSamples?: number; + /** + * How many samples should we average to decide what the current lag is? + */ + sampleCount?: number; + /** + * Above how many ms of lag is the CPU considered busy? + */ + lagThreshold?: number; + } + + interface PaceAjaxOptions { + /** + * Which HTTP methods should we track? + */ + trackMethods?: string[]; + /** + * Should we track web socket connections? + */ + trackWebSockets?: boolean; + /** + * A list of regular expressions or substrings of URLS we should ignore (for both tracking and restarting) + */ + ignoreURLs?: (string | RegExp)[]; + } + + interface Pace { + options: PaceOptions; + + start(options?: PaceOptions): void; + restart(): void; + stop(): void; + track(fn: () => void, ...args: any[]): void; + ignore(fn: () => void, ...args: any[]): void; + + on(event: string, handler: (...args: any[]) => void, context?: any): void; + off(event: string, handler?: (...args: any[]) => void): void; + once(event: string, handler: (...args: any[]) => void, context?: any): void; + } + + enum PaceEvent { start, stop, restart, done, hide } } -interface PaceElementsOptions { - /** - * How frequently in ms should we check for the elements being tested for using the element monitor? - */ - checkInterval?: number; - /** - * What elements should we wait for before deciding the page is fully loaded (not required) - */ - selectors?: string[]; -} - -interface PaceEventLagOptions { - /** - * When we first start measuring event lag, not much is going on in the browser yet, so it's not uncommon for the numbers to be abnormally low for the first few samples. This configures how many samples we need before we consider a low number to mean completion. - */ - minSamples?: number; - /** - * How many samples should we average to decide what the current lag is? - */ - sampleCount?: number; - /** - * Above how many ms of lag is the CPU considered busy? - */ - lagThreshold?: number; -} - -interface PaceAjaxOptions { - /** - * Which HTTP methods should we track? - */ - trackMethods?: string[]; - /** - * Should we track web socket connections? - */ - trackWebSockets?: boolean; - /** - * A list of regular expressions or substrings of URLS we should ignore (for both tracking and restarting) - */ - ignoreURLs?: (string | RegExp)[]; -} - -interface Pace { - options: PaceOptions; - - start(options?: PaceOptions): void; - restart(): void; - stop(): void; - track(fn: () => void, ...args: any[]): void; - ignore(fn: () => void, ...args: any[]): void; - - on(event: string, handler: (...args: any[]) => void, context?: any): void; - off(event: string, handler?: (...args: any[]) => void): void; - once(event: string, handler: (...args: any[]) => void, context?: any): void; -} - -declare enum PaceEvent { start, stop, restart, done, hide } - -declare var pace: Pace; +declare var pace: PaceInterfaces.Pace; declare module "pace" { export = pace; } From 55542c4c6ccb98f47e3b538c80b2934ff53b02b3 Mon Sep 17 00:00:00 2001 From: Wolfgang Steiner Date: Sun, 14 Feb 2016 15:33:01 +0100 Subject: [PATCH 03/69] added types for collector functions (DragSource, DragTarget, DragLayer) --- react-dnd/react-dnd.d.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/react-dnd/react-dnd.d.ts b/react-dnd/react-dnd.d.ts index 982478f78..13071b550 100644 --- a/react-dnd/react-dnd.d.ts +++ b/react-dnd/react-dnd.d.ts @@ -38,14 +38,14 @@ declare module __ReactDnd { export function DragSource

( type: Identifier | ((props: P) => Identifier), spec: DragSourceSpec

, - collect: (connect: DragSourceConnector, monitor: DragSourceMonitor) => Object, + collect: DragSourceCollector, options?: DndOptions

): (componentClass: React.ComponentClass

) => DndComponentClass

; export function DropTarget

( types: Identifier | Identifier[] | ((props: P) => Identifier | Identifier[]), spec: DropTargetSpec

, - collect: (connect: DropTargetConnector, monitor: DropTargetMonitor) => Object, + collect: DropTargetCollector, options?: DndOptions

): (componentClass: React.ComponentClass

) => DndComponentClass

; @@ -54,10 +54,14 @@ declare module __ReactDnd { ): (componentClass: React.ComponentClass

) => ContextComponentClass

; export function DragLayer

( - collect: (monitor: DragLayerMonitor) => Object, + collect: DragLayerCollector, options?: DndOptions

): (componentClass: React.ComponentClass

) => DndComponentClass

; + type DragSourceCollector = (connect: DragSourceConnector, monitor: DragSourceMonitor) => Object; + type DropTargetCollector = (connect: DropTargetConnector, monitor: DropTargetMonitor) => Object; + type DragLayerCollector = (monitor: DragLayerMonitor) => Object; + // Shared // ---------------------------------------------------------------------- From 05efb3d4151b65a1438b1b8a16a3aa23ca2ef89e Mon Sep 17 00:00:00 2001 From: omni360 Date: Mon, 15 Feb 2016 17:36:05 +0800 Subject: [PATCH 04/69] add sat.js type definitions --- .../angular-google-analytics-service.d.ts | 124 +- backbone/backbone-with-lodash-tests.ts | 626 ++-- dynatable/dynatable-tests.ts | 458 +-- dynatable/dynatable.d.ts | 2310 ++++++------- i18next/i18next.d.ts | 320 +- polymer/polymer-tests.ts | 172 +- sat/sat-tests.ts | 23 + sat/sat.d.ts | 142 + tedious/tedious.d.ts | 1052 +++--- teechart/teechart.d.ts | 1366 ++++---- timezonecomplete/timezonecomplete.d.ts | 3038 ++++++++--------- tmp/tmp.d.ts | 96 +- tween.js/tween.js.d.ts | 200 +- tweenjs/tweenjs.d.ts | 332 +- typeahead/typeahead.d.ts | 2414 ++++++------- underscore/underscore-tests.ts | 1016 +++--- unity-webapi/unity-webapi.d.ts | 200 +- 17 files changed, 7027 insertions(+), 6862 deletions(-) create mode 100644 sat/sat-tests.ts create mode 100644 sat/sat.d.ts diff --git a/angular-google-analytics/angular-google-analytics-service.d.ts b/angular-google-analytics/angular-google-analytics-service.d.ts index 5f478621c..e69b63d90 100644 --- a/angular-google-analytics/angular-google-analytics-service.d.ts +++ b/angular-google-analytics/angular-google-analytics-service.d.ts @@ -1,62 +1,62 @@ -// Type definitions for angular-google-analytics v1.1.0 -// Project: https://github.com/revolunet/angular-google-analytics -// Definitions by: Matt Wheatley -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -declare module angular.google.analytics { - interface AnalyticsService { - /** - * @summary If logging is enabled then all outbound calls are accessible via an in-memory array. - * This is useful for troubleshooting and seeing the order of outbound calls with parameters. - */ - log: Array; - - /** - * @summary If in offline mode then all calls are queued to an in-memory array for future processing. - * All calls queued to the offlineQueue are not outbound calls yet and hence do not show up in the log. - */ - offlineQueue: Array; - - /** - * @summary Returns the current URL that would be sent if a `trackPage` call was made. - * @return {string} The URL - */ - getUrl: () => string; - - /** - * @summary Manually create classic analytics (ga.js) script tag - */ - createScriptTag: () => void; - - /** - * @summary Manually create universal analytics (analytics.js) script tag - */ - createAnalyticsScriptTag: () => void; - - /** - * @summary Allows for advanced configuration and definitions in univeral analytics only. This is a no-op when using classic analytics. - */ - set: (key: string, value: any, accountName?: string) => void; - - /** - * @summary Creates a new page view event - * @param {string} pageURL URL of page view - * @param {string} title Page Title - * @param {Object} dimensions Additional dimensions and metrics - */ - trackPage: (pageURL: string, title?: string, dimensions?: { [expr: string]: any }) => void; - - /** - * @summary Create a new event - */ - trackEvent: (category: string, action: string, label: string, value?: any, nonInteractionFlag?: boolean, dimensions?: { [expr: string]: any }) => void; - - trackException: (descrption: string, isFatal: boolean) => void; - - /** - * @summary While in offline mode, no calls to the ga function or pushes to the gaq array are made. - * This will queue all calls for later sending once offline mode is reset to false. - */ - offline: (offlineMode: boolean) => void; - } -} +// Type definitions for angular-google-analytics v1.1.0 +// Project: https://github.com/revolunet/angular-google-analytics +// Definitions by: Matt Wheatley +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module angular.google.analytics { + interface AnalyticsService { + /** + * @summary If logging is enabled then all outbound calls are accessible via an in-memory array. + * This is useful for troubleshooting and seeing the order of outbound calls with parameters. + */ + log: Array; + + /** + * @summary If in offline mode then all calls are queued to an in-memory array for future processing. + * All calls queued to the offlineQueue are not outbound calls yet and hence do not show up in the log. + */ + offlineQueue: Array; + + /** + * @summary Returns the current URL that would be sent if a `trackPage` call was made. + * @return {string} The URL + */ + getUrl: () => string; + + /** + * @summary Manually create classic analytics (ga.js) script tag + */ + createScriptTag: () => void; + + /** + * @summary Manually create universal analytics (analytics.js) script tag + */ + createAnalyticsScriptTag: () => void; + + /** + * @summary Allows for advanced configuration and definitions in univeral analytics only. This is a no-op when using classic analytics. + */ + set: (key: string, value: any, accountName?: string) => void; + + /** + * @summary Creates a new page view event + * @param {string} pageURL URL of page view + * @param {string} title Page Title + * @param {Object} dimensions Additional dimensions and metrics + */ + trackPage: (pageURL: string, title?: string, dimensions?: { [expr: string]: any }) => void; + + /** + * @summary Create a new event + */ + trackEvent: (category: string, action: string, label: string, value?: any, nonInteractionFlag?: boolean, dimensions?: { [expr: string]: any }) => void; + + trackException: (descrption: string, isFatal: boolean) => void; + + /** + * @summary While in offline mode, no calls to the ga function or pushes to the gaq array are made. + * This will queue all calls for later sending once offline mode is reset to false. + */ + offline: (offlineMode: boolean) => void; + } +} diff --git a/backbone/backbone-with-lodash-tests.ts b/backbone/backbone-with-lodash-tests.ts index dc7ebd2de..0138b2603 100644 --- a/backbone/backbone-with-lodash-tests.ts +++ b/backbone/backbone-with-lodash-tests.ts @@ -1,314 +1,314 @@ -/// +/// /// -/// - -function test_events() { - - var object = new Backbone.Events(); - object.on("alert", (eventName: string) => alert("Triggered " + eventName)); - - object.trigger("alert", "an event"); - - var onChange = () => alert('whatever'); - var context: any; - - object.off("change", onChange); - object.off("change"); - object.off(null, onChange); - object.off(null, null, context); - object.off(); -} - -class SettingDefaults extends Backbone.Model { - - // 'defaults' could be set in one of the following ways: - - defaults() { - return { - name: "Joe" - } - } - - constructor(attributes?: any, options?: any) { - this.defaults = { - name: "Joe" - } - // super has to come last - super(attributes, options); - } - - // or set it like this - initialize() { - this.defaults = { - name: "Joe" - } - - } - - // same patterns could be used for setting 'Router.routes' and 'View.events' -} - -class Sidebar extends Backbone.Model { - - promptColor() { - var cssColor = prompt("Please enter a CSS color:"); - this.set({ color: cssColor }); - } -} - -class Note extends Backbone.Model { - initialize() { } - author() { } - coordinates() { } - allowedToEdit(account: any) { - return true; - } -} - -class PrivateNote extends Note { - allowedToEdit(account: any) { - return account.owns(this); - } - - set(attributes: any, options?: any): Backbone.Model { - return Backbone.Model.prototype.set.call(this, attributes, options); - } -} - -function test_models() { - - var sidebar = new Sidebar(); - sidebar.on('change:color', (model: {}, color: string) => $('#sidebar').css({ background: color })); - sidebar.set({ color: 'white' }); - sidebar.promptColor(); - - ////////// - - var note = new PrivateNote(); - - note.get("title"); - - note.set({ title: "March 20", content: "In his eyes she eclipses..." }); - - note.set("title", "A Scandal in Bohemia"); -} - -class Employee extends Backbone.Model { - reports: EmployeeCollection; - - constructor(attributes?: any, options?: any) { - super(options); - this.reports = new EmployeeCollection(); - this.reports.url = '../api/employees/' + this.id + '/reports'; - } - - more() { - this.reports.reset(); - } -} - -class EmployeeCollection extends Backbone.Collection { - findByName(key: any) { } -} - -class Book extends Backbone.Model { - title: string; - author: string; - published: boolean; -} - -class Library extends Backbone.Collection { - // This model definition is here only to test type compatibility of the model, but it - // is not necessary in working code as it is automatically inferred through generics. - model: typeof Book; -} - -class Books extends Backbone.Collection { } - -function test_collection() { - - var books = new Books(); - - var book1: Book = new Book({ title: "Title 1", author: "Mike" }); - books.add(book1); - - // Objects can be added to collection by casting to model type. - // Compiler will check if object properties are valid for the cast. - // This gives better type checking than declaring an `any` overload. - books.add({ title: "Title 2", author: "Mikey" }); - - var model: Book = book1.collection.first(); - if (model !== book1) { - throw new Error("Error"); - } - - books.each(book => - book.get("title")); - - var titles = books.map(book => - book.get("title")); - - var publishedBooks = books.filter(book => - book.get("published") === true); - - var alphabetical = books.sortBy((book: Book): number => null); -} - -////////// - -Backbone.history.start(); - -module v1Changes { - module events { - function test_once() { - var model = new Employee; - model.once('invalid', () => { }, this); - model.once('invalid', () => { }); - } - - function test_listenTo() { - var model = new Employee; - var view = new Backbone.View(); - view.listenTo(model, 'invalid', () => { }); - } - - function test_listenToOnce() { - var model = new Employee; - var view = new Backbone.View(); - view.listenToOnce(model, 'invalid', () => { }); - } - - function test_stopListening() { - var model = new Employee; - var view = new Backbone.View(); - view.stopListening(model, 'invalid', () => { }); - view.stopListening(model, 'invalid'); - view.stopListening(model); - } - } - - module ModelAndCollection { - function test_url() { - Employee.prototype.url = () => '/employees'; - EmployeeCollection.prototype.url = () => '/employees'; - } - - function test_parse() { - var model = new Employee(); - model.parse('{}', {}); - var collection = new EmployeeCollection; - collection.parse('{}', {}); - } - - function test_toJSON() { - var model = new Employee(); - model.toJSON({}); - var collection = new EmployeeCollection; - collection.toJSON({}); - } - - function test_sync() { - var model = new Employee(); - model.sync(); - var collection = new EmployeeCollection; - collection.sync(); - } - } - - module Model { - function test_validationError() { - var model = new Employee; - if (model.validationError) { - console.log('has validation errors'); - } - } - - function test_fetch() { - var model = new Employee({ id: 1 }); - model.fetch({ - success: () => { }, - error: () => { } - }); - } - - function test_set() { - var model = new Employee; - model.set({ name: 'JoeDoe', age: 21 }, { validate: false }); - model.set('name', 'JoeDoes', { validate: false }); - } - - function test_destroy() { - var model = new Employee; - model.destroy({ - wait: true, - success: (m?, response?, options?) => { }, - error: (m?, jqxhr?, options?) => { } - }); - - model.destroy({ - success: (m?, response?, options?) => { }, - error: (m?, jqxhr?) => { } - }); - - model.destroy({ - success: () => { }, - error: (m?, jqxhr?) => { } - }); - } - - function test_save() { - var model = new Employee; - - model.save({ - name: 'Joe Doe', - age: 21 - }, - { - wait: true, - validate: false, - success: (m?, response?, options?) => { }, - error: (m?, jqxhr?, options?) => { } - }); - - model.save({ - name: 'Joe Doe', - age: 21 - }, - { - success: () => { }, - error: (m?, jqxhr?) => { } - }); - } - - function test_validate() { - var model = new Employee; - - model.validate({ name: 'JoeDoe', age: 21 }, { validateAge: false }) - } - } - - module Collection { - function test_fetch() { - var collection = new EmployeeCollection; - collection.fetch({ reset: true }); - } - - function test_create() { - var collection = new EmployeeCollection; - var model = new Employee; - - collection.create(model, { - validate: false - }); - } - } - - module Router { - function test_navigate() { - var router = new Backbone.Router; - - router.navigate('/employees', { trigger: true }); - router.navigate('/employees', true); - } - } -} +/// + +function test_events() { + + var object = new Backbone.Events(); + object.on("alert", (eventName: string) => alert("Triggered " + eventName)); + + object.trigger("alert", "an event"); + + var onChange = () => alert('whatever'); + var context: any; + + object.off("change", onChange); + object.off("change"); + object.off(null, onChange); + object.off(null, null, context); + object.off(); +} + +class SettingDefaults extends Backbone.Model { + + // 'defaults' could be set in one of the following ways: + + defaults() { + return { + name: "Joe" + } + } + + constructor(attributes?: any, options?: any) { + this.defaults = { + name: "Joe" + } + // super has to come last + super(attributes, options); + } + + // or set it like this + initialize() { + this.defaults = { + name: "Joe" + } + + } + + // same patterns could be used for setting 'Router.routes' and 'View.events' +} + +class Sidebar extends Backbone.Model { + + promptColor() { + var cssColor = prompt("Please enter a CSS color:"); + this.set({ color: cssColor }); + } +} + +class Note extends Backbone.Model { + initialize() { } + author() { } + coordinates() { } + allowedToEdit(account: any) { + return true; + } +} + +class PrivateNote extends Note { + allowedToEdit(account: any) { + return account.owns(this); + } + + set(attributes: any, options?: any): Backbone.Model { + return Backbone.Model.prototype.set.call(this, attributes, options); + } +} + +function test_models() { + + var sidebar = new Sidebar(); + sidebar.on('change:color', (model: {}, color: string) => $('#sidebar').css({ background: color })); + sidebar.set({ color: 'white' }); + sidebar.promptColor(); + + ////////// + + var note = new PrivateNote(); + + note.get("title"); + + note.set({ title: "March 20", content: "In his eyes she eclipses..." }); + + note.set("title", "A Scandal in Bohemia"); +} + +class Employee extends Backbone.Model { + reports: EmployeeCollection; + + constructor(attributes?: any, options?: any) { + super(options); + this.reports = new EmployeeCollection(); + this.reports.url = '../api/employees/' + this.id + '/reports'; + } + + more() { + this.reports.reset(); + } +} + +class EmployeeCollection extends Backbone.Collection { + findByName(key: any) { } +} + +class Book extends Backbone.Model { + title: string; + author: string; + published: boolean; +} + +class Library extends Backbone.Collection { + // This model definition is here only to test type compatibility of the model, but it + // is not necessary in working code as it is automatically inferred through generics. + model: typeof Book; +} + +class Books extends Backbone.Collection { } + +function test_collection() { + + var books = new Books(); + + var book1: Book = new Book({ title: "Title 1", author: "Mike" }); + books.add(book1); + + // Objects can be added to collection by casting to model type. + // Compiler will check if object properties are valid for the cast. + // This gives better type checking than declaring an `any` overload. + books.add({ title: "Title 2", author: "Mikey" }); + + var model: Book = book1.collection.first(); + if (model !== book1) { + throw new Error("Error"); + } + + books.each(book => + book.get("title")); + + var titles = books.map(book => + book.get("title")); + + var publishedBooks = books.filter(book => + book.get("published") === true); + + var alphabetical = books.sortBy((book: Book): number => null); +} + +////////// + +Backbone.history.start(); + +module v1Changes { + module events { + function test_once() { + var model = new Employee; + model.once('invalid', () => { }, this); + model.once('invalid', () => { }); + } + + function test_listenTo() { + var model = new Employee; + var view = new Backbone.View(); + view.listenTo(model, 'invalid', () => { }); + } + + function test_listenToOnce() { + var model = new Employee; + var view = new Backbone.View(); + view.listenToOnce(model, 'invalid', () => { }); + } + + function test_stopListening() { + var model = new Employee; + var view = new Backbone.View(); + view.stopListening(model, 'invalid', () => { }); + view.stopListening(model, 'invalid'); + view.stopListening(model); + } + } + + module ModelAndCollection { + function test_url() { + Employee.prototype.url = () => '/employees'; + EmployeeCollection.prototype.url = () => '/employees'; + } + + function test_parse() { + var model = new Employee(); + model.parse('{}', {}); + var collection = new EmployeeCollection; + collection.parse('{}', {}); + } + + function test_toJSON() { + var model = new Employee(); + model.toJSON({}); + var collection = new EmployeeCollection; + collection.toJSON({}); + } + + function test_sync() { + var model = new Employee(); + model.sync(); + var collection = new EmployeeCollection; + collection.sync(); + } + } + + module Model { + function test_validationError() { + var model = new Employee; + if (model.validationError) { + console.log('has validation errors'); + } + } + + function test_fetch() { + var model = new Employee({ id: 1 }); + model.fetch({ + success: () => { }, + error: () => { } + }); + } + + function test_set() { + var model = new Employee; + model.set({ name: 'JoeDoe', age: 21 }, { validate: false }); + model.set('name', 'JoeDoes', { validate: false }); + } + + function test_destroy() { + var model = new Employee; + model.destroy({ + wait: true, + success: (m?, response?, options?) => { }, + error: (m?, jqxhr?, options?) => { } + }); + + model.destroy({ + success: (m?, response?, options?) => { }, + error: (m?, jqxhr?) => { } + }); + + model.destroy({ + success: () => { }, + error: (m?, jqxhr?) => { } + }); + } + + function test_save() { + var model = new Employee; + + model.save({ + name: 'Joe Doe', + age: 21 + }, + { + wait: true, + validate: false, + success: (m?, response?, options?) => { }, + error: (m?, jqxhr?, options?) => { } + }); + + model.save({ + name: 'Joe Doe', + age: 21 + }, + { + success: () => { }, + error: (m?, jqxhr?) => { } + }); + } + + function test_validate() { + var model = new Employee; + + model.validate({ name: 'JoeDoe', age: 21 }, { validateAge: false }) + } + } + + module Collection { + function test_fetch() { + var collection = new EmployeeCollection; + collection.fetch({ reset: true }); + } + + function test_create() { + var collection = new EmployeeCollection; + var model = new Employee; + + collection.create(model, { + validate: false + }); + } + } + + module Router { + function test_navigate() { + var router = new Backbone.Router; + + router.navigate('/employees', { trigger: true }); + router.navigate('/employees', true); + } + } +} diff --git a/dynatable/dynatable-tests.ts b/dynatable/dynatable-tests.ts index 27834af2c..25f3fb257 100644 --- a/dynatable/dynatable-tests.ts +++ b/dynatable/dynatable-tests.ts @@ -1,229 +1,229 @@ -/// -/// - - -// Using the global setup option -// ============================= -$.dynatableSetup({ features: { pushState: false }, dataset: { perPageDefault: 5, perPageOptions: [2, 5, 10] } }); - - -// Without any settings (using all defaults) -// ========================================= -var dynatableWithDefaultSettings = $('#example-table').dynatable(); - - -// Building a config piece by piece -// ================================ -// JQueryDynatable.Features -var featsConfig: JQueryDynatable.Features = { - paginate: true, - sort: false, - pushState: true, - search: true, - recordCount: true, - perPageSelect: true -}; -var featsPartialConfig: JQueryDynatable.Features = { - search: false, - paginate: false -}; -// JQueryDynatable.Column -var col: JQueryDynatable.Column = { - index: 0, - label: 'Col from JS', - id: 'someColId', - attributeWriter: function myAttrWriter(data: any): any { - return data[this.id]; - }, - attributeReader: function myAttrReader(cell: Element, data: any): string { - return $(cell).html(); - }, - sorts: ['un', 'deux'], - hidden: false, - textAlign: 'right' -}; -// JQueryDynatable.Table -var tableConfig: JQueryDynatable.Table = { - defaultColumnIdStyle: 'underscore', - columns: [col], // Just for fun & testing... because will be reset by dynatable - headRowSelector: '.my-head-row-selector', - bodyRowSelector: 'tbody tr', - headRowClass: 'custom-head-row-class' -}; -var tablePartialConfig: JQueryDynatable.Table = { - defaultColumnIdStyle: 'trimDash' -}; -// JQueryDynatable.Inputs -var tableInputs: JQueryDynatable.Inputs = { - // Must match exactly target value, strict comparison! - queries: $('#countryFilter, #yearFilter'), - // I believe this setting is unused! - sorts: null, - multisort: ['shiftKey'], - // I believe this setting is unused! - page: null, - queryEvent: 'keyup', - recordCountTarget: $('#record-count'), - recordCountPlacement: 'after', - paginationLinkTarget: '#record-count', - paginationLinkPlacement: 'before', - paginationClass: 'paginationClass', - paginationLinkClass: 'paginationLinkClass', - paginationPrevClass: 'paginationPrevClass', - paginationNextClass: 'paginationNextClass', - paginationActiveClass: 'paginationActiveClass', - paginationDisabledClass: 'paginationDisabledClass', - paginationPrev: '< Prev.', - paginationNext: 'Next >', - paginationGap: [3, 1, 1, 3], - searchTarget: $('#record-count').get(0), - searchPlacement: 'after', - searchText: 'Search: ', - perPageTarget: '#record-count', - perPagePlacement: 'before', - perPageText: 'Display: ', - pageText: 'Pages: ', - recordCountPageBoundTemplate: '{pageLowerBound} t-o {pageUpperBound} o-f', - recordCountPageUnboundedTemplate: '{recordsShown} -of-', - recordCountTotalTemplate: '{recordsQueryCount} x {collectionName}', - recordCountFilteredTemplate: ' (found from {recordsTotal} total entries)', - recordCountText: 'Rendering', - recordCountTextTemplate: '{text} {pageTemplate} {totalTemplate} {filteredTemplate}', - recordCountTemplate: '!!{textTemplate}!!', - processingText: 'Working...' -}; -// JQueryDynatable.Dataset -var tableDataset: JQueryDynatable.Dataset = { - ajax: false, - ajaxUrl: null, - ajaxCache: null, - ajaxOnLoad: false, - ajaxMethod: 'GET', - ajaxDataType: 'json', - totalRecordCount: null, - queries: {}, - queryRecordCount: null, - page: null, - perPageDefault: 3, - perPageOptions: [2, 3, 10], - sorts: {}, - sortsKeys: [], - sortTypes: {}, - records: null -}; -// JQueryDynatable.Writers -var tableWriters: JQueryDynatable.Writers = { - _rowWriter: function exampleRowWriter(rowIndex, record, columns, cellWriter) { - var tr = ''; - // grab the record's attribute for each column - for (var i = 0, len = columns.length; i < len; i++) { - tr += cellWriter(columns[i], record); - } - return '' + tr + ''; - }, - _cellWriter: function exampleCellWriter(column, record) { - var html = column.attributeWriter(record), - td = '