/// /// /* © Microsoft. All rights reserved. This library is supported for use in Windows Tailored Apps only. Build: 6.2.8093.0 Version: 0.5 */ (function (global, undefined) { function initializeProperties(target, members) { var keys = Object.keys(members); var properties; var i, len; for (i = 0, len = keys.length; i < len; i++) { var key = keys[i]; var enumerable = key.charCodeAt(0) !== /*_*/95; var member = members[key]; if (member && typeof member === 'object') { if (member.value !== undefined || typeof member.get === 'function' || typeof member.set === 'function') { if (member.enumerable === undefined) { member.enumerable = enumerable; } properties = properties || {}; properties[key] = member; continue; } } if (!enumerable) { properties = properties || {}; properties[key] = { value: member, enumerable: enumerable, configurable: true, writable: true } continue; } target[key] = member; } if (properties) { Object.defineProperties(target, properties); } } (function (rootNamespace) { // Create the rootNamespace in the global namespace if (!global[rootNamespace]) { global[rootNamespace] = Object.create(Object.prototype); } // Cache the rootNamespace we just created in a local variable var _rootNamespace = global[rootNamespace]; if (!_rootNamespace.Namespace) { _rootNamespace.Namespace = Object.create(Object.prototype); } function defineWithParent(parentNamespace, name, members) { /// /// Defines a new namespace with the specified name, under the specified parent namespace. /// /// /// The parent namespace which will contain the new namespace. /// /// /// Name of the new namespace. /// /// /// Members in the new namespace. /// /// /// The newly defined namespace. /// var currentNamespace = parentNamespace, namespaceFragments = name.split("."); for (var i = 0, len = namespaceFragments.length; i < len; i++) { var namespaceName = namespaceFragments[i]; if (!currentNamespace[namespaceName]) { Object.defineProperty(currentNamespace, namespaceName, { value: {}, writable: false, enumerable: true, configurable: true } ); } currentNamespace = currentNamespace[namespaceName]; } if (members) { initializeProperties(currentNamespace, members); } return currentNamespace; }; function define(name, members) { /// /// Defines a new namespace with the specified name. /// /// /// Name of the namespace. This could be a dot-separated nested name. /// /// /// Members in the new namespace. /// /// /// The newly defined namespace. /// return defineWithParent(global, name, members); } // Establish members of the "WinJS.Namespace" namespace Object.defineProperties(_rootNamespace.Namespace, { defineWithParent: { value: defineWithParent, writable: true, enumerable: true }, define: { value: define, writable: true, enumerable: true } }); })("WinJS"); (function (WinJS) { function define(constructor, instanceMembers, staticMembers) { /// /// Defines a class using the given constructor and with the specified instance members. /// /// /// A constructor function that will be used to instantiate this class. /// /// /// The set of instance fields, properties and methods to be made available on the class. /// /// /// The set of static fields, properties and methods to be made available on the class. /// /// /// The newly defined class. /// constructor = constructor || function () { }; if (instanceMembers) { initializeProperties(constructor.prototype, instanceMembers); } if (staticMembers) { initializeProperties(constructor, staticMembers); } return constructor; } function derive(baseClass, constructor, instanceMembers, staticMembers) { /// /// Uses prototypal inheritance to create a sub-class based on the supplied baseClass parameter. /// /// /// The class to inherit from. /// /// /// A constructor function that will be used to instantiate this class. /// /// /// The set of instance fields, properties and methods to be made available on the class. /// /// /// The set of static fields, properties and methods to be made available on the class. /// /// /// The newly defined class. /// if (baseClass) { constructor = constructor || function () { }; var basePrototype = baseClass.prototype; constructor.prototype = Object.create(basePrototype); Object.defineProperty(constructor.prototype, "_super", { value: basePrototype }); Object.defineProperty(constructor.prototype, "constructor", { value: constructor }); if (instanceMembers) { initializeProperties(constructor.prototype, instanceMembers); } if (staticMembers) { initializeProperties(constructor, staticMembers); } return constructor; } else { return define(constructor, instanceMembers, staticMembers); } } function mix(constructor) { /// /// Defines a class using the given constructor and the union of the set of instance members /// specified by all the mixin objects. The mixin parameter list can be of variable length. /// /// /// A constructor function that will be used to instantiate this class. /// /// /// The newly defined class. /// constructor = constructor || function () { }; var i, len; for (i = 0, len = arguments.length; i < len; i++) { initializeProperties(constructor.prototype, arguments[i]); } return constructor; } // Establish members of "WinJS.Class" namespace WinJS.Namespace.define("WinJS.Class", { define: define, derive: derive, mix: mix }); })(WinJS); })(this); (function (global, WinJS) { // Establish members of "WinJS.Utilities" namespace WinJS.Namespace.defineWithParent(WinJS, "Utilities", { getMember: function (name, root) { /// /// Gets the leaf-level type or namespace as specified by the name. /// /// /// The name of the member. /// /// /// The root to start in, defaults to the global object. /// /// /// The leaf-level type of namespace inside the specified parent namespace. /// root = root || global; if (!name) { return null; } return name.split(".").reduce(function (currentNamespace, name) { if (currentNamespace) { return currentNamespace[name]; } return null; }, root); }, ready: function(callback, async) { /// /// Ensures the given function only executes after the DOMContentLoaded event has fired /// for the current page. /// /// Promise which completes after DOMContentLoaded has fired. /// /// A JS Function to execute after DOMContentLoaded has fired. /// /// /// If true then the callback should be asynchronously executed. /// return new WinJS.Promise(function (c, e) { function complete() { if (callback) { try { callback(); c(); } catch (err) { e(err); } } else { c(); } }; var readyState = this.testReadyState || document.readyState; if(readyState === "complete" || document.body !== null) { if(async) { msQueueCallback(complete); } else { complete(); } } else { window.addEventListener("DOMContentLoaded", complete, false); } }); } }); WinJS.Namespace.define("WinJS", { validation: { value: false, writable: true, enumerable: true, configurable: false } }); })(this, WinJS); // This feature is intended to be replaced by a native implementation in IE10 // (function (global) { if (global.msSetImmediate) { global.msQueueCallback = global.msSetImmediate; } else { // frameLen would ideally be snapped to the vblank of the monitor // var frameLen = 16; // We reserve 30% of each frame for IE to render, ideally this would // be tuned based on actual render time // var userFrameTime = frameLen * .7; var _work = []; var dispatchFrame = function () { var start = new Date(); var end; var q = _work; while (q.length > 0) { q.shift()(); end = new Date(); var elapsed = end - start; if (elapsed >= userFrameTime) { break; } } }; // name is intentionally obscure, we want most people // to use the promise implementation until we get a // final design from IE. // global.msQueueCallback = function (f) { _work.push(f); }; setInterval(dispatchFrame, frameLen); } })(this); (function (WinJS, undefined) { function createEventProperties(events) { /// /// Creates a object which has properties for each name passed to the function. /// The names are prefixed with 'on'. /// /// /// Variable argument list of property names. /// /// /// Object which has properties for each event name passed to the function. /// var props = {}; for (var i = 0, len = arguments.length; i < len; i++) { (function (name) { var wrapperFunction; var userHandler; props["on" + name] = { get: function () { return userHandler; }, set: function (handler) { if (handler) { if (!wrapperFunction) { wrapperFunction = function (evt) { userHandler(evt); }; this.addEventListener(name, wrapperFunction, false); } userHandler = handler; } else { this.removeEventListener(name, wrapperFunction, false); wrapperFunction = null; userHandler = null; } }, enumerable: true } })(arguments[i]); } return props; } var EventMixinEvent = WinJS.Class.define( function (type, detail, target) { this.detail = detail; this.target = target; this.timeStamp = Date.now(); this.type = type; }, { bubbles: { value: false, writable: false }, cancelable: { value: false, writable: false }, currentTarget: { get: function () { return this.target; } }, defaultPrevented: { get: function () { return this._preventDefaultCalled; } }, trusted: { value: false, writable: false }, eventPhase: { value: 0, writable: false }, target: null, timeStamp: null, type: null, preventDefault: function () { this._preventDefaultCalled = true; }, stopImmediatePropagation: function () { this._stopImmediatePropagationCalled = true; }, stopPropagation: function () { } } ); var eventMixin = { _listeners: null, addEventListener: function (type, listener, useCapture) { /// /// Adds an event listener to the control. /// /// /// The type (name) of the event. /// /// /// The listener to invoke when the event gets raised. /// /// /// Specifies whether or not to initiate capture. /// useCapture = useCapture || false; this._listeners = this._listeners || {}; var eventListeners = (this._listeners[type] = this._listeners[type] || []); for (var i = 0, len = eventListeners.length; i < len; i++) { var l = eventListeners[i]; if (l.useCapture === useCapture && l.listener === listener) { return; } } eventListeners.push({ listener: listener, useCapture: useCapture}); }, dispatchEvent: function (type, details) { /// /// Raises an event of the specified type and with additional properties. /// /// /// The type (name) of the event. /// /// /// The set of additional properties to be attached to the event object when the event is raised. /// /// /// Boolean indicating whether preventDefault was called on the event. /// var eventValue = new EventMixinEvent(type, details, this); var listeners = this._listeners && this._listeners[type]; if (listeners) { // Need to copy the array to protect against people unregistering while we are dispatching listeners = listeners.slice(0, listeners.length); for (var i = 0, len = listeners.length; i < len && !eventValue._stopImmediatePropagationCalled; i++) { listeners[i].listener(eventValue); } } return eventValue.defaultPrevented || false; }, removeEventListener: function (type, listener, useCapture) { /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove from the invoke list. /// /// /// Specifies whether or not to initiate capture. /// useCapture = useCapture || false; var listeners = this._listeners && this._listeners[type]; if (listeners) { for (var i = 0, len = listeners.length; i < len; i++) { var l = listeners[i]; if (l.listener === listener && l.useCapture === useCapture) { listeners.splice(i, 1); if (listeners.length === 0) { delete this._listeners[type]; } // Only want to remove one element for each call to removeEventListener break; } } } } }; WinJS.Namespace.defineWithParent(WinJS, "Utilities", { createEventProperties: createEventProperties, eventMixin: eventMixin }); })(WinJS); (function (global, WinJS) { var ListenerType = WinJS.Class.mix(WinJS.Class.define(null), WinJS.Utilities.eventMixin); var listeners = new ListenerType(); var errorET = "error"; function isPromise(value) { return typeof value === "object" && value && typeof value.then === "function"; } // Helper to notify listeners on successful fulfillment of a promise // function notifySuccess(notifier, listeners, value) { for (var i = 0, len = listeners.length; i < len; i++) { var listener = listeners[i]; // // NOTE: promise here is always an instance of ThenPromise and we are OK // reaching into its private members _complete and _error in order to // notify it of fulfillment. // var promise = listener.promise; var onComplete = listener.onComplete; try { if (onComplete) { // If we have a onComplete handler then the fulfillment value of the // ThenPromise is the result of calling the onComplete handler. // promise._complete(onComplete(value)); } else { // If we do not have an onComplete handler the ThenPromise is fulfilled // by the current value. // promise._complete(value); } } catch (exception) { // If an error occurs while executing the users onComplete handler then the // ThenPromise is itself in error with the exception value as its fulfillment // value. // promise._exception(exception); } } } // Helper to notify listeners on error fulfillment of a promise // function notifyError(notifier, listeners, value) { for (var i = 0, len = listeners.length; i < len; i++) { var listener = listeners[i]; // // NOTE: promise here is always an instance of ThenPromise and we are OK // reaching into its private members _complete and _error in order to // notify it of fulfillment. // var promise = listener.promise; var onError = listener.onError; try { if (onError) { // If we have a onError handler then the fulfillment value of the // ThenPromise is the result of calling the onError handler // promise._handledError(value, notifier, onError); } else { // If we do not have an onError handler the ThenPromise is in error // and is fulfilled by the current error value. // promise._chainedError(value, notifier); } } catch (exception) { // If an exception occurs while executing the users onError handler then // the ThenPromise is in error with the exception value as its fulfillment // value. // promise._exception(exception); } } } // Global error counter, for each error which enters the system we increment this once and then // the error number travels with the error as it traverses the tree of potential handlers. // var error_number = 0; var state_working = 0; var state_waiting = 1; var state_waiting_canceled = 2; var state_fulfilled_error = 3; var state_fulfilled_success = 4; var state_fulfilled_min = state_fulfilled_error; function generateErrorHandler(onerror) { return function (errorValue, context) { if (this._state !== state_working) { return; } this._value = errorValue; this._state = state_fulfilled_error; if (listeners._listeners && listeners._listeners[errorET]) { var details = onerror(this, errorValue, context); WinJS.Promise.dispatchEvent(errorET, details); } this._notify(); // We have now entered a fulfilled state, cleanup anything which was // around as part of being async (async operation object, back pointer // used for cancellation, etc). // this._cleanup(); } } function onerrorForChainedError(promise, errorValue, context) { var exception = context._isException; var errorId = context._errorId; promise._isException = exception || false; promise._errorId = errorId; return { exception: exception ? errorValue : null, error: exception ? null : errorValue, promise: promise, id: errorId, parent: context } } function onerrorForError(promise, errorValue) { promise._isException = false; promise._errorId = ++error_number; return { error: errorValue, promise: promise, id: promise._errorId }; } function onerrorForException(promise, exceptionValue) { promise._isException = true; promise._errorId = ++error_number; return { exception: exceptionValue, promise: promise, id: promise._errorId }; } var PromiseBase = WinJS.Class.define( null, { _listeners: null, _state: state_working, _value: null, _cancel: function () { switch (this._state) { case state_fulfilled_error: case state_fulfilled_success: case state_waiting_canceled: return; case state_working: this._error(new Error("Canceled")); break; case state_waiting: this._state = state_waiting_canceled; // If this is waiting on a completed value which is a promise then // request that value to cancel itself. // if (typeof this._value.cancel === "function") { this._value.cancel(); } break; } this._cleanup(); }, _complete: function (completeValue) { // If we are in any state that isn't state_working then we have already // commited to a realized value and any duplicate calls to _complete will // be ignored. // if (this._state !== state_working) { return; } this._value = completeValue; if (isPromise(completeValue)) { // If _complete was called with a value which is itself a promise then // we block on that promise being fulfilled. If that promise is fulfilled // with a success value then this Promise continues to be successful, but // if that promise is fulfilled with an error value this promise will move // to an error state as well. // this._state = state_waiting; var that = this; completeValue.then( function (value) { that._state = state_working; that._complete(value); }, function (value) { that._state = state_working; if (completeValue instanceof PromiseBase) { that._chainedError(value, completeValue); } else { that._error(value); } }, function (value) { that._progress(value); } ); } else { this._state = state_fulfilled_success; this._notify(); } this._cleanup(); }, _handledError: function (errorValue, context, handler) { if (listeners._listeners && listeners._listeners[errorET]) { WinJS.Promise.dispatchEvent(errorET, { exception: context._isException ? errorValue : null, error: context._isException ? null : errorValue, promise: this, handler: handler, id: context._errorId, parent: context }); } this._complete(handler(errorValue)); }, _chainedError: generateErrorHandler(onerrorForChainedError), _error: generateErrorHandler(onerrorForError), _exception: generateErrorHandler(onerrorForException), _notify: function () { // Take ownership of the list of listeners, we will notify them all // and then drop them on the floor for garbage collection. // var listeners = this._listeners; this._listeners = null; if (listeners) { // If there are listeners and we are in a fulfilled state then we // notify those listeners of our value. // switch (this._state) { case state_fulfilled_success: notifySuccess(this, listeners, this._value); break; case state_fulfilled_error: notifyError(this, listeners, this._value); break; } } }, _progress: function (progressValue) { if (this._state >= state_fulfilled_min) { return; } if (this._listeners) { // If there are listeners walk through the list and notify any of them // which are listening for progress with the progress value. // for (var i = 0, len = this._listeners.length; i < len; i++) { var listeners = this._listeners[i]; var onProgress = listeners.onProgress; try { if (onProgress) { onProgress(progressValue); } } catch (e) { // // Swallow exception thrown from user progress handler // } // Progress waterfalls through Promises which do not contain a // terminating clause (complete or error). // if (!(listeners.onComplete || listeners.onError)) { listeners.promise._progress(progressValue); } } } }, then: function (onComplete, onError, onProgress) { /// /// Allows specifying work to be done on the realization of the promised value, /// error handling to be performed in the event that the Promise fails to realize /// a value and handling of progress notifications along the way. /// /// /// Function to be called if the Promise is fulfilled successfully with a value. /// The value will be passed as the single argument. If null then the Promise will /// provide a default implementation which simply returns the value. The value returned /// from the function will become the fulfilled value of the Promise returned by /// then(). If an exception is thrown while executing the function the Promise returned /// by then() will move into the error state. /// /// /// Function to be called if the Promise is fulfilled with an error. The error /// will be passed as the single argument. If null then the Promise will provide a default /// implementation which simply forwards the error. The value returned from the function /// will become the fulfilled value of the Promise returned by then(). /// /// /// Function to be called if the Promise reports progress. Data about the progress /// will be passed as the single argument. Promises are not required to support /// progress. /// /// /// Promise whose value will be the result of executing the provided complete or /// error function. /// // We have at least one callback to make, ensure we have a list to store it in // this._listeners = this._listeners || []; // Create the promise that will be the return value of the then() call. Pass it // ourselves so that if canceled it notify this instance. // var p = new ThenPromise(this); this._listeners.push({ promise: p, onComplete: onComplete, onError: onError, onProgress: onProgress }); // If we are already done then trigger notification immediately. // if (this._state >= state_fulfilled_min) { this._notify(); } return p; } } ); // ThenPromise is the type of instances that are returned from calling .then() on a // PromiseBase. It is kind of strange because it doesn't setup anything in its // constructor to call _complete or _error. PromiseBase's implementation reaches // around to the private members to call _complete and _error as needed on a // ThenPromise instance. // var ThenPromise = WinJS.Class.derive(PromiseBase, function (creator) { // 'creator' is the promise on which .then() was called resulting in this instance // this._creator = creator; }, { _cleanup: function () { this._creator = null; }, cancel: function () { if (this._creator) { // When we are canceled we need to propagate that up the chain. // this._creator.cancel(); } this._cancel(); } } ); var CompletePromise = WinJS.Class.derive(PromiseBase, function (value) { this._complete(value); }, { _cleanup: function () { }, cancel: function () { } } ); var ErrorPromise = WinJS.Class.derive(PromiseBase, function (error) { this._error(error); }, { _cleanup: function () { }, cancel: function () { } } ); // Promise implements the contract that we have checked into our libraries today. // var Promise = WinJS.Class.derive(PromiseBase, function (init, cancel) { /// /// A Promise provides a mechanism to schedule work to be done on a value that /// has not yet been computed. It is a very convinent abstraction for managing /// interactions with asynchronous APIs. /// /// /// Function which is called during construction of the Promise. The function /// is given three arguments (complete, error, progress). Inside the function /// the creator of the Promise should wire up the notifications supported by /// this value. /// /// /// Function to call if a down-stream consumer of this Promise wants to /// attempt to cancel its undone work. Promises are not required to be /// cancelable. /// this._onCancel = cancel; try { var that = this; init( function completeCallback(value) { that._complete(value); }, function errorCallback(value) { that._error(value); }, function progressCallback(value) { that._progress(value); } ); } catch (e) { this._exception(e); } }, { _cleanup: function () { this._onCancel = null; }, cancel: function () { /// /// Attempts to cancel the realization of a promised value. If the Promise hasn't /// already been fulfilled and cancellation is supported the Promise will enter /// the error state with a value of new Error("Canceled"). /// if (this._onCancel) { this._onCancel(); } this._cancel(); } }, { addEventListener: function (eventType, listener, capture) { /// /// Adds an event listener to the control. /// /// /// The type (name) of the event. /// /// /// The listener to invoke when the event gets raised. /// /// /// Specifies whether or not to initiate capture. /// listeners.addEventListener(eventType, listener, capture); }, any: function (values) { /// /// Returns a Promise which is fulfilled when one of the input Promises /// has been fulfilled. /// /// /// Array of values including Promise objects or Object whose property /// values include Promise objects. /// /// /// Promise which on fulfillment yields the value of the input which is /// complete or in error. /// var errorCount = 0; return new Promise( function (c, e, p) { var keys = Object.keys(values); var len = keys.length; var errors = Array.isArray(values) ? [] : {}; keys.forEach(function (key) { Promise.as(values[key]).then( function () { c({ key: key, value: values[key] }); }, function () { e({ key: key, value: values[key] }); } ); }); }, function () { var keys = Object.keys(values); keys.forEach(function (key) { var promise = Promise.as(values[key]); if (typeof promise.cancel === "function") { promise.cancel(); } }); } ); }, as: function (value) { /// /// Returns a Promise, if the value is already a Promise it is returned /// otherwise the value is wrapped in a Promise. /// /// /// Value to be treated as a Promise. /// /// /// Promise. /// if (isPromise(value)) { return value; } return new CompletePromise(value); }, dispatchEvent: function (eventType, details) { /// /// Raises an event of the specified type and with additional properties. /// /// /// The type (name) of the event. /// /// /// The set of additional properties to be attached to the event object when the event is raised. /// /// /// Boolean indicating whether preventDefault was called on the event. /// return listeners.dispatchEvent(eventType, details); }, is: function (value) { /// /// Determine whether a value fulfills the Promise contract. /// /// /// A value which may be a Promise. /// /// /// Whether or not the specified value is a Promise /// return isPromise(value); }, join: function (values) { /// /// Creates a Promise that is fulfilled when all the values are realized. /// /// /// Record whose fields contains values, some of which may be Promises. /// /// /// Promise whose value is a record with the same field names as the input where /// each field value is a realized value. /// return new Promise( function (c, e, p) { var keys = Object.keys(values); var errors = Array.isArray(values) ? [] : {}; var results = Array.isArray(values) ? [] : {}; var pending = keys.length; var argDone = function (key) { if ((--pending) === 0) { if (Object.keys(errors).length === 0) { c(results); } else { e(errors); } } else { p({ Key: key, Done: true }); } }; keys.forEach(function (key) { Promise.then(values[key], function (value) { results[key] = value; argDone(key); }, function (value) { errors[key] = value; argDone(key); } ); }); }, function () { Object.keys(values).forEach(function (key) { var promise = Promise.as(values[key]); if (typeof promise.cancel === "function") { promise.cancel(); } }); }); }, removeEventListener: function (eventType, listener, capture) { /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove from the invoke list. /// /// /// Specifies whether or not to initiate capture. /// listeners.removeEventListener(eventType, listener, capture); }, then: function (value, complete, error, progress) { /// /// Static forwarder to the Promise instance method then(). /// /// /// Value to be treated as a Promise. /// /// /// Function to be called if the Promise is fulfilled successfully with a value. /// If null then the Promise will provide a default implementation which simply /// returns the value. The value will be passed as the single argument. /// /// /// Function to be called if the Promise is fulfilled with an error. The error /// will be passed as the single argument. /// /// /// Function to be called if the Promise reports progress. Data about the progress /// will be passed as the single argument. Promises are not required to support /// progress. /// /// /// Promise whose value will be the result of executing the provided complete function. /// return Promise.as(value).then(complete, error, progress); }, thenEach: function (values, complete, error, progress) { /// /// Performs an operation on all of the input promises and returns a Promise /// which is in the shape of the input and contains the result of the operation /// having been performed on each input. /// /// /// Values (array or record) of which some or all are promises. /// /// /// Function to be called if the Promise is fulfilled successfully with a value. /// If null then the Promise will provide a default implementation which simply /// returns the value. The value will be passed as the single argument. /// /// /// Function to be called if the Promise is fulfilled with an error. The error /// will be passed as the single argument. /// /// /// Function to be called if the Promise reports progress. Data about the progress /// will be passed as the single argument. Promises are not required to support /// progress. /// /// /// Promise that is the result of calling Promise.join on the parameter 'values' /// var result = Array.isArray(values) ? [] : {}; Object.keys(values).forEach(function (key) { result[key] = Promise.as(values[key]).then(complete, error, progress); }); return Promise.join(result); }, timeout: function (timeout) { /// /// Create a promise which is fulfilled after a timeout /// /// /// Timeout period, if 0 or not specified this is implemented using /// msSetImmediate, otherwise using setTimeout. /// /// /// Promise which is completed asynchronously after 'timeout' milliseconds /// if (!timeout) { return new WinJS.Promise( function timeoutComplete(c) { msQueueCallback(c); } ); } else { var id = 0; return new WinJS.Promise( function (c) { id = setTimeout(c, timeout); }, function () { if (id) { clearTimeout(id); } } ); } }, wrap: function (value) { /// /// Wrap a non-Promise value in a promise, this is useful if you need /// to pass a value to a function which requires a Promise. /// /// /// Some non-Promise value to be wrapped in a promise /// /// /// Promise which is successfully fulfilled with the specified value /// return new CompletePromise(value); }, wrapError: function (error) { /// /// Wrap a non-Promise error value in a promise, this is useful if you need /// to pass an error to a function which requires a Promise. /// /// /// Some non-Promise error value to be wrapped in a promise /// /// /// Promise which is fulfilled in an error state with the specified value /// return new ErrorPromise(error); } } ); Object.defineProperties(Promise, WinJS.Utilities.createEventProperties(errorET)); // Publish WinJS.Promise // WinJS.Namespace.define("WinJS", { Promise: Promise }); })(this, WinJS); (function (global, WinJS) { WinJS.Namespace.define("WinJS", { // ErrorFromName establishes a simple pattern for returning error codes. // ErrorFromName: WinJS.Class.derive(Error, function (name, message) { this.name = name; this.message = message || name; }, { } ) }); })(this, WinJS); (function () { WinJS.Namespace.define("WinJS", { xhr: function (options) { /// /// Wraps calls to XMLHttpRequest in a Promise. /// /// /// Options which will be applied to the XMLHttpRequest object including type, /// url, user, password, headers and responseType. /// /// /// Promise which will yield the XMLHttpRequest object when completed /// var req; return new WinJS.Promise( function (c, e, p) { req = new XMLHttpRequest(); req.onreadystatechange = function () { if (req.readyState === 4) { if (req.status >= 200 && req.status < 300) { c(req); } else { e(req); } req.onreadystatechange = function () { }; } else { p(req); } }; req.open( options.type || "GET", options.url, // Promise based XHR does not support sync. // true, options.user, options.password ); req.responseType = options.responseType || ""; Object.keys(options.headers || {}).forEach(function (k) { req.setRequestHeader(k, options.headers[k]); }); req.send(options.data); }, function () { req.abort(); } ); } }); })(); (function (global, undefined) { var setInnerHTML = setInnerHTMLUnsafe = function (element, text) { /// /// Sets the innerHTML property of a element to the provided text /// /// /// Element on which the innerHTML property is to be set /// /// /// Value to be set to the innerHTML property /// element.innerHTML = text; }; var setOuterHTML = setOuterHTMLUnsafe = function (element, text) { /// /// Sets the outerHTML property of a element to the provided text /// /// /// Element on which the outerHTML property is to be set /// /// /// Value to be set to the outerHTML property /// element.outerHTML = text; }; var insertAdjacentHTML = insertAdjacentHTMLUnsafe = function (element, position, text) { /// /// Calls insertAdjacentHTML on the element /// /// /// Element on which insertAdjacentHTML is to be called /// /// /// Position relative to the element to insert the HTML /// /// /// Value to be provided to insertAdjacentHTML /// element.insertAdjacentHTML(position, text); }; var msWWA = global.msWWA; if (msWWA) { setInnerHTMLUnsafe = function (element, text) { /// /// Sets the innerHTML property of a element to the provided text /// /// /// Element on which the innerHTML property is to be set /// /// /// Value to be set to the innerHTML property /// msWWA.execUnsafeLocalFunction(function () { element.innerHTML = text; }); }; setOuterHTMLUnsafe = function (element, text) { /// /// Sets the outerHTML property of a element to the provided text /// under the context of msWWA.execUnsafeLocalFunction. /// /// /// Element on which the outerHTML property is to be set /// /// /// Value to be set to the outerHTML property /// msWWA.execUnsafeLocalFunction(function () { element.outerHTML = text; }); }; insertAdjacentHTMLUnsafe = function (element, position, text) { /// /// Calls insertAdjacentHTML on the element under the context /// of msWWA.execUnsafeLocalFunction. /// /// /// Element on which insertAdjacentHTML is to be called /// /// /// Position relative to the element to insert the HTML /// /// /// Value to be provided to insertAdjacentHTML /// msWWA.execUnsafeLocalFunction(function () { element.insertAdjacentHTML(position, text); }); }; } else if (global.toStaticHTML) { // If we ever get isStaticHTML we can attempt to recreate the behavior we have in the local // compartment, in the mean-time all we can do is sanitize the input. // setInnerHTML = function (element, text) { /// /// Sets the innerHTML property of a element to the provided text /// while calling toStaticHTML on the input text to sanitize it. /// /// /// Element on which the innerHTML property is to be set /// /// /// Value to be set to the innerHTML property /// element.innerHTML = global.toStaticHTML(text); }; setOuterHTML = function (element, text) { /// /// Sets the outerHTML property of a element to the provided text /// while calling toStaticHTML on the input text to sanitize it. /// /// /// Element on which the outerHTML property is to be set /// /// /// Value to be set to the outerHTML property /// element.outerHTML = global.toStaticHTML(text); }; insertAdjacentHTML = function (element, position, text) { /// /// Calls insertAdjacentHTML on the element while calling toStaticHTML /// on the input text to sanitize it. /// /// /// Element on which insertAdjacentHTML is to be called /// /// /// Position relative to the element to insert the HTML /// /// /// Value to be provided to insertAdjacentHTML /// element.insertAdjacentHTML(position, global.toStaticHTML(text)); }; } WinJS.Namespace.define("WinJS.Utilities", { setInnerHTML: setInnerHTML, setInnerHTMLUnsafe: setInnerHTMLUnsafe, setOuterHTML: setOuterHTML, setOuterHTMLUnsafe: setOuterHTMLUnsafe, insertAdjacentHTML: insertAdjacentHTML, insertAdjacentHTMLUnsafe: insertAdjacentHTMLUnsafe }); }(this));