From 489927d0b8a721ac6c19d1530e2ecb481d0ee063 Mon Sep 17 00:00:00 2001 From: Steve Ognibene Date: Sat, 17 May 2014 11:46:38 -0400 Subject: [PATCH 1/2] Work on getting extensions for base types working -New MicrosoftAjaxBaseTypeExtensions module -A few other minor enhancements and JS doc stuff --- microsoft-ajax/microsoft.ajax.d.ts | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/microsoft-ajax/microsoft.ajax.d.ts b/microsoft-ajax/microsoft.ajax.d.ts index 5e00ecd0ce..be05be47da 100644 --- a/microsoft-ajax/microsoft.ajax.d.ts +++ b/microsoft-ajax/microsoft.ajax.d.ts @@ -269,6 +269,46 @@ interface String { //#endregion +declare module MicrosoftAjaxBaseTypeExtensions { + /** + * Provides static functions that extend the built-in ECMAScript (JavaScript) Function type by including exception + * details and support for application-compilation modes (debug or release). + * @see {@link http://msdn.microsoft.com/en-us/library/dd409270(v=vs.100).aspx} + */ + interface Function { + /** + * Creates a new function. + * @param args A list of arguments the function accepts. + */ + new (...args: string[]): Function; + (...args: string[]): Function; + prototype: Function; + + /** + * Creates a delegate function that retains the context first used during an objects creation. + * @see {@link http://msdn.microsoft.com/en-us/library/dd393582(v=vs.100).aspx } + */ + createCallback(method: Function, ...context: any[]): Function; + /** + * Creates a callback function that retains the parameter initially used during an object's creation. + * @see {@link http://msdn.microsoft.com/en-us/library/dd409287(v=vs.100).aspx } + */ + createDelegate(instance: any, method: Function): Function; + + /** + * A function that does nothing. + * @see {@link http://msdn.microsoft.com/en-us/library/dd393667(v=vs.100).aspx } + */ + emptyMethod(): Function; + + /** + * Validates the parameters to a method are as expected. + * @see {@link http://msdn.microsoft.com/en-us/library/dd393712(v=vs.100).aspx } + */ + validateParameters(parameters: any, expectedParameters: Object[], validateParameterCount?: boolean): any; + } +} + //#region ASP.NET Types /** @@ -489,6 +529,15 @@ declare function $create(type: Type, properties?: any, events?: any, references? */ declare function $find(id: string, parent?: Sys.Component): Sys.Component; +/** +* Returns the specified Component object. This member is static and can be invoked without creating an instance of the class. +* @see {@link http://msdn.microsoft.com/en-us/library/bb397441(v=vs.100).aspx} +* @param id A string that contains the ID of the component to find. +* @param parent (Optional) The component or element that contains the component to find. +* @return A Component object that contains the component requested by ID, if found; otherwise, null. +*/ +declare function $find(id: string, parent?: HTMLElement): Sys.Component; + /* * Provides a shortcut to the addHandler method of the Sys.UI.DomEvent class. This member is static and can be invoked without creating an instance of the class. * @see {@link http://msdn.microsoft.com/en-us/library/bb311019(v=vs.100).aspx} @@ -589,6 +638,16 @@ declare module Sys { */ remove_navigate(handler: Function): void; + /** + * Raised before all objects in the client application are disposed, typically when the DOM window.unload event is raised. + */ + add_unload(handler: Function): void; + + /** + * Raised before all objects in the client application are disposed, typically when the DOM window.unload event is raised. + */ + remove_unload(handler: Function): void; + //#endregion //#region Methods @@ -635,6 +694,11 @@ declare module Sys { */ findComponent(id: string, parent?: Sys.Component): Sys.Component; /** + * Returns the specified Component object. This member is static and can be invoked without creating an instance of the class. + * @return A Component object that contains the component requested by ID, if found; otherwise, null. + */ + findComponent(id: string, parent?: HTMLElement): Sys.Component; + /** * Returns an array of all components that have been registered with the application by using the addComponent method. This member is static and can be invoked without creating an instance of the class. */ getComponents(): Sys.Component[]; @@ -760,6 +824,16 @@ declare module Sys { */ remove_disposing(handler: Function): void; + /** + * Gets the ID of the current Component object. + */ + get_id(): string; + /** + * Sets the ID of the current Component object. + * @param value A string that contains the ID of the component. + */ + set_id(value: string): void; + /** * Raised when the raisePropertyChanged method of the current Component object is called. */ @@ -1523,6 +1597,11 @@ declare module Sys { /** * A static object of type EventArgs that is used as a convenient way to specify an empty EventArgs instance. */ + static Empty: EventArgs; + + /** + * An object of type EventArgs that is used as a convenient way to specify an empty EventArgs instance. + */ Empty: EventArgs; //#endregion From d87ed0be2e0de267e2f5a1c3eda34cd2de1e242f Mon Sep 17 00:00:00 2001 From: Steve Ognibene Date: Sat, 17 May 2014 11:47:40 -0400 Subject: [PATCH 2/2] Refactoring to ensure each set of tests is in its own function removed //#region declarations as the functions provide the same functionality (for compilation testing purposes anyway) plus eliminate scope conflicts. --- microsoft-ajax/microsoft.ajax-tests.ts | 558 ++++++++++++++++--------- 1 file changed, 349 insertions(+), 209 deletions(-) diff --git a/microsoft-ajax/microsoft.ajax-tests.ts b/microsoft-ajax/microsoft.ajax-tests.ts index 15b52ffcf2..9e54d0c147 100644 --- a/microsoft-ajax/microsoft.ajax-tests.ts +++ b/microsoft-ajax/microsoft.ajax-tests.ts @@ -4,231 +4,371 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped /// -//#region Global Namespace Tests +function GlobalNamespace_Tests() { -var arrayVar = new Array("Saturn", "Mars", "Jupiter"); + var arrayVar = new Array("Saturn", "Mars", "Jupiter"); -//#endregion - -//#region Global Shortcut Methods - -$addHandler($get("Button1"), "click", () => { }); -$addHandlers($get("Button1"), {}); -$removeHandler($get("Button1"), "click", () => { }); -$find('MyComponent'); -$find('MyComponent', $find('#test')); - -//#endregion - -//#region Sys.Application Tests - -var component = new Sys.Component(); -var element = document.getElementById("#test"); -var id = "#test"; -var propertyName = "test"; -var parent = component; -var registerObject = new Object(); - -function loadHandler() { } -function initHandler() { } -function navigateHandler() { } -function unloadHandler() { } - -Sys.Application.add_load(loadHandler); -Sys.Application.remove_load(loadHandler); -Sys.Application.add_init(initHandler); -Sys.Application.remove_init(initHandler); -Sys.Application.add_navigate(navigateHandler); -Sys.Application.remove_navigate(navigateHandler); -Sys.Application.add_unload(unloadHandler); -Sys.Application.remove_unload(unloadHandler); -Sys.Application.addComponent(component); -Sys.Application.addHistoryPoint("state", "title"); -Sys.Application.beginCreateComponents(); -Sys.Application.beginUpdate(); -Sys.Application.dispose(); -Sys.Application.disposeElement(element, false); -Sys.Application.endCreateComponents(); -Sys.Application.endUpdate(); -Sys.Application.findComponent(id, parent); -Sys.Application.findComponent(id); -$find(id, parent); - -var componentArray = Sys.Application.getComponents(); -for (var i = 0; i < componentArray.length; i++) { - var id = componentArray[i].get_id(); + $addHandler($get("Button1"), "click", () => { }); + $addHandlers($get("Button1"), {}); + $removeHandler($get("Button1"), "click", () => { }); + $find('MyComponent'); + $find('MyComponent', $find('#test')); } -Sys.Application.initialize(); -Sys.Application.notifyScriptLoaded(); -Sys.Application.raiseLoad(); -Sys.Application.raisePropertyChanged(propertyName); -Sys.Application.registerDisposableObject(registerObject); -Sys.Application.removeComponent(component); -Sys.Application.unregisterDisposableObject(registerObject); -Sys.Application.endUpdate(); -Sys.Application.get_enableHistory(); -Sys.Application.set_enableHistory(true); -Sys.Application.get_isCreatingComponents(); -Sys.Application.get_isDisposing(); - -//#endregion - -//#region Sys.ApplicationLoadEventArgs Tests - -var a = new Sys.ApplicationLoadEventArgs(new Array(), true); - -var components = a.get_components(); -var isPartialReload = a.get_isPartialLoad(); - -//#endregion - -//#region Sys.Browser Tests - -var browser = Sys.Browser(); - -//#endregion - -//#region Sys.CancelEventArgs Tests - -var args = new Sys.CancelEventArgs(); - -var divElem = 'AlertDiv'; -var messageElem = 'AlertMessage'; - -Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus); - -function CheckStatus(sender, args) { - - var prm = Sys.WebForms.PageRequestManager.getInstance(); - - if (prm.get_isInAsyncPostBack() && args.get_postBackElement().id == 'CancelRefresh') { - prm.abortPostBack(); +function BaseClassExtensions_Function_Tests() { + + /** Sample code from http://msdn.microsoft.com/en-us/library/dd409287(v=vs.100).aspx */ + var createDelegateTest = function () { + var context = ""; + var method: MicrosoftAjaxBaseTypeExtensions.Function; + var a = (Function).createCallback(method, context); } - else if (prm.get_isInAsyncPostBack() && args.get_postBackElement().id == 'RefreshButton') { - - args.set_cancel(true); - ActivateAlertDiv('visible', 'Still working on previous request.'); + + /** Sample code from http://msdn.microsoft.com/en-us/library/dd393582(v=vs.100).aspx */ + var createDelegateTest = function () { + var instance = this; + var method: MicrosoftAjaxBaseTypeExtensions.Function; + var a = (Function).createDelegate(instance, method); } - else if (!prm.get_isInAsyncPostBack() && args.get_postBackElement().id == 'RefreshButton') { - ActivateAlertDiv('visible', 'Processing....'); + + /** Sample code from http://msdn.microsoft.com/en-us/library/dd393712(v=vs.100).aspx */ + var validateParametersTest = function () { + var arguments = ['test1', 'test2']; + var insert = function Array$insert(array, index, item) { + var e = (Function).validateParameters(arguments, [ + { name: "array", type: Array, elementMayBeNull: true }, + { name: "index", mayBeNull: true }, + { name: "item", mayBeNull: true } + ]); + if (e) throw e; + } + }; +} + + +function Sys_Application_Tests() { + + var component = new Sys.Component(); + var element = document.getElementById("#test"); + var id = "#test"; + var propertyName = "test"; + var registerObject = new Object(); + + function loadHandler() { } + function initHandler() { } + function navigateHandler() { } + function unloadHandler() { } + + Sys.Application.add_load(loadHandler); + Sys.Application.remove_load(loadHandler); + Sys.Application.add_init(initHandler); + Sys.Application.remove_init(initHandler); + Sys.Application.add_navigate(navigateHandler); + Sys.Application.remove_navigate(navigateHandler); + Sys.Application.add_unload(unloadHandler); + Sys.Application.remove_unload(unloadHandler); + Sys.Application.addComponent(component); + Sys.Application.addHistoryPoint("state", "title"); + Sys.Application.beginCreateComponents(); + Sys.Application.beginUpdate(); + Sys.Application.dispose(); + Sys.Application.disposeElement(element, false); + Sys.Application.endCreateComponents(); + Sys.Application.endUpdate(); + Sys.Application.findComponent(id, element); + Sys.Application.findComponent(id, component); + Sys.Application.findComponent(id); + $find(id, element); + + var componentArray = Sys.Application.getComponents(); + for (var i = 0; i < componentArray.length; i++) { + var cid = componentArray[i].get_id(); + } + + Sys.Application.initialize(); + Sys.Application.notifyScriptLoaded(); + Sys.Application.raiseLoad(); + Sys.Application.raisePropertyChanged(propertyName); + Sys.Application.registerDisposableObject(registerObject); + Sys.Application.removeComponent(component); + Sys.Application.unregisterDisposableObject(registerObject); + Sys.Application.endUpdate(); + Sys.Application.get_enableHistory(); + Sys.Application.set_enableHistory(true); + Sys.Application.get_isCreatingComponents(); + Sys.Application.get_isDisposing(); +} + + +function Sys_Application_LoadEventArgs_Tests() { + var a = new Sys.ApplicationLoadEventArgs(new Array(), true); + + var components = a.get_components(); + var isPartialReload = a.get_isPartialLoad(); +} + + +function Sys_Browser_Tests() { + var browser = Sys.Browser(); +} + + + +function Sys_CancelEventArgs_Tests() { + + var args = new Sys.CancelEventArgs(); + + var divElem = 'AlertDiv'; + var messageElem = 'AlertMessage'; + + Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(CheckStatus); + + var CheckStatus = function(sender, args) { + + var prm = Sys.WebForms.PageRequestManager.getInstance(); + + if (prm.get_isInAsyncPostBack() && args.get_postBackElement().id == 'CancelRefresh') { + prm.abortPostBack(); + } + else if (prm.get_isInAsyncPostBack() && args.get_postBackElement().id == 'RefreshButton') { + + args.set_cancel(true); + ActivateAlertDiv('visible', 'Still working on previous request.'); + } + else if (!prm.get_isInAsyncPostBack() && args.get_postBackElement().id == 'RefreshButton') { + ActivateAlertDiv('visible', 'Processing....'); + } + } + + var ActivateAlertDiv = function(visString, msg) { + var adiv = $get(divElem); + var aspan = $get(messageElem); + adiv.style.visibility = visString; + aspan.innerHTML = msg; } } -function ActivateAlertDiv(visString, msg) { - var adiv = $get(divElem); - var aspan = $get(messageElem); - adiv.style.visibility = visString; - aspan.innerHTML = msg; + +function Sys_CollectionChange_Tests() { + var action = Sys.NotifyCollectionChangedAction.add; + var newItems = []; + var newStartingIndex = 1; + var oldItems = []; + var oldStartingIndex = 2; + + var MyCChg = new Sys.CollectionChange(action, newItems, newStartingIndex, oldItems, oldStartingIndex); + + action = MyCChg.action; + newItems = MyCChg.newItems; + newStartingIndex = MyCChg.newStartingIndex; + oldItems = MyCChg.oldItems; + oldStartingIndex = MyCChg.oldStartingIndex; } -//#endregion -//#region Sys.CollectionChange Tests - -var action = Sys.NotifyCollectionChangedAction.add; -var newItems = []; -var newStartingIndex = 1; -var oldItems = []; -var oldStartingIndex = 2; - -var MyCChg = new Sys.CollectionChange(action, newItems, newStartingIndex, oldItems, oldStartingIndex); - -action = MyCChg.action; -newItems = MyCChg.newItems; -newStartingIndex = MyCChg.newStartingIndex; -oldItems = MyCChg.oldItems; -oldStartingIndex = MyCChg.oldStartingIndex; - -//#endregion - -//#region Sys.CommandEventArg Tests - -var commandName = "command name"; -var commandArgument = "command argument"; -var commandSource = "command source"; -var argsObj = new Sys.CommandEventArgs(commandName, commandArgument, commandSource); -var empty = argsObj.Empty; -commandName = argsObj.get_commandName(); -commandArgument = argsObj.get_commandArgument(); - -//#endregion - -//#region Sys.Component Tests - -var aComponent = new Sys.Component(); - -aComponent.add_disposing(() => { }); -aComponent.remove_disposing(() => { }); - -aComponent.add_propertyChanged(() => { }); -aComponent.remove_propertyChanged(() => { }); - -aComponent.beginUpdate(); - -aComponent.create(type, properties, events, references, element); - -aComponent.dispose(); - -aComponent.endUpdate(); - -aComponent.initialize(); - -aComponent.raisePropertyChanged("propertyName"); - -aComponent.updated(); - -//#endregion - -//#region Sys.CultureInfo Tests - -var currentCultureInfoObj = Sys.CultureInfo.CurrentCulture; -var dtfCCObject = currentCultureInfoObj.dateTimeFormat; -var invariantCultureInfoObj = Sys.CultureInfo.InvariantCulture; -var dtfICObject = invariantCultureInfoObj.dateTimeFormat; - -var newCulture = new Sys.CultureInfo("name", "numberFormat", "dateTimeFormat"); - -var format = newCulture.dateTimeFormat; -var name = newCulture.name; -var numberFormat = newCulture.numberFormat; - -//#endregion - -//#region ASP.NET Types Tests - -Type.registerNamespace("Samples"); - -var Samples; -Samples.A = function () { } -var a = Samples.A; -a.registerClass('Samples.A'); - - -Samples.B = function () { } -var b = Samples.B; -b.registerClass('Samples.B'); - -Samples.C = function () { - var c = Samples.C; - c.initializeBase(this); +function Sys_CommandEventArg_Tests() { + var commandName = "command name"; + var commandArgument = "command argument"; + var commandSource = "command source"; + var argsObj = new Sys.CommandEventArgs(commandName, commandArgument, commandSource); + var empty = argsObj.Empty; + commandName = argsObj.get_commandName(); + commandArgument = argsObj.get_commandArgument(); } -Samples.C.registerClass('Samples.C', Samples.A, Samples.B); -var isDerived; -isDerived = Samples.B.inheritsFrom(Samples.A); -// Output: "false". -alert(isDerived); +function Sys_Component_Tests() { + var aComponent = new Sys.Component(); + var properties: any; + var events: any; + var references: any; + var element: HTMLElement; + var handler: Function; + var MyControl = new Type; + + aComponent.add_disposing(() => { }); + aComponent.remove_disposing(() => { }); -isDerived = Samples.C.inheritsFrom(Samples.A); -// Output: "true". -alert(isDerived); + aComponent.add_propertyChanged(() => { }); + aComponent.remove_propertyChanged(() => { }); -var implementsInterface; -implementsInterface = Samples.C.implementsInterface(Samples.B); -// Output: "true". -alert(implementsInterface); + aComponent.beginUpdate(); -//#endregion + $create(MyControl, { id: 'c1', visible: true }, { click: handler }, null, $get('button1')); + aComponent.dispose(); + + aComponent.endUpdate(); + + aComponent.initialize(); + + aComponent.raisePropertyChanged("propertyName"); + + aComponent.updated(); +} + +function Sys_CultureInfo_Tests() { + var currentCultureInfoObj = Sys.CultureInfo.CurrentCulture; + var dtfCCObject = currentCultureInfoObj.dateTimeFormat; + var invariantCultureInfoObj = Sys.CultureInfo.InvariantCulture; + var dtfICObject = invariantCultureInfoObj.dateTimeFormat; + + var newCulture = new Sys.CultureInfo("name", "numberFormat", "dateTimeFormat"); + + var format = newCulture.dateTimeFormat; + var name = newCulture.name; + var numberFormat = newCulture.numberFormat; +} + + +function AspNetTypes_Tests() { + Type.registerNamespace("Samples"); + + + var Samples; + Samples.A = function () { }; + var a = Samples.A; + a.registerClass('Samples.A'); + + + Samples.B = function () { }; + var b = Samples.B; + b.registerClass('Samples.B'); + + Samples.C = function () { + var c = Samples.C; + c.initializeBase(this); + }; + + Samples.C.registerClass('Samples.C', Samples.A, Samples.B); + + var isDerived; + isDerived = Samples.B.inheritsFrom(Samples.A); + // Output: "false". + alert(isDerived); + + isDerived = Samples.C.inheritsFrom(Samples.A); + // Output: "true". + alert(isDerived); + + var implementsInterface; + implementsInterface = Samples.C.implementsInterface(Samples.B); + // Output: "true". + alert(implementsInterface); +} + + + +/** Sample code from http://msdn.microsoft.com/en-us/library/bb386520(v=vs.100).aspx */ +function CreatingCustomNonVisualClientComponentsTests() { + var Demo: any; + Type.registerNamespace("Demo"); + + Demo.Timer = function () { + Demo.Timer.initializeBase(this); + + this._interval = 1000; + this._enabled = false; + this._timer = null; + } + + Demo.Timer.prototype = { + // OK to declare value types in the prototype + + + get_interval: function () { + /// Interval in milliseconds + return this._interval; + }, + set_interval: function (value) { + if (this._interval !== value) { + this._interval = value; + this.raisePropertyChanged('interval'); + + if (!this.get_isUpdating() && (this._timer !== null)) { + this._restartTimer(); + } + } + }, + + get_enabled: function () { + /// True if timer is enabled, false if disabled. + return this._enabled; + }, + set_enabled: function (value) { + if (value !== this.get_enabled()) { + this._enabled = value; + this.raisePropertyChanged('enabled'); + if (!this.get_isUpdating()) { + if (value) { + this._startTimer(); + } + else { + this._stopTimer(); + } + } + } + }, + + // events + add_tick: function (handler) { + /// Adds a event handler for the tick event. + /// The handler to add to the event. + this.get_events().addHandler("tick", handler); + }, + remove_tick: function (handler) { + /// Removes a event handler for the tick event. + /// The handler to remove from the event. + this.get_events().removeHandler("tick", handler); + }, + + dispose: function () { + // call set_enabled so the property changed event fires, for potentially attached listeners. + this.set_enabled(false); + // make sure it stopped so we aren't called after disposal + this._stopTimer(); + // be sure to call base.dispose() + Demo.Timer.callBaseMethod(this, 'dispose'); + }, + + updated: function () { + Demo.Timer.callBaseMethod(this, 'updated'); + // called after batch updates, this.beginUpdate(), this.endUpdate(). + if (this._enabled) { + this._restartTimer(); + } + }, + + _timerCallback: function () { + var handler = this.get_events().getHandler("tick"); + if (handler) { + handler(this, Sys.EventArgs.Empty); + } + }, + + _restartTimer: function () { + this._stopTimer(); + this._startTimer(); + }, + + _startTimer: function () { + // save timer cookie for removal later + this._timer = window.setInterval((Function).createDelegate(this, this._timerCallback), this._interval); + }, + + _stopTimer: function () { + if (this._timer) { + window.clearInterval(this._timer); + this._timer = null; + } + } + } + + Demo.Timer.registerClass('Demo.Timer', Sys.Component); + + // Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler + // invoke Sys.Application.notifyScriptLoaded to notify ScriptManager + // that this is the end of the script. + if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); + +} \ No newline at end of file