/// /*! © Microsoft. All rights reserved. This library is supported for use in Windows Store apps only. Build: 1.0.9200.20789.win8_ldr.130802-2151 Version: Microsoft.WinJS.1.0 */ /// (function baseInit(global, undefined) { "use strict"; 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. /// /// /// The name of the new namespace. /// /// /// The members of 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. /// /// /// The name of the namespace. This could be a dot-separated name for nested namespaces. /// /// /// The members of 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, configurable: true }, define: { value: define, writable: true, enumerable: true, configurable: true } }); })("WinJS"); (function (WinJS) { function define(constructor, instanceMembers, staticMembers) { /// /// /// Defines a class using the given constructor and the specified instance members. /// /// /// A constructor function that is used to instantiate this class. /// /// /// The set of instance fields, properties, and methods made available on the class. /// /// /// The set of static fields, properties, and methods made available on the class. /// /// /// The newly-defined class. /// /// constructor = constructor || function () { }; WinJS.Utilities.markSupportedForProcessing(constructor); if (instanceMembers) { initializeProperties(constructor.prototype, instanceMembers); } if (staticMembers) { initializeProperties(constructor, staticMembers); } return constructor; } function derive(baseClass, constructor, instanceMembers, staticMembers) { /// /// /// Creates a sub-class based on the supplied baseClass parameter, using prototypal inheritance. /// /// /// The class to inherit from. /// /// /// A constructor function that is 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); WinJS.Utilities.markSupportedForProcessing(constructor); Object.defineProperty(constructor.prototype, "constructor", { value: constructor, writable: true, configurable: true, enumerable: true }); 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 is of variable length. /// /// /// A constructor function that is used to instantiate this class. /// /// /// The newly-defined class. /// /// constructor = constructor || function () { }; var i, len; for (i = 1, 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 baseUtilsInit(global, WinJS) { "use strict"; var hasWinRT = !!global.Windows; var strings = { get notSupportedForProcessing() { return WinJS.Resources._getWinJSString("base/notSupportedForProcessing").value; } }; function nop(v) { return v; } function getMemberFiltered(name, root, filter) { return name.split(".").reduce(function (currentNamespace, name) { if (currentNamespace) { return filter(currentNamespace[name]); } return null; }, root); } // Establish members of "WinJS.Utilities" namespace WinJS.Namespace.define("WinJS.Utilities", { // Used for mocking in tests _setHasWinRT: { value: function (value) { hasWinRT = value; }, configurable: false, writable: false, enumerable: false }, /// Determine if WinRT is accessible in this script context. hasWinRT: { get: function () { return hasWinRT; }, configurable: false, enumerable: true }, _getMemberFiltered: getMemberFiltered, getMember: function (name, root) { /// /// /// Gets the leaf-level type or namespace specified by the name parameter. /// /// /// The name of the member. /// /// /// The root to start in. Defaults to the global object. /// /// /// The leaf-level type or namespace in the specified parent namespace. /// /// if (!name) { return null; } return getMemberFiltered(name, root || global, nop); }, ready: function (callback, async) { /// /// /// Ensures that the specified function executes only after the DOMContentLoaded event has fired /// for the current page. /// /// A promise that completes after DOMContentLoaded has occurred. /// /// A function that executes after DOMContentLoaded has occurred. /// /// /// If true, the callback should be executed asynchronously. /// /// return new WinJS.Promise(function (c, e) { function complete() { if (callback) { try { callback(); c(); } catch (err) { e(err); } } else { c(); } } var readyState = WinJS.Utilities.testReadyState; if (!readyState) { if (global.document) { readyState = document.readyState; } else { readyState = "complete"; } } if (readyState === "complete" || (global.document && document.body !== null)) { if (async) { msSetImmediate(complete); } else { complete(); } } else { global.addEventListener("DOMContentLoaded", complete, false); } }); }, /// Determines if strict declarative processing is enabled in this script context. strictProcessing: { get: function () { return true; }, configurable: false, enumerable: true, }, markSupportedForProcessing: { value: function (func) { /// /// /// Marks a function as being compatible with declarative processing, such as WinJS.UI.processAll /// or WinJS.Binding.processAll. /// /// /// The function to be marked as compatible with declarative processing. /// /// /// The input function. /// /// func.supportedForProcessing = true; return func; }, configurable: false, writable: false, enumerable: true }, requireSupportedForProcessing: { value: function (value) { /// /// /// Asserts that the value is compatible with declarative processing, such as WinJS.UI.processAll /// or WinJS.Binding.processAll. If it is not compatible an exception will be thrown. /// /// /// The value to be tested for compatibility with declarative processing. If the /// value is a function it must be marked with a property 'supportedForProcessing' /// with a value of true. /// /// /// The input value. /// /// var supportedForProcessing = true; supportedForProcessing = supportedForProcessing && !(value === global); supportedForProcessing = supportedForProcessing && !(value === global.location); supportedForProcessing = supportedForProcessing && !(value instanceof HTMLIFrameElement); supportedForProcessing = supportedForProcessing && !(typeof value === "function" && !value.supportedForProcessing); switch (global.frames.length) { case 0: break; case 1: supportedForProcessing = supportedForProcessing && !(value === global.frames[0]); break; default: for (var i = 0, len = global.frames.length; supportedForProcessing && i < len; i++) { supportedForProcessing = supportedForProcessing && !(value === global.frames[i]); } break; } if (supportedForProcessing) { return value; } throw new WinJS.ErrorFromName("WinJS.Utilities.requireSupportedForProcessing", WinJS.Resources._formatString(strings.notSupportedForProcessing, value)); }, configurable: false, writable: false, enumerable: true }, }); WinJS.Namespace.define("WinJS", { validation: false, strictProcessing: { value: function () { /// /// /// Strict processing is always enforced, this method has no effect. /// /// }, configurable: false, writable: false, enumerable: false }, }); })(this, WinJS); (function logInit() { "use strict"; var spaceR = /\s+/g; var typeR = /^(error|warn|info|log)$/; function format(message, tag, type) { /// /// /// Adds tags and type to a logging message. /// /// The message to be formatted. /// The tag(s) to be applied to the message. Multiple tags should be separated by spaces. /// The type of the message. /// The formatted message. /// var m = message; if (typeof (m) === "function") { m = m(); } return ((type && typeR.test(type)) ? ("") : (type ? (type + ": ") : "")) + (tag ? tag.replace(spaceR, ":") + ": " : "") + m; } function defAction(message, tag, type) { var m = WinJS.Utilities.formatLog(message, tag, type); console[(type && typeR.test(type)) ? type : "log"](m); } function escape(s) { // \s (whitespace) is used as separator, so don't escape it return s.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&"); } WinJS.Namespace.define("WinJS.Utilities", { startLog: function (options) { /// /// /// Configures a logger that writes messages containing the specified tags from WinJS.log to console.log. /// /// The tags for messages to log. Multiple tags should be separated by spaces. /// /// /// /// Configure a logger to write WinJS.log output. /// /// /// May contain .type, .tags, .excludeTags and .action properties. /// - .type is a required tag. /// - .excludeTags is a space-separated list of tags, any of which will result in a message not being logged. /// - .tags is a space-separated list of tags, any of which will result in a message being logged. /// - .action is a function that, if present, will be called with the log message, tags and type. The default is to log to the console. /// /// options = options || {}; if (typeof options === "string") { options = { tags: options }; } var el = options.type && new RegExp("^(" + escape(options.type).replace(spaceR, " ").split(" ").join("|") + ")$"); var not = options.excludeTags && new RegExp("(^|\\s)(" + escape(options.excludeTags).replace(spaceR, " ").split(" ").join("|") + ")(\\s|$)", "i"); var has = options.tags && new RegExp("(^|\\s)(" + escape(options.tags).replace(spaceR, " ").split(" ").join("|") + ")(\\s|$)", "i"); var action = options.action || defAction; if (!el && !not && !has && !WinJS.log) { WinJS.log = action; return; } var result = function (message, tag, type) { if (!((el && !el.test(type)) // if the expected log level is not satisfied || (not && not.test(tag)) // if any of the excluded categories exist || (has && !has.test(tag)))) { // if at least one of the included categories doesn't exist action(message, tag, type); } result.next && result.next(message, tag, type); }; result.next = WinJS.log; WinJS.log = result; }, stopLog: function () { /// /// /// Removes the previously set up logger. /// /// delete WinJS.log; }, formatLog: format }); })(); (function eventsInit(WinJS, undefined) { "use strict"; function createEventProperty(name) { var eventPropStateName = "_on" + name + "state"; return { get: function () { var state = this[eventPropStateName]; return state && state.userHandler; }, set: function (handler) { var state = this[eventPropStateName]; if (handler) { if (!state) { state = { wrapper: function (evt) { return state.userHandler(evt); }, userHandler: handler }; Object.defineProperty(this, eventPropStateName, { value: state, enumerable: false, writable:true, configurable: true }); this.addEventListener(name, state.wrapper, false); } state.userHandler = handler; } else if (state) { this.removeEventListener(name, state.wrapper, false); this[eventPropStateName] = null; } }, enumerable: true } } function createEventProperties(events) { /// /// /// Creates an object that has one property for each name passed to the function. /// /// /// A variable list of property names. /// /// /// The object with the specified properties. The names of the properties are prefixed with 'on'. /// /// var props = {}; for (var i = 0, len = arguments.length; i < len; i++) { var name = arguments[i]; props["on" + name] = createEventProperty(name); } return props; } var EventMixinEvent = WinJS.Class.define( function EventMixinEvent_ctor(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 () { } }, { supportedForProcessing: false, } ); 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. /// /// /// if true initiates capture, otherwise false. /// /// 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 the specified 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. /// /// /// true if preventDefault was called on the event. /// /// var listeners = this._listeners && this._listeners[type]; if (listeners) { var eventValue = new EventMixinEvent(type, details, this); // 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; } return false; }, removeEventListener: function (type, listener, useCapture) { /// /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove. /// /// /// Specifies whether 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.define("WinJS.Utilities", { _createEventProperty: createEventProperty, createEventProperties: createEventProperties, eventMixin: eventMixin }); })(WinJS); (function resourcesInit(global, WinJS, undefined) { "use strict"; var resourceMap; var mrtEventHook = false; var contextChangedET = "contextchanged"; var ListenerType = WinJS.Class.mix(WinJS.Class.define(null, { /* empty */ }, { supportedForProcessing: false }), WinJS.Utilities.eventMixin); var listeners = new ListenerType(); var strings = { get malformedFormatStringInput() { return WinJS.Resources._getWinJSString("base/malformedFormatStringInput").value; }, }; WinJS.Namespace.define("WinJS.Resources", { addEventListener: function (type, listener, useCapture) { /// /// /// Registers an event handler for the specified event. /// /// /// The name of the event to handle. /// /// /// The listener to invoke when the event gets raised. /// /// /// Set to true to register the event handler for the capturing phase; set to false to register for the bubbling phase. /// /// if (WinJS.Utilities.hasWinRT && !mrtEventHook) { if (type === contextChangedET) { try { Windows.ApplicationModel.Resources.Core.ResourceManager.current.defaultContext.qualifierValues.addEventListener("mapchanged", function (e) { WinJS.Resources.dispatchEvent(contextChangedET, { qualifier: e.key, changed: e.target[e.key] }); }, false); mrtEventHook = true; } catch (e) { } } } listeners.addEventListener(type, listener, useCapture); }, removeEventListener: listeners.removeEventListener.bind(listeners), dispatchEvent: listeners.dispatchEvent.bind(listeners), _formatString: function (string) { var args = arguments; if (args.length > 1) { string = string.replace(/({{)|(}})|{(\d+)}|({)|(})/g, function (unused, left, right, index, illegalLeft, illegalRight) { if (illegalLeft || illegalRight) { throw WinJS.Resources._formatString(strings.malformedFormatStringInput, illegalLeft || illegalRight); } return (left && "{") || (right && "}") || args[(index|0) + 1]; }); } return string; }, _getStringWinRT: function (resourceId) { if (!resourceMap) { var mainResourceMap = Windows.ApplicationModel.Resources.Core.ResourceManager.current.mainResourceMap; try { resourceMap = mainResourceMap.getSubtree('Resources'); } catch (e) { } if (!resourceMap) { resourceMap = mainResourceMap; } } var stringValue; var langValue; var resCandidate; try { resCandidate = resourceMap.getValue(resourceId); if (resCandidate) { stringValue = resCandidate.valueAsString; if (stringValue === undefined) { stringValue = resCandidate.toString(); } } } catch (e) {} if (!stringValue) { return { value: resourceId, empty: true }; } try { langValue = resCandidate.getQualifierValue("Language"); } catch (e) { return { value: stringValue }; } return { value: stringValue, lang: langValue }; }, _getStringJS: function (resourceId) { var str = global.strings && global.strings[resourceId]; if (typeof str === "string") { str = { value: str }; } return str || { value: resourceId, empty: true }; } }); Object.defineProperties(WinJS.Resources, WinJS.Utilities.createEventProperties(contextChangedET)); var getStringImpl; WinJS.Resources.getString = function (resourceId) { /// /// /// Retrieves the resource string that has the specified resource id. /// /// /// The resource id of the string to retrieve. /// /// /// An object that can contain these properties: /// /// value: /// The value of the requested string. This property is always present. /// /// empty: /// A value that specifies whether the requested string wasn't found. /// If its true, the string wasn't found. If its false or undefined, /// the requested string was found. /// /// lang: /// The language of the string, if specified. This property is only present /// for multi-language resources. /// /// /// getStringImpl = getStringImpl || (WinJS.Utilities.hasWinRT ? WinJS.Resources._getStringWinRT : WinJS.Resources._getStringJS); return getStringImpl(resourceId); }; })(this, WinJS); (function promiseInit(global, undefined) { "use strict"; global.Debug && (global.Debug.setNonUserCodeExceptions = true); var ListenerType = WinJS.Class.mix(WinJS.Class.define(null, { /*empty*/ }, { supportedForProcessing: false }), WinJS.Utilities.eventMixin); var promiseEventListeners = new ListenerType(); // make sure there is a listeners collection so that we can do a more trivial check below promiseEventListeners._listeners = {}; var errorET = "error"; var canceledName = "Canceled"; var tagWithStack = false; var tag = { promise: 0x01, thenPromise: 0x02, errorPromise: 0x04, exceptionPromise: 0x08, completePromise: 0x10, }; tag.all = tag.promise | tag.thenPromise | tag.errorPromise | tag.exceptionPromise | tag.completePromise; // // 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. // // When someone has registered to be told about errors (WinJS.Promise.callonerror) promises // which are in error will get tagged with a ._errorId field. This tagged field is the // contract by which nested promises with errors will be identified as chaining for the // purposes of the callonerror semantics. If a nested promise in error is encountered without // a ._errorId it will be assumed to be foreign and treated as an interop boundary and // a new error id will be minted. // var error_number = 1; // // The state machine has a interesting hiccup in it with regards to notification, in order // to flatten out notification and avoid recursion for synchronous completion we have an // explicit set of *_notify states which are responsible for notifying their entire tree // of children. They can do this because they know that immediate children are always // ThenPromise instances and we can therefore reach into their state to access the // _listeners collection. // // So, what happens is that a Promise will be fulfilled through the _completed or _error // messages at which point it will enter a *_notify state and be responsible for to move // its children into an (as appropriate) success or error state and also notify that child's // listeners of the state transition, until leaf notes are reached. // var state_created, // -> working state_working, // -> error | error_notify | success | success_notify | canceled | waiting state_waiting, // -> error | error_notify | success | success_notify | waiting_canceled state_waiting_canceled, // -> error | error_notify | success | success_notify | canceling state_canceled, // -> error | error_notify | success | success_notify | canceling state_canceling, // -> error_notify state_success_notify, // -> success state_success, // -> . state_error_notify, // -> error state_error; // -> . // Noop function, used in the various states to indicate that they don't support a given // message. Named with the somewhat cute name '_' because it reads really well in the states. function _() { } // Initial state // state_created = { name: "created", enter: function (promise) { promise._setState(state_working); }, cancel: _, done: _, then: _, _completed: _, _error: _, _notify: _, _progress: _, _setCompleteValue: _, _setErrorValue: _ }; // Ready state, waiting for a message (completed/error/progress), able to be canceled // state_working = { name: "working", enter: _, cancel: function (promise) { promise._setState(state_canceled); }, done: done, then: then, _completed: completed, _error: error, _notify: _, _progress: progress, _setCompleteValue: setCompleteValue, _setErrorValue: setErrorValue }; // Waiting state, if a promise is completed with a value which is itself a promise // (has a then() method) it signs up to be informed when that child promise is // fulfilled at which point it will be fulfilled with that value. // state_waiting = { name: "waiting", enter: function (promise) { var waitedUpon = promise._value; var error = function (value) { if (waitedUpon._errorId) { promise._chainedError(value, waitedUpon); } else { // Because this is an interop boundary we want to indicate that this // error has been handled by the promise infrastructure before we // begin a new handling chain. // callonerror(promise, value, detailsForHandledError, waitedUpon, error); promise._error(value); } }; error.handlesOnError = true; waitedUpon.then( promise._completed.bind(promise), error, promise._progress.bind(promise) ); }, cancel: function (promise) { promise._setState(state_waiting_canceled); }, done: done, then: then, _completed: completed, _error: error, _notify: _, _progress: progress, _setCompleteValue: setCompleteValue, _setErrorValue: setErrorValue }; // Waiting canceled state, when a promise has been in a waiting state and receives a // request to cancel its pending work it will forward that request to the child promise // and then waits to be informed of the result. This promise moves itself into the // canceling state but understands that the child promise may instead push it to a // different state. // state_waiting_canceled = { name: "waiting_canceled", enter: function (promise) { // Initiate a transition to canceling. Triggering a cancel on the promise // that we are waiting upon may result in a different state transition // before the state machine pump runs again. promise._setState(state_canceling); var waitedUpon = promise._value; if (waitedUpon.cancel) { waitedUpon.cancel(); } }, cancel: _, done: done, then: then, _completed: completed, _error: error, _notify: _, _progress: progress, _setCompleteValue: setCompleteValue, _setErrorValue: setErrorValue }; // Canceled state, moves to the canceling state and then tells the promise to do // whatever it might need to do on cancelation. // state_canceled = { name: "canceled", enter: function (promise) { // Initiate a transition to canceling. The _cancelAction may change the state // before the state machine pump runs again. promise._setState(state_canceling); promise._cancelAction(); }, cancel: _, done: done, then: then, _completed: completed, _error: error, _notify: _, _progress: progress, _setCompleteValue: setCompleteValue, _setErrorValue: setErrorValue }; // Canceling state, commits to the promise moving to an error state with an error // object whose 'name' and 'message' properties contain the string "Canceled" // state_canceling = { name: "canceling", enter: function (promise) { var error = new Error(canceledName); error.name = error.message; promise._value = error; promise._setState(state_error_notify); }, cancel: _, done: _, then: _, _completed: _, _error: _, _notify: _, _progress: _, _setCompleteValue: _, _setErrorValue: _ }; // Success notify state, moves a promise to the success state and notifies all children // state_success_notify = { name: "complete_notify", enter: function (promise) { promise.done = CompletePromise.prototype.done; promise.then = CompletePromise.prototype.then; if (promise._listeners) { var queue = [promise]; var p; while (queue.length) { p = queue.pop(); p._state._notify(p, queue); } } promise._setState(state_success); }, cancel: _, done: null, /*error to get here */ then: null, /*error to get here */ _completed: _, _error: _, _notify: notifySuccess, _progress: _, _setCompleteValue: _, _setErrorValue: _ }; // Success state, moves a promise to the success state and does NOT notify any children. // Some upstream promise is owning the notification pass. // state_success = { name: "success", enter: function (promise) { promise.done = CompletePromise.prototype.done; promise.then = CompletePromise.prototype.then; promise._cleanupAction(); }, cancel: _, done: null, /*error to get here */ then: null, /*error to get here */ _completed: _, _error: _, _notify: notifySuccess, _progress: _, _setCompleteValue: _, _setErrorValue: _ }; // Error notify state, moves a promise to the error state and notifies all children // state_error_notify = { name: "error_notify", enter: function (promise) { promise.done = ErrorPromise.prototype.done; promise.then = ErrorPromise.prototype.then; if (promise._listeners) { var queue = [promise]; var p; while (queue.length) { p = queue.pop(); p._state._notify(p, queue); } } promise._setState(state_error); }, cancel: _, done: null, /*error to get here*/ then: null, /*error to get here*/ _completed: _, _error: _, _notify: notifyError, _progress: _, _setCompleteValue: _, _setErrorValue: _ }; // Error state, moves a promise to the error state and does NOT notify any children. // Some upstream promise is owning the notification pass. // state_error = { name: "error", enter: function (promise) { promise.done = ErrorPromise.prototype.done; promise.then = ErrorPromise.prototype.then; promise._cleanupAction(); }, cancel: _, done: null, /*error to get here*/ then: null, /*error to get here*/ _completed: _, _error: _, _notify: notifyError, _progress: _, _setCompleteValue: _, _setErrorValue: _ }; // // The statemachine implementation follows a very particular pattern, the states are specified // as static stateless bags of functions which are then indirected through the state machine // instance (a Promise). As such all of the functions on each state have the promise instance // passed to them explicitly as a parameter and the Promise instance members do a little // dance where they indirect through the state and insert themselves in the argument list. // // We could instead call directly through the promise states however then every caller // would have to remember to do things like pumping the state machine to catch state transitions. // var PromiseStateMachine = WinJS.Class.define(null, { _listeners: null, _nextState: null, _state: null, _value: null, cancel: function () { /// /// /// Attempts to cancel the fulfillment of a promised value. If the promise hasn't /// already been fulfilled and cancellation is supported, the promise enters /// the error state with a value of Error("Canceled"). /// /// this._state.cancel(this); this._run(); }, done: function Promise_done(onComplete, onError, onProgress) { /// /// /// Allows you to specify the work to be done on the fulfillment of the promised value, /// the error handling to be performed if the promise fails to fulfill /// a value, and the handling of progress notifications along the way. /// /// After the handlers have finished executing, this function throws any error that would have been returned /// from then() as a promise in the error state. /// /// /// The function to be called if the promise is fulfilled successfully with a value. /// The fulfilled value is passed as the single argument. If the value is null, /// the fulfilled value is returned. The value returned /// from the function becomes the fulfilled value of the promise returned by /// then(). If an exception is thrown while executing the function, the promise returned /// by then() moves into the error state. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. If it is null, the error is forwarded. /// The value returned from the function is the fulfilled value of the promise returned by then(). /// /// /// the function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// this._state.done(this, onComplete, onError, onProgress); }, then: function Promise_then(onComplete, onError, onProgress) { /// /// /// Allows you to specify the work to be done on the fulfillment of the promised value, /// the error handling to be performed if the promise fails to fulfill /// a value, and the handling of progress notifications along the way. /// /// /// The function to be called if the promise is fulfilled successfully with a value. /// The value is passed as the single argument. If the value is null, the value is returned. /// The value returned from the function becomes the fulfilled value of the promise returned by /// then(). If an exception is thrown while this function is being executed, the promise returned /// by then() moves into the error state. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. If it is null, the error is forwarded. /// The value returned from the function becomes the fulfilled value of the promise returned by then(). /// /// /// The function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// /// The promise whose value is the result of executing the complete or /// error function. /// /// return this._state.then(this, onComplete, onError, onProgress); }, _chainedError: function (value, context) { var result = this._state._error(this, value, detailsForChainedError, context); this._run(); return result; }, _completed: function (value) { var result = this._state._completed(this, value); this._run(); return result; }, _error: function (value) { var result = this._state._error(this, value, detailsForError); this._run(); return result; }, _progress: function (value) { this._state._progress(this, value); }, _setState: function (state) { this._nextState = state; }, _setCompleteValue: function (value) { this._state._setCompleteValue(this, value); this._run(); }, _setChainedErrorValue: function (value, context) { var result = this._state._setErrorValue(this, value, detailsForChainedError, context); this._run(); return result; }, _setExceptionValue: function (value) { var result = this._state._setErrorValue(this, value, detailsForException); this._run(); return result; }, _run: function () { while (this._nextState) { this._state = this._nextState; this._nextState = null; this._state.enter(this); } } }, { supportedForProcessing: false }); // // Implementations of shared state machine code. // function completed(promise, value) { var targetState; if (value && typeof value === "object" && typeof value.then === "function") { targetState = state_waiting; } else { targetState = state_success_notify; } promise._value = value; promise._setState(targetState); } function createErrorDetails(exception, error, promise, id, parent, handler) { return { exception: exception, error: error, promise: promise, handler: handler, id: id, parent: parent }; } function detailsForHandledError(promise, errorValue, context, handler) { var exception = context._isException; var errorId = context._errorId; return createErrorDetails( exception ? errorValue : null, exception ? null : errorValue, promise, errorId, context, handler ); } function detailsForChainedError(promise, errorValue, context) { var exception = context._isException; var errorId = context._errorId; setErrorInfo(promise, errorId, exception); return createErrorDetails( exception ? errorValue : null, exception ? null : errorValue, promise, errorId, context ); } function detailsForError(promise, errorValue) { var errorId = ++error_number; setErrorInfo(promise, errorId); return createErrorDetails( null, errorValue, promise, errorId ); } function detailsForException(promise, exceptionValue) { var errorId = ++error_number; setErrorInfo(promise, errorId, true); return createErrorDetails( exceptionValue, null, promise, errorId ); } function done(promise, onComplete, onError, onProgress) { pushListener(promise, { c: onComplete, e: onError, p: onProgress }); } function error(promise, value, onerrorDetails, context) { promise._value = value; callonerror(promise, value, onerrorDetails, context); promise._setState(state_error_notify); } function notifySuccess(promise, queue) { var value = promise._value; var listeners = promise._listeners; if (!listeners) { return; } promise._listeners = null; var i, len; for (i = 0, len = Array.isArray(listeners) ? listeners.length : 1; i < len; i++) { var listener = len === 1 ? listeners : listeners[i]; var onComplete = listener.c; var target = listener.promise; if (target) { try { target._setCompleteValue(onComplete ? onComplete(value) : value); } catch (ex) { target._setExceptionValue(ex); } if (target._state !== state_waiting && target._listeners) { queue.push(target); } } else { CompletePromise.prototype.done.call(promise, onComplete); } } } function notifyError(promise, queue) { var value = promise._value; var listeners = promise._listeners; if (!listeners) { return; } promise._listeners = null; var i, len; for (i = 0, len = Array.isArray(listeners) ? listeners.length : 1; i < len; i++) { var listener = len === 1 ? listeners : listeners[i]; var onError = listener.e; var target = listener.promise; if (target) { try { if (onError) { if (!onError.handlesOnError) { callonerror(target, value, detailsForHandledError, promise, onError); } target._setCompleteValue(onError(value)) } else { target._setChainedErrorValue(value, promise); } } catch (ex) { target._setExceptionValue(ex); } if (target._state !== state_waiting && target._listeners) { queue.push(target); } } else { ErrorPromise.prototype.done.call(promise, null, onError); } } } function callonerror(promise, value, onerrorDetailsGenerator, context, handler) { if (promiseEventListeners._listeners[errorET]) { if (value instanceof Error && value.message === canceledName) { return; } promiseEventListeners.dispatchEvent(errorET, onerrorDetailsGenerator(promise, value, context, handler)); } } function progress(promise, value) { var listeners = promise._listeners; if (listeners) { var i, len; for (i = 0, len = Array.isArray(listeners) ? listeners.length : 1; i < len; i++) { var listener = len === 1 ? listeners : listeners[i]; var onProgress = listener.p; if (onProgress) { try { onProgress(value); } catch (ex) { } } if (!(listener.c || listener.e) && listener.promise) { listener.promise._progress(value); } } } } function pushListener(promise, listener) { var listeners = promise._listeners; if (listeners) { // We may have either a single listener (which will never be wrapped in an array) // or 2+ listeners (which will be wrapped). Since we are now adding one more listener // we may have to wrap the single listener before adding the second. listeners = Array.isArray(listeners) ? listeners : [listeners]; listeners.push(listener); } else { listeners = listener; } promise._listeners = listeners; } // The difference beween setCompleteValue()/setErrorValue() and complete()/error() is that setXXXValue() moves // a promise directly to the success/error state without starting another notification pass (because one // is already ongoing). function setErrorInfo(promise, errorId, isException) { promise._isException = isException || false; promise._errorId = errorId; } function setErrorValue(promise, value, onerrorDetails, context) { promise._value = value; callonerror(promise, value, onerrorDetails, context); promise._setState(state_error); } function setCompleteValue(promise, value) { var targetState; if (value && typeof value === "object" && typeof value.then === "function") { targetState = state_waiting; } else { targetState = state_success; } promise._value = value; promise._setState(targetState); } function then(promise, onComplete, onError, onProgress) { var result = new ThenPromise(promise); pushListener(promise, { promise: result, c: onComplete, e: onError, p: onProgress }); return result; } // // Internal implementation detail promise, ThenPromise is created when a promise needs // to be returned from a then() method. // var ThenPromise = WinJS.Class.derive(PromiseStateMachine, function (creator) { if (tagWithStack && (tagWithStack === true || (tagWithStack & tag.thenPromise))) { this._stack = WinJS.Promise._getStack(); } this._creator = creator; this._setState(state_created); this._run(); }, { _creator: null, _cancelAction: function () { if (this._creator) { this._creator.cancel(); } }, _cleanupAction: function () { this._creator = null; } }, { supportedForProcessing: false } ); // // Slim promise implementations for already completed promises, these are created // under the hood on synchronous completion paths as well as by WinJS.Promise.wrap // and WinJS.Promise.wrapError. // var ErrorPromise = WinJS.Class.define( function ErrorPromise_ctor(value) { if (tagWithStack && (tagWithStack === true || (tagWithStack & tag.errorPromise))) { this._stack = WinJS.Promise._getStack(); } this._value = value; callonerror(this, value, detailsForError); }, { cancel: function () { /// /// /// Attempts to cancel the fulfillment of a promised value. If the promise hasn't /// already been fulfilled and cancellation is supported, the promise enters /// the error state with a value of Error("Canceled"). /// /// }, done: function ErrorPromise_done(unused, onError) { /// /// /// Allows you to specify the work to be done on the fulfillment of the promised value, /// the error handling to be performed if the promise fails to fulfill /// a value, and the handling of progress notifications along the way. /// /// After the handlers have finished executing, this function throws any error that would have been returned /// from then() as a promise in the error state. /// /// /// The function to be called if the promise is fulfilled successfully with a value. /// The fulfilled value is passed as the single argument. If the value is null, /// the fulfilled value is returned. The value returned /// from the function becomes the fulfilled value of the promise returned by /// then(). If an exception is thrown while executing the function, the promise returned /// by then() moves into the error state. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. If it is null, the error is forwarded. /// The value returned from the function is the fulfilled value of the promise returned by then(). /// /// /// the function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// var value = this._value; if (onError) { try { if (!onError.handlesOnError) { callonerror(null, value, detailsForHandledError, this, onError); } var result = onError(value); if (result && typeof result === "object" && typeof result.done === "function") { // If a promise is returned we need to wait on it. result.done(); } return; } catch (ex) { value = ex; } } if (value instanceof Error && value.message === canceledName) { // suppress cancel return; } // force the exception to be thrown asyncronously to avoid any try/catch blocks // setImmediate(function () { throw value; }); }, then: function ErrorPromise_then(unused, onError) { /// /// /// Allows you to specify the work to be done on the fulfillment of the promised value, /// the error handling to be performed if the promise fails to fulfill /// a value, and the handling of progress notifications along the way. /// /// /// The function to be called if the promise is fulfilled successfully with a value. /// The value is passed as the single argument. If the value is null, the value is returned. /// The value returned from the function becomes the fulfilled value of the promise returned by /// then(). If an exception is thrown while this function is being executed, the promise returned /// by then() moves into the error state. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. If it is null, the error is forwarded. /// The value returned from the function becomes the fulfilled value of the promise returned by then(). /// /// /// The function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// /// The promise whose value is the result of executing the complete or /// error function. /// /// // If the promise is already in a error state and no error handler is provided // we optimize by simply returning the promise instead of creating a new one. // if (!onError) { return this; } var result; var value = this._value; try { if (!onError.handlesOnError) { callonerror(null, value, detailsForHandledError, this, onError); } result = new CompletePromise(onError(value)); } catch (ex) { // If the value throw from the error handler is the same as the value // provided to the error handler then there is no need for a new promise. // if (ex === value) { result = this; } else { result = new ExceptionPromise(ex); } } return result; } }, { supportedForProcessing: false } ); var ExceptionPromise = WinJS.Class.derive(ErrorPromise, function ExceptionPromise_ctor(value) { if (tagWithStack && (tagWithStack === true || (tagWithStack & tag.exceptionPromise))) { this._stack = WinJS.Promise._getStack(); } this._value = value; callonerror(this, value, detailsForException); }, { /* empty */ }, { supportedForProcessing: false } ); var CompletePromise = WinJS.Class.define( function CompletePromise_ctor(value) { if (tagWithStack && (tagWithStack === true || (tagWithStack & tag.completePromise))) { this._stack = WinJS.Promise._getStack(); } if (value && typeof value === "object" && typeof value.then === "function") { var result = new ThenPromise(null); result._setCompleteValue(value); return result; } this._value = value; }, { cancel: function () { /// /// /// Attempts to cancel the fulfillment of a promised value. If the promise hasn't /// already been fulfilled and cancellation is supported, the promise enters /// the error state with a value of Error("Canceled"). /// /// }, done: function CompletePromise_done(onComplete) { /// /// /// Allows you to specify the work to be done on the fulfillment of the promised value, /// the error handling to be performed if the promise fails to fulfill /// a value, and the handling of progress notifications along the way. /// /// After the handlers have finished executing, this function throws any error that would have been returned /// from then() as a promise in the error state. /// /// /// The function to be called if the promise is fulfilled successfully with a value. /// The fulfilled value is passed as the single argument. If the value is null, /// the fulfilled value is returned. The value returned /// from the function becomes the fulfilled value of the promise returned by /// then(). If an exception is thrown while executing the function, the promise returned /// by then() moves into the error state. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. If it is null, the error is forwarded. /// The value returned from the function is the fulfilled value of the promise returned by then(). /// /// /// the function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// if (!onComplete) { return; } try { var result = onComplete(this._value); if (result && typeof result === "object" && typeof result.done === "function") { result.done(); } } catch (ex) { // force the exception to be thrown asynchronously to avoid any try/catch blocks setImmediate(function () { throw ex; }); } }, then: function CompletePromise_then(onComplete) { /// /// /// Allows you to specify the work to be done on the fulfillment of the promised value, /// the error handling to be performed if the promise fails to fulfill /// a value, and the handling of progress notifications along the way. /// /// /// The function to be called if the promise is fulfilled successfully with a value. /// The value is passed as the single argument. If the value is null, the value is returned. /// The value returned from the function becomes the fulfilled value of the promise returned by /// then(). If an exception is thrown while this function is being executed, the promise returned /// by then() moves into the error state. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. If it is null, the error is forwarded. /// The value returned from the function becomes the fulfilled value of the promise returned by then(). /// /// /// The function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// /// The promise whose value is the result of executing the complete or /// error function. /// /// try { // If the value returned from the completion handler is the same as the value // provided to the completion handler then there is no need for a new promise. // var newValue = onComplete ? onComplete(this._value) : this._value; return newValue === this._value ? this : new CompletePromise(newValue); } catch (ex) { return new ExceptionPromise(ex); } } }, { supportedForProcessing: false } ); // // Promise is the user-creatable WinJS.Promise object. // function timeout(timeoutMS) { var id; return new WinJS.Promise( function (c) { if (timeoutMS) { id = setTimeout(c, timeoutMS); } else { setImmediate(c); } }, function () { if (id) { clearTimeout(id); } } ); } function timeoutWithPromise(timeout, promise) { var cancelPromise = function () { promise.cancel(); } var cancelTimeout = function () { timeout.cancel(); } timeout.then(cancelPromise); promise.then(cancelTimeout, cancelTimeout); return promise; } var staticCanceledPromise; var Promise = WinJS.Class.derive(PromiseStateMachine, function Promise_ctor(init, oncancel) { /// /// /// A promise provides a mechanism to schedule work to be done on a value that /// has not yet been computed. It is a convenient abstraction for managing /// interactions with asynchronous APIs. /// /// /// The function that is called during construction of the promise. The function /// is given three arguments (complete, error, progress). Inside this function /// you should add event listeners for the notifications supported by this value. /// /// /// The function to call if a consumer of this promise wants /// to cancel its undone work. Promises are not required to /// support cancellation. /// /// if (tagWithStack && (tagWithStack === true || (tagWithStack & tag.promise))) { this._stack = WinJS.Promise._getStack(); } this._oncancel = oncancel; this._setState(state_created); this._run(); try { var complete = this._completed.bind(this); var error = this._error.bind(this); var progress = this._progress.bind(this); init(complete, error, progress); } catch (ex) { this._setExceptionValue(ex); } }, { _oncancel: null, _cancelAction: function () { if (this._oncancel) { try { this._oncancel(); } catch (ex) { } } }, _cleanupAction: function () { this._oncancel = null; } }, { addEventListener: function Promise_addEventListener(eventType, listener, capture) { /// /// /// Adds an event listener to the control. /// /// /// The type (name) of the event. /// /// /// The listener to invoke when the event is raised. /// /// /// Specifies whether or not to initiate capture. /// /// promiseEventListeners.addEventListener(eventType, listener, capture); }, any: function Promise_any(values) { /// /// /// Returns a promise that is fulfilled when one of the input promises /// has been fulfilled. /// /// /// An array that contains promise objects or objects whose property /// values include promise objects. /// /// /// A promise that on fulfillment yields the value of the input (complete or error). /// /// return new Promise( function (complete, error, progress) { var keys = Object.keys(values); var errors = Array.isArray(values) ? [] : {}; if (keys.length === 0) { complete(); } var canceled = 0; keys.forEach(function (key) { Promise.as(values[key]).then( function () { complete({ key: key, value: values[key] }); }, function (e) { if (e instanceof Error && e.name === canceledName) { if ((++canceled) === keys.length) { complete(WinJS.Promise.cancel); } return; } error({ 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 Promise_as(value) { /// /// /// Returns a promise. If the object is already a promise it is returned; /// otherwise the object is wrapped in a promise. /// /// /// The value to be treated as a promise. /// /// /// A promise. /// /// if (value && typeof value === "object" && typeof value.then === "function") { return value; } return new CompletePromise(value); }, /// /// Canceled promise value, can be returned from a promise completion handler /// to indicate cancelation of the promise chain. /// cancel: { get: function () { return (staticCanceledPromise = staticCanceledPromise || new ErrorPromise(new WinJS.ErrorFromName(canceledName))); } }, dispatchEvent: function Promise_dispatchEvent(eventType, details) { /// /// /// Raises an event of the specified type and properties. /// /// /// The type (name) of the event. /// /// /// The set of additional properties to be attached to the event object. /// /// /// Specifies whether preventDefault was called on the event. /// /// return promiseEventListeners.dispatchEvent(eventType, details); }, is: function Promise_is(value) { /// /// /// Determines whether a value fulfills the promise contract. /// /// /// A value that may be a promise. /// /// /// true if the specified value is a promise, otherwise false. /// /// return value && typeof value === "object" && typeof value.then === "function"; }, join: function Promise_join(values) { /// /// /// Creates a promise that is fulfilled when all the values are fulfilled. /// /// /// An object whose fields contain values, some of which may be promises. /// /// /// A promise whose value is an object with the same field names as those of the object in the values parameter, where /// each field value is the fulfilled value of a promise. /// /// return new Promise( function (complete, error, progress) { var keys = Object.keys(values); var errors = Array.isArray(values) ? [] : {}; var results = Array.isArray(values) ? [] : {}; var undefineds = 0; var pending = keys.length; var argDone = function (key) { if ((--pending) === 0) { var errorCount = Object.keys(errors).length; if (errorCount === 0) { complete(results); } else { var canceledCount = 0; keys.forEach(function (key) { var e = errors[key]; if (e instanceof Error && e.name === canceledName) { canceledCount++; } }); if (canceledCount === errorCount) { complete(WinJS.Promise.cancel); } else { error(errors); } } } else { progress({ Key: key, Done: true }); } }; keys.forEach(function (key) { var value = values[key]; if (value === undefined) { undefineds++; } else { Promise.then(value, function (value) { results[key] = value; argDone(key); }, function (value) { errors[key] = value; argDone(key); } ); } }); pending -= undefineds; if (pending === 0) { complete(results); return; } }, function () { Object.keys(values).forEach(function (key) { var promise = Promise.as(values[key]); if (typeof promise.cancel === "function") { promise.cancel(); } }); } ); }, removeEventListener: function Promise_removeEventListener(eventType, listener, capture) { /// /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove. /// /// /// Specifies whether or not to initiate capture. /// /// promiseEventListeners.removeEventListener(eventType, listener, capture); }, supportedForProcessing: false, then: function Promise_then(value, onComplete, onError, onProgress) { /// /// /// A static version of the promise instance method then(). /// /// /// the value to be treated as a promise. /// /// /// The function to be called if the promise is fulfilled with a value. /// If it is null, the promise simply /// returns the value. The value is passed as the single argument. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. /// /// /// The function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// /// A promise whose value is the result of executing the provided complete function. /// /// return Promise.as(value).then(onComplete, onError, onProgress); }, thenEach: function Promise_thenEach(values, onComplete, onError, onProgress) { /// /// /// Performs an operation on all the input promises and returns a promise /// that has the shape of the input and contains the result of the operation /// that has been performed on each input. /// /// /// A set of values (which could be either an array or an object) of which some or all are promises. /// /// /// The function to be called if the promise is fulfilled with a value. /// If the value is null, the promise returns the value. /// The value is passed as the single argument. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. /// /// /// The function to be called if the promise reports progress. Data about the progress /// is passed as the single argument. Promises are not required to support /// progress. /// /// /// A promise that is the result of calling Promise.join on the values parameter. /// /// var result = Array.isArray(values) ? [] : {}; Object.keys(values).forEach(function (key) { result[key] = Promise.as(values[key]).then(onComplete, onError, onProgress); }); return Promise.join(result); }, timeout: function Promise_timeout(time, promise) { /// /// /// Creates a promise that is fulfilled after a timeout. /// /// /// The timeout period in milliseconds. If this value is zero or not specified /// setImmediate is called, otherwise setTimeout is called. /// /// /// A promise that will be canceled if it doesn't complete before the /// timeout has expired. /// /// /// A promise that is completed asynchronously after the specified timeout. /// /// var to = timeout(time); return promise ? timeoutWithPromise(to, promise) : to; }, wrap: function Promise_wrap(value) { /// /// /// Wraps a non-promise value in a promise. You can use this function if you need /// to pass a value to a function that requires a promise. /// /// /// Some non-promise value to be wrapped in a promise. /// /// /// A promise that is successfully fulfilled with the specified value /// /// return new CompletePromise(value); }, wrapError: function Promise_wrapError(error) { /// /// /// Wraps a non-promise error value in a promise. You can use this function if you need /// to pass an error to a function that requires a promise. /// /// /// A non-promise error value to be wrapped in a promise. /// /// /// A promise that is in an error state with the specified value. /// /// return new ErrorPromise(error); }, _veryExpensiveTagWithStack: { get: function () { return tagWithStack; }, set: function (value) { tagWithStack = value; } }, _veryExpensiveTagWithStack_tag: tag, _getStack: function () { if (Debug.debuggerEnabled) { try { throw new Error(); } catch (e) { return e.stack; } } }, } ); Object.defineProperties(Promise, WinJS.Utilities.createEventProperties(errorET)); var SignalPromise = WinJS.Class.derive(PromiseStateMachine, function (cancel) { this._oncancel = cancel; this._setState(state_created); this._run(); }, { _cancelAction: function () { this._oncancel && this._oncancel(); }, _cleanupAction: function () { this._oncancel = null; } }, { supportedForProcessing: false } ); var Signal = WinJS.Class.define( function Signal_ctor(oncancel) { this._promise = new SignalPromise(oncancel); }, { promise: { get: function () { return this._promise; } }, cancel: function Signal_cancel() { this._promise.cancel(); }, complete: function Signal_complete(value) { this._promise._completed(value); }, error: function Signal_error(value) { this._promise._error(value); }, progress: function Signal_progress(value) { this._promise._progress(value); } }, { supportedForProcessing: false, } ); // Publish WinJS.Promise // WinJS.Namespace.define("WinJS", { Promise: Promise, _Signal: Signal }); }(this)); (function errorsInit(global, WinJS) { "use strict"; WinJS.Namespace.define("WinJS", { // ErrorFromName establishes a simple pattern for returning error codes. // ErrorFromName: WinJS.Class.derive(Error, function (name, message) { /// /// /// Creates an Error object with the specified name and message properties. /// /// The name of this error. The name is meant to be consumed programmatically and should not be localized. /// The message for this error. The message is meant to be consumed by humans and should be localized. /// Error instance with .name and .message properties populated /// this.name = name; this.message = message || name; }, { /* empty */ }, { supportedForProcessing: false, }) }); })(this, WinJS); (function xhrInit() { "use strict"; WinJS.Namespace.define("WinJS", { xhr: function (options) { /// /// /// Wraps calls to XMLHttpRequest in a promise. /// /// /// The options that are applied to the XMLHttpRequest object. They are: type, /// url, user, password, headers, responseType, data, and customRequestInitializer. /// /// /// A promise that returns the XMLHttpRequest object when it completes. /// /// var req; return new WinJS.Promise( function (c, e, p) { /// req = new XMLHttpRequest(); req.onreadystatechange = function () { if (req._canceled) { return; } 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]); }); if (options.customRequestInitializer) { options.customRequestInitializer(req); } req.send(options.data); }, function () { req._canceled = true; req.abort(); } ); } }); })(); (function safeHTMLInit(global, undefined) { "use strict"; var setInnerHTML, setInnerHTMLUnsafe, setOuterHTML, setOuterHTMLUnsafe, insertAdjacentHTML, insertAdjacentHTMLUnsafe; var strings = { get nonStaticHTML() { return WinJS.Resources._getWinJSString("base/nonStaticHTML").value; }, }; setInnerHTML = setInnerHTMLUnsafe = function (element, text) { /// /// /// Sets the innerHTML property of the specified element to the specified text. /// /// /// The element on which the innerHTML property is to be set. /// /// /// The value to be set to the innerHTML property. /// /// element.innerHTML = text; }; setOuterHTML = setOuterHTMLUnsafe = function (element, text) { /// /// /// Sets the outerHTML property of the specified element to the specified text. /// /// /// The element on which the outerHTML property is to be set. /// /// /// The value to be set to the outerHTML property. /// /// element.outerHTML = text; }; insertAdjacentHTML = insertAdjacentHTMLUnsafe = function (element, position, text) { /// /// /// Calls insertAdjacentHTML on the specified element. /// /// /// The element on which insertAdjacentHTML is to be called. /// /// /// The position relative to the element at which to insert the HTML. /// /// /// The value to be provided to insertAdjacentHTML. /// /// element.insertAdjacentHTML(position, text); }; var msApp = global.MSApp; if (msApp) { setInnerHTMLUnsafe = function (element, text) { /// /// /// Sets the innerHTML property of the specified element to the specified text. /// /// /// The element on which the innerHTML property is to be set. /// /// /// The value to be set to the innerHTML property. /// /// msApp.execUnsafeLocalFunction(function () { element.innerHTML = text; }); }; setOuterHTMLUnsafe = function (element, text) { /// /// /// Sets the outerHTML property of the specified element to the specified text /// in the context of msWWA.execUnsafeLocalFunction. /// /// /// The element on which the outerHTML property is to be set. /// /// /// The value to be set to the outerHTML property. /// /// msApp.execUnsafeLocalFunction(function () { element.outerHTML = text; }); }; insertAdjacentHTMLUnsafe = function (element, position, text) { /// /// /// Calls insertAdjacentHTML on the specified element in the context /// of msWWA.execUnsafeLocalFunction. /// /// /// The element on which insertAdjacentHTML is to be called. /// /// /// The position relative to the element at which to insert the HTML. /// /// /// Value to be provided to insertAdjacentHTML. /// /// msApp.execUnsafeLocalFunction(function () { element.insertAdjacentHTML(position, text); }); }; } else if (global.msIsStaticHTML) { var check = function (str) { if (!global.msIsStaticHTML(str)) { throw new WinJS.ErrorFromName("WinJS.Utitilies.NonStaticHTML", strings.nonStaticHTML); } } // 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 specified text /// if it passes a msIsStaticHTML check. /// /// /// The element on which the innerHTML property is to be set. /// /// /// The value to be set to the innerHTML property. /// /// check(text); element.innerHTML = text; }; setOuterHTML = function (element, text) { /// /// /// Sets the outerHTML property of a element to the specified text /// if it passes a msIsStaticHTML check. /// /// /// The element on which the outerHTML property is to be set. /// /// /// The value to be set to the outerHTML property. /// /// check(text); element.outerHTML = text; }; insertAdjacentHTML = function (element, position, text) { /// /// /// Calls insertAdjacentHTML on the element if it passes /// a msIsStaticHTML check. /// /// /// The element on which insertAdjacentHTML is to be called. /// /// /// The position relative to the element at which to insert the HTML. /// /// /// The value to be provided to insertAdjacentHTML. /// /// check(text); element.insertAdjacentHTML(position, text); }; } WinJS.Namespace.define("WinJS.Utilities", { setInnerHTML: setInnerHTML, setInnerHTMLUnsafe: setInnerHTMLUnsafe, setOuterHTML: setOuterHTML, setOuterHTMLUnsafe: setOuterHTMLUnsafe, insertAdjacentHTML: insertAdjacentHTML, insertAdjacentHTMLUnsafe: insertAdjacentHTMLUnsafe }); }(this)); (function getWinJSStringInit() { "use strict"; var appxVersion = "Microsoft.WinJS.1.0"; var developerPrefix = "Developer."; if (appxVersion.indexOf(developerPrefix) === 0) { appxVersion = appxVersion.substring(developerPrefix.length); } WinJS.Namespace.define("WinJS.Resources", { _getWinJSString: function (id) { return WinJS.Resources.getString("ms-resource://" + appxVersion + "/" + id); } }); }(this)); (function controlInit(global, WinJS, undefined) { "use strict"; // not supported in WebWorker if (!global.document) { return; } var DOMEventMixin = { _domElement: 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. /// /// /// true to initiate capture; otherwise, false. /// /// (this.element || this._domElement).addEventListener(type, listener, useCapture || false); }, dispatchEvent: function (type, eventProperties) { /// /// /// Raises an event of the specified type, adding the specified 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. /// /// /// true if preventDefault was called on the event, otherwise false. /// /// var eventValue = document.createEvent("Event"); eventValue.initEvent(type, false, false); eventValue.detail = eventProperties; if (typeof eventProperties === "object") { Object.keys(eventProperties).forEach(function (key) { eventValue[key] = eventProperties[key]; }); } return (this.element || this._domElement).dispatchEvent(eventValue); }, removeEventListener: function (type, listener, useCapture) { /// /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove. /// /// /// true to initiate capture; otherwise, false. /// /// (this.element || this._domElement).removeEventListener(type, listener, useCapture || false); } }; function setOptions(control, options) { /// /// /// Adds the set of declaratively specified options (properties and events) to the specified control. /// If name of the options property begins with "on", the property value is a function and the control /// supports addEventListener. The setOptions method calls the addEventListener method on the control. /// /// /// The control on which the properties and events are to be applied. /// /// /// The set of options that are specified declaratively. /// /// _setOptions(control, options); }; function _setOptions(control, options, eventsOnly) { if (typeof options === "object") { var keys = Object.keys(options); for (var i = 0, len = keys.length; i < len; i++) { var key = keys[i]; var value = options[key]; if (key.length > 2) { var ch1 = key[0]; var ch2 = key[1]; if ((ch1 === 'o' || ch1 === 'O') && (ch2 === 'n' || ch2 === 'N')) { if (typeof value === "function") { if (control.addEventListener) { control.addEventListener(key.substr(2), value); continue; } } } } if (!eventsOnly) { control[key] = value; } } } }; WinJS.Namespace.define("WinJS.UI", { DOMEventMixin: DOMEventMixin, setOptions: setOptions, _setOptions: _setOptions }); })(this, WinJS); (function declarativeControlsInit(global, WinJS, undefined) { "use strict"; // not supported in WebWorker if (!global.document) { return; } var strings = { get errorActivatingControl() { return WinJS.Resources._getWinJSString("base/errorActivatingControl").value; }, }; var markSupportedForProcessing = WinJS.Utilities.markSupportedForProcessing; var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing; var processedAllCalled = false; function createSelect(element) { var result = function select(selector) { /// /// /// Walks the DOM tree from the given element to the root of the document, whenever /// a selector scope is encountered select performs a lookup within that scope for /// the given selector string. The first matching element is returned. /// /// The selector string. /// The target element, if found. /// var current = element; var selected; while (current) { if (current.msParentSelectorScope) { var scope = current.parentNode; if (scope) { // In order to be inclusive of the scope in the lookup we do a QSA across // the parent of the scope and see if this scope is in the list. var scopeParent = scope.parentNode; if (scopeParent && -1 !== Array.prototype.indexOf.call(scopeParent.querySelectorAll(selector), scope)) { selected = scope; } if (selected = selected || scope.querySelector(selector)) { break; } } } current = current.parentNode; } return selected || document.querySelector(selector); } return markSupportedForProcessing(result); } function activate(element, handler) { return new WinJS.Promise(function activate2(complete, error) { try { var options; var optionsAttribute = element.getAttribute("data-win-options"); if (optionsAttribute) { options = WinJS.UI.optionsParser(optionsAttribute, global, { select: createSelect(element) }); } var ctl; var count = 1; // handler is required to call complete if it takes that parameter // if (handler.length > 2) { count++; } var checkComplete = function checkComplete() { count--; if (count === 0) { element.winControl = element.winControl || ctl; complete(ctl); } }; // async exceptions from the handler get dropped on the floor... // ctl = new handler(element, options, checkComplete); checkComplete(); } catch (err) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.errorActivatingControl, err && err.message), "winjs controls", "error"); error(err); } }); }; function processAllImpl(rootElement) { return new WinJS.Promise(function processAllImpl2(complete, error) { msWriteProfilerMark("WinJS.UI:processAll,StartTM"); rootElement = rootElement || document.body; var pending = 0; var selector = "[data-win-control]"; var allElements = rootElement.querySelectorAll(selector); var elements = []; if (getControlHandler(rootElement)) { elements.push(rootElement); } for (var i = 0, len = allElements.length; i < len; i++) { elements.push(allElements[i]); } // bail early if there is nothing to process // if (elements.length === 0) { complete(rootElement); return; } var checkAllComplete = function () { pending = pending - 1; if (pending < 0) { msWriteProfilerMark("WinJS.UI:processAll,StopTM"); complete(rootElement); } } // First go through and determine which elements to activate // var controls = new Array(elements.length); for (var i = 0, len = elements.length; i < len; i++) { var element = elements[i]; var control; var instance = element.winControl; if (instance) { control = instance.constructor; // already activated, don't need to add to controls array } else { controls[i] = control = getControlHandler(element); } if (control && control.isDeclarativeControlContainer) { i += element.querySelectorAll(selector).length; } } // Now go through and activate those // msWriteProfilerMark("WinJS.UI:processAllActivateControls,StartTM"); for (var i = 0, len = elements.length; i < len; i++) { var ctl = controls[i]; if (ctl) { pending++; activate(elements[i], ctl).then(checkAllComplete, error); } } msWriteProfilerMark("WinJS.UI:processAllActivateControls,StopTM"); checkAllComplete(); }); }; function getControlHandler(element) { if (element.getAttribute) { var evaluator = element.getAttribute("data-win-control"); if (evaluator) { return WinJS.Utilities._getMemberFiltered(evaluator.trim(), global, requireSupportedForProcessing); } } }; WinJS.Namespace.define("WinJS.UI", { scopedSelect: function (selector, element) { /// /// /// Walks the DOM tree from the given element to the root of the document, whenever /// a selector scope is encountered select performs a lookup within that scope for /// the given selector string. The first matching element is returned. /// /// The selector string. /// The target element, if found. /// return createSelect(element)(selector); }, processAll: function (rootElement) { /// /// /// Applies declarative control binding to all elements, starting at the specified root element. /// /// /// The element at which to start applying the binding. If this parameter is not specified, the binding is applied to the entire document. /// /// /// A promise that is fulfilled when binding has been applied to all the controls. /// /// if (!processedAllCalled) { return WinJS.Utilities.ready().then(function () { processedAllCalled = true; return processAllImpl(rootElement); }); } else { return processAllImpl(rootElement); } }, process: function (element) { /// /// /// Applies declarative control binding to the specified element. /// /// /// The element to bind. /// /// /// A promise that is fulfilled after the control is activated. The value of the /// promise is the control that is attached to element. /// /// if (element && element.winControl) { return WinJS.Promise.as(element.winControl); } var handler = getControlHandler(element); if (!handler) { return WinJS.Promise.as(); // undefined, no handler } else { return activate(element, handler); } } }); })(this, WinJS); (function elementListUtilities(global, WinJS, undefined) { "use strict"; // not supported in WebWorker if (!global.document) { return; } var QueryCollection = WinJS.Class.derive(Array, function (items) { /// /// /// Represents the result of a query selector, and provides /// various operations that perform actions over the elements of /// the collection. /// /// /// The items resulting from the query. /// /// if (items) { this.include(items); } }, { forEach: function (callbackFn, thisArg) { /// /// /// Performs an action on each item in the QueryCollection /// /// /// Action to perform on each item. /// /// /// Argument to bind to callbackFn /// /// /// Returns the QueryCollection /// /// Array.prototype.forEach.apply(this, [callbackFn, thisArg]); return this; }, get: function (index) { /// /// /// Gets an item from the QueryCollection. /// /// /// The index of the item to return. /// /// /// A single item from the collection. /// /// return this[index]; }, setAttribute: function (name, value) { /// /// /// Sets an attribute value on all the items in the collection. /// /// /// The name of the attribute to be set. /// /// /// The value of the attribute to be set. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { item.setAttribute(name, value); }); return this; }, getAttribute: function (name) { /// /// /// Gets an attribute value from the first element in the collection. /// /// /// The name of the attribute. /// /// /// The value of the attribute. /// /// if (this.length > 0) { return this[0].getAttribute(name); } }, addClass: function (name) { /// /// /// Adds the specified class to all the elements in the collection. /// /// /// The name of the class to add. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { WinJS.Utilities.addClass(item, name); }); return this; }, hasClass: function (name) { /// /// /// Determines whether the specified class exists on the first element of the collection. /// /// /// The name of the class. /// /// /// true if the element has the specified class; otherwise, false. /// /// if (this.length > 0) { return WinJS.Utilities.hasClass(this[0], name); } return false; }, removeClass: function (name) { /// /// /// Removes the specified class from all the elements in the collection. /// /// /// The name of the class to be removed. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { WinJS.Utilities.removeClass(item, name); }); return this; }, toggleClass: function (name) { /// > /// /// Toggles (adds or removes) the specified class on all the elements in the collection. /// If the class is present, it is removed; if it is absent, it is added. /// /// /// The name of the class to be toggled. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { WinJS.Utilities.toggleClass(item, name); }); return this; }, listen: function (eventType, listener, capture) { /// /// /// Registers the listener for the specified event on all the elements in the collection. /// /// /// The name of the event. /// /// /// The event handler function to be called when the event occurs. /// /// /// true if capture == true is to be passed to addEventListener; otherwise, false. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { item.addEventListener(eventType, listener, capture); }); return this; }, removeEventListener: function (eventType, listener, capture) { /// /// /// Unregisters the listener for the specified event on all the elements in the collection. /// /// /// The name of the event. /// /// /// The event handler function. /// /// /// true if capture == true; otherwise, false. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { item.removeEventListener(eventType, listener, capture); }); return this; }, setStyle: function (name, value) { /// /// /// Sets the specified style property for all the elements in the collection. /// /// /// The name of the style property. /// /// /// The value for the property. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { item.style[name] = value; }); return this; }, clearStyle: function (name) { /// /// /// Clears the specified style property for all the elements in the collection. /// /// /// The name of the style property to be cleared. /// /// /// This QueryCollection object. /// /// this.forEach(function (item) { item.style[name] = ""; }); return this; }, query: function (query) { /// /// /// Executes a query selector on all the elements in the collection /// and aggregates the result into a QueryCollection. /// /// /// The query selector string. /// /// /// A QueryCollection object containing the aggregate results of /// executing the query on all the elements in the collection. /// /// var newCollection = new WinJS.Utilities.QueryCollection(); this.forEach(function (item) { newCollection.include(item.querySelectorAll(query)); }); return newCollection; }, include: function (items) { /// /// /// Adds a set of items to this QueryCollection. /// /// /// The items to add to the QueryCollection. This may be an /// array-like object, a document fragment, or a single item. /// /// if (typeof items.length === "number") { for (var i = 0; i < items.length; i++) { this.push(items[i]); } } else if (items.DOCUMENT_FRAGMENT_NODE && items.nodeType === items.DOCUMENT_FRAGMENT_NODE) { this.include(items.childNodes); } else { this.push(items); } }, control: function (ctor, options) { /// /// /// Creates controls that are attached to the elements in this QueryCollection. /// /// /// A constructor function that is used to create controls to attach to the elements. /// /// /// The options passed to the newly-created controls. /// /// /// This QueryCollection object. /// /// /// /// /// Configures the controls that are attached to the elements in this QueryCollection. /// /// /// The options passed to the controls. /// /// /// This QueryCollection object. /// /// if (ctor && typeof (ctor) === "function") { this.forEach(function (element) { element.winControl = new ctor(element, options); }); } else { options = ctor; this.forEach(function (element) { WinJS.UI.process(element).done(function (control) { control && WinJS.UI.setOptions(control, options); }) }); } return this; } }, { supportedForProcessing: false, }); WinJS.Namespace.define("WinJS.Utilities", { QueryCollection: QueryCollection, query: function (query, element) { /// /// /// Executes a query selector on the specified element or the entire document. /// /// /// The query selector to be executed. /// /// /// The element on which to execute the query. If this parameter is not specified, the /// query is executed on the entire document. /// /// /// The QueryCollection that contains the results of the query. /// /// return new WinJS.Utilities.QueryCollection((element || document).querySelectorAll(query)); }, id: function (id) { /// /// /// Looks up an element by ID and wraps the result in a QueryCollection. /// /// /// The ID of the element. /// /// /// A QueryCollection that contains the element, if it is found. /// /// var e = document.getElementById(id); return new WinJS.Utilities.QueryCollection(e ? [e] : []); }, children: function (element) { /// /// /// Creates a QueryCollection that contains the children of the specified parent element. /// /// /// The parent element. /// /// /// The QueryCollection that contains the children of the element. /// /// return new WinJS.Utilities.QueryCollection(element.children); } }); })(this, WinJS); (function elementUtilities(global, WinJS, undefined) { "use strict"; // not supported in WebWorker if (!global.document) { return; } function removeEmpties(arr) { var len = arr.length; for (var i = len - 1; i >= 0; i--) { if (!arr[i]) { arr.splice(i, 1); len--; } } return len; } function getClassName(e) { var name = e.className || ""; if (typeof (name) == "string") { return name; } else { return name.baseVal || ""; } }; function setClassName(e, value) { // SVG elements (which use e.className.baseVal) are never undefined, // so this logic makes the comparison a bit more compact. // var name = e.className || ""; if (typeof (name) == "string") { e.className = value; } else { e.className.baseVal = value; } return e; }; function getDimension(element, property) { return WinJS.Utilities.convertToPixels(element, window.getComputedStyle(element, null)[property]); } WinJS.Namespace.define("WinJS.Utilities", { _dataKey: "_msDataKey", _pixelsRE: /^-?\d+(px)?$/i, _numberRE: /^-?\d+/i, /// /// Defines a set of keyboard values. /// Key: { /// /// BACKSPACE key. /// backspace: 8, /// /// TAB key. /// tab: 9, /// /// ENTER key. /// enter: 13, /// /// Shift key. /// shift: 16, /// /// CTRL key. /// ctrl: 17, /// /// ALT key /// alt: 18, /// /// Pause key. /// pause: 19, /// /// CAPS LOCK key. /// capsLock: 20, /// /// ESCAPE key. /// escape: 27, /// /// SPACE key. /// space: 32, /// /// PAGE UP key. /// pageUp: 33, /// /// PAGE DOWN key. /// pageDown: 34, /// /// END key. /// end: 35, /// /// HOME key. /// home: 36, /// /// Left arrow key. /// leftArrow: 37, /// /// Up arrow key. /// upArrow: 38, /// /// Right arrow key. /// rightArrow: 39, /// /// Down arrow key. /// downArrow: 40, /// /// INSERT key. /// insert: 45, /// /// DELETE key. /// deleteKey: 46, /// /// Number 0 key. /// num0: 48, /// /// Number 1 key. /// num1: 49, /// /// Number 2 key. /// num2: 50, /// /// Number 3 key. /// num3: 51, /// /// Number 4 key. /// num4: 52, /// /// Number 5 key. /// num5: 53, /// /// Number 6 key. /// num6: 54, /// /// Number 7 key. /// num7: 55, /// /// Number 8 key. /// num8: 56, /// /// Number 9 key. /// num9: 57, /// /// A key. /// a: 65, /// /// B key. /// b: 66, /// /// C key. /// c: 67, /// /// D key. /// d: 68, /// /// E key. /// e: 69, /// /// F key. /// f: 70, /// /// G key. /// g: 71, /// /// H key. /// h: 72, /// /// I key. /// i: 73, /// /// J key. /// j: 74, /// /// K key. /// k: 75, /// /// L key. /// l: 76, /// /// M key. /// m: 77, /// /// N key. /// n: 78, /// /// O key. /// o: 79, /// /// P key. /// p: 80, /// /// Q key. /// q: 81, /// /// R key. /// r: 82, /// /// S key. /// s: 83, /// /// T key. /// t: 84, /// /// U key. /// u: 85, /// /// V key. /// v: 86, /// /// W key. /// w: 87, /// /// X key. /// x: 88, /// /// Y key. /// y: 89, /// /// Z key. /// z: 90, /// /// Left Windows key. /// leftWindows: 91, /// /// Right Windows key. /// rightWindows: 92, /// /// Menu key. /// menu: 93, /// /// Number pad 0 key. /// numPad0: 96, /// /// Number pad 1 key. /// numPad1: 97, /// /// Number pad 2 key. /// numPad2: 98, /// /// Number pad 3 key. /// numPad3: 99, /// /// Number pad 4 key. /// numPad4: 100, /// /// Number pad 5 key. /// numPad5: 101, /// /// Number pad 6 key. /// numPad6: 102, /// /// Number pad 7 key. /// numPad7: 103, /// /// Number pad 8 key. /// numPad8: 104, /// /// Number pad 9 key. /// numPad9: 105, /// /// Multiplication key. /// multiply: 106, /// /// Addition key. /// add: 107, /// /// Subtraction key. /// subtract: 109, /// /// Decimal point key. /// decimalPoint: 110, /// /// Division key. /// divide: 111, /// /// F1 key. /// F1: 112, /// /// F2 key. /// F2: 113, /// /// F3 key. /// F3: 114, /// /// F4 key. /// F4: 115, /// /// F5 key. /// F5: 116, /// /// F6 key. /// F6: 117, /// /// F7 key. /// F7: 118, /// /// F8 key. /// F8: 119, /// /// F9 key. /// F9: 120, /// /// F10 key. /// F10: 121, /// /// F11 key. /// F11: 122, /// /// F12 key. /// F12: 123, /// /// NUMBER LOCK key. /// numLock: 144, /// /// SCROLL LOCK key. /// scrollLock: 145, /// /// Browser back key. /// browserBack: 166, /// /// Browser forward key. /// browserForward: 167, /// /// SEMICOLON key. /// semicolon: 186, /// /// EQUAL key. /// equal: 187, /// /// COMMA key. /// comma: 188, /// /// DASH key. /// dash: 189, /// /// PERIOD key. /// period: 190, /// /// FORWARD SLASH key. /// forwardSlash: 191, /// /// Accent grave key. /// graveAccent: 192, /// /// OPEN BRACKET key. /// openBracket: 219, /// /// BACKSLASH key. /// backSlash: 220, /// /// CLOSE BRACKET key. /// closeBracket: 221, /// /// SINGLE QUOTE key. /// singleQuote: 222 }, data: function (element) { /// /// /// Gets the data value associated with the specified element. /// /// /// The element. /// /// /// The value associated with the element. /// /// if (!element[WinJS.Utilities._dataKey]) { element[WinJS.Utilities._dataKey] = {}; } return element[WinJS.Utilities._dataKey]; }, hasClass: function (e, name) { /// /// /// Determines whether the specified element has the specified class. /// /// /// The element. /// /// /// The name of the class. /// /// /// true if the specified element contains the specified class; otherwise, false. /// /// var className = getClassName(e); var names = className.trim().split(" "); var l = names.length; for (var i = 0; i < l; i++) { if (names[i] == name) { return true; } } return false; }, addClass: function (e, name) { /// /// /// Adds the specified class(es) to the specified element. Multiple classes can be added using space delimited names. /// /// /// The element to which to add the class. /// /// /// The name of the class to add, multiple classes can be added using space delimited names /// /// /// The element. /// /// var className = getClassName(e); var names = className.split(" "); var l = removeEmpties(names); var toAdd; // we have a fast path for the common case of a single name in the class name // if (name.indexOf(" ") >= 0) { var namesToAdd = name.split(" "); removeEmpties(namesToAdd); for (var i = 0; i < l; i++) { var found = namesToAdd.indexOf(names[i]) if (found >= 0) { namesToAdd.splice(found, 1); } } if (namesToAdd.length > 0) { toAdd = namesToAdd.join(" "); } } else { var saw = false; for (var i = 0; i < l; i++) { if (names[i] === name) { saw = true; break; } } if (!saw) { toAdd = name; } } if (toAdd) { if (l > 0 && names[0].length > 0) { setClassName(e, className + " " + toAdd); } else { setClassName(e, toAdd); } } return e; }, removeClass: function (e, name) { /// /// /// Removes the specified class from the specified element. /// /// /// The element from which to remove the class. /// /// /// The name of the class to remove. /// /// /// The element. /// /// var original = getClassName(e); var namesToRemove; var namesToRemoveLen; if (name.indexOf(" ") >= 0) { namesToRemove = name.split(" "); namesToRemoveLen = removeEmpties(namesToRemove); } else { // early out for the case where you ask to remove a single // name and that name isn't found. // if (original.indexOf(name) < 0) { return e; } namesToRemove = [name]; namesToRemoveLen = 1; } var removed; var names = original.split(" "); var namesLen = removeEmpties(names); for (var i = namesLen - 1; i >= 0; i--) { if (namesToRemove.indexOf(names[i]) >= 0) { names.splice(i, 1); removed = true; } } if (removed) { setClassName(e, names.join(" ")); } return e; }, toggleClass: function (e, name) { /// /// /// Toggles (adds or removes) the specified class on the specified element. /// If the class is present, it is removed; if it is absent, it is added. /// /// /// The element on which to toggle the class. /// /// /// The name of the class to toggle. /// /// /// The element. /// /// var className = getClassName(e); var names = className.trim().split(" "); var l = names.length; var found = false; for (var i = 0; i < l; i++) { if (names[i] == name) { found = true; } } if (!found) { if (l > 0 && names[0].length > 0) { setClassName(e, className + " " + name); } else { setClassName(e, className + name); } } else { setClassName(e, names.reduce(function (r, e) { if (e == name) { return r; } else if (r && r.length > 0) { return r + " " + e; } else { return e; } }, "")); } return e; }, getRelativeLeft: function (element, parent) { /// /// /// Gets the left coordinate of the specified element relative to the specified parent. /// /// /// The element. /// /// /// The parent element. /// /// /// The relative left coordinate. /// /// if (!element) { return 0; } var left = element.offsetLeft; var e = element.parentNode; while (e) { left -= e.offsetLeft; if (e === parent) break; e = e.parentNode; } return left; }, getRelativeTop: function (element, parent) { /// /// /// Gets the top coordinate of the element relative to the specified parent. /// /// /// The element. /// /// /// The parent element. /// /// /// The relative top coordinate. /// /// if (!element) { return 0; } var top = element.offsetTop; var e = element.parentNode; while (e) { top -= e.offsetTop; if (e === parent) break; e = e.parentNode; } return top; }, empty: function (element) { /// /// /// Removes all the child nodes from the specified element. /// /// /// The element. /// /// /// The element. /// /// if (element.childNodes && element.childNodes.length > 0) { for (var i = element.childNodes.length - 1; i >= 0; i--) { element.removeChild(element.childNodes.item(i)); } } return element; }, _isDOMElement: function (element) { return element && typeof element === "object" && typeof element.tagName === "string"; }, getContentWidth: function (element) { /// /// /// Gets the width of the content of the specified element. The content width does not include borders or padding. /// /// /// The element. /// /// /// The content width of the element. /// /// var border = getDimension(element, "borderLeftWidth") + getDimension(element, "borderRightWidth"), padding = getDimension(element, "paddingLeft") + getDimension(element, "paddingRight"); return element.offsetWidth - border - padding; }, getTotalWidth: function (element) { /// /// /// Gets the width of the element, including margins. /// /// /// The element. /// /// /// The width of the element including margins. /// /// var margin = getDimension(element, "marginLeft") + getDimension(element, "marginRight"); return element.offsetWidth + margin; }, getContentHeight: function (element) { /// /// /// Gets the height of the content of the specified element. The content height does not include borders or padding. /// /// /// The element. /// /// /// The content height of the element. /// /// var border = getDimension(element, "borderTopWidth") + getDimension(element, "borderBottomWidth"), padding = getDimension(element, "paddingTop") + getDimension(element, "paddingBottom"); return element.offsetHeight - border - padding; }, getTotalHeight: function (element) { /// /// /// Gets the height of the element, including its margins. /// /// /// The element. /// /// /// The height of the element including margins. /// /// var margin = getDimension(element, "marginTop") + getDimension(element, "marginBottom"); return element.offsetHeight + margin; }, getPosition: function (element) { /// /// /// Gets the position of the specified element. /// /// /// The element. /// /// /// An object that contains the left, top, width and height properties of the element. /// /// var fromElement = element, offsetParent = element.offsetParent, top = element.offsetTop, left = element.offsetLeft; while ((element = element.parentNode) && element !== document.body && element !== document.documentElement) { top -= element.scrollTop; var dir = document.defaultView.getComputedStyle(element, null).direction; left -= dir !== "rtl" ? element.scrollLeft : -element.scrollLeft; if (element === offsetParent) { top += element.offsetTop; left += element.offsetLeft; offsetParent = element.offsetParent; } } return { left: left, top: top, width: fromElement.offsetWidth, height: fromElement.offsetHeight }; }, convertToPixels: function (element, value) { /// /// /// Converts a CSS positioning string for the specified element to pixels. /// /// /// The element. /// /// /// The CSS positioning string. /// /// /// The number of pixels. /// /// if (!this._pixelsRE.test(value) && this._numberRE.test(value)) { var previousValue = element.style.left; element.style.left = value; value = element.style.pixelLeft; element.style.left = previousValue; return value; } else { return parseInt(value, 10) || 0; } }, eventWithinElement: function (element, event) { /// /// /// Determines whether the specified event occurred within the specified element. /// /// /// The element. /// /// /// The event. /// /// /// true if the event occurred within the element; otherwise, false. /// /// var related = event.relatedTarget; if (related && related !== element) { return element.contains(related); } return false; } }); })(this, WinJS); (function fragmentControlInit(global) { "use strict"; // not supported in WebWorker if (!global.document) { return; } function abs(uri) { var a = document.createElement("a"); a.href = uri; return a.href.toLowerCase(); } var viewMap = {}; function selfhost(uri) { return document.location.href.toLowerCase() === uri.toLowerCase(); } WinJS.Namespace.define("WinJS.UI.Pages", { _mixin: { load: function (uri) { /// /// /// Creates a copy of the DOM elements from the specified URI. In order for this override /// to be used, the page that contains the load override needs to be defined by calling /// WinJS.UI.Pages.define() before WinJS.UI.Pages.render() is called. /// /// /// The URI from which to copy the DOM elements. /// /// /// A promise whose fulfilled value is the set of unparented DOM elements, if asynchronous processing is necessary. If not, returns nothing. /// /// if (!this.selfhost) { return WinJS.UI.Fragments.renderCopy(abs(uri)); } }, init: function (element, options) { /// /// /// Initializes the control before the content of the control is set. /// Use the processed method for any initialization that should be done after the content /// of the control has been set. /// /// /// The DOM element that will contain all the content for the page. /// /// /// The options passed to the constructor of the page. /// /// /// A promise that is fulfilled when initialization is complete, if asynchronous processing is necessary. If not, returns nothing. /// /// }, processed: function (element, options) { /// /// /// Initializes the control after the content of the control is set. /// /// /// The DOM element that will contain all the content for the page. /// /// /// The options that are to be passed to the constructor of the page. /// /// /// A promise that is fulfilled when initialization is complete, if asynchronous processing is necessary. If not, returns nothing. /// /// }, render: function (element, options, loadResult) { /// /// /// Renders the control, typically by adding the elements specified in the loadResult parameter to the specified element. /// /// /// The DOM element that will contain all the content for the page. /// /// /// The options passed into the constructor of the page. /// /// /// The elements returned from the load method. /// /// /// A promise that is fulfilled when rendering is complete, if asynchronous processing is necessary. If not, returns nothing. /// /// if (!this.selfhost) { element.appendChild(loadResult); } return element; }, ready: function (element, options) { /// /// /// Called after all initialization and rendering is complete. At this /// time the element is ready for use. /// /// /// The DOM element that contains all the content for the page. /// /// /// The options passed into the constructor of the page /// /// /// A promise that is fulfilled when the element is ready for use, if asynchronous processing is necessary. If not, returns nothing /// /// }, error: function (err) { /// /// /// Called if any error occurs during the processing of the page. /// /// /// The error that occurred. /// /// /// Nothing if the error was handled, or an error promise if the error was not handled. /// /// return WinJS.Promise.wrapError(err); } }, define: function (uri, members) { /// /// /// Creates a new page control from the specified URI that contains the specified members. /// Multiple calls to this method for the same URI are allowed, and all members will be /// merged. /// /// /// The URI for the content that defines the page. /// /// /// Additional members that the control will have. /// /// /// A constructor function that creates the page. /// /// uri = abs(uri); var base = viewMap[uri.toLowerCase()]; if (!base) { base = WinJS.Class.define( // This needs to follow the WinJS.UI.processAll "async constructor" // pattern to interop nicely in the "Views.Control" use case. // function PageControl_ctor(element, options, complete, parentedPromise) { var that = this; this.element = element = element || document.createElement("div"); element.msSourceLocation = uri; this.uri = uri; this.selfhost = selfhost(uri); element.winControl = this; WinJS.Utilities.addClass(element, "pagecontrol"); msWriteProfilerMark("WinJS.UI.Pages:createPage,StartTM"); var load = WinJS.Promise.wrap(). then(function Pages_load() { return that.load(uri); }); var renderCalled = load.then(function Pages_init(loadResult) { return WinJS.Promise.join({ loadResult: loadResult, initResult: that.init(element, options) }); }).then(function Pages_render(result) { return that.render(element, options, result.loadResult); }); this.elementReady = renderCalled.then(function () { return element; }); this.renderComplete = renderCalled. then(function Pages_processAll(f) { return WinJS.UI.processAll(f).then(function () { return f; }); }).then(function Pages_processed(f) { return that.processed(element, options); }).then(function () { return that; }); var callComplete = function () { complete && complete(that); msWriteProfilerMark("WinJS.UI.Pages:createPage,StopTM"); }; // promises guarantee order, so this will be called prior to ready path below // this.renderComplete.then(callComplete, callComplete); this.renderComplete.then(function () { return parentedPromise; }).then(function Pages_ready() { that.ready(element, options); }).then( null, function Pages_error(err) { return that.error(err); } ); }, WinJS.UI.Pages._mixin ); base = WinJS.Class.mix(base, WinJS.UI.DOMEventMixin); viewMap[uri.toLowerCase()] = base; } // Lazily mix in the members, allowing for multiple definitions of "define" to augment // the shared definition of the member. // if (members) { base = WinJS.Class.mix(base, members); } if (selfhost(uri)) { WinJS.Utilities.ready(function () { WinJS.UI.Pages.render(uri, document.body); }); } return base; }, get: function (uri) { /// /// /// Gets an already-defined page control for the specified URI, or creates a new one. /// /// /// The URI for the content that defines the page. /// /// /// A constructor function that creates the page. /// /// uri = abs(uri); var ctor = viewMap[uri.toLowerCase()]; if (!ctor) { ctor = WinJS.UI.Pages.define(uri); } return ctor; }, _remove: function (uri) { uri = abs(uri); WinJS.UI.Fragments.clearCache(uri); delete viewMap[uri] }, render: function (uri, element, options, parentedPromise) { /// /// /// Creates a page control from the specified URI inside /// the specified element with the specified options. /// /// /// The URI for the content that defines the page. /// /// /// The element to populate with the page. /// /// /// The options for configuring the page. /// /// /// A promise that is fulfilled when the specified element is parented to the final document. /// /// /// A promise that is fulfilled when the page is done rendering /// /// var ctor = WinJS.UI.Pages.get(uri); var control = new ctor(element, options, null, parentedPromise); return control.renderComplete; } }); WinJS.Namespace.define("WinJS.UI", { /// /// Enables you to include an HTML page dynamically. /// /// HtmlControl /// /// /// ]]> /// /// HtmlControl: WinJS.Class.define(function HtmlControl_ctor(element, options, complete) { /// /// /// Initializes a new instance of HtmlControl to define a new page control. /// /// /// The element that hosts the HtmlControl. /// /// /// The options for configuring the page. The uri option is required in order to specify the source /// document for the content of the page. /// /// WinJS.UI.Pages.render(options.uri, element, options). then(complete, function () { complete(); }); }) }); })(this); (function fragmentLoaderInit(global, WinJS, undefined) { "use strict"; var strings = { get invalidFragmentUri() { return WinJS.Resources._getWinJSString("base/invalidFragmentUri").value; }, }; // not supported in WebWorker if (!global.document) { return; } var forEach = function (arrayLikeValue, action) { for (var i = 0, l = arrayLikeValue.length; i < l; i++) { action(arrayLikeValue[i], i); } }; var head = document.head || document.getElementsByTagName("head")[0]; var scripts = {}; var styles = {}; var links = {}; var initialized = false; var cacheStore = {}; function addScript(scriptTag, fragmentHref, position) { // We synthesize a name for inline scripts because today we put the // inline scripts in the same processing pipeline as src scripts. If // we seperated inline scripts into their own logic, we could simplify // this somewhat. // var src = scriptTag.src; if (!src) { src = fragmentHref + "script[" + position + "]"; } src = src.toLowerCase(); if (!(src in scripts)) { scripts[src] = true; var n = document.createElement("script"); if (scriptTag.language) { n.setAttribute("language", "javascript"); } n.setAttribute("type", scriptTag.type); if (scriptTag.id) { n.setAttribute("id", scriptTag.id); } if (scriptTag.src) { n.setAttribute("src", scriptTag.src); } else { n.text = scriptTag.text; } head.appendChild(n); } }; function addStyle(styleTag, fragmentHref, position) { var src = (fragmentHref + "script[" + position + "]").toLowerCase(); if (!(src in styles)) { styles[src] = true; head.appendChild(styleTag.cloneNode(true)); } }; function addLink(styleTag) { var src = styleTag.href.toLowerCase(); if (!(src in links)) { links[src] = true; var n = styleTag.cloneNode(false); n.href = styleTag.href; head.appendChild(n); } }; function getStateRecord(href, removeFromCache) { if (typeof href === "string") { return loadFromCache(href, removeFromCache); } else { var state = { docfrag: WinJS.Utilities.data(href).docFragment }; if (!state.docfrag) { var fragment = document.createDocumentFragment(); while (href.childNodes.length > 0) { fragment.appendChild(href.childNodes[0]); }; state.docfrag = WinJS.Utilities.data(href).docFragment = fragment; href.setAttribute("data-win-hasfragment", ""); } if (removeFromCache) { clearCache(href); } return WinJS.Promise.as(state); } } function createEntry(state, href) { return WinJS.UI.Fragments._populateDocument(state, href). then(function () { if (state.document) { return processDocument(href, state); } else { return state; } }). then(WinJS.UI.Fragments._cleanupDocument). then(function () { return state; }); } function loadFromCache(href, removeFromCache) { var fragmentId = href.toLowerCase(); var state = cacheStore[fragmentId]; if (state) { if (removeFromCache) { delete cacheStore[fragmentId]; } if (state.promise) { return state.promise; } else { return WinJS.Promise.as(state); } } else { state = {}; if (!removeFromCache) { cacheStore[fragmentId] = state; } var result = state.promise = createEntry(state, href); state.promise.then(function () { delete state.promise; }); return result; } } function processDocument(href, state) { // Once the control's static state has been loaded in the temporary iframe, // this method spelunks the iframe's document to retrieve all relevant information. Also, // this performs any needed fixups on the DOM (like adjusting relative URLs). var cd = state.document; var b = cd.body; forEach(cd.querySelectorAll('link[rel="stylesheet"], link[type="text/css"]'), addLink); forEach(cd.getElementsByTagName('style'), function (e, i) { addStyle(e, href, i); }); forEach(cd.getElementsByTagName('script'), function (e, i) { addScript(e, href, i); }); forEach(b.getElementsByTagName('img'), function (e) { e.src = e.src; }); forEach(b.getElementsByTagName('a'), function (e) { // for # only anchor tags, we don't update the href // if (e.href !== "") { var href = e.getAttribute("href"); if (href && href[0] != "#") { e.href = e.href; } } }); // strip inline scripts from the body, they got copied to the // host document with the rest of the scripts above... // var localScripts = b.getElementsByTagName("script"); while (localScripts.length > 0) { var s = localScripts[0]; s.parentNode.removeChild(s); } // Create the docfrag which is just the body children // var fragment = document.createDocumentFragment(); var imported = document.importNode(cd.body, true); while (imported.childNodes.length > 0) { fragment.appendChild(imported.childNodes[0]); } state.docfrag = fragment; return state; }; function initialize() { if (initialized) { return; } initialized = true; forEach(head.querySelectorAll("script"), function (e) { scripts[e.src.toLowerCase()] = true; }); forEach(head.querySelectorAll('link[rel="stylesheet"], link[type="text/css"]'), function (e) { links[e.href.toLowerCase()] = true; }); }; function renderCopy(href, target) { /// /// /// Copies the contents of the specified URI into the specified element. /// /// /// The URI that contains the fragment to copy. /// /// /// The element to which the fragment is appended. /// /// /// A promise that is fulfilled when the fragment has been loaded. /// If a target element is not specified, the copied fragment is the /// completed value. /// /// return renderImpl(href, target, true); }; function renderImpl(href, target, copy) { initialize(); return getStateRecord(href, !copy).then(function (state) { var frag = state.docfrag; if (copy) { frag = frag.cloneNode(true); } var child = frag.firstChild; while (child) { if (child.nodeType === 1 /*Element node*/) { child.msParentSelectorScope = true; } child = child.nextSibling; } if (target) { target.appendChild(frag); return target; } else { return frag; } }); }; function render(href, target) { /// /// /// Copies the contents of the specified URI into the specified element. /// /// /// The URI that contains the fragment to copy. /// /// /// The element to which the fragment is appended. /// /// /// A promise that is fulfilled when the fragment has been loaded. /// If a target element is not specified, the copied fragment is the /// completed value. /// /// return renderImpl(href, target, false); }; function cache(href) { /// /// /// Starts loading the fragment at the specified location. The returned promise completes /// when the fragment is ready to be copied. /// /// /// The URI that contains the fragment to be copied. /// /// /// A promise that is fulfilled when the fragment has been prepared for copying. /// /// initialize(); return getStateRecord(href).then(function (state) { return state.docfrag; }); }; function clearCache(href) { /// /// /// Removes any cached information about the specified fragment. This method does not unload any scripts /// or styles that are referenced by the fragment. /// /// /// The URI that contains the fragment to be cleared. If no URI is provided, the entire contents of the cache are cleared. /// /// if (!href) { cacheStore = {}; } else if (typeof (href) == "string") { delete cacheStore[href.toLowerCase()]; } else { delete WinJS.Utilities.data(href).docFragment; href.removeAttribute("data-win-hasfragment"); } }; function forceLocal(uri) { if (WinJS.Utilities.hasWinRT) { // we force the URI to be cannonicalized and made absolute by IE // var a = document.createElement("a"); a.href = uri; var absolute = a.href; // WinRT Uri class doesn't provide URI construction, but can crack the URI // appart to let us reliably discover the scheme. // var wuri = new Windows.Foundation.Uri(absolute); // Only "ms-appx" (local package content) are allowed when running in the local // context. Both strings are known to be safe to compare in any culture (including Turkish). // var scheme = wuri.schemeName; if (scheme !== "ms-appx") { throw new WinJS.ErrorFromName("WinJS.UI.Fragments.InvalidUri", strings.invalidFragmentUri); } return absolute; } return uri; } WinJS.Namespace.define("WinJS.UI.Fragments", { renderCopy: renderCopy, render: render, cache: cache, clearCache: clearCache, _cacheStore: { get: function () { return cacheStore; } }, _forceLocal: forceLocal }); })(this, WinJS); (function fragmentLoader2Init(global) { "use strict"; // not supported in WebWorker if (!global.document) { return; } function cleanupDocumentIFrame(state) { // This is to work around a weird bug where removing the // IFrame from the DOM triggers DOMContentLoaded a second time. var temp = state.iframe; if (temp) { temp.contentDocument.removeEventListener("DOMContentLoaded", state.domContentLoaded, false); temp.parentNode.removeChild(temp); delete state.document; delete state.iframe; delete state.domContentLoaded; } }; function populateDocumentIFrame(state, href) { return new WinJS.Promise(function (c, e, p) { var temp = document.createElement('iframe'); temp.src = href; temp.style.display = 'none'; state.domContentLoaded = function () { state.document = temp.contentDocument; state.iframe = temp; c(); }; document.body.appendChild(temp); temp.contentWindow.onerror = function (e) { // It's OK to swallow these as they will occur in the main document // return true; }; temp.contentDocument.addEventListener("DOMContentLoaded", state.domContentLoaded, false); }); }; function cleanupDocumentXHR(state) { if (state.document) { delete state.document; } return WinJS.Promise.as(); }; function populateDocumentXHR(state, href) { // Because we later use "setInnerHTMLUnsafe" ("Unsafe" is the magic word here), we // want to force the href to only support local package content when running // in the local context. When running in the web context, this will be a no-op. // href = WinJS.UI.Fragments._forceLocal(href); var htmlDoc = document.implementation.createHTMLDocument("frag"); var base = htmlDoc.createElement("base"); htmlDoc.head.appendChild(base); var anchor = htmlDoc.createElement("a"); htmlDoc.body.appendChild(anchor); base.href = document.location.href; // Initialize base URL to primary document URL anchor.setAttribute("href", href); // Resolve the relative path to an absolute path base.href = anchor.href; // Update the base URL to be the resolved absolute path // 'anchor' is no longer needed at this point and will be removed by the innerHTML call state.document = htmlDoc; return WinJS.xhr({ url: href }).then(function (req) { WinJS.Utilities.setInnerHTMLUnsafe(htmlDoc.documentElement, req.responseText); htmlDoc.head.appendChild(base); }); }; if (global.MSApp) { WinJS.Namespace.define("WinJS.UI.Fragments", { _populateDocument: populateDocumentXHR, _cleanupDocument: cleanupDocumentXHR }); } else { WinJS.Namespace.define("WinJS.UI.Fragments", { _populateDocument: populateDocumentIFrame, _cleanupDocument: cleanupDocumentIFrame }); } })(this); (function optionsLexerInit(global, undefined) { "use strict"; /* Lexical grammar is defined in ECMA-262-5, section 7. Lexical productions used in this grammar defined in ECMA-262-5: Production Section -------------------------------- Identifier 7.6 NullLiteral 7.8.1 BooleanLiteral 7.8.2 NumberLiteral 7.8.3 StringLiteral 7.8.4 */ var tokenType = { leftBrace: 1, // { rightBrace: 2, // } leftBracket: 3, // [ rightBracket: 4, // ] separator: 5, // ECMA-262-5, 7.2 colon: 6, // : semicolon: 7, // ; comma: 8, // , dot: 9, // . nullLiteral: 10, // ECMA-262-5, 7.8.1 (null) trueLiteral: 11, // ECMA-262-5, 7.8.2 (true) falseLiteral: 12, // ECMA-262-5, 7.8.2 (false) numberLiteral: 13, // ECMA-262-5, 7.8.3 stringLiteral: 14, // ECMA-262-5, 7.8.4 identifier: 15, // ECMA-262-5, 7.6 reservedWord: 16, thisKeyword: 17, leftParentheses: 18, // ( rightParentheses: 19, // ) eof: 20, error: 21 }; // debugging - this costs something like 20% // //Object.keys(tokenType).forEach(function (key) { // tokenType[key] = key.toString(); //}); var tokens = { leftBrace: { type: tokenType.leftBrace, length: 1 }, rightBrace: { type: tokenType.rightBrace, length: 1 }, leftBracket: { type: tokenType.leftBracket, length: 1 }, rightBracket: { type: tokenType.rightBracket, length: 1 }, colon: { type: tokenType.colon, length: 1 }, semicolon: { type: tokenType.semicolon, length: 1 }, comma: { type: tokenType.comma, length: 1 }, dot: { type: tokenType.dot, length: 1 }, nullLiteral: { type: tokenType.nullLiteral, length: 4, value: null, keyword: true }, trueLiteral: { type: tokenType.trueLiteral, length: 4, value: true, keyword: true }, falseLiteral: { type: tokenType.falseLiteral, length: 5, value: false, keyword: true }, thisKeyword: { type: tokenType.thisKeyword, length: 4, value: "this", keyword: true }, leftParentheses: { type: tokenType.leftParentheses, length: 1 }, rightParentheses: { type: tokenType.rightParentheses, length: 1 }, eof: { type: tokenType.eof, length: 0 } }; function reservedWord(word) { return { type: tokenType.reservedWord, value: word, length: word.length, keyword: true }; } function reservedWordLookup(identifier) { // Moving from a simple object literal lookup for reserved words to this // switch was worth a non-trivial performance increase (5-7%) as this path // gets taken for any identifier. // switch (identifier.charCodeAt(0)) { case /*b*/98: switch (identifier) { case 'break': return reservedWord(identifier); } break; case /*c*/99: switch (identifier) { case 'case': case 'catch': case 'class': case 'const': case 'continue': return reservedWord(identifier); } break; case /*d*/100: switch (identifier) { case 'debugger': case 'default': case 'delete': case 'do': return reservedWord(identifier); } break; case /*e*/101: switch (identifier) { case 'else': case 'enum': case 'export': case 'extends': return reservedWord(identifier); } break; case /*f*/102: switch (identifier) { case 'false': return tokens.falseLiteral; case 'finally': case 'for': case 'function': return reservedWord(identifier); } break; case /*i*/105: switch (identifier) { case 'if': case 'import': case 'in': case 'instanceof': return reservedWord(identifier); } break; case /*n*/110: switch (identifier) { case 'null': return tokens.nullLiteral; case 'new': return reservedWord(identifier); } break; case /*r*/114: switch (identifier) { case 'return': return reservedWord(identifier); } break; case /*s*/115: switch (identifier) { case 'super': case 'switch': return reservedWord(identifier); } break; case /*t*/116: switch (identifier) { case 'true': return tokens.trueLiteral; case 'this': return tokens.thisKeyword; case 'throw': case 'try': case 'typeof': return reservedWord(identifier); } break; case /*v*/118: switch (identifier) { case 'var': case 'void': return reservedWord(identifier); } break; case /*w*/119: switch (identifier) { case 'while': case 'with': return reservedWord(identifier); } break; } return; } var lexer = (function () { function isIdentifierStartCharacter(code, text, offset, limit) { // The ES5 spec decalares that identifiers consist of a bunch of unicode classes, without // WinRT support for determining unicode class membership we are looking at 2500+ lines of // javascript code to encode the relevant class tables. Instead we look for everything // which is legal and < 0x7f, we exclude whitespace and line terminators, and then accept // everything > 0x7f. // // Here's the ES5 production: // // Lu | Ll | Lt | Lm | Lo | Nl // $ // _ // \ UnicodeEscapeSequence // switch (code) { case (code >= /*a*/97 && code <= /*z*/122) && code: case (code >= /*A*/65 && code <= /*Z*/90) && code: case /*$*/36: case /*_*/95: return true; case isWhitespace(code) && code: case isLineTerminator(code) && code: return false; case (code > 0x7f) && code: return true; case /*\*/92: if (offset + 4 < limit) { if (text.charCodeAt(offset) === /*u*/117 && isHexDigit(text.charCodeAt(offset + 1)) && isHexDigit(text.charCodeAt(offset + 2)) && isHexDigit(text.charCodeAt(offset + 3)) && isHexDigit(text.charCodeAt(offset + 4))) { return true; } } return false; default: return false; } } /* // Hand-inlined into readIdentifierPart function isIdentifierPartCharacter(code) { // See comment in isIdentifierStartCharacter. // // Mn | Mc | Nd | Pc // | // switch (code) { case isIdentifierStartCharacter(code) && code: case isDecimalDigit(code) && code: return true; default: return false; } } */ function readIdentifierPart(text, offset, limit) { var hasEscape = false; while (offset < limit) { var code = text.charCodeAt(offset); switch (code) { //case isIdentifierStartCharacter(code) && code: case (code >= /*a*/97 && code <= /*z*/122) && code: case (code >= /*A*/65 && code <= /*Z*/90) && code: case /*$*/36: case /*_*/95: break; case isWhitespace(code) && code: case isLineTerminator(code) && code: return hasEscape ? -offset : offset; case (code > 0x7f) && code: break; //case isDecimalDigit(code) && code: case (code >= /*0*/48 && code <= /*9*/57) && code: break; case /*\*/92: if (offset + 5 < limit) { if (text.charCodeAt(offset + 1) === /*u*/117 && isHexDigit(text.charCodeAt(offset + 2)) && isHexDigit(text.charCodeAt(offset + 3)) && isHexDigit(text.charCodeAt(offset + 4)) && isHexDigit(text.charCodeAt(offset + 5))) { offset += 5; hasEscape = true; break; } } return hasEscape ? -offset : offset; default: return hasEscape ? -offset : offset; } offset++; } return hasEscape ? -offset : offset; } function readIdentifierToken(text, offset, limit) { var startOffset = offset; offset = readIdentifierPart(text, offset, limit); var hasEscape = false; if (offset < 0) { offset = -offset; hasEscape = true; } var identifier = text.substr(startOffset, offset - startOffset); if (hasEscape) { identifier = "" + JSON.parse('"' + identifier + '"'); } var wordToken = reservedWordLookup(identifier); if (wordToken) { return wordToken; } return { type: tokenType.identifier, length: offset - startOffset, value: identifier }; } function isHexDigit(code) { switch (code) { case (code >= /*0*/48 && code <= /*9*/57) && code: case (code >= /*a*/97 && code <= /*f*/102) && code: case (code >= /*A*/65 && code <= /*F*/70) && code: return true; default: return false; } } function readHexIntegerLiteral(text, offset, limit) { while (offset < limit && isHexDigit(text.charCodeAt(offset))) { offset++; } return offset; } function isDecimalDigit(code) { switch (code) { case (code >= /*0*/48 && code <= /*9*/57) && code: return true; default: return false; } } function readDecimalDigits(text, offset, limit) { while (offset < limit && isDecimalDigit(text.charCodeAt(offset))) { offset++; } return offset; } function readDecimalLiteral(text, offset, limit) { offset = readDecimalDigits(text, offset, limit); if (offset < limit && text.charCodeAt(offset) === /*.*/46 && offset + 1 < limit && isDecimalDigit(text.charCodeAt(offset + 1))) { offset = readDecimalDigits(text, offset + 2, limit); } if (offset < limit) { var code = text.charCodeAt(offset); if (code === /*e*/101 || code === /*E*/69) { var tempOffset = offset + 1; if (tempOffset < limit) { code = text.charCodeAt(tempOffset); if (code === /*+*/43 || code === /*-*/45) { tempOffset++; } offset = readDecimalDigits(text, tempOffset, limit); } } } return offset; } function readDecimalLiteralToken(text, start, offset, limit) { var offset = readDecimalLiteral(text, offset, limit); var length = offset - start; return { type: tokenType.numberLiteral, length: length, value: +text.substr(start, length) }; } function isLineTerminator(code) { switch (code) { case 0x000A: // line feed case 0x000D: // carriage return case 0x2028: // line separator case 0x2029: // paragraph separator return true; default: return false; } } function readStringLiteralToken(text, offset, limit) { var startOffset = offset; var quoteCharCode = text.charCodeAt(offset); var hasEscape = false; offset++; while (offset < limit && !isLineTerminator(text.charCodeAt(offset))) { if (offset + 1 < limit && text.charCodeAt(offset) === /*\*/92) { hasEscape = true; switch (text.charCodeAt(offset + 1)) { case quoteCharCode: case 0x005C: // \ case 0x000A: // line feed case 0x2028: // line separator case 0x2029: // paragraph separator offset += 2; continue; case 0x000D: // carriage return if (offset + 2 < limit && text.charCodeAt(offset + 2) === 0x000A) { // Skip \r\n offset += 3; } else { offset += 2; } continue; } } offset++; if (text.charCodeAt(offset - 1) === quoteCharCode) { break; } } var length = offset - startOffset; // If we don't have a terminating quote go through the escape path. hasEscape = hasEscape || length === 1 || text.charCodeAt(offset - 1) !== quoteCharCode; var stringValue; if (hasEscape) { stringValue = eval(text.substr(startOffset, length)); } else { stringValue = text.substr(startOffset + 1, length - 2); } return { type: tokenType.stringLiteral, length: length, value: stringValue }; } function isWhitespace(code) { switch (code) { case 0x0009: // tab case 0x000B: // vertical tab case 0x000C: // form feed case 0x0020: // space case 0x00A0: // no-breaking space case 0xFEFF: // BOM return true; // There are no category Zs between 0x00A0 and 0x1680. // case (code < 0x1680) && code: return false; // Unicode category Zs // case 0x1680: case 0x180e: case (code >= 0x2000 && code <= 0x200a) && code: case 0x202f: case 0x205f: case 0x3000: return true; default: return false; } } // Hand-inlined isWhitespace. function readWhitespace(text, offset, limit) { while (offset < limit) { var code = text.charCodeAt(offset); switch (code) { case 0x0009: // tab case 0x000B: // vertical tab case 0x000C: // form feed case 0x0020: // space case 0x00A0: // no-breaking space case 0xFEFF: // BOM break; // There are no category Zs between 0x00A0 and 0x1680. // case (code < 0x1680) && code: return offset; // Unicode category Zs // case 0x1680: case 0x180e: case (code >= 0x2000 && code <= 0x200a) && code: case 0x202f: case 0x205f: case 0x3000: break; default: return offset; } offset++; } return offset; } function lex(result, text, offset, limit) { while (offset < limit) { var startOffset = offset; var code = text.charCodeAt(offset++); var type = undefined; var token = undefined; switch (code) { case isWhitespace(code) && code: case isLineTerminator(code) && code: offset = readWhitespace(text, offset, limit); token = { type: tokenType.separator, length: offset - startOffset }; // don't include whitespace in the token stream. continue; case /*"*/34: case /*'*/39: token = readStringLiteralToken(text, offset - 1, limit); break; case /*(*/40: token = tokens.leftParentheses; break; case /*)*/41: token = tokens.rightParentheses; break; case /*+*/43: case /*-*/45: if (offset < limit) { var afterSign = text.charCodeAt(offset); if (afterSign === /*.*/46) { var signOffset = offset + 1; if (signOffset < limit && isDecimalDigit(text.charCodeAt(signOffset))) { token = readDecimalLiteralToken(text, startOffset, signOffset, limit); break; } } else if (isDecimalDigit(afterSign)) { token = readDecimalLiteralToken(text, startOffset, offset, limit); break; } } token = { type: tokenType.error, length: offset - startOffset, value: text.substring(startOffset, offset) }; break; case /*,*/44: token = tokens.comma; break; case /*.*/46: token = tokens.dot; if (offset < limit && isDecimalDigit(text.charCodeAt(offset))) { token = readDecimalLiteralToken(text, startOffset, offset, limit); } break; case /*0*/48: var ch2 = (offset < limit ? text.charCodeAt(offset) : 0); if (ch2 === /*x*/120 || ch2 === /*X*/88) { var hexOffset = readHexIntegerLiteral(text, offset + 1, limit); token = { type: tokenType.numberLiteral, length: hexOffset - startOffset, value: +text.substr(startOffset, hexOffset - startOffset) }; } else { token = readDecimalLiteralToken(text, startOffset, offset, limit); } break; case (code >= /*1*/49 && code <= /*9*/57) && code: token = readDecimalLiteralToken(text, startOffset, offset, limit); break; case /*:*/58: token = tokens.colon; break; case /*;*/59: token = tokens.semicolon; break; case /*[*/91: token = tokens.leftBracket; break; case /*]*/93: token = tokens.rightBracket; break; case /*{*/123: token = tokens.leftBrace; break; case /*}*/125: token = tokens.rightBrace; break; default: if (isIdentifierStartCharacter(code, text, offset, limit)) { token = readIdentifierToken(text, offset - 1, limit); break; } token = { type: tokenType.error, length: offset - startOffset, value: text.substring(startOffset, offset) }; break; } offset += (token.length - 1); result.push(token); } } return function (text) { var result = []; lex(result, text, 0, text.length); result.push(tokens.eof); return result; }; })(); lexer.tokenType = tokenType; WinJS.Namespace.define("WinJS.UI", { _optionsLexer: lexer }); })(this); (function optionsParserInit(global, undefined) { "use strict"; var strings = { get invalidOptionsRecord() { return WinJS.Resources._getWinJSString("base/invalidOptionsRecord").value; }, get unexpectedTokenExpectedToken() { return WinJS.Resources._getWinJSString("base/unexpectedTokenExpectedToken").value; }, get unexpectedTokenExpectedTokens() { return WinJS.Resources._getWinJSString("base/unexpectedTokenExpectedTokens").value; }, get unexpectedTokenGeneric() { return WinJS.Resources._getWinJSString("base/unexpectedTokenGeneric").value; }, }; /* Notation is described in ECMA-262-5 (ECMAScript Language Specification, 5th edition) section 5. Lexical grammar is defined in ECMA-262-5, section 7. Lexical productions used in this grammar defined in ECMA-262-5: Production Section -------------------------------- Identifier 7.6 NullLiteral 7.8.1 BooleanLiteral 7.8.2 NumberLiteral 7.8.3 StringLiteral 7.8.4 Syntactic grammar for the value of the data-win-options attribute. OptionsLiteral: ObjectLiteral ObjectLiteral: { } { ObjectProperties } { ObjectProperties , } ObjectProperties: ObjectProperty ObjectProperties, ObjectProperty ObjectProperty: PropertyName : Value PropertyName: (from ECMA-262-6, 11.1.5) StringLiteral NumberLiteral Identifier ArrayLiteral: [ ] [ Elision ] [ ArrayElements ] [ ArrayElements , ] [ ArrayElements , Elision ] ArrayElements: Value Elision Value ArrayElements , Value ArrayElements , Elision Value Elision: , Elision , Value: NullLiteral NumberLiteral BooleanLiteral StringLiteral ArrayLiteral ObjectLiteral IdentifierExpression ObjectQueryExpression AccessExpression: [ Value ] . Identifier AccessExpressions: AccessExpression AccessExpressions AccessExpression IdentifierExpression: Identifier Identifier AccessExpressions ObjectQueryExpression: Identifier ( StringLiteral ) Identifier ( StringLiteral ) AccessExpressions NOTE: We have factored the above grammar to allow the infrastructure to be used by the BindingInterpreter as well. The BaseInterpreter does NOT provide an implementation of _evaluateValue(), this is expected to be provided by the derived class since right now the two have different grammars for Value AccessExpression: [ Value ] . Identifier AccessExpressions: AccessExpression AccessExpressions AccessExpression Identifier: Identifier (from ECMA-262-6, 7.6) IdentifierExpression: Identifier Identifier AccessExpressions Value: *** Provided by concrete interpreter *** */ var lexer = WinJS.UI._optionsLexer; var tokenType = lexer.tokenType; var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing; function tokenTypeName(type) { var keys = Object.keys(tokenType); for (var i = 0, len = keys.length; i < len; i++) { if (type === tokenType[keys[i]]) { return keys[i]; } } return ""; } var BaseInterpreter = WinJS.Class.define(null, { _error: function (message) { throw new WinJS.ErrorFromName("WinJS.UI.ParseError", message); }, _currentOffset: function () { var l = this._tokens.length; var p = this._pos; var offset = 0; for (var i = 0; i < p; i++) { offset += this._tokens[i].length; } return offset; }, _evaluateAccessExpression: function (value) { switch (this._current.type) { case tokenType.dot: this._read(); switch (this._current.type) { case tokenType.identifier: case this._current.keyword && this._current.type: var id = this._current.value; this._read(); return value[id]; default: this._unexpectedToken(tokenType.identifier, tokenType.reservedWord); break; } return; case tokenType.leftBracket: this._read(); var index = this._evaluateValue(); this._read(tokenType.rightBracket); return value[index]; // default: is unreachable because all the callers are conditional on // the next token being either a . or { // } }, _evaluateAccessExpressions: function (value) { while (true) { switch (this._current.type) { case tokenType.dot: case tokenType.leftBracket: value = this._evaluateAccessExpression(value); break; default: return value; } } }, _evaluateIdentifier: function (nested, value) { var id = this._readIdentifier(); value = nested ? value[id] : this._context[id]; return value; }, _evaluateIdentifierExpression: function () { var value = this._evaluateIdentifier(false); switch (this._current.type) { case tokenType.dot: case tokenType.leftBracket: return this._evaluateAccessExpressions(value); default: return value; } }, _initialize: function (tokens, originalSource, context, functionContext) { this._originalSource = originalSource; this._tokens = tokens; this._context = context; this._functionContext = functionContext; this._pos = 0; this._current = this._tokens[0]; }, _read: function (expected) { if (expected && this._current.type !== expected) { this._unexpectedToken(expected); } if (this._current !== tokenType.eof) { this._current = this._tokens[++this._pos]; } }, _peek: function (expected) { if (expected && this._current.type !== expected) { return; } if (this._current !== tokenType.eof) { return this._tokens[this._pos + 1]; } }, _readAccessExpression: function (parts) { switch (this._current.type) { case tokenType.dot: this._read(); switch (this._current.type) { case tokenType.identifier: case this._current.keyword && this._current.type: parts.push(this._current.value); this._read(); break; default: this._unexpectedToken(tokenType.identifier, tokenType.reservedWord); break; } return; case tokenType.leftBracket: this._read(); parts.push(this._evaluateValue()); this._read(tokenType.rightBracket); return; // default: is unreachable because all the callers are conditional on // the next token being either a . or { // } }, _readAccessExpressions: function (parts) { while (true) { switch (this._current.type) { case tokenType.dot: case tokenType.leftBracket: this._readAccessExpression(parts); break; default: return; } } }, _readIdentifier: function () { var id = this._current.value; this._read(tokenType.identifier); return id; }, _readIdentifierExpression: function () { var parts = []; if (this._peek(tokenType.thisKeyword) && parts.length === 0) { this._read(); } else { parts.push(this._readIdentifier()); } switch (this._current.type) { case tokenType.dot: case tokenType.leftBracket: this._readAccessExpressions(parts); break; } return parts; }, _unexpectedToken: function (expected) { var unexpected = (this._current.type === tokenType.error ? "'" + this._current.value + "'" : tokenTypeName(this._current.type)); if (expected) { if (arguments.length == 1) { expected = tokenTypeName(expected); this._error(WinJS.Resources._formatString(strings.unexpectedTokenExpectedToken, unexpected, expected, this._currentOffset())); } else { var names = []; for (var i = 0, len = arguments.length; i < len; i++) { names.push(tokenTypeName(arguments[i])); } expected = names.join(", "); this._error(WinJS.Resources._formatString(strings.unexpectedTokenExpectedTokens, unexpected, expected, this._currentOffset())); } } else { this._error(WinJS.Resources._formatString(strings.unexpectedTokenGeneric, unexpected, this._currentOffset())); } } }, { supportedForProcessing: false, } ); var OptionsInterpreter = WinJS.Class.derive(BaseInterpreter, function (tokens, originalSource, context, functionContext) { this._initialize(tokens, originalSource, context, functionContext); }, { _error: function (message) { throw new WinJS.ErrorFromName("WinJS.UI.ParseError", WinJS.Resources._formatString(strings.invalidOptionsRecord, this._originalSource, message)); }, _evaluateArrayLiteral: function () { var a = []; this._read(tokenType.leftBracket); this._readArrayElements(a); this._read(tokenType.rightBracket); return a; }, _evaluateObjectLiteral: function () { var o = {}; this._read(tokenType.leftBrace); this._readObjectProperties(o); this._tryReadComma(); this._read(tokenType.rightBrace); return o; }, _evaluateOptionsLiteral: function () { var value = this._evaluateValue(); if (this._current.type !== tokenType.eof) { this._unexpectedToken(tokenType.eof); } return value; }, _peekValue: function () { switch (this._current.type) { case tokenType.falseLiteral: case tokenType.nullLiteral: case tokenType.stringLiteral: case tokenType.trueLiteral: case tokenType.numberLiteral: case tokenType.leftBrace: case tokenType.leftBracket: case tokenType.identifier: return true; default: return false; } }, _evaluateValue: function () { switch (this._current.type) { case tokenType.falseLiteral: case tokenType.nullLiteral: case tokenType.stringLiteral: case tokenType.trueLiteral: case tokenType.numberLiteral: var value = this._current.value; this._read(); return value; case tokenType.leftBrace: return this._evaluateObjectLiteral(); case tokenType.leftBracket: return this._evaluateArrayLiteral(); case tokenType.identifier: if (this._peek(tokenType.identifier).type == tokenType.leftParentheses) { return requireSupportedForProcessing(this._evaluateObjectQueryExpression()); } return requireSupportedForProcessing(this._evaluateIdentifierExpression()); default: this._unexpectedToken(tokenType.falseLiteral, tokenType.nullLiteral, tokenType.stringLiteral, tokenType.trueLiteral, tokenType.numberLiteral, tokenType.leftBrace, tokenType.leftBracket, tokenType.identifier); break; } }, _tryReadElement: function (a) { if (this._peekValue()) { a.push(this._evaluateValue()); return true; } else { return false; } }, _tryReadComma: function () { if (this._peek(tokenType.comma)) { this._read(); return true; } return false; }, _tryReadElision: function (a) { var found = false; while (this._tryReadComma()) { a.push(undefined); found = true; } return found; }, _readArrayElements: function (a) { while (!this._peek(tokenType.rightBracket)) { var elision = this._tryReadElision(a); var element = this._tryReadElement(a); var comma = this._peek(tokenType.comma); if (element && comma) { // if we had a element followed by a comma, eat the comma and try to read the next element this._read(); } else if (element || elision) { // if we had a element without a trailing comma or if all we had were commas we're done break; } else { // if we didn't have a element or elision then we are done and in error this._unexpectedToken(tokenType.falseLiteral, tokenType.nullLiteral, tokenType.stringLiteral, tokenType.trueLiteral, tokenType.numberLiteral, tokenType.leftBrace, tokenType.leftBracket, tokenType.identifier); break; } } }, _readObjectProperties: function (o) { while (!this._peek(tokenType.rightBrace)) { var property = this._tryReadObjectProperty(o); var comma = this._peek(tokenType.comma); if (property && comma) { // if we had a property followed by a comma, eat the comma and try to read the next property this._read(); } else if (property) { // if we had a property without a trailing comma we're done break; } else { // if we didn't have a property then we are done and in error this._unexpectedToken(tokenType.numberLiteral, tokenType.stringLiteral, tokenType.identifier); break; } } }, _tryReadObjectProperty: function (o) { switch (this._current.type) { case tokenType.numberLiteral: case tokenType.stringLiteral: case tokenType.identifier: case this._current.keyword && this._current.type: var propertyName = this._current.value; this._read(); this._read(tokenType.colon); o[propertyName] = this._evaluateValue(); return true; default: return false; } }, _failReadObjectProperty: function () { this._unexpectedToken(tokenType.numberLiteral, tokenType.stringLiteral, tokenType.identifier, tokenType.reservedWord); }, _evaluateObjectQueryExpression: function () { var functionName = this._current.value; this._read(tokenType.identifier); this._read(tokenType.leftParentheses); var queryExpression = this._current.value; this._read(tokenType.stringLiteral); this._read(tokenType.rightParentheses); var value = requireSupportedForProcessing(this._functionContext[functionName])(queryExpression); switch (this._current.type) { case tokenType.dot: case tokenType.leftBracket: return this._evaluateAccessExpressions(value); default: return value; } }, run: function () { return this._evaluateOptionsLiteral(); } }, { supportedForProcessing: false, } ); var parser = function (text, context, functionContext) { var tokens = lexer(text); var interpreter = new OptionsInterpreter(tokens, text, context || {}, functionContext || {}); return interpreter.run(); }; parser._BaseInterpreter = BaseInterpreter; WinJS.Namespace.define("WinJS.UI", { optionsParser: parser }); })(this); (function tabManagerInit(global, WinJS, undefined) { "use strict"; // not supported in WebWorker if (!global.document) { return; } function fireEvent(element, name) { var event = document.createEvent('UIEvent'); event.initUIEvent(name, false, false, window, 1); element.dispatchEvent(event); } function onBeforeActivate(e, element, hasFocus) { fireEvent(element, hasFocus ? "onTabExit" : "onTabEnter"); e.cancelBubble = true; return false; } function prefixBeforeActivateHandler(e) { return onBeforeActivate(e, e.srcElement.nextSibling, e.shiftKey); } function postfixBeforeActivateHandler(e) { return onBeforeActivate(e, e.srcElement.previousSibling, !e.shiftKey); } function TabHelperObject(element, tabIndex) { function createCatcher(beforeActivateHandler) { var fragment = document.createElement("DIV"); fragment.tabIndex = (tabIndex ? tabIndex : 0); fragment.attachEvent("onbeforeactivate", beforeActivateHandler); fragment.setAttribute("aria-hidden", true); return fragment; }; var parent = element.parentNode; // Insert prefix focus catcher var catcherBegin = createCatcher(prefixBeforeActivateHandler); parent.insertBefore(catcherBegin, element); // Insert postfix focus catcher var catcherEnd = createCatcher(postfixBeforeActivateHandler); parent.insertBefore(catcherEnd, element.nextSibling); var refCount = 1; this.addRef = function () { refCount++; }; this.release = function () { if (--refCount === 0) { if (catcherBegin.parentElement) { parent.removeChild(catcherBegin); } if (catcherEnd.parentElement) { parent.removeChild(catcherEnd); } } return refCount; }; this.updateTabIndex = function (tabIndex) { catcherBegin.tabIndex = tabIndex; catcherEnd.tabIndex = tabIndex; } } WinJS.Namespace.define("WinJS.UI.TrackTabBehavior", { attach: function (element, tabIndex) { /// if (!element["win-trackTabHelperObject"]) { element["win-trackTabHelperObject"] = new TabHelperObject(element, tabIndex); } else { element["win-trackTabHelperObject"].addRef(); } return element["win-trackTabHelperObject"]; }, detach: function (element) { /// if (!element["win-trackTabHelperObject"].release()) { delete element["win-trackTabHelperObject"]; } } }); WinJS.Namespace.define("WinJS.UI", { TabContainer: WinJS.Class.define(function TabContainer_ctor(element, options) { /// /// /// Constructs the TabContainer. /// /// /// The DOM element to be associated with the TabContainer. /// /// /// The set of options to be applied initially to the TabContainer. /// /// /// A constructed TabContainer. /// /// // TabContainer uses 2 TrackTabBehavior for its implementation: one for itself, another one for the active element. // When onTabEnter is caught on TabContainer, it directly set focus on the active element. // When onTabExit is caught on the active element (_tabExitHandler), it first prevents focus from being set on any element, // effectively letting the focus skip any remaining items in the TabContainer. Then, when onTabExit is caught on // TabContainer, it turns back on the possibility to receive focus on child elements. this._element = element; this._tabIndex = 0; var that = this; this._tabExitHandler = function () { that._canFocus(false); }; element.addEventListener("onTabEnter", function () { if (that.childFocus) { that.childFocus.focus(); } else { that._canFocus(false); } }); element.addEventListener("onTabExit", function () { that._canFocus(true); }); this._elementTabHelper = WinJS.UI.TrackTabBehavior.attach(element, this._tabIndex); }, { // Public members /// /// Gets or sets the child element that has focus. /// childFocus: { set: function (e) { if (e != this._focusElement) { if (this._focusElement) { WinJS.UI.TrackTabBehavior.detach(this._focusElement); this._childTabHelper = null; this._focusElement.removeEventListener("onTabExit", this._tabExitHandler); } if (e && e.parentNode) { this._focusElement = e; this._childTabHelper = WinJS.UI.TrackTabBehavior.attach(e, this._tabIndex); this._focusElement.addEventListener("onTabExit", this._tabExitHandler); } else { //#DBG _ASSERT(!!e.parentNode); this._focusElement = null; } } }, get: function () { return this._focusElement; } }, /// /// Gets or sets the tab order of the control within its container. /// tabIndex: { set: function (tabIndex) { this._tabIndex = tabIndex; this._elementTabHelper.updateTabIndex(tabIndex); if (this._childTabHelper) { this._childTabHelper.updateTabIndex(tabIndex); } }, get: function () { return this._tabIndex; } }, // Private members _element: null, _skipper: function (e) { e.cancelBubble = true; return false; }, _canFocus: function (canfocus) { if (canfocus) { this._element.detachEvent("onbeforeactivate", this._skipper); } else { this._element.attachEvent("onbeforeactivate", this._skipper); } }, _focusElement: null }, { // Static Members supportedForProcessing: false, }) }); })(this, WinJS); (function transitionAnimationInit(global, WinJS, undefined) { "use strict"; // not supported in WebWorker if (!global.document) { return; } function makeArray(elements) { if (elements instanceof Array || elements instanceof NodeList || elements instanceof HTMLCollection) { return elements; } else if (elements) { return [elements]; } else { return []; } } var keyframeCounter = 0; function getUniqueKeyframeName() { return "WinJSUIAnimation" + ++keyframeCounter; } function isUniqueKeyframeName(s) { return "WinJSUIAnimation" === s.substring(0, 16); } function resolveStyles(elem) { window.getComputedStyle(elem, null).opacity; } function copyWithEvaluation(iElem, elem) { return function (obj) { var newObj = {}; for (var p in obj) { var v = obj[p]; if (typeof v === "function") { v = v(iElem, elem); } newObj[p] = v; } if (!newObj.exactTiming) { newObj.delay += UI._libraryDelay; } return newObj; }; } var activeActions = [ ]; var reason_ended = 0; var reason_interrupted = 1; var reason_canceled = 2; function stopExistingAction(id, prop) { var key = id + "|" + prop; var finish = activeActions[key]; if (finish) { finish(reason_interrupted); } } function registerAction(id, prop, finish) { activeActions[id + "|" + prop] = finish; } function unregisterAction(id, prop) { delete activeActions[id + "|" + prop]; } var StyleCache = WinJS.Class.define( // Constructor function StyleCache_ctor(id, desc, style) { this.cref = 0; this.id = id; this.desc = desc; this.removed = {}; this.prevStyles = desc.props.map(function (p) { return style[p[0]]; }); this.prevNames = this.names = style[desc.nameProp]; desc.styleCaches[id] = this; }, { // Members destroy: function StyleCache_destroy(style) { var desc = this.desc; delete desc.styleCaches[this.id]; if (this.prevNames === "" && this.prevStyles.every(function(s) { return s === ""; })) { style[desc.shorthandProp] = ""; } else { desc.props.forEach(function (p, i) { style[p[0]] = this.prevStyles[i]; }, this); style[desc.nameProp] = this.prevNames; } }, removeName: function StyleCache_removeName(style, name, elem) { var nameValue = this.names; var names = nameValue.split(", "); var index = names.lastIndexOf(name); if (index >= 0) { names.splice(index, 1); this.names = nameValue = names.join(", "); if (nameValue === "" && this.desc.isTransition) { nameValue = "none"; } } if (--this.cref) { style[this.desc.nameProp] = nameValue; if (!isUniqueKeyframeName(name)) { this.removed[name] = true; } } else { if (elem && nameValue === "none") { style[this.desc.nameProp] = nameValue; resolveStyles(elem); } this.destroy(style); } } }); function setTemporaryStyles(elem, id, style, actions, desc) { var styleCache = desc.styleCaches[id] || new StyleCache(id, desc, style); styleCache.cref += actions.length; actions.forEach(function(action) { stopExistingAction(id, action.property); }); if (desc.isTransition || actions.some(function(action) { return styleCache.removed[action[desc.nameField]]; })) { resolveStyles(elem); styleCache.removed = { }; } var newShorthand = actions.map(function(action) { return action[desc.nameField] + " " + desc.props.map(function(p) { return (p[1] ? action[p[1]] : "") + p[2]; }).join(" "); }).join(", "); var newNames = actions.map(function(action) { return action[desc.nameField]; }).join(", "); if (styleCache.names !== "") { newShorthand = styleCache.names + ", " + newShorthand; newNames = styleCache.names + ", " + newNames; } style[desc.shorthandProp] = newShorthand; styleCache.names = newNames; return styleCache; } var elementTransitionProperties = { shorthandProp: "transition", nameProp: "transition-property", nameField: "property", props: [ ["transition-duration", "duration", "ms"], ["transition-timing-function", "timing", ""], ["transition-delay", "delay", "ms"] ], isTransition: true, styleCaches: [] }; function completePromise(c, synchronous) { if (synchronous) { c(); } else { setImmediate(c); } } var uniformizeStyle; function executeElementTransition(elem, index, transitions, promises, animate) { if (transitions.length > 0) { var style = elem.style; var id = elem.uniqueID; if (!uniformizeStyle) { uniformizeStyle = document.createElement("DIV").style; } transitions = transitions.map(copyWithEvaluation(index, elem)); transitions.forEach(function (transition) { if (transition.hasOwnProperty("from")) { style[transition.property] = transition.from; } uniformizeStyle[transition.property] = transition.to; transition.to = uniformizeStyle[transition.property]; }); if (animate) { var styleCache = setTemporaryStyles(elem, id, style, transitions, elementTransitionProperties); var listener = elem.disabled ? document : elem; transitions.forEach(function (transition) { var finish; promises.push(new WinJS.Promise(function (c, e, p) { finish = function (reason) { if (onTransitionEnd) { listener.removeEventListener("transitionend", onTransitionEnd, false); unregisterAction(id, transition.property); styleCache.removeName(style, transition.property, reason ? elem : null); clearTimeout(timeoutId); onTransitionEnd = null; } completePromise(c, reason === reason_canceled); }; var onTransitionEnd = function (event) { if (event.srcElement === elem && event.propertyName === transition.property) { finish(); } }; registerAction(id, transition.property, finish); listener.addEventListener("transitionend", onTransitionEnd, false); var padding = 0; if (style[transition.property] !== transition.to) { style[transition.property] = transition.to; padding = 50; } var timeoutId = setTimeout(function() { timeoutId = setTimeout(finish, transition.delay + transition.duration); }, padding); }, function () { finish(reason_canceled); })); }); } else { transitions.forEach(function (transition) { style[transition.property] = transition.to; }); } } } var elementAnimationProperties = { shorthandProp: "animation", nameProp: "animation-name", nameField: "keyframe", props: [ ["animation-duration", "duration", "ms"], ["animation-timing-function", "timing", ""], ["animation-delay", "delay", "ms"], ["animation-iteration-count", "", "1"], ["animation-direction", "", "normal"], ["animation-fill-mode", "", "both"] ], isTransition: false, styleCaches: [] }; function executeElementAnimation(elem, index, anims, promises, animate) { if (animate && anims.length > 0) { var style = elem.style; var id = elem.uniqueID; anims = anims.map(copyWithEvaluation(index, elem)); var styleElem; var listener = elem.disabled ? document : elem; anims.forEach(function (anim) { if (!anim.keyframe) { if (!styleElem) { styleElem = document.createElement("STYLE"); document.documentElement.appendChild(styleElem); } anim.keyframe = getUniqueKeyframeName(); var kf = "@keyframes " + anim.keyframe + " { from {" + anim.property + ":" + anim.from + ";} to {" + anim.property + ":" + anim.to + ";}}"; styleElem.sheet.insertRule(kf, 0); } }); var styleCache = setTemporaryStyles(elem, id, style, anims, elementAnimationProperties); anims.forEach(function (anim) { var finish; promises.push(new WinJS.Promise(function (c, e, p) { finish = function (reason) { if (onAnimationEnd) { listener.removeEventListener("animationend", onAnimationEnd, false); unregisterAction(id, anim.property); styleCache.removeName(style, anim.keyframe); clearTimeout(timeoutId); onAnimationEnd = null; } completePromise(c, reason === reason_canceled); }; var onAnimationEnd = function (event) { if (event.srcElement === elem && event.animationName === anim.keyframe) { finish(); } }; registerAction(id, anim.property, finish); var timeoutId = setTimeout(function() { timeoutId = setTimeout(finish, anim.delay + anim.duration); }, 50); listener.addEventListener("animationend", onAnimationEnd, false); }, function() { finish(reason_canceled); })); }); if (styleElem) { setTimeout(function () { var parentElement = styleElem.parentElement; if (parentElement) { parentElement.removeChild(styleElem); } }, 50); } } } var enableCount = 0; var animationSettings; function initAnimations() { if (!animationSettings) { if (WinJS.Utilities.hasWinRT) { animationSettings = new Windows.UI.ViewManagement.UISettings(); } else { animationSettings = { animationsEnabled: true }; } } } function isAnimationEnabled() { /// /// /// Determines whether the WinJS Animation Library will perform animations. /// /// /// true if WinJS animations will be performed. /// false if WinJS animations are suppressed. /// /// initAnimations(); return enableCount + animationSettings.animationsEnabled > 0; } function createImmediatePromise() { var complete; return new WinJS.Promise(function (c, e, p) { complete = c; setImmediate(c); }, function() { complete(); }); } function applyAction(element, action, execAction) { try { var animate = WinJS.UI.isAnimationEnabled(); var elems = makeArray(element); var actions = makeArray(action); var promises = []; for (var i = 0; i < elems.length; i++) { if (elems[i] instanceof Array) { for (var j = 0; j < elems[i].length; j++) { execAction(elems[i][j], i, actions, promises, animate); } } else { execAction(elems[i], i, actions, promises, animate); } } if (promises.length) { return WinJS.Promise.join(promises); } else { return createImmediatePromise(); } } catch (e) { return WinJS.Promise.wrapError(e); } } var UI = WinJS.Namespace.define("WinJS.UI", { disableAnimations: function () { /// /// /// Disables animations in the WinJS Animation Library /// by decrementing the animation enable count. /// /// enableCount--; }, enableAnimations: function () { /// /// /// Enables animations in the WinJS Animation Library /// by incrementing the animation enable count. /// /// enableCount++; }, isAnimationEnabled: isAnimationEnabled, _libraryDelay: 34, executeAnimation: function (element, animation) { /// /// /// Perform a CSS animation that can coexist with other /// Animation Library animations. Applications are not expected /// to call this function directly; they should prefer to use /// the high-level animations in the Animation Library. /// /// /// Single element or collection of elements on which /// to perform a CSS animation. /// /// /// Single animation description or array of animation descriptions. /// /// /// Promise object that completes when the CSS animation is complete. /// /// return applyAction(element, animation, executeElementAnimation); }, executeTransition: function (element, transition) { /// /// /// Perform a CSS transition that can coexist with other /// Animation Library animations. Applications are not expected /// to call this function directly; they should prefer to use /// the high-level animations in the Animation Library. /// /// /// Single element or collection of elements on which /// to perform a CSS transition. /// /// /// Single transition description or array of transition descriptions. /// /// /// Promise object that completes when the CSS transition is complete. /// /// return applyAction(element, transition, executeElementTransition); } }); })(this, WinJS); (function utilitiesInit(global, undefined) { "use strict"; var markSupportedForProcessing = WinJS.Utilities.markSupportedForProcessing; WinJS.Namespace.define("WinJS.UI", { eventHandler: function (handler) { /// /// /// Marks a event handler function as being compatible with declarative processing. /// /// /// The handler to be marked as compatible with declarative processing. /// /// /// The input handler. /// /// return markSupportedForProcessing(handler); } }); WinJS.Namespace.define("WinJS.Utilities", { _clamp: function (value, lowerBound, upperBound, defaultValue) { var n = Math.max(lowerBound, Math.min(upperBound, +value)); return n === 0 ? 0 : n || Math.max(lowerBound, Math.min(upperBound, defaultValue)); } }); }(this)); (function applicationInit(global, WinJS, undefined) { "use strict"; global.Debug && (global.Debug.setNonUserCodeExceptions = true); var checkpointET = "checkpoint", unloadET = "unload", activatedET = "activated", loadedET = "loaded", readyET = "ready", errorET = "error", settingsET = "settings"; var outstandingPromiseErrors; var eventQueue = []; var running = false; var registered = false; // check for WinRT & document, which means we will disabled application WinRT stuff in web worker context // var useWinRT = WinJS.Utilities.hasWinRT && global.document; var ListenerType = WinJS.Class.mix(WinJS.Class.define(null, { /* empty */ }, { supportedForProcessing: false }), WinJS.Utilities.eventMixin); var listeners = new ListenerType(); var pendingDeferrals = {}; var pendingDeferralID = 0; function safeSerialize(obj) { var str; try { str = JSON.stringify(obj); } catch (err) { // primitives, undefined, null, etc, all get serialized fine. In the // case that stringify fails (typically due to circular graphs) we // just show "[object]". While we may be able to tighten the condition // for the exception, we never way this serialize to fail. // // Note: we make this be a JSON string, so that consumers of the log // can always call JSON.parse. str = JSON.stringify("[object]"); } return str; } var pendingDrain; var terminateAppHandler = function (data, e) { // This is the unhandled exception handler in WinJS. This handler is invoked whenever a promise // has an exception occur that is not handled (via an error handler passed to then() or a call to done()). // // To see the original exception stack, look at data.stack. // For more information on debugging and exception handling go to http://go.microsoft.com/fwlink/p/?LinkId=253583. debugger; MSApp.terminateApp(data); }; function captureDeferral(obj) { var id = "def" + (pendingDeferralID++); return { deferral: pendingDeferrals[id] = obj.getDeferral(), id: id } } function completeDeferral(deferral, deferralID) { // If we have a deferralID we our table to find the // deferral. Since we remove it on completion, this // ensures that we never double notify a deferral // in the case of a user call "Application.stop" in // the middle of processing an event // if (deferralID) { deferral = pendingDeferrals[deferralID]; delete pendingDeferrals[deferralID]; } if (deferral) { deferral.complete(); } } function cleanupAllPendingDeferrals() { if (pendingDeferrals) { Object.keys(pendingDeferrals).forEach(function (k) { pendingDeferrals[k].complete(); }); pendingDeferrals = {}; } } function dispatchEvent(eventRecord) { msWriteProfilerMark("WinJS.Application:Event_" + eventRecord.type + ",StartTM"); var waitForPromise = WinJS.Promise.as(); eventRecord.setPromise = function (promise) { /// /// /// Used to inform the application object that asynchronous work is being performed, and that this /// event handler should not be considered complete until the promise completes. /// /// /// The promise to wait for. /// /// waitForPromise = waitForPromise.then(function () { return promise; }); }; eventRecord.detail = eventRecord.detail || {}; if (typeof (eventRecord.detail) === "object") { eventRecord.detail.setPromise = eventRecord.setPromise; } try { if (listeners._listeners) { var handled = false; l = listeners._listeners[eventRecord.type]; if (l) { l.forEach(function dispatchOne(e) { handled = e.listener(eventRecord) || handled; }); } } // Fire built in listeners last, for checkpoint this is important // as it lets our built in serialization see any mutations to // app.sessionState // var l = builtInListeners[eventRecord.type]; if (l) { l.forEach(function dispatchOne(e) { e(eventRecord, handled); }); } } catch (err) { queueEvent({ type: errorET, detail: err }); } function cleanup(r) { msWriteProfilerMark("WinJS.Application:Event_" + eventRecord.type + ",StopTM"); if (eventRecord._deferral) { completeDeferral(eventRecord._deferral, eventRecord._deferralID); } return r; } return waitForPromise.then(cleanup, function(r) { return WinJS.Promise.wrapError(cleanup(r)); }); } function drainQueue(queue) { pendingDrain = true; if (queue.length === 0) { if (eventQueue.length > 0) { return drainQueue(copyAndClearQueue()); } pendingDrain = false; return WinJS.Promise.as(queue); } function drainNext() { if (running) { return drainQueue(queue.slice(1)); } } function drainError(err) { queueEvent({ type: errorET, detail: err }); return drainNext(); } return dispatchEvent(queue[0]). then(drainNext, drainError); } function queueEvent(eventRecord) { /// /// /// Queues an event to be processed by the WinJS.Application event queue. /// /// /// The event object is expected to have a type property that is /// used as the event name when dispatching on the WinJS.Application /// event queue. The entire object is provided to event listeners /// in the detail property of the event. /// /// msWriteProfilerMark("WinJS.Application:Event_" + eventRecord.type + " queued,Info"); eventQueue.push(eventRecord); if (running && !pendingDrain) { drainQueue(copyAndClearQueue()); } } function copyAndClearQueue() { var queue = eventQueue; eventQueue = []; return queue; } var builtInListeners = { activated: [ function Application_activatedHandler() { queueEvent({ type: readyET }); } ], checkpoint: [ function Application_checkpointHandler(e) { // comes from state.js WinJS.Application._oncheckpoint(e); } ], error: [ function Application_errorHandler(e, handled) { if (handled) { return; } WinJS.log && WinJS.log(safeSerialize(e), "winjs", "error"); if (useWinRT && WinJS.Application._terminateApp) { var data = e.detail; var number = data && (data.number || (data.exception && data.exception.number) || (data.error && data.error.number) || data.errorCode || 0); var terminateData = { description: safeSerialize(data), // note: because of how we listen to events, we rarely get a stack stack: data && (data.stack || (data.exception && data.exception.stack) || (data.error && data.error.stack) || null), errorNumber: number, number: number }; WinJS.Application._terminateApp(terminateData, e); } } ], }; // loaded == DOMContentLoaded // activated == after WinRT Activated // ready == after all of the above // function activatedHandler(e) { var def = captureDeferral(e.activatedOperation); WinJS.Application._loadState(e).then(function () { queueEvent({ type: activatedET, detail: e, _deferral: def.deferral, _deferralID: def.id }); }); } function suspendingHandler(e) { var def = captureDeferral(e.suspendingOperation); WinJS.Application.queueEvent({ type: checkpointET, _deferral: def.deferral, _deferralID: def.id }); } function domContentLoadedHandler(e) { queueEvent({ type: loadedET }); if (!useWinRT) { var activatedArgs = { arguments: "", kind: "Windows.Launch", previousExecutionState: 0 //Windows.ApplicationModel.Activation.ApplicationExecutionState.NotRunning }; WinJS.Application._loadState(activatedArgs).then(function () { queueEvent({ type: activatedET, detail: activatedArgs }); }); } } function beforeUnloadHandler(e) { cleanupAllPendingDeferrals(); queueEvent({ type: unloadET }); } function errorHandler(e) { var flattenedError = {}; for (var k in e) { flattenedError[k] = e[k]; } var data; var handled = true; var prev = WinJS.Application._terminateApp; try { WinJS.Application._terminateApp = function (d, e) { handled = false; data = d; if (prev !== terminateAppHandler) { prev(d, e); } } dispatchEvent({ type: errorET, detail: { error: flattenedError, errorLine: event.errorLine, errorCharacter: event.errorCharacter, errorCode: event.errorCode, errorUrl: event.errorUrl, errorMessage: event.errorMessage } }); } finally { WinJS.Application._terminateApp = prev; } return handled; } function promiseErrorHandler(e) { // // e.detail looks like: { exception, error, promise, handler, id, parent } // var details = e.detail; var id = details.id; // If the error has a parent promise then this is not the origination of the // error so we check if it has a handler, and if so we mark that the error // was handled by removing it from outstandingPromiseErrors // if (details.parent) { if (details.handler && outstandingPromiseErrors) { delete outstandingPromiseErrors[id]; } return; } // If this is the first promise error to occur in this period we need to schedule // a helper to come along after a setImmediate that propagates any remaining // errors to the application's queue. // var shouldScheduleErrors = !outstandingPromiseErrors; // Indicate that this error was orignated and needs to be handled // outstandingPromiseErrors = outstandingPromiseErrors || []; outstandingPromiseErrors[id] = details; if (shouldScheduleErrors) { setImmediate(function () { var errors = outstandingPromiseErrors; outstandingPromiseErrors = null; errors.forEach(function (error) { queueEvent({ type: errorET, detail: error }); }); }); } } // capture this early // if (global.document) { document.addEventListener("DOMContentLoaded", domContentLoadedHandler, false); } function commandsRequested(e) { var event = { e: e, applicationcommands: undefined }; listeners.dispatchEvent(settingsET, event); } function register() { if (!registered) { registered = true; global.addEventListener("beforeunload", beforeUnloadHandler, false); if (useWinRT) { global.addEventListener("error", errorHandler, false); var wui = Windows.UI.WebUI.WebUIApplication; wui.addEventListener("activated", activatedHandler, false); wui.addEventListener("suspending", suspendingHandler, false); var settingsPane = Windows.UI.ApplicationSettings.SettingsPane.getForCurrentView(); settingsPane.addEventListener("commandsrequested", commandsRequested); } WinJS.Promise.addEventListener("error", promiseErrorHandler); } } function unregister() { if (registered) { registered = false; global.removeEventListener("beforeunload", beforeUnloadHandler, false); if (useWinRT) { global.removeEventListener("error", errorHandler, false); var wui = Windows.UI.WebUI.WebUIApplication; wui.removeEventListener("activated", activatedHandler, false); wui.removeEventListener("suspending", suspendingHandler, false); var settingsPane = Windows.UI.ApplicationSettings.SettingsPane.getForCurrentView(); settingsPane.removeEventListener("commandsrequested", commandsRequested); } WinJS.Promise.removeEventListener("error", promiseErrorHandler); } } WinJS.Namespace.define("WinJS.Application", { stop: function () { /// /// /// Stops application event processing and resets WinJS.Application /// to its initial state. /// /// // Need to clear out the event properties explicitly to clear their backing // state. // this.onactivated = null; this.oncheckpoint = null; this.onerror = null; this.onloaded = null; this.onready = null; this.onsettings = null; this.onunload = null; listeners = new ListenerType(); WinJS.Application.sessionState = {}; running = false; copyAndClearQueue(); unregister(); cleanupAllPendingDeferrals(); }, 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 is raised. /// /// /// true to initiate capture; otherwise, false. /// /// listeners.addEventListener(eventType, listener, capture); }, removeEventListener: function (eventType, listener, capture) { /// /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove. /// /// /// Specifies whether or not to initiate capture. /// /// listeners.removeEventListener(eventType, listener, capture); }, checkpoint: function () { /// /// /// Queues a checkpoint event. /// /// queueEvent({ type: checkpointET }); }, start: function () { /// /// /// Starts processing events in the WinJS.Application event queue. /// /// register(); var queue = copyAndClearQueue(); running = true; drainQueue(queue); }, queueEvent: queueEvent, _terminateApp: terminateAppHandler, }); Object.defineProperties(WinJS.Application, WinJS.Utilities.createEventProperties(checkpointET, unloadET, activatedET, loadedET, readyET, settingsET, errorET)); })(this, WinJS); (function navigationInit(WinJS, undefined) { "use strict"; var navigatedEventName = "navigated"; var navigatingEventName = "navigating"; var beforenavigateEventName = "beforenavigate"; var ListenerType = WinJS.Class.mix(WinJS.Class.define(null, { /* empty */ }, { supportedForProcessing: false }), WinJS.Utilities.eventMixin); var listeners = new ListenerType(); var history = { backStack: [], current: { location: "", initialPlaceholder: true }, forwardStack: [] }; var raiseBeforeNavigate = function (proposed) { msWriteProfilerMark("WinJS.Navigation:navigation,StartTM"); return WinJS.Promise.as(). then(function () { var waitForPromise = WinJS.Promise.as(); var defaultPrevented = listeners.dispatchEvent(beforenavigateEventName, { setPromise: function (promise) { /// /// /// Used to inform the ListView that asynchronous work is being performed, and that this /// event handler should not be considered complete until the promise completes. /// /// /// The promise to wait for. /// /// waitForPromise = waitForPromise.then(function() { return promise; }); }, location: proposed.location, state: proposed.state }); return waitForPromise.then(function beforeNavComplete(cancel) { return defaultPrevented || cancel; }); }); }; var raiseNavigating = function (delta) { return WinJS.Promise.as(). then(function () { var waitForPromise = WinJS.Promise.as(); listeners.dispatchEvent(navigatingEventName, { setPromise: function (promise) { /// /// /// Used to inform the ListView that asynchronous work is being performed, and that this /// event handler should not be considered complete until the promise completes. /// /// /// The promise to wait for. /// /// waitForPromise = waitForPromise.then(function() { return promise; }); }, location: history.current.location, state: history.current.state, delta: delta }); return waitForPromise; }); }; var raiseNavigated = function (value, err) { msWriteProfilerMark("WinJS.Navigation:navigation,StopTM"); var waitForPromise = WinJS.Promise.as(); var detail = { value: value, location: history.current.location, state: history.current.state, setPromise: function (promise) { /// /// /// Used to inform the ListView that asynchronous work is being performed, and that this /// event handler should not be considered complete until the promise completes. /// /// /// The promise to wait for. /// /// waitForPromise = waitForPromise.then(function() { return promise; }); } }; if (!value && err) { detail.error = err; } listeners.dispatchEvent(navigatedEventName, detail); return waitForPromise; }; var go = function (distance, fromStack, toStack, delta) { distance = Math.min(distance, fromStack.length); if (distance > 0) { return raiseBeforeNavigate(fromStack[fromStack.length - distance]). then(function goBeforeCompleted(cancel) { if (!cancel) { toStack.push(history.current); while (distance - 1 != 0) { distance--; toStack.push(fromStack.pop()); } history.current = fromStack.pop(); return raiseNavigating(delta).then( raiseNavigated, function (err) { raiseNavigated(undefined, err || true); throw err; }).then(function () { return true; }); } else { return false; } }); } return WinJS.Promise.wrap(false); } WinJS.Namespace.define("WinJS.Navigation", { /// /// Determines whether it is possible to navigate forwards. /// canGoForward: { get: function () { return history.forwardStack.length > 0; } }, /// /// Determines whether it is possible to navigate backwards. /// canGoBack: { get: function () { return history.backStack.length > 0; } }, /// /// Gets the current location. /// location: { get: function () { return history.current.location; } }, /// /// Gets or sets the navigation state. /// state: { get: function () { return history.current.state; }, set: function (value) { history.current.state = value; } }, /// /// Gets or sets the navigation history. /// history: { get: function () { return history; }, set: function (value) { var s = history = value; // ensure the require fields are present // s.backStack = s.backStack || []; s.forwardStack = s.forwardStack || []; s.current = s.current || { location: "", initialPlaceholder: true }; s.current.location = s.current.location || ""; } }, forward: function (distance) { /// /// /// Navigates forwards. /// /// /// The number of entries to go forward. /// /// /// A promise that is completed with a value that indicates whether or not /// the navigation was successful. /// /// distance = distance || 1; return go(distance, history.forwardStack, history.backStack, distance); }, back: function (distance) { /// /// /// Navigates backwards. /// /// /// The number of entries to go back into the history. /// /// /// A promise that is completed with a value that indicates whether or not /// the navigation was successful. /// /// distance = distance || 1; return go(distance, history.backStack, history.forwardStack, -distance); }, navigate: function (location, initialState) { /// /// /// Navigates to a location. /// /// /// The location to navigate to. Generally the location is a string, but /// it may be anything. /// /// /// The navigation state that may be accessed through WinJS.Navigation.state. /// /// /// A promise that is completed with a value that indicates whether or not /// the navigation was successful. /// /// var proposed = { location: location, state: initialState }; return raiseBeforeNavigate(proposed). then(function navBeforeCompleted(cancel) { if (!cancel) { if (!history.current.initialPlaceholder) { history.backStack.push(history.current); } history.forwardStack = []; history.current = proposed; // error or no, we go from navigating -> navigated // cancelation should be handled with "beforenavigate" // return raiseNavigating().then( raiseNavigated, function (err) { raiseNavigated(undefined, err || true); throw err; }).then(function () { return true; }); } else { return false; } }); }, 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); }, removeEventListener: function (eventType, listener, capture) { /// /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove. /// /// /// Specifies whether or not to initiate capture. /// /// listeners.removeEventListener(eventType, listener, capture); } }); Object.defineProperties(WinJS.Navigation, WinJS.Utilities.createEventProperties(navigatedEventName, navigatingEventName, beforenavigateEventName)); })(WinJS); (function stateInit(global) { "use strict"; function initWithWinRT() { var local, temp, roaming; var IOHelper = WinJS.Class.define( function IOHelper_ctor(folder) { this.folder = folder; this._path = folder.path; }, { exists: function (fileName) { /// /// /// Determines if the specified file exists in the container /// /// /// The file which may exist within this folder /// /// /// Promise with either true (file exists) or false. /// /// return this.folder.getFileAsync(fileName). then( function () { return true; }, function () { return false; } ); }, remove: function (fileName) { /// /// /// Delets a file in the container /// /// /// The file to be deleted /// /// /// Promise which is fulfilled when the file has been deleted /// /// var that = this; return that.folder.getFileAsync(fileName). then( function (fileItem) { return fileItem.deleteAsync(); }, function () { return false; } ); }, writeText: function (fileName, str) { /// /// /// Writes a file to the container with the specified text /// /// /// The file to write to /// /// /// Content to be written to the file /// /// /// Promise with the count of characters written /// /// var sto = Windows.Storage; var that = this; return that.folder.createFileAsync(fileName, sto.CreationCollisionOption.openIfExists). then(function (fileItem) { if (sto.FileIO) { return sto.FileIO.writeTextAsync(fileItem, str); } else { return sto.StorageHelpers.writeAllTextUsingFileAsync(fileItem, str); } }); }, readText: function (fileName, def) { /// /// /// Reads the contents of a file from the container, if the file /// doesn't exist, def is returned. /// /// /// The file to read from /// /// /// Default value to be returned if the file failed to open /// /// /// Promise containing the contents of the file, or def. /// /// var sto = Windows.Storage; var that = this; function onerror() { return def; } return that.folder.getFileAsync(fileName). then(function (fileItem) { if (sto.FileIO) { return sto.FileIO.readTextAsync(fileItem); } else { return sto.StorageHelpers.readAllTextUsingFileAsync(fileItem); } }, onerror); } }, { supportedForProcessing: false, }); WinJS.Namespace.define("WinJS.Application", { /// /// Allows access to create files in the application local storage, which is preserved across runs /// of an application and does not roam. /// local: { get: function () { if (!local) { local = new IOHelper(Windows.Storage.ApplicationData.current.localFolder); } return local; } }, /// /// Allows access to create files in the application temp storage, which may be reclaimed /// by the system between application runs. /// temp: { get: function () { if (!temp) { temp = new IOHelper(Windows.Storage.ApplicationData.current.temporaryFolder); } return temp; } }, /// /// Allows access to create files in the application roaming storage, which is preserved across runs /// of an application and roams with the user across multiple machines. /// roaming: { get: function () { if (!roaming) { roaming = new IOHelper(Windows.Storage.ApplicationData.current.roamingFolder); } return roaming; } } }); }; function initWithStub() { var InMemoryHelper = WinJS.Class.define( function InMemoryHelper_ctor() { this.storage = {}; }, { exists: function (fileName) { /// /// /// Determines if the specified file exists in the container /// /// /// The filename which may exist within this folder /// /// /// Promise with either true (file exists) or false. /// /// // force conversion to boolean // return WinJS.Promise.as(this.storage[fileName] !== undefined); }, remove: function (fileName) { /// /// /// Deletes a file in the container /// /// /// The file to be deleted /// /// /// Promise which is fulfilled when the file has been deleted /// /// delete this.storage[fileName]; return WinJS.Promise.as(); }, writeText: function (fileName, str) { /// /// /// Writes a file to the container with the specified text /// /// /// The filename to write to /// /// /// Content to be written to the file /// /// /// Promise with the count of characters written /// /// this.storage[fileName] = str; return WinJS.Promise.as(str.length); }, readText: function (fileName, def) { /// /// /// Reads the contents of a file from the container, if the file /// doesn't exist, def is returned. /// /// /// The filename to read from /// /// /// Default value to be returned if the file failed to open /// /// /// Promise containing the contents of the file, or def. /// /// var result = this.storage[fileName]; return WinJS.Promise.as(typeof result === "string" ? result : def); } }, { supportedForProcessing: false, } ); WinJS.Namespace.define("WinJS.Application", { /// /// Allows access to create files in the application local storage, which is preserved across runs /// of an application and does not roam. /// local: new InMemoryHelper(), /// /// Allows access to create files in the application temp storage, which may be reclaimed /// by the system between application runs. /// temp: new InMemoryHelper(), /// /// Allows access to create files in the application roaming storage, which is preserved across runs /// of an application and roams with the user across multiple machines. /// roaming: new InMemoryHelper() }); } if (WinJS.Utilities.hasWinRT) { initWithWinRT(); } else { initWithStub(); } WinJS.Namespace.define("WinJS.Application", { sessionState: {}, _loadState: function (e) { var app = WinJS.Application; // we only restore state if we are coming back from a clear termination from PLM // if (e.previousExecutionState === 3 /* ApplicationExecutionState.Terminated */) { return app.local.readText("_sessionState.json", "{}"). then(function (str) { var sessionState = JSON.parse(str); if (sessionState && Object.keys(sessionState).length > 0) { app._sessionStateLoaded = true; } app.sessionState = sessionState; }). then(null, function (err) { app.queueEvent({ type: "error", detail: err }); }); } else { return WinJS.Promise.as(); } }, _oncheckpoint: function (e) { var app = WinJS.Application; var sessionState = app.sessionState; if ((sessionState && Object.keys(sessionState).length > 0) || app._sessionStateLoaded) { e.setPromise( app.local.writeText("_sessionState.json", JSON.stringify(sessionState)). then(null, function (err) { app.queueEvent({ type: "error", detail: err }); }) ); } } }); })(this); (function bindingParserInit(global, undefined) { "use strict"; var strings = { get invalidBinding() { return WinJS.Resources._getWinJSString("base/invalidBinding").value; }, get bindingInitializerNotFound() { return WinJS.Resources._getWinJSString("base/bindingInitializerNotFound").value; }, }; /* See comment for data-win-options attribute grammar for context. Syntactic grammar for the value of the data-win-bind attribute. BindDeclarations: BindDeclaration BindDeclarations ; BindDeclaration BindDeclaration: DestinationPropertyName : SourcePropertyName DestinationPropertyName : SourcePropertyName InitializerName DestinationPropertyName: IdentifierExpression SourcePropertyName: IdentifierExpression InitializerName: IdentifierExpression Value: NumberLiteral StringLiteral AccessExpression: [ Value ] . Identifier AccessExpressions: AccessExpression AccessExpressions AccessExpression IdentifierExpression: Identifier Identifier AccessExpressions */ var lexer = WinJS.UI._optionsLexer; var tokenType = lexer.tokenType; var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing; var BindingInterpreter = WinJS.Class.derive(WinJS.UI.optionsParser._BaseInterpreter, function (tokens, originalSource, context) { this._initialize(tokens, originalSource, context); }, { _error: function (message) { throw new WinJS.ErrorFromName("WinJS.Binding.ParseError", WinJS.Resources._formatString(strings.invalidBinding, this._originalSource, message)); }, _evaluateInitializerName: function () { if (this._current.type === tokenType.identifier) { var initializer = this._evaluateIdentifierExpression(); if (WinJS.log && !initializer) { WinJS.log(WinJS.Resources._formatString(strings.bindingInitializerNotFound, this._originalSource), "winjs binding", "error"); } return requireSupportedForProcessing(initializer); } return; }, _evaluateValue: function () { switch (this._current.type) { case tokenType.stringLiteral: case tokenType.numberLiteral: var value = this._current.value; this._read(); return value; default: this._unexpectedToken(tokenType.stringLiteral, tokenType.numberLiteral); return; } }, _readBindDeclarations: function () { var bindings = []; while (true) { switch (this._current.type) { case tokenType.identifier: case tokenType.thisKeyword: bindings.push(this._readBindDeclaration()); break; case tokenType.semicolon: this._read(); break; case tokenType.eof: return bindings; default: this._unexpectedToken(tokenType.identifier, tokenType.semicolon, tokenType.eof); return; } } }, _readBindDeclaration: function () { var dest = this._readDestinationPropertyName(); this._read(tokenType.colon); var src = this._readSourcePropertyName(); var initializer = this._evaluateInitializerName(); return { destination: dest, source: src, initializer: initializer }; }, _readDestinationPropertyName: function () { return this._readIdentifierExpression(); }, _readSourcePropertyName: function () { return this._readIdentifierExpression(); }, run: function () { return this._readBindDeclarations(); } }, { supportedForProcessing: false, } ); function parser(text, context) { msWriteProfilerMark("WinJS.Binding:bindingParser,StartTM"); var tokens = lexer(text); var interpreter = new BindingInterpreter(tokens, text, context || {}); var res = interpreter.run(); msWriteProfilerMark("WinJS.Binding:bindingParser,StopTM"); return res; } WinJS.Namespace.define("WinJS.Binding", { _bindingParser: parser }); })(this); (function dataInit(WinJS, undefined) { "use strict"; var strings = { get exceptionFromBindingInitializer() { return WinJS.Resources._getWinJSString("base/exceptionFromBindingInitializer").value; }, get propertyIsUndefined() { return WinJS.Resources._getWinJSString("base/propertyIsUndefined").value; }, get unsupportedDataTypeForBinding() { return WinJS.Resources._getWinJSString("base/unsupportedDataTypeForBinding").value; }, }; var observableMixin = { _listeners: null, _pendingNotifications: null, _notifyId: 0, _getObservable: function () { return this; }, _cancel: function (name) { var v = this._pendingNotifications; var hit = false; if (v) { var k = Object.keys(v); for (var i = k.length - 1; i >= 0; i--) { var entry = v[k[i]]; if (entry.target === name) { if (entry.promise) { entry.promise.cancel(); entry.promise = null; } delete v[k[i]]; hit = true; } } } return hit; }, notify: function (name, newValue, oldValue) { /// /// /// Notifies listeners that a property value was updated. /// /// The name of the property that is being updated. /// The new value for the property. /// The old value for the property. /// A promise that is completed when the notifications are complete. /// var listeners = this._listeners && this._listeners[name]; if (listeners) { var that = this; // Handle the case where we are updating a value that is currently updating // that._cancel(name); // Starting new work, we cache the work description and queue up to do the notifications // that._pendingNotifications = that._pendingNotifications || {}; var x = that._notifyId++; var cap = that._pendingNotifications[x] = { target: name }; var cleanup = function () { delete that._pendingNotifications[x]; } // Binding guarantees async notification, so we do timeout() // cap.promise = WinJS.Promise.timeout(). then(function () { // cap.promise is removed after canceled, so we use this as a signal // to indicate that we should abort early // for (var i = 0, l = listeners.length; i < l && cap.promise; i++) { try { listeners[i](newValue, oldValue); } catch (e) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.exceptionFromBindingInitializer, e.toString()), "winjs binding", "error"); } } }). then(cleanup). then(function () { return newValue; }); return cap.promise; } return WinJS.Promise.as(); }, bind: function (name, action) { /// /// /// Links the specified action to the property specified in the name parameter. /// This function is invoked when the value of the property may have changed. /// It is not guaranteed that the action will be called only when a value has actually changed, /// nor is it guaranteed that the action will be called for every value change. The implementation /// of this function coalesces change notifications, such that multiple updates to a property /// value may result in only a single call to the specified action. /// /// /// The name of the property to which to bind the action. /// /// /// The function to invoke asynchronously when the property may have changed. /// /// /// This object is returned. /// /// this._listeners = this._listeners || {}; var listeners = this._listeners[name] = this._listeners[name] || []; // duplicate detection, multiple binds with the same action should have no effect // var found = false; for (var i = 0, l = listeners.length; i < l; i++) { if (listeners[i] === action) { found = true; break; } } if (!found) { listeners.push(action); // out of band notification, we want to avoid a broadcast to all listeners // so we can't just call notify. // action(unwrap(this[name])); } return this; }, unbind: function (name, action) { /// /// /// Removes one or more listeners from the notification list for a given property. /// /// /// The name of the property to unbind. If this parameter is omitted, all listeners /// for all events are removed. /// /// /// The function to remove from the listener list for the specified property. If this parameter is omitted, all listeners /// are removed for the specific property. /// /// /// This object is returned. /// /// this._listeners = this._listeners || {}; if (name && action) { // this assumes we rarely have more than one // listener, so we optimize to not do a lot of // array manipulation, although it means we // may do some extra GC churn in the other cases... // var listeners = this._listeners[name]; if (listeners) { var nl; for (var i = 0, l = listeners.length; i < l; i++) { if (listeners[i] !== action) { (nl = nl || []).push(listeners[i]); } } this._listeners[name] = nl; } // we allow any pending notification sweep to complete, // which means that "unbind" inside of a notification // will not prevent that notification from occuring. // } else if (name) { this._cancel(name); delete this._listeners[name]; } else { var that = this; if (that._pendingNotifications) { var v = that._pendingNotifications; that._pendingNotifications = {}; Object.keys(v).forEach(function (k) { var n = v[k]; if (n.promise) { n.promise.cancel(); } }); } this._listeners = {}; } return this; } }; var dynamicObservableMixin = { _backingData: null, _initObservable: function (data) { this._backingData = data || {}; }, getProperty: function (name) { /// /// /// Gets a property value by name. /// /// /// The name of property to get. /// /// /// The value of the property as an observable object. /// /// var data = this._backingData[name]; if (WinJS.log && data === undefined) { WinJS.log(WinJS.Resources._formatString(strings.propertyIsUndefined, name), "winjs binding", "warn"); } return as(data); }, setProperty: function (name, value) { /// /// /// Updates a property value and notifies any listeners. /// /// /// The name of the property to update. /// /// /// The new value of the property. /// /// /// This object is returned. /// /// this.updateProperty(name, value); return this; }, addProperty: function (name, value) { /// /// /// Adds a property with change notification to this object, including a ECMAScript5 property definition. /// /// /// The name of the property to add. /// /// /// The value of the property. /// /// /// This object is returned. /// /// // we could walk Object.keys to more deterministically determine this, // however in the normal case this avoids a bunch of string compares // if (!this[name]) { Object.defineProperty(this, name, { get: function () { return this.getProperty(name); }, set: function (value) { this.setProperty(name, value); }, enumerable: true, configurable: true } ); } return this.setProperty(name, value); }, updateProperty: function (name, value) { /// /// /// Updates a property value and notifies any listeners. /// /// /// The name of the property to update. /// /// /// The new value of the property. /// /// /// A promise that completes when the notifications for /// this property change have been processed. If multiple notifications are coalesced, /// the promise may be canceled or the value of the promise may be updated. /// The fulfilled value of the promise is the new value of the property for /// which the notifications have been completed. /// /// var oldValue = this._backingData[name]; var newValue = unwrap(value); if (oldValue !== newValue) { this._backingData[name] = newValue; // This will complete when the listeners are notified, even // if a new value is used. The only time this promise will fail // (cancel) will be if we start notifying and then have to // cancel in the middle of processing it. That's a pretty // subtle contract. // return this.notify(name, newValue, oldValue); } return WinJS.Promise.as(); }, removeProperty: function (name) { /// /// /// Removes a property value. /// /// /// The name of the property to remove. /// /// /// This object is returned. /// /// var oldValue = this._backingData[name]; var value; // capture "undefined" // in strict mode these may throw try { delete this._backingData[name]; } catch (e) { } try { delete this[name]; } catch (e) { } this.notify(name, value, oldValue); return this; } }; // Merge "obsevable" into "dynamicObservable" // Object.keys(observableMixin).forEach(function (k) { dynamicObservableMixin[k] = observableMixin[k]; }); var bind = function (observable, bindingDescriptor) { /// /// /// Binds to one or more properties on the observable object or or on child values /// of that object. /// /// /// The object to bind to. /// /// /// An object literal containing the binding declarations. Binding declarations take the form: /// { propertyName: (function | bindingDeclaration), ... } /// /// For example, binding to a nested member of an object is declared like this: /// bind(someObject, { address: { street: function(v) { ... } } }); /// /// /// An object that contains at least a "cancel" field, which is /// a function that removes all bindings associated with this bind /// request. /// /// return bindImpl(observable, bindingDescriptor); } var bindRefId = 0; var createBindRefId = function () { return "bindHandler" + (bindRefId++); }; var createProxy = function (func, bindStateRef) { if (!WinJS.Utilities.hasWinRT) { return func; } var id = createBindRefId(); WinJS.Utilities._getWeakRefElement(bindStateRef)[id] = func; return function (n, o) { var bindState = WinJS.Utilities._getWeakRefElement(bindStateRef); if (bindState) { bindState[id](n, o); } }; } var bindImpl = function (observable, bindingDescriptor, bindStateRef) { observable = WinJS.Binding.as(observable); if (!observable) { return { cancel: function () { }, empty: true }; } var bindState; if (!bindStateRef) { bindStateRef = createBindRefId(); bindState = {}; WinJS.Utilities._createWeakRef(bindState, bindStateRef); } var complexLast = {}; var simpleLast = null; function cancelSimple() { if (simpleLast) { simpleLast.forEach(function (e) { e.source.unbind(e.prop, e.listener); }); } simpleLast = null; } function cancelComplex(k) { if (complexLast[k]) { complexLast[k].complexBind.cancel(); delete complexLast[k]; } } Object.keys(bindingDescriptor).forEach(function (k) { var listener = bindingDescriptor[k]; if (listener instanceof Function) { // Create a proxy for the listener which indirects weakly through the bind // state, if this is the root object tack the bind state onto the listener // listener = createProxy(listener, bindStateRef); listener.bindState = bindState; simpleLast = simpleLast || []; simpleLast.push({ source: observable, prop: k, listener: listener }); observable.bind(k, listener); } else { var propChanged = function (v) { cancelComplex(k); var complexBind = bindImpl(as(v), listener, bindStateRef); // In the case that we hit an "undefined" in the chain, we prop the change // notification to all listeners, basically saying that x.y.z where "y" // is undefined resolves to undefined. // if (complexBind.empty) { var recursiveNotify = function (root) { Object.keys(root).forEach(function (key) { var item = root[key]; if (item instanceof Function) { item(undefined, undefined); } else { recursiveNotify(item); } }); }; recursiveNotify(listener); } complexLast[k] = { source: v, complexBind: complexBind }; }; // Create a proxy for the listener which indirects weakly through the bind // state, if this is the root object tack the bind state onto the listener // propChanged = createProxy(propChanged, bindStateRef); propChanged.bindState = bindState; simpleLast = simpleLast || []; simpleLast.push({ source: observable, prop: k, listener: propChanged }); observable.bind(k, propChanged); } }); return { cancel: function () { cancelSimple(); Object.keys(complexLast).forEach(function (k) { cancelComplex(k); }); } } }; var ObservableProxy = WinJS.Class.mix(function (data) { this._initObservable(data); Object.defineProperties(this, expandProperties(data)); }, dynamicObservableMixin); var expandProperties = function (shape) { /// /// /// Wraps the specified object so that all its properties /// are instrumented for binding. This is meant to be used in /// conjunction with the binding mixin. /// /// /// The specification for the bindable object. /// /// /// An object with a set of properties all of which are wired for binding. /// /// var props = {}; function addToProps(k) { props[k] = { get: function () { return this.getProperty(k); }, set: function (value) { this.setProperty(k, value); }, enumerable: true, configurable: true // enables delete }; } while (shape && shape !== Object.prototype) { Object.keys(shape).forEach(addToProps); shape = Object.getPrototypeOf(shape); } return props; }; var define = function (data) { /// /// /// Creates a new constructor function that supports observability with /// the specified set of properties. /// /// /// The object to use as the pattern for defining the set of properties, for example: /// var MyPointClass = define({x:0,y:0}); /// /// /// A constructor function with 1 optional argument that is the initial state of /// the properties. /// /// // Common unsupported types, we just coerce to be an empty record // if (!data || typeof (data) !== "object" || (data instanceof Date) || (data instanceof Array)) { if (WinJS.validation) { throw new WinJS.ErrorFromName("WinJS.Binding.UnsupportedDataType", WinJS.Resources._formatString(strings.unsupportedDataTypeForBinding)); } else { return; } } return WinJS.Class.mix( function (init) { /// /// /// Creates a new observable object. /// /// /// The initial values for the properties. /// /// this._initObservable(init || Object.create(data)); }, WinJS.Binding.dynamicObservableMixin, WinJS.Binding.expandProperties(data) ); }; var as = function (data) { /// /// /// Returns an observable object. This may be an observable proxy for the specified object, an existing proxy, or /// the specified object itself if it directly supports observability. /// /// /// Object to provide observability for. /// /// /// The observable object. /// /// if (!data) { return data; } var type = typeof data; if (type === "object" && !(data instanceof Date) && !(data instanceof Array)) { if (data._getObservable) { return data._getObservable(); } var observable = new ObservableProxy(data); observable.backingData = data; Object.defineProperty( data, "_getObservable", { value: function () { return observable; }, enumerable: false, writable: false } ); return observable; } else { return data; } }; var unwrap = function (data) { /// /// /// Returns the original (non-observable) object is returned if the specified object is an observable proxy, . /// /// /// The object for which to retrieve the original value. /// /// /// If the specified object is an observable proxy, the original object is returned, otherwise the same object is returned. /// /// if (data && data.backingData) return data.backingData; else return data; }; WinJS.Namespace.define("WinJS.Binding", { // must use long form because mixin has "get" and "set" as members, so the define // method thinks it's a property mixin: { value: dynamicObservableMixin, enumerable: false, writable: true, configurable: true }, dynamicObservableMixin: { value: dynamicObservableMixin, enumerable: true, writable: true, configurable: true }, observableMixin: { value: observableMixin, enumerable: true, writable: true, configurable: true }, expandProperties: expandProperties, define: define, as: as, unwrap: unwrap, bind: bind }); })(WinJS); (function dataTemplateInit(WinJS, undefined) { "use strict"; var markSupportedForProcessing = WinJS.Utilities.markSupportedForProcessing; WinJS.Namespace.define("WinJS.Binding", { /// /// Provides a reusable declarative binding element. /// /// Template ///
Place content here
]]>
/// /// /// /// Template: WinJS.Class.define( function Template_ctor(element, options) { /// /// /// Creates a template that provides a reusable declarative binding element. /// /// /// The DOM element to convert to a template. /// /// /// If this parameter is supplied, the template is loaded from the URI and /// the content of the element parameter is ignored. /// /// msWriteProfilerMark("WinJS.Binding:newTemplate,StartTM"); this._element = element = element || document.createElement("div"); var that = this; if (element) { element.renderItem = function (item, recycled) { return that.renderItem(item, recycled); }; } if (options) { this.href = options.href; this.enableRecycling = !!options.enableRecycling; if (options.processTimeout) { this.processTimeout = options.processTimeout; } } if (!this.href) { this.element.style.display = "none"; } this.bindingCache = { expressions: {} }; msWriteProfilerMark("WinJS.Binding:newTemplate,StopTM"); }, { /// /// Number of milliseconds to delay instantiating declarative controls. Zero (0) will result in no delay, any negative number /// will result in a setImmediate delay, any positive number will be treated as the number of milliseconds. /// processTimeout: 0, /// element: { get: function () { return this._element; }, }, renderItem: function (item, recycled) { /// /// /// Renders an instance of this template bound to the data contained in item. If /// the recycled parameter is present, and enableRecycling is true, then the template attempts /// to reuse the DOM elements from the recycled parameter. /// /// /// The object that contains the data to bind to. Only item.data is required. /// /// /// A previously-generated template instance. /// /// /// The DOM element. /// /// var that = this; // we only enable element cache when we are trying // to recycle. Otherwise our element cache would // grow unbounded. // if (this.enableRecycling && !this.bindingCache.elements) { this.bindingCache.elements = {}; } if (this.enableRecycling && recycled && recycled.msOriginalTemplate === this) { // If we are asked to recycle, we cleanup any old work no matter what // var cacheEntry = this.bindingCache.elements[recycled.id]; var okToReuse = true; if (cacheEntry) { cacheEntry.bindings.forEach(function (v) { v(); }); cacheEntry.bindings = []; okToReuse = !cacheEntry.nocache; } // If our cache indicates that we hit a non-cancelable thing, then we are // in an unknown state, so we actually can't recycle the tree. We have // cleaned up what we can, but at this point we need to reset and create // a new tree. // if (okToReuse) { // Element recycling requires that there be no other content in "recycled" other than this // templates' output. // var renderComplete = WinJS.Promise.as(item).then(function (i) { WinJS.Binding.processAll(recycled, i.data, true, that.bindingCache); }); return { element: recycled, renderComplete: renderComplete }; } } var render = this._renderImpl(item.then(function (i) { return i.data; })); render.element = render.element.then(function (e) { e.msOriginalTemplate = that; return e; }); return render; }, render: function (dataContext, container) { /// /// /// Binds values from the specified data context to elements that are descendents of the specified root element /// and have the declarative binding attributes (data-win-bind). /// /// /// The object to use for default data binding. /// /// /// The element to which to add this rendered template. If this parameter is omitted, a new DIV is created. /// /// /// A promise that is completed after binding has finished. The value is /// either the element specified in the container parameter or the created DIV. /// /// var render = this._renderImpl(dataContext, container); return render.element.then(function () { return render.renderComplete; }); }, _renderImpl: function (dataContext, container) { msWriteProfilerMark("WinJS.Binding:templateRender,StartTM"); var d = container || document.createElement("div"); WinJS.Utilities.addClass(d, "win-template"); WinJS.Utilities.addClass(d, "win-loading"); var that = this; function done() { WinJS.Utilities.removeClass(d, "win-loading"); msWriteProfilerMark("WinJS.Binding:templateRender,StopTM"); return d; } var initial = d.children.length; var element = WinJS.UI.Fragments.renderCopy(that.href || that.element, d); var renderComplete = element. then(function Template_renderImpl_renderComplete_then() { var work; // If no existing children, we can do the faster path of just calling // on the root element... // if (initial === 0) { work = function (f, a, b, c) { return f(d, a, b, c); }; } // We only grab the newly added nodes (always at the end) // and in the common case of only adding a single new element // we avoid the "join" overhead // else { var all = d.children; if (all.length === initial + 1) { work = function (f, a, b, c) { return f(all[initial], a, b, c); }; } else { // we have to capture the elements first, in case // doing the work affects the children order/content // var elements = []; for (var i = initial, l = all.length; i < l; i++) { elements.push(all[i]); } work = function (f, a, b, c) { var join = []; elements.forEach(function (e) { join.push(f(e, a, b, c)); }); return WinJS.Promise.join(join); }; } } var child = d.firstElementChild; while (child) { child.msParentSelectorScope = true; child = child.nextElementSibling; } // This allows "0" to mean no timeout (at all) and negative values // mean setImmediate (no setTimeout). Since Promise.timeout uses // zero to mean setImmediate, we have to coerce. // var timeout = that.processTimeout; function complete() { return work(WinJS.UI.processAll). then(function () { return dataContext; }). then(function Template_renderImpl_Binding_processAll(data) { // !initial -- skipRoot when we do process on the container return work(WinJS.Binding.processAll, data, !initial, that.bindingCache); }); } if (timeout) { if (timeout < 0) { timeout = 0; } return WinJS.Promise.timeout(timeout).then(complete); } else { return complete(); } }).then(done, function (err) { done(); return WinJS.Promise.wrapError(err); }); return { element: element, renderComplete: renderComplete }; } } ) }); markSupportedForProcessing(WinJS.Binding.Template.prototype.render); Object.defineProperties(WinJS.Binding.Template, { isDeclarativeControlContainer: { value: true, writable: false, configurable: false }, render: { value: function (href, dataContext, container) { /// /// /// Renders a template based on a URI. /// /// /// The URI from which to load the template. /// /// /// The object to use for default data binding. /// /// /// The element to which to add this rendered template. If this parameter is omitted, a new DIV is created. /// /// /// A promise that is completed after binding has finished. The value is /// either the object in the container parameter or the created DIV. /// /// return new WinJS.Binding.Template(null, { href: href }).render(dataContext, container); } } }); if (WinJS.Utilities && WinJS.Utilities.QueryCollection) { WinJS.Class.mix(WinJS.Utilities.QueryCollection, { template: function (templateElement, data, renderDonePromiseCallback) { /// /// /// Renders a template that is bound to the given data /// and parented to the elements included in the QueryCollection. /// If the QueryCollection contains multiple elements, the template /// is rendered multiple times, once at each element in the QueryCollection /// per item of data passed. /// /// /// The DOM element to which the template control is attached to. /// /// /// The data to render. If the data is an array (or any other object /// that has a forEach method) then the template is rendered /// multiple times, once for each item in the collection. /// /// /// If supplied, this function is called /// each time the template gets rendered, and is passed a promise /// that is fulfilled when the template rendering is complete. /// /// /// The QueryCollection. /// /// if (templateElement instanceof WinJS.Utilities.QueryCollection) { templateElement = templateElement[0]; } var template = templateElement.winControl; if (data === null || data === undefined || !data.forEach) { data = [data]; } renderDonePromiseCallback = renderDonePromiseCallback || function () { }; var that = this; var donePromises = []; data.forEach(function (datum) { that.forEach(function (element) { donePromises.push(template.render(datum, element)); }); }); renderDonePromiseCallback(WinJS.Promise.join(donePromises)); return this; } }); } })(WinJS); (function declarativeInit(WinJS, global, undefined) { "use strict"; var uid = (Math.random() * 1000) >> 0; var optimizeBindingReferences = false; var strings = { get attributeBindingSingleProperty() { return WinJS.Resources._getWinJSString("base/attributeBindingSingleProperty").value; }, get cannotBindToThis() { return WinJS.Resources._getWinJSString("base/cannotBindToThis").value; }, get creatingNewProperty() { return WinJS.Resources._getWinJSString("base/creatingNewProperty").value; }, get dataSourceNotFound() { return WinJS.Resources._getWinJSString("base/dataSourceNotFound").value; }, get duplicateBindingDetected() { return WinJS.Resources._getWinJSString("base/duplicateBindingDetected").value; }, get elementNotFound() { return WinJS.Resources._getWinJSString("base/elementNotFound").value; }, get errorInitializingBindings() { return WinJS.Resources._getWinJSString("base/errorInitializingBindings").value; }, get propertyDoesNotExist() { return WinJS.Resources._getWinJSString("base/propertyDoesNotExist").value; }, get idBindingNotSupported() { return WinJS.Resources._getWinJSString("base/idBindingNotSupported").value; }, get nestedDOMElementBindingNotSupported() { return WinJS.Resources._getWinJSString("base/nestedDOMElementBindingNotSupported").value; } } var markSupportedForProcessing = WinJS.Utilities.markSupportedForProcessing; var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing; function registerAutoDispose(bindable, callback) { var d = bindable._autoDispose; d && d.push(callback); } function autoDispose(bindable) { bindable._autoDispose = (bindable._autoDispose || []).filter(function (callback) { return callback(); }); } function inContainer(baseElement, control, start) { if (control && control.constructor.isDeclarativeControlContainer) { return true; } if (start && start !== baseElement && start.parentNode && start.parentNode !== baseElement) { start = start.parentNode; return inContainer(baseElement, start.winControl, start); } return false; } function checkBindingToken(element, bindingId) { if (element) { if (element.winBindingToken === bindingId) { return element; } else { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.duplicateBindingDetected, element.id), "winjs binding", "error"); } } else { return element; } } function setBindingToken(element) { if (element.winBindingToken) { return element.winBindingToken; } var bindingToken = "_win_bind" + (uid++); Object.defineProperty(element, "winBindingToken", { configurable: false, writable: false, enumerable: false, value: bindingToken }); return bindingToken; } function initializerOneBinding(bind, ref, bindingId, source, e, pend, cacheEntry) { var initializer = bind.initializer; if (initializer) { initializer = initializer.winControl || initializer["data-win-control"] || initializer; } if (initializer instanceof Function) { var result = initializer(source, bind.source, e, bind.destination); if (cacheEntry) { if (result && result.cancel) { cacheEntry.bindings.push(function() { result.cancel(); }); } else { // notify the cache that we encountered an uncancellable thing // cacheEntry.nocache = true; } } return result; } else if (initializer && initializer.render) { pend.count++; // notify the cache that we encountered an uncancellable thing // if (cacheEntry) { cacheEntry.nocache = true; } requireSupportedForProcessing(initializer.render).call(initializer, getValue(source, bind.source), e). then(function () { pend.checkComplete(); }); } } function makeBinding(ref, bindingId, pend, bindable, bind, cacheEntry) { var first = true; var bindResult; var canceled = false; autoDispose(bindable); var resolveWeakRef = function () { if (canceled) { return; } var found = checkBindingToken(WinJS.Utilities._getWeakRefElement(ref), bindingId); if (!found) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.elementNotFound, ref), "winjs binding", "info"); if (bindResult) { bindResult.cancel(); } } return found; } var bindingAction = function (v) { var found = resolveWeakRef(); if (found) { nestedSet(found, bind.destination, v); } if (first) { pend.checkComplete(); first = false; } }; registerAutoDispose(bindable, resolveWeakRef); bindResult = bindWorker(bindable, bind.source, bindingAction); if (bindResult) { var cancel = bindResult.cancel; bindResult.cancel = function () { canceled = true; return cancel.call(bindResult); }; if (cacheEntry) { cacheEntry.bindings.push(function () { bindResult.cancel(); }); } } return bindResult; } function sourceOneBinding(bind, ref, bindingId, source, e, pend, cacheEntry) { var bindable; if (source !== global) { source = WinJS.Binding.as(source); } if (source._getObservable) { bindable = source._getObservable(); } if (bindable) { pend.count++; // declarative binding must use a weak ref to the target element // return makeBinding(ref, bindingId, pend, bindable, bind, cacheEntry); } else { nestedSet(e, bind.destination, getValue(source, bind.source)); } } function filterIdBinding(declBind, bindingStr) { for (var bindIndex = declBind.length - 1; bindIndex >= 0; bindIndex--) { var bind = declBind[bindIndex]; var dest = bind.destination; if (dest.length === 1 && dest[0] === "id") { if (WinJS.validation) { throw new WinJS.ErrorFromName("WinJS.Binding.IdBindingNotSupported", WinJS.Resources._formatString(strings.idBindingNotSupported, bindingStr)); } WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.idBindingNotSupported, bindingStr), "winjs binding", "error"); declBind.splice(bindIndex, 1); } } return declBind; } function calcBinding(bindingStr, bindingCache) { if (bindingCache) { var declBindCache = bindingCache.expressions[bindingStr]; var declBind; if (!declBindCache) { declBind = filterIdBinding(WinJS.Binding._bindingParser(bindingStr, global), bindingStr); bindingCache.expressions[bindingStr] = declBind; } if (!declBind) { declBind = declBindCache; } return declBind; } else { return filterIdBinding(WinJS.Binding._bindingParser(bindingStr, global), bindingStr); } } function declarativeBindImpl(rootElement, dataContext, skipRoot, bindingCache, c, e, p) { msWriteProfilerMark("WinJS.Binding:processAll,StartTM"); var pend = { count: 0, checkComplete: function checkComplete() { this.count--; if (this.count === 0) { msWriteProfilerMark("WinJS.Binding:processAll,StopTM"); c(); } } }; var baseElement = (rootElement || document.body); var attr = "data-win-bind" var elements = baseElement.querySelectorAll("[" + attr + "]"); var neg; if (!skipRoot && baseElement.getAttribute(attr)) { neg = baseElement; } pend.count++; var source = dataContext || global; WinJS.Utilities._DOMWeakRefTable_fastLoadPath = true; try { for (var i = (neg ? -1 : 0), l = elements.length; i < l; i++) { var element = i < 0 ? neg : elements[i]; // If this element is inside of a declarative control container // (e.g. WinJS.Binding.Template) then we don't process it. // if (inContainer(baseElement, element.winControl, element)) { continue; } var original = element.getAttribute(attr); var declBind = calcBinding(original, bindingCache); if (!declBind.implemented) { for (var bindIndex = 0, bindLen = declBind.length; bindIndex < bindLen; bindIndex++) { var bind = declBind[bindIndex]; if (bind.initializer) { bind.implementation = initializerOneBinding; } else { bind.implementation = sourceOneBinding; } } declBind.implemented = true; } pend.count++; var ref = element.id; var bindingId = setBindingToken(element); if (!ref) { // We use our own counter here, as the IE "uniqueId" is only // global to a document, which means that binding against // unparented DOM elements would get duplicate IDs. // // The elements may not be parented at this point, but they // will be parented by the time the binding action is fired. // if (optimizeBindingReferences) { ref = bindingId; } else { element.id = ref = bindingId; } } WinJS.Utilities._createWeakRef(element, ref); var elementData = WinJS.Utilities.data(element); elementData.winBindings = null; var cacheEntry; if (bindingCache && bindingCache.elements) { cacheEntry = bindingCache.elements[ref]; if (!cacheEntry) { bindingCache.elements[ref] = cacheEntry = { bindings: [] }; } } for (var bindIndex2 = 0, bindLen2 = declBind.length; bindIndex2 < bindLen2; bindIndex2++) { var bind2 = declBind[bindIndex2]; var cancel2 = bind2.implementation(bind2, ref, bindingId, source, element, pend, cacheEntry); if (cancel2) { elementData.winBindings = elementData.winBindings || []; elementData.winBindings.push(cancel2); } } pend.count--; } } finally { WinJS.Utilities._DOMWeakRefTable_fastLoadPath = false; } pend.checkComplete(); } function declarativeBind(rootElement, dataContext, skipRoot, bindingCache) { /// /// /// Binds values from the specified data context to elements that are descendants of the specified root element /// and have declarative binding attributes (data-win-bind). /// /// /// The element at which to start traversing to find elements to bind to. If this parameter is omitted, the entire document /// is searched. /// /// /// The object to use for default data binding. /// /// /// If true, the elements to be bound skip the specified root element and include only the children. /// /// /// The cached binding data. /// /// /// A promise that completes when each item that contains binding declarations has /// been processed and the update has started. /// /// return new WinJS.Promise(function (c, e, p) { declarativeBindImpl(rootElement, dataContext, skipRoot, bindingCache, c, e, p); }).then(null, function (e) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.errorInitializingBindings, e && e.message), "winjs binding", "error"); return WinJS.Promise.wrapError(e); }); } function converter(convert) { /// /// /// Creates a default binding initializer for binding between a source /// property and a destination property with a provided converter function /// that is executed on the value of the source property. /// /// /// The conversion that operates over the result of the source property /// to produce a value that is set to the destination property. /// /// /// The binding initializer. /// /// var userConverter = function (source, sourceProperties, dest, destProperties) { var ref = dest.id; var bindingId = setBindingToken(dest); if (!ref) { uid++; if (optimizeBindingReferences) { ref = bindingId; } else { dest.id = ref = bindingId; } } WinJS.Utilities._createWeakRef(dest, ref); var workerResult = bindWorker(WinJS.Binding.as(source), sourceProperties, function (v) { var found = checkBindingToken(WinJS.Utilities._getWeakRefElement(ref), bindingId); if (found) { nestedSet(found, destProperties, convert(requireSupportedForProcessing(v))); } else if (workerResult) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.elementNotFound, ref), "winjs binding", "info"); workerResult.cancel(); } }); return workerResult; }; return markSupportedForProcessing(userConverter); } function getValue(obj, path) { if (obj !== global) { obj = requireSupportedForProcessing(obj); } if (path) { for (var i = 0, len = path.length; i < len && (obj !== null && obj !== undefined); i++) { obj = requireSupportedForProcessing(obj[path[i]]); } } return obj; } function nestedSet(dest, destProperties, v) { requireSupportedForProcessing(v); dest = requireSupportedForProcessing(dest); for (var i = 0, len = (destProperties.length - 1) ; i < len; i++) { dest = requireSupportedForProcessing(dest[destProperties[i]]); if (!dest) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.propertyDoesNotExist, destProperties[i], destProperties.join(".")), "winjs binding", "error"); return; } else if (dest instanceof Node) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.nestedDOMElementBindingNotSupported, destProperties[i], destProperties.join(".")), "winjs binding", "error"); return; } } if (destProperties.length === 0) { WinJS.log && WinJS.log(strings.cannotBindToThis, "winjs binding", "error"); return; } var prop = destProperties[destProperties.length - 1]; if (WinJS.log) { if (dest[prop] === undefined) { WinJS.log(WinJS.Resources._formatString(strings.creatingNewProperty, prop, destProperties.join(".")), "winjs binding", "warn"); } } dest[prop] = v; } function attributeSet(dest, destProperties, v) { dest = requireSupportedForProcessing(dest); if (!destProperties || destProperties.length !== 1 || !destProperties[0]) { WinJS.log && WinJS.log(strings.attributeBindingSingleProperty, "winjs binding", "error"); return; } dest.setAttribute(destProperties[0], v); } function setAttribute(source, sourceProperties, dest, destProperties) { /// /// /// Creates a one-way binding between the source object and /// an attribute on the destination element. /// /// /// The source object. /// /// /// The path on the source object to the source property. /// /// /// The destination object (must be a DOM element). /// /// /// The path on the destination object to the destination property, this must be a single name. /// /// /// An object with a cancel method that is used to coalesce bindings. /// /// var ref = dest.id; var bindingId = setBindingToken(dest); if (!ref) { uid++; if (optimizeBindingReferences) { ref = bindingId; } else { dest.id = ref = bindingId; } } WinJS.Utilities._createWeakRef(dest, ref); var workerResult = bindWorker(WinJS.Binding.as(source), sourceProperties, function (v) { var found = checkBindingToken(WinJS.Utilities._getWeakRefElement(ref), bindingId); if (found) { attributeSet(found, destProperties, requireSupportedForProcessing(v)); } else if (workerResult) { WinJS.log && WinJS.log(WinJS.Resources._formatString(strings.elementNotFound, ref), "winjs binding", "info"); workerResult.cancel(); } }); return workerResult; } function setAttributeOneTime(source, sourceProperties, dest, destProperties) { /// /// /// Sets an attribute on the destination element to the value of the source property /// /// /// The source object. /// /// /// The path on the source object to the source property. /// /// /// The destination object (must be a DOM element). /// /// /// The path on the destination object to the destination property, this must be a single name. /// /// return attributeSet(dest, destProperties, getValue(source, sourceProperties)); } var defaultBindImpl = converter(function defaultBind_passthrough(v) { return v; }); function defaultBind(source, sourceProperties, dest, destProperties) { /// /// /// Creates a one-way binding between the source object and /// the destination object. /// /// /// The source object. /// /// /// The path on the source object to the source property. /// /// /// The destination object. /// /// /// The path on the destination object to the destination property. /// /// /// An object with a cancel method that is used to coalesce bindings. /// /// return defaultBindImpl(source, sourceProperties, dest, destProperties); } function bindWorker(bindable, sourceProperties, func) { if (sourceProperties.length > 1) { var root = {}; var current = root; for (var i = 0, l = sourceProperties.length - 1; i < l; i++) { current = current[sourceProperties[i]] = {}; } current[sourceProperties[sourceProperties.length - 1]] = func; return WinJS.Binding.bind(bindable, root, true); } else if (sourceProperties.length === 1) { bindable.bind(sourceProperties[0], func, true); return { cancel: function () { bindable.unbind(sourceProperties[0], func); this.cancel = noop; } }; } else { // can't bind to object, so we just push it through // func(bindable); } } function noop() { } function oneTime(source, sourceProperties, dest, destProperties) { /// /// /// Sets the destination property to the value of the source property. /// /// /// The source object. /// /// /// The path on the source object to the source property. /// /// /// The destination object. /// /// /// The path on the destination object to the destination property. /// /// /// An object with a cancel method that is used to coalesce bindings. /// /// nestedSet(dest, destProperties, getValue(source, sourceProperties)); return { cancel: noop }; } function initializer(customInitializer) { /// /// /// Marks a custom initializer function as being compatible with declarative data binding. /// /// /// The custom initializer to be marked as compatible with declarative data binding. /// /// /// The input customInitializer. /// /// return markSupportedForProcessing(customInitializer); } WinJS.Namespace.define("WinJS.Binding", { processAll: declarativeBind, oneTime: initializer(oneTime), defaultBind: initializer(defaultBind), converter: converter, initializer: initializer, setAttribute: initializer(setAttribute), setAttributeOneTime: initializer(setAttributeOneTime), optimizeBindingReferences: { get: function () { return optimizeBindingReferences; }, set: function (v) { if (!!v && WinJS.Utilities.hasWinRT && global.msSetWeakWinRTProperty && global.msGetWeakWinRTProperty) { optimizeBindingReferences = true; } else { optimizeBindingReferences = false; } } }, }); })(WinJS, this); (function DOMWeakRefTableInit(global, undefined) { "use strict"; var U = WinJS.Utilities; // Defaults var SWEEP_PERIOD = 500; var TIMEOUT = 1000; var table = {}; var cleanupToken; function cleanup() { if (U._DOMWeakRefTable_sweepPeriod === 0) { // If we're using post cleanupToken = 0; // indicate that cleanup has run } var keys = Object.keys(table); var time = Date.now() - U._DOMWeakRefTable_timeout; var i, len; for (i = 0, len = keys.length; i < len; i++) { var id = keys[i]; if (table[id].time < time) { delete table[id]; } } unscheduleCleanupIfNeeded(); } function scheduleCleanupIfNeeded() { if ((Debug.debuggerEnabled && U._DOMWeakRefTable_noTimeoutUnderDebugger) || cleanupToken) { return; } var period = U._DOMWeakRefTable_sweepPeriod; if (period === 0) { setImmediate(cleanup); cleanupToken = 1; } else { cleanupToken = setInterval(cleanup, U._DOMWeakRefTable_sweepPeriod); } } function unscheduleCleanupIfNeeded() { if (Debug.debuggerEnabled && U._DOMWeakRefTable_noTimeoutUnderDebugger) { return; } var period = U._DOMWeakRefTable_sweepPeriod; if (period === 0) { // if we're using post if (!cleanupToken) { // and there isn't already one scheduled if (Object.keys(table).length !== 0) { // and there are items in the table setImmediate(cleanup); // schedule another call to cleanup cleanupToken = 1; // and protect against overscheduling } } } else if (cleanupToken) { if (Object.keys(table).length === 0) { clearInterval(cleanupToken); cleanupToken = 0; } } } function createWeakRef(element, id) { table[id] = { element: element, time: Date.now() }; scheduleCleanupIfNeeded(); return id; } function getWeakRefElement(id) { if (WinJS.Utilities._DOMWeakRefTable_fastLoadPath) { var entry = table[id]; if (entry) { return entry.element; } else { return document.getElementById(id); } } else { var element = document.getElementById(id); if (element) { delete table[id]; unscheduleCleanupIfNeeded(); } else { var entry = table[id]; if (entry) { entry.time = Date.now(); element = entry.element; } } return element; } } WinJS.Namespace.define("WinJS.Utilities", { _DOMWeakRefTable_noTimeoutUnderDebugger: true, _DOMWeakRefTable_sweepPeriod: SWEEP_PERIOD, _DOMWeakRefTable_timeout: TIMEOUT, _DOMWeakRefTable_tableSize: { get: function () { return Object.keys(table).length; } }, _DOMWeakRefTable_fastLoadPath: false, _createWeakRef: createWeakRef, _getWeakRefElement: getWeakRefElement }); if (WinJS.Utilities.hasWinRT && global.msSetWeakWinRTProperty && global.msGetWeakWinRTProperty) { var host = new Windows.Foundation.Uri("about://blank"); WinJS.Namespace.define("WinJS.Utilities", { _createWeakRef: function (element, id) { msSetWeakWinRTProperty(host, id, element); return id; }, _getWeakRefElement: function (id) { return msGetWeakWinRTProperty(host, id); } }); } }(this)); // WinJS.Binding.List // (function listInit(global, undefined) { "use strict"; var strings = { get sparseArrayNotSupported() { return WinJS.Resources._getWinJSString("base/sparseArrayNotSupported").value; }, get illegalListLength() { return WinJS.Resources._getWinJSString("base/illegalListLength").value; }, }; function copyargs(args) { return Array.prototype.slice.call(args, 0); } function cloneItem(item) { return { handle: item.handle, key: item.key, data: item.data, groupKey: item.groupKey, groupSize: item.groupSize, firstItemKey: item.firstItemKey, firstItemIndexHint: item.firstItemIndexHint }; } function asNumber(n) { return n === undefined ? undefined : +n; } var createEvent = WinJS.Utilities._createEventProperty; var ListBase = WinJS.Class.define( null, { _annotateWithIndex: function (item, index) { var result = cloneItem(item); result.index = index; return result; }, /// /// The value identified by the specified key has been replaced with a different value. /// onitemchanged: createEvent("itemchanged"), /// /// A new value has been inserted into the list. /// oniteminserted: createEvent("iteminserted"), /// /// The value identified by the specified key has been moved from one index in the list to another index. /// onitemmoved: createEvent("itemmoved"), /// /// The value identified by the specified key has been mutated. /// onitemmutated: createEvent("itemmutated"), /// /// The value identified by the specified key has been removed from the list. /// onitemremoved: createEvent("itemremoved"), /// /// The list has been refreshed. Any references to items in the list may be incorrect. /// onreload: createEvent("reload"), _notifyItemChanged: function (key, index, oldValue, newValue, oldItem, newItem) { if (this._listeners && this._listeners.itemchanged) { this.dispatchEvent("itemchanged", { key: key, index: index, oldValue: oldValue, newValue: newValue, oldItem: oldItem, newItem: newItem }); } }, _notifyItemInserted: function (key, index, value) { if (this._listeners && this._listeners.iteminserted) { this.dispatchEvent("iteminserted", { key: key, index: index, value: value }); } var len = this.length; if (len !== this._lastNotifyLength) { this.notify("length", len, this._lastNotifyLength); this._lastNotifyLength = len; } }, _notifyItemMoved: function (key, oldIndex, newIndex, value) { if (this._listeners && this._listeners.itemmoved) { this.dispatchEvent("itemmoved", { key: key, oldIndex: oldIndex, newIndex: newIndex, value: value }); } }, _notifyItemMutated: function (key, value, item) { if (this._listeners && this._listeners.itemmutated) { this.dispatchEvent("itemmutated", { key: key, value: value, item: item }); } }, _notifyItemRemoved: function (key, index, value, item) { if (this._listeners && this._listeners.itemremoved) { this.dispatchEvent("itemremoved", { key: key, index: index, value: value, item: item }); } var len = this.length; if (len !== this._lastNotifyLength) { this.notify("length", len, this._lastNotifyLength); this._lastNotifyLength = len; } }, _notifyReload: function () { if (this._listeners && this._listeners.reload) { this.dispatchEvent("reload"); } if (len !== this._lastNotifyLength) { var len = this.length; this.notify("length", len, this._lastNotifyLength); this._lastNotifyLength = len; } }, _normalizeIndex: function (index) { index = asNumber(index); return index < 0 ? this.length + index : index; }, // ABSTRACT: length // Notifications: // // ABSTRACT: notifyMutated: function (index) _notifyMutatedFromKey: function (key) { var item = this.getItemFromKey(key); this._notifyItemMutated(key, item.data, item); }, notifyReload: function () { /// /// /// Forces the list to send a reload notification to any listeners. /// /// this._notifyReload(); }, // NOTE: performance can be improved in a number of the projections by overriding getAt/_getArray/_getFromKey/_getKey // getAt: function (index) { /// /// /// Gets the value at the specified index. /// /// The index of the value to get. /// The value at the specified index. /// index = asNumber(index); var item = this.getItem(index); return item && item.data; }, // returns [ data* ] _getArray: function () { var results = new Array(this.length); for (var i = 0, len = this.length; i < len; i++) { var item = this.getItem(i); if (item) { results[i] = item.data; } } return results; }, // returns data _getFromKey: function (key) { var item = this.getItemFromKey(key); return item && item.data; }, // ABSTRACT: getItem(index) // ABSTRACT: getItemFromKey(key) // returns string _getKey: function (index) { index = asNumber(index); var item = this.getItem(index); return item && item.key; }, // Normal list non-modifiying operations // concat: function (item) { /// /// /// Returns a new list consisting of a combination of two arrays. /// /// Additional items to add to the end of the list. /// An array containing the concatenation of the list and any other supplied items. /// var a = this._getArray(); return a.concat.apply(a, arguments); }, join: function (separator) { /// /// /// Returns a string consisting of all the elements of a list separated by the specified separator string. /// /// A string used to separate the elements of a list. If this parameter is omitted, the list elements are separated with a comma. /// The elements of a list separated by the specified separator string. /// return this._getArray().join(separator || ","); }, slice: function (begin, end) { /// /// /// Extracts a section of a list and returns a new list. /// /// The index that specifies the beginning of the section. /// The index that specifies the end of the section. /// Returns a section of an array. /// return this._getArray().slice(begin, end); }, indexOf: function (searchElement, fromIndex) { /// /// /// Gets the index of the first occurrence of the specified value in a list. /// /// The value to locate in the list. /// The index at which to begin the search. If fromIndex is omitted, the search starts at index 0. /// Index of the first occurrence of a value in a list or -1 if not found. /// fromIndex = asNumber(fromIndex); fromIndex = Math.max(0, this._normalizeIndex(fromIndex) || 0); for (var i = fromIndex, len = this.length; i < len; i++) { var item = this.getItem(i); if (item && item.data === searchElement) { return i; } } return -1; }, // ABSTRACT: indexOfKey(key) lastIndexOf: function (searchElement, fromIndex) { /// /// /// Gets the index of the last occurrence of the specified value in a list. /// /// The value to locate in the list. /// The index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the list. /// The index of the last occurrence of a value in a list, or -1 if not found. /// fromIndex = asNumber(fromIndex); var length = this.length; fromIndex = Math.min(this._normalizeIndex(fromIndex !== undefined ? fromIndex : length), length - 1); var i; for (i = fromIndex; i >= 0; i--) { var item = this.getItem(i); if (item && item.data === searchElement) { return i; } } return -1; }, // // Normal list projection operations // every: function (callback, thisArg) { /// /// /// Checks whether the specified callback function returns true for all elements in a list. /// /// A function that accepts up to three arguments. This function is called for each element in the list until it returns false or the end of the list is reached. /// An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used. /// True if the callback returns true for all elements in the list. /// return this._getArray().every(callback, thisArg); }, filter: function (callback, thisArg) { /// /// /// Returns the elements of a list that meet the condition specified in a callback function. /// /// A function that accepts up to three arguments. The function is called for each element in the list. /// An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used. /// An array containing the elements that meet the condition specified in the callback function. /// return this._getArray().filter(callback, thisArg); }, forEach: function (callback, thisArg) { /// /// /// Calls the specified callback function for each element in a list. /// /// A function that accepts up to three arguments. The function is called for each element in the list. /// An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used. /// this._getArray().forEach(callback, thisArg); }, map: function (callback, thisArg) { /// /// /// Calls the specified callback function on each element of a list, and returns an array that contains the results. /// /// A function that accepts up to three arguments. The function is called for each element in the list. /// An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used. /// An array containing the result of calling the callback function on each element in the list. /// return this._getArray().map(callback, thisArg); }, some: function (callback, thisArg) { /// /// /// Checks whether the specified callback function returns true for any element of a list. /// /// A function that accepts up to three arguments. The function is called for each element in the list until it returns true, or until the end of the list. /// An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used. /// True if callback returns true for any element in the list. /// return this._getArray().some(callback, thisArg); }, reduce: function (callback, initialValue) { /// /// /// Accumulates a single result by calling the specified callback function for all elements in a list. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. /// /// A function that accepts up to four arguments. The function is called for each element in the list. /// If initialValue is specified, it is used as the value with which to start the accumulation. The first call to the function provides this value as an argument instead of a list value. /// The return value from the last call to the callback function. /// if (arguments.length > 1) { return this._getArray().reduce(callback, initialValue); } return this._getArray().reduce(callback); }, reduceRight: function (callback, initialValue) { /// /// /// Accumulates a single result by calling the specified callback function for all elements in a list, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. /// /// A function that accepts up to four arguments. The function is called for each element in the list. /// If initialValue is specified, it is used as the value with which to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a list value. /// The return value from last call to callback function. /// if (arguments.length > 1) { return this._getArray().reduceRight(callback, initialValue); } return this._getArray().reduceRight(callback); }, // // Live Projections - if you want the lifetime of the returned projections to // be shorter than that of the list object on which they are based you have // to remember to call .dispose() on them when done. // createFiltered: function (predicate) { /// /// /// Creates a live filtered projection over this list. As the list changes, the filtered projection reacts to those changes and may also change. /// /// A function that accepts a single argument. The createFiltered function calls the callback with each element in the list. If the function returns true, that element will be included in the filtered list. /// Filtered projection over the list. /// return new FilteredListProjection(this, predicate); }, createGrouped: function (groupKey, groupData, groupSorter) { /// /// /// Creates a live grouped projection over this list. As the list changes, the grouped projection reacts to those changes and may also change. The grouped projection sorts all the elements of the list to be in group-contiguous order. The grouped projection also contains a .groups property which is a WinJS.Binding.List representing the groups that were found in the list. /// /// A function that accepts a single argument. The function is called with each element in the list, the function should return a string representing the group containing the element. /// A function that accepts a single argument. The function is called on an element in the list for each group. It should return the value that should be set as the data of the .groups list element for this group. /// A function that accepts two arguments. The function is called with the key of groups found in the list. It must return one of the following numeric values: negative if the first argument is less than the second, zero if the two arguments are equivalent, positive if the first argument is greater than the second. If omitted, the groups are sorted in ascending, ASCII character order. /// A grouped projection over the list. /// return new GroupedSortedListProjection(this, groupKey, groupData, groupSorter); }, createSorted: function (sorter) { /// /// /// Creates a live sorted projection over this list. As the list changes, the sorted projection reacts to those changes and may also change. /// /// A function that accepts two arguments. The function is called with elements in the list. It must return one of the following numeric values: negative if the first argument is less than the second, zero if the two arguments are equivalent, positive if the first argument is greater than the second. /// A sorted projection over the list. /// return new SortedListProjection(this, sorter); } }, { supportedForProcessing: false, } ); WinJS.Class.mix(ListBase, WinJS.Binding.observableMixin); WinJS.Class.mix(ListBase, WinJS.Utilities.eventMixin); var ListBaseWithMutators = WinJS.Class.derive(ListBase, null, { // ABSTRACT: setAt(index, value) // Normal list modifying operations // // returns data from tail of list pop: function () { /// /// /// Removes the last element from a list and returns it. /// /// Last element from the list. /// return this.splice(-1, 1)[0]; }, push: function (value) { /// /// /// Appends new element(s) to a list, and returns the new length of the list. /// /// The element to insert at the end of the list. /// The new length of the list. /// if (arguments.length === 1) { this.splice(this.length, 0, value); return this.length; } else { var args = copyargs(arguments); args.splice(0, 0, this.length, 0); this.splice.apply(this, args); return this.length; } }, // returns data from head of list shift: function () { /// /// /// Removes the first element from a list and returns it. /// /// First element from the list. /// return this.splice(0, 1)[0]; }, unshift: function (value) { /// /// /// Appends new element(s) to a list, and returns the new length of the list. /// /// The element to insert at the start of the list. /// The new length of the list. /// if (arguments.length === 1) { this.splice(0, 0, value); } else { var args = copyargs(arguments); // Wow, this looks weird. Insert 0, 0 at the beginning of splice. args.splice(0, 0, 0, 0); this.splice.apply(this, args); } return this.length; } // ABSTRACT: splice(index, howMany, values...) // ABSTRACT: _spliceFromKey(key, howMany, values...) }, { supportedForProcessing: false, } ); var emptyOptions = {}; var List = WinJS.Class.derive(ListBaseWithMutators, function (list, options) { /// /// /// Creates a WinJS.Binding.List object. /// /// The array containing the elements to initalize the list. /// If options.binding is true, the list will contain the result of calling WinJS.Binding.as() on the element values. If options.proxy is true, the list specified as the first parameter is used as the storage for the WinJS.Binding.List. This option should be used with care because uncoordinated edits to the data storage will result in errors. /// The newly-constructed WinJS.Binding.List instance. /// this._currentKey = 0; this._keys = null; this._keyMap = {}; // options: // - binding: binding.as on items // - proxy: proxy over input data // options = options || emptyOptions; this._proxy = options.proxy; this._binding = options.binding; if (this._proxy) { if (Object.keys(list).length !== list.length) { throw new WinJS.ErrorFromName("WinJS.Binding.List.NotSupported", strings.sparseArrayNotSupported); } this._data = list; this._currentKey = list.length; } else if (list) { var keyDataMap = this._keyMap; var pos = 0, i = 0; for (var len = list.length; i < len; i++) { if (i in list) { var item = list[i]; if (this._binding) { item = WinJS.Binding.as(item); } var key = pos.toString(); pos++; keyDataMap[key] = { handle: key, key: key, data: item }; } } if (pos !== i) { this._initializeKeys(); } this._currentKey = pos; } }, { _currentKey: 0, _keys: null, _keyMap: null, _modifyingData: 0, _initializeKeys: function () { if (this._keys) { return; } var keys = []; if (this._data) { // If this list is a proxy over the data then we will have been lazily initializing // the entries in the list, however the 1:1 mapping between index and key is about // to go away so this is our last chance to pull the items out of the data. // var keyMap = this._keyMap; var data = this._data; for (var i = 0, len = data.length; i < len; i++) { if (i in data) { var key = i.toString(); keys[i] = key; if (!(key in keyMap)) { var item = data[i]; if (this._binding) { item = WinJS.Binding.as(item); } keyMap[key] = { handle: key, key: key, data: item }; } } } } else { // In the case where List owns the data we will have created the keyMap at initialization // time and can use that to harvest all the keys into the _keys list. // Object.keys(this._keyMap).forEach(function (key) { keys[key >>> 0] = key; }); } this._keys = keys; }, _lazyPopulateEntry: function (index) { if (this._data && index in this._data) { var item = this._data[index]; if (this._binding) { item = WinJS.Binding.as(item); } var key = index.toString(); var entry = { handle: key, key: key, data: item }; this._keyMap[entry.key] = entry; return entry; } }, _assignKey: function () { return (++this._currentKey).toString(); }, /// Gets or sets the length of the list, which is an integer value one higher than the highest element defined in the list. length: { get: function () { // If we are proxying use the underlying list's length // If we have already allocated keys use that length // If we haven't allocated keys then we can use _currentKey which was set at initialization time // to be length of the input list. if (this._data) { return this._data.length; } else if (this._keys) { return this._keys.length; } else { return this._currentKey; } }, set: function (value) { if (typeof value === "number" && value >= 0) { this._initializeKeys(); var current = this.length; if (current > value) { this.splice(value, current - value); } else { // We don't support setting lengths to longer in order to have sparse behavior value = current; } if (this._data) { this._modifyingData++; try { this._data.length = value; } finally { this._modifyingData--; } } if (this._keys) { this._keys.length = value; } } else { throw new WinJS.ErrorFromName("WinJS.Binding.List.IllegalLength", strings.illegalListLength); } } }, getItem: function (index) { /// /// /// Gets a key/data pair for the specified list index. /// /// The index of value to retrieve. /// An object with .key and .data properties. /// var entry; var key; index = asNumber(index); if (this._keys) { key = this._keys[index]; entry = key && this._keyMap[key]; } else { key = index.toString(); entry = this._keyMap[key] || this._lazyPopulateEntry(index); } return entry; }, getItemFromKey: function (key) { /// /// /// Gets a key/data pair for the list item key specified. /// /// The key of the value to retrieve. /// An object with .key and .data properties. /// var entry; // if we have a keys list we know to go through the keyMap, or if we are not // proxying through _data we also know to go through the keyMap. if (this._keys || !this._data) { entry = this._keyMap[key]; } else { entry = this.getItem(key >>> 0); } return entry; }, indexOfKey: function (key) { /// /// /// Gets the index of the first occurrence of a key in a list. /// /// The key to locate in the list. /// The index of the first occurrence of a key in a list, or -1 if not found. /// var index = -1; if (this._keys) { index = this._keys.indexOf(key); } else { var t = key >>> 0; if (t < this._currentKey) { index = t; } } return index; }, move: function (index, newIndex) { /// /// /// Moves the value at index to the specified position. /// /// The original index of the value. /// The index of the value after the move. /// index = asNumber(index); newIndex = asNumber(newIndex); this._initializeKeys(); if (index === newIndex || index < 0 || newIndex < 0 || index >= this.length || newIndex >= this.length) { return; } if (this._data) { this._modifyingData++; try { var item = this._data.splice(index, 1)[0]; this._data.splice(newIndex, 0, item); } finally { this._modifyingData--; } } var key = this._keys.splice(index, 1)[0]; this._keys.splice(newIndex, 0, key); this._notifyItemMoved(key, index, newIndex, this.getItemFromKey(key).data); }, notifyMutated: function (index) { /// /// /// Forces the list to send a itemmutated notification to any listeners for the value at the specified index. /// /// The index of the value that was mutated. /// index = asNumber(index); var key = this._keys ? this._keys[index] : index.toString(); this._notifyMutatedFromKey(key); }, setAt: function (index, newValue) { /// /// /// Replaces the value at the specified index with a new value. /// /// The index of the value that was replaced. /// The new value. /// index = asNumber(index); this._initializeKeys(); var length = this.length; if (index === length) { this.push(newValue); } else if (index < length) { if (this._data) { this._modifyingData++; try { this._data[index] = newValue; } finally { this._modifyingData--; } } if (this._binding) { newValue = WinJS.Binding.as(newValue); } if (index in this._keys) { var key = this._keys[index]; var oldEntry = this._keyMap[key]; var newEntry = cloneItem(oldEntry); newEntry.data = newValue; this._keyMap[key] = newEntry; this._notifyItemChanged(key, index, oldEntry.data, newValue, oldEntry, newEntry); } } }, _setAtKey: function (key, newValue) { this.setAt(this.indexOfKey(key), newValue); }, // These are the traditional Array mutators, they don't result in projections. In particular // having both sort and sorted is a bit confusing. It may be the case that we want to eliminate // the various array helpers outside of the standard push/pop,shift/unshift,splice,get*,setAt // and then offer up the specific projections: filter, sorted, grouped. Anything else can be // obtained through _getArray(). // reverse: function () { /// /// /// Returns a list with the elements reversed. This method reverses the elements of a list object in place. It does not create a new list object during execution. /// /// The reversed list. /// this._initializeKeys(); if (this._data) { this._modifyingData++; try { this._data.reverse(); } finally { this._modifyingData--; } } this._keys.reverse(); this._notifyReload(); return this; }, sort: function (sortFunction) { /// /// /// Returns a list with the elements sorted. This method sorts the elements of a list object in place. It does not create a new list object during execution. /// /// The function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order. /// The sorted list. /// this._initializeKeys(); if (this._data) { this._modifyingData++; try { this._data.sort(sortFunction); } finally { this._modifyingData--; } } var that = this; this._keys.sort(function (l, r) { l = that._keyMap[l]; r = that._keyMap[r]; if (sortFunction) { return sortFunction(l.data, r.data); } l = (l && l.data || "").toString(); r = (l && r.data || "").toString(); return l < r ? -1 : l === r ? 0 : 1; }); this._notifyReload(); }, pop: function () { /// /// /// Removes the last element from a list and returns it. /// /// Last element from the list. /// if (this.length === 0) { return; } this._initializeKeys(); var key = this._keys.pop(); var entry = this._keyMap[key]; var data = entry && entry.data; if (this._data) { this._modifyingData++; try { this._data.pop(); } finally { this._modifyingData--; } } delete this._keyMap[key]; this._notifyItemRemoved(key, this._keys.length, data, entry); return data; }, push: function (value) { /// /// /// Appends new element(s) to a list, and returns the new length of the list. /// /// The element to insert at the end of the list. /// The new length of the list. /// this._initializeKeys(); var length = arguments.length; for (var i = 0; i < length; i++) { var item = arguments[i]; if (this._binding) { item = WinJS.Binding.as(item); } var key = this._assignKey(); this._keys.push(key); if (this._data) { this._modifyingData++; try { this._data.push(arguments[i]) } finally { this._modifyingData--; } } this._keyMap[key] = { handle: key, key: key, data: item }; this._notifyItemInserted(key, this._keys.length - 1, item); } return this.length; }, shift: function () { /// /// /// Removes the first element from a list and returns it. /// /// First element from the list. /// if (this.length === 0) { return; } this._initializeKeys(); var key = this._keys.shift(); var entry = this._keyMap[key]; var data = entry && entry.data; if (this._data) { this._modifyingData++; try { this._data.shift(); } finally { this._modifyingData--; } } delete this._keyMap[key]; this._notifyItemRemoved(key, 0, data, entry); return data; }, unshift: function (value) { /// /// /// Appends new element(s) to a list, and returns the new length of the list. /// /// The element to insert at the start of the list. /// The new length of the list. /// this._initializeKeys(); var length = arguments.length; for (var i = length - 1; i >= 0; i--) { var item = arguments[i]; if (this._binding) { item = WinJS.Binding.as(item); } var key = this._assignKey(); this._keys.unshift(key); if (this._data) { this._modifyingData++; try { this._data.unshift(arguments[i]) } finally { this._modifyingData--; } } this._keyMap[key] = { handle: key, key: key, data: item }; this._notifyItemInserted(key, 0, item); } return this.length; }, splice: function (index, howMany, item) { /// /// /// Removes elements from a list and, if necessary, inserts new elements in their place, returning the deleted elements. /// /// The zero-based location in the list from which to start removing elements. /// The number of elements to remove. /// The elements to insert into the list in place of the deleted elements. /// The deleted elements. /// index = asNumber(index); this._initializeKeys(); index = Math.max(0, this._normalizeIndex(index)); howMany = Math.max(0, Math.min(howMany || 0, this.length - index)); var result = []; while (howMany) { var key = this._keys[index]; var entry = this._keyMap[key]; var data = entry && entry.data; result.push(data); this._keys.splice(index, 1); if (this._data) { this._modifyingData++; try { this._data.splice(index, 1); } finally { this._modifyingData--; } } delete this._keyMap[key]; this._notifyItemRemoved(key, index, data, entry); --howMany; } if (arguments.length > 2) { for (var i = 2, len = arguments.length; i < len; i++) { var additionalItem = arguments[i]; if (this._binding) { additionalItem = WinJS.Binding.as(additionalItem); } var pos = Math.min(index + i - 2, this.length); var newKey = this._assignKey(); this._keys.splice(pos, 0, newKey); if (this._data) { this._modifyingData++; try { this._data.splice(pos, 0, arguments[i]); } finally { this._modifyingData--; } } this._keyMap[newKey] = { handle: newKey, key: newKey, data: additionalItem }; this._notifyItemInserted(newKey, pos, additionalItem); } } return result; }, // returns [ data* ] of removed items _spliceFromKey: function (key, howMany) { this._initializeKeys(); var args = copyargs(arguments); args[0] = this._keys.indexOf(key); return this.splice.apply(this, args); } }, { supportedForProcessing: false, } ); var ListProjection = WinJS.Class.derive(ListBaseWithMutators, null, { _list: null, _myListeners: null, _addListListener: function (name, func) { var l = { name: name, handler: func.bind(this) }; this._myListeners = this._myListeners || []; this._myListeners.push(l); this._list.addEventListener(name, l.handler); }, // ABSTRACT: _listReload() dispose: function () { /// /// /// Disconnects this WinJS.Binding.List projection from its underlying WinJS.Binding.List. This is important only if they have different lifetimes. /// /// var list = this._list; var listeners = this._myListeners; this._myListeners = []; for (var i = 0, len = listeners.length; i < len; i++) { var l = listeners[i]; list.removeEventListener(l.name, l.handler); } // Set this to an empty list and tell everyone that they need to reload to avoid // consumers null-refing on an empty list. this._list = new WinJS.Binding.List(); this._listReload(); }, getItemFromKey: function (key) { /// /// /// Gets a key/data pair for the specified key. /// /// The key of the value to retrieve. /// An object with .key and .data properties. /// return this._list.getItemFromKey(key); }, move: function (index, newIndex) { /// /// /// Moves the value at index to position newIndex. /// /// The original index of the value. /// The index of the value after the move. /// index = asNumber(index); newIndex = asNumber(newIndex); if (index === newIndex || index < 0 || newIndex < 0 || index >= this.length || newIndex >= this.length) { return; } index = this._list.indexOfKey(this._getKey(index)); newIndex = this._list.indexOfKey(this._getKey(newIndex)); this._list.move(index, newIndex); }, _notifyMutatedFromKey: function (key) { this._list._notifyMutatedFromKey(key); }, splice: function (index, howMany, item) { /// /// /// Removes elements from a list and, if necessary, inserts new elements in their place, returning the deleted elements. /// /// The zero-based location in the list from which to start removing elements. /// The number of elements to remove. /// The elements to insert into the list in place of the deleted elements. /// The deleted elements. /// index = asNumber(index); index = Math.max(0, this._normalizeIndex(index)); var args = copyargs(arguments); if (index === this.length) { // In order to getAt the tail right we just push on to the end of the underlying list args[0] = this._list.length; return this._list.splice.apply(this._list, args); } else { args[0] = this._getKey(index); return this._spliceFromKey.apply(this, args); } }, _setAtKey: function (key, value) { this._list._setAtKey(key, value); }, }, { supportedForProcessing: false, } ); var FilteredListProjection = WinJS.Class.derive(ListProjection, function (list, filter) { this._list = list; this._addListListener("itemchanged", this._listItemChanged); this._addListListener("iteminserted", this._listItemInserted); this._addListListener("itemmutated", this._listItemMutated); this._addListListener("itemmoved", this._listItemMoved); this._addListListener("itemremoved", this._listItemRemoved); this._addListListener("reload", this._listReload); this._filter = filter; this._initFilteredKeys(); }, { _filter: null, _filteredKeys: null, _initFilteredKeys: function () { var filter = this._filter; var list = this._list; var keys = []; for (var i = 0, len = list.length; i < len; i++) { var item = list.getItem(i); if (item && filter(item.data)) { keys.push(item.key); } } this._filteredKeys = keys; }, _findInsertionPosition: function (key, index) { // find the spot to insert this by identifing the previous element in the list var filter = this._filter; var previousKey; while ((--index) >= 0) { var item = this._list.getItem(index); if (item && filter(item.data)) { previousKey = item.key; break; } } var filteredKeys = this._filteredKeys; var filteredIndex = previousKey ? (filteredKeys.indexOf(previousKey) + 1) : 0; return filteredIndex; }, _listItemChanged: function (event) { var key = event.detail.key; var index = event.detail.index; var oldValue = event.detail.oldValue; var newValue = event.detail.newValue; var oldItem = event.detail.oldItem; var newItem = event.detail.newItem; var filter = this._filter; var oldInFilter = filter(oldValue); var newInFilter = filter(newValue); if (oldInFilter && newInFilter) { var filteredKeys = this._filteredKeys; var filteredIndex = filteredKeys.indexOf(key); this._notifyItemChanged(key, filteredIndex, oldValue, newValue, oldItem, newItem); } else if (oldInFilter) { this._listItemRemoved({ detail: { key: key, index: index, value: oldValue, item: oldItem } }); } else if (newInFilter) { this._listItemInserted({ detail: { key: key, index: index, value: newValue } }); } }, _listItemInserted: function (event) { var key = event.detail.key; var index = event.detail.index; var value = event.detail.value; var filter = this._filter; if (filter(value)) { var filteredIndex = this._findInsertionPosition(key, index); var filteredKeys = this._filteredKeys; filteredKeys.splice(filteredIndex, 0, key); this._notifyItemInserted(key, filteredIndex, value); } }, _listItemMoved: function (event) { var key = event.detail.key; var newIndex = event.detail.newIndex; var value = event.detail.value; var filteredKeys = this._filteredKeys; var oldFilteredIndex = filteredKeys.indexOf(key); if (oldFilteredIndex !== -1) { filteredKeys.splice(oldFilteredIndex, 1); var newFilteredIndex = this._findInsertionPosition(key, newIndex); filteredKeys.splice(newFilteredIndex, 0, key); this._notifyItemMoved(key, oldFilteredIndex, newFilteredIndex, value); } }, _listItemMutated: function (event) { var key = event.detail.key; var value = event.detail.value; var item = event.detail.item; var filter = this._filter; var filteredKeys = this._filteredKeys; var filteredIndex = filteredKeys.indexOf(key); if (filteredIndex !== -1) { if (!filter(value)) { filteredKeys.splice(filteredIndex, 1); this._notifyItemRemoved(key, filteredIndex, value, item); return; } } else { if (filter(value)) { this._listItemInserted({ detail: { key: key, index: this._list.indexOfKey(key), value: value } }); return; } } this._notifyItemMutated(key, value, item); }, _listItemRemoved: function (event) { var key = event.detail.key; var value = event.detail.value; var item = event.detail.item; var filteredKeys = this._filteredKeys; var filteredIndex = filteredKeys.indexOf(key); if (filteredIndex !== -1) { filteredKeys.splice(filteredIndex, 1); this._notifyItemRemoved(key, filteredIndex, value, item); } }, _listReload: function () { this._initFilteredKeys(); this._notifyReload(); }, /// Returns an integer value one higher than the highest element defined in an list. length: { get: function () { return this._filteredKeys.length; }, set: function (value) { if (typeof value === "number" && value >= 0) { var current = this.length; if (current > value) { this.splice(value, current - value); } } else { throw new WinJS.ErrorFromName("WinJS.Binding.List.IllegalLength", strings.illegalListLength); } } }, getItem: function (index) { /// /// /// Returns a key/data pair for the specified index. /// /// The index of the value to retrieve. /// An object with .key and .data properties. /// index = asNumber(index); return this.getItemFromKey(this._filteredKeys[index]); }, indexOfKey: function (key) { /// /// /// Returns the index of the first occurrence of a key in a list. /// /// The key to locate in the list. /// The index of the first occurrence of a key in a list, or -1 if not found. /// return this._filteredKeys.indexOf(key); }, notifyMutated: function (index) { /// /// /// Forces the list to send a itemmutated notification to any listeners for the value at the specified index. /// /// The index of the value that was mutated. /// index = asNumber(index); return this._notifyMutatedFromKey(this._filteredKeys[index]); }, setAt: function (index, value) { /// /// /// Replaces the value at the specified index with a new value. /// /// The index of the value that was replaced. /// The new value. /// index = asNumber(index); this._setAtKey(this._filteredKeys[index], value); }, // returns [ data* ] of removed items _spliceFromKey: function (key, howMany) { // first add in all the new items if we have any, this should serve to push key to the right if (arguments.length > 2) { var args = copyargs(arguments); args[1] = 0; // howMany this._list._spliceFromKey.apply(this._list, args); } // now we can remove anything that needs to be removed, since they are not necessarially contiguous // in the underlying list we remove them one by one. var result = []; if (howMany) { var keysToRemove = []; var filteredKeys = this._filteredKeys; var filteredKeyIndex = filteredKeys.indexOf(key); for (var i = filteredKeyIndex, len = filteredKeys.length; i < len && (i - filteredKeyIndex) < howMany; i++) { var key = filteredKeys[i]; keysToRemove.push(key); } var that = this; keysToRemove.forEach(function (key) { result.push(that._list._spliceFromKey(key, 1)[0]); }); } return result; } }, { supportedForProcessing: false, } ); // We need a stable sort in order to implement SortedListProjection because we want to be able to // perform insertions in a predictable location s.t. if we were to apply another sorted projection // over the same list (now containing the inserted data) the resulting order would be the same. // function mergeSort(m, sorter) { var length = m.length; if (length <= 1) { return m; } var middle = (length / 2) >>> 0; var left = mergeSort(m.slice(0, middle), sorter); var right = mergeSort(m.slice(middle), sorter); return merge(left, right, sorter); } function merge(left, right, sorter) { var result = []; while (left.length && right.length) { var r = sorter(left[0], right[0]); if (r <= 0) { result.push(left.shift()); } else { result.push(right.shift()); } } if (left.length) { result.push.apply(result, left); } if (right.length) { result.push.apply(result, right); } return result; } var SortedListProjection = WinJS.Class.derive(ListProjection, function (list, sortFunction) { this._list = list; this._addListListener("itemchanged", this._listItemChanged); this._addListListener("iteminserted", this._listItemInserted); this._addListListener("itemmoved", this._listItemMoved); this._addListListener("itemmutated", this._listItemMutated); this._addListListener("itemremoved", this._listItemRemoved); this._addListListener("reload", this._listReload); this._sortFunction = sortFunction; this._initSortedKeys(); }, { _sortFunction: null, _sortedKeys: null, _initSortedKeys: function () { var list = this._list; var keys = []; for (var i = 0, len = list.length; i < len; i++) { var item = list.getItem(i); if (item) { keys[i] = item.key; } } var sorter = this._sortFunction; var sorted = mergeSort(keys, function (l, r) { l = list.getItemFromKey(l).data; r = list.getItemFromKey(r).data; return sorter(l, r); }); this._sortedKeys = sorted; }, _findInsertionPos: function (key, index, value, startingMin, startingMax) { var sorter = this._sortFunction; var sortedKeys = this._sortedKeys; var min = Math.max(0, startingMin || -1); var max = Math.min(sortedKeys.length, startingMax || Number.MAX_VALUE); var mid = min; while (min <= max) { mid = ((min + max) / 2) >>> 0; var sortedKey = sortedKeys[mid]; if (!sortedKey) { break; } var sortedItem = this.getItemFromKey(sortedKey); var r = sorter(sortedItem.data, value); if (r < 0) { min = mid + 1; } else if (r === 0) { return this._findStableInsertionPos(key, index, min, max, mid, value); } else { max = mid - 1; } } return min; }, _findBeginningOfGroup: function (mid, sorter, list, sortedKeys, value) { // we made it to the beginning of the list without finding something // that sorts equal to this value, insert this key at the beginning of // this section of keys. var min = 0; var max = mid; while (min <= max) { mid = ((min + max) / 2) >>> 0; var sortedKey = sortedKeys[mid]; var sortedItem = list.getItemFromKey(sortedKey); var r = sorter(sortedItem.data, value); if (r < 0) { min = mid + 1; } else { max = mid - 1; } } return min; }, _findEndOfGroup: function (mid, sorter, list, sortedKeys, value) { // we made it ot the end of the list without finding something that sorts // equal to this value, insert this key at the end of this section of // keys. var min = mid; var max = sortedKeys.length; while (min <= max) { mid = ((min + max) / 2) >>> 0; var sortedKey = sortedKeys[mid]; if (!sortedKey) { return sortedKeys.length; } var sortedItem = list.getItemFromKey(sortedKey); var r = sorter(sortedItem.data, value); if (r <= 0) { min = mid + 1; } else { max = mid - 1; } } return min; }, _findStableInsertionPos: function (key, index, min, max, mid, value) { var list = this._list; var length = list.length; var sorter = this._sortFunction; var sortedKeys = this._sortedKeys; if (index < (length / 2)) { for (var i = index - 1; i >= 0; i--) { var item = list.getItem(i); if (sorter(item.data, value) === 0) { // we have found the next item to the left, insert this item to // the right of that. if ((length - min) > max) { return sortedKeys.indexOf(item.key, min) + 1; } else { return sortedKeys.lastIndexOf(item.key, max) + 1; } } } return this._findBeginningOfGroup(mid, sorter, list, sortedKeys, value); } else { for (var i = index + 1; i < length; i++) { var item = list.getItem(i); if (sorter(item.data, value) === 0) { // we have found the next item to the right, insert this item // to the left of that. if ((length - min) > max) { return sortedKeys.indexOf(item.key, min); } else { return sortedKeys.lastIndexOf(item.key, max); } } } return this._findEndOfGroup(mid, sorter, list, sortedKeys, value); } }, _listItemChanged: function (event) { var key = event.detail.key; var newValue = event.detail.newValue; var oldValue = event.detail.oldValue; var sortFunction = this._sortFunction; if (sortFunction(oldValue, newValue) === 0) { var sortedIndex = this.indexOfKey(key); this._notifyItemChanged(key, sortedIndex, oldValue, newValue, event.detail.oldItem, event.detail.newItem); } else { this._listItemRemoved({ detail: { key: key, index: event.detail.index, value: event.detail.oldValue, item: event.detail.oldItem } }); this._listItemInserted({ detail: { key: key, index: event.detail.index, value: event.detail.newValue } }); } }, _listItemInserted: function (event, knownMin, knownMax) { var key = event.detail.key; var index = event.detail.index; var value = event.detail.value; var sortedIndex = this._findInsertionPos(key, index, value, knownMin, knownMax); this._sortedKeys.splice(sortedIndex, 0, key); this._notifyItemInserted(key, sortedIndex, value); }, _listItemMoved: function (event, knownMin, knownMax) { var key = event.detail.key; var newIndex = event.detail.newIndex; var value = event.detail.value; var sortedKeys = this._sortedKeys; var oldSortedIndex = sortedKeys.indexOf(key, knownMin); sortedKeys.splice(oldSortedIndex, 1); var newSortedIndex = this._findInsertionPos(key, newIndex, value, knownMin, knownMax); sortedKeys.splice(newSortedIndex, 0, key); if (newSortedIndex !== oldSortedIndex) { // The move in the underlying list resulted in a move in the sorted list // this._notifyItemMoved(key, oldSortedIndex, newSortedIndex, value); } else { // The move in the underlying list resulted in no change in the sorted list // } }, _listItemMutated: function (event) { var key = event.detail.key; var value = event.detail.value; var item = event.detail.item; var index = this._list.indexOfKey(key); var sortedIndex = this._sortedKeys.indexOf(key); this._sortedKeys.splice(sortedIndex, 1); var targetIndex = this._findInsertionPos(key, index, value); this._sortedKeys.splice(sortedIndex, 0, key); if (sortedIndex === targetIndex) { this._notifyItemMutated(key, value, item); return; } this._listItemRemoved({ detail: { key: key, index: index, value: value, item: item } }); this._listItemInserted({ detail: { key: key, index: index, value: value } }); }, _listItemRemoved: function (event, knownMin) { var key = event.detail.key; var value = event.detail.value; var item = event.detail.item; var sortedKeys = this._sortedKeys; var sortedIndex = sortedKeys.indexOf(key, knownMin); sortedKeys.splice(sortedIndex, 1); this._notifyItemRemoved(key, sortedIndex, value, item); }, _listReload: function () { this._initSortedKeys(); this._notifyReload(); }, /// Gets or sets the length of the list. Returns an integer value one higher than the highest element defined in a list. length: { get: function () { return this._sortedKeys.length; }, set: function (value) { if (typeof value === "number" && value >= 0) { var current = this.length; if (current > value) { this.splice(value, current - value); } } else { throw new WinJS.ErrorFromName("WinJS.Binding.List.IllegalLength", strings.illegalListLength); } } }, getItem: function (index) { /// /// /// Returns a key/data pair for the specified index. /// /// The index of the value to retrieve. /// An object with .key and .data properties. /// index = asNumber(index); return this.getItemFromKey(this._sortedKeys[index]); }, indexOfKey: function (key) { /// /// /// Returns the index of the first occurrence of a key. /// /// The key to locate in the list. /// The index of the first occurrence of a key in a list, or -1 if not found. /// return this._sortedKeys.indexOf(key); }, notifyMutated: function (index) { /// /// /// Forces the list to send a itemmutated notification to any listeners for the value at the specified index. /// /// The index of the value that was mutated. /// index = asNumber(index); this._notifyMutatedFromKey(this._sortedKeys[index]); }, setAt: function (index, value) { /// /// /// Replaces the value at the specified index with a new value. /// /// The index of the value that was replaced. /// The new value. /// index = asNumber(index); this._setAtKey(this._sortedKeys[index], value); }, // returns [ data* ] of removed items _spliceFromKey: function (key, howMany) { // first add in all the new items if we have any, this should serve to push key to the right if (arguments.length > 2) { var args = copyargs(arguments); args[1] = 0; // howMany this._list._spliceFromKey.apply(this._list, args); } // now we can remove anything that needs to be removed, since they are not necessarially contiguous // in the underlying list we remove them one by one. var result = []; if (howMany) { var keysToRemove = []; var sortedKeys = this._sortedKeys; var sortedKeyIndex = sortedKeys.indexOf(key); for (var i = sortedKeyIndex, len = sortedKeys.length; i < len && (i - sortedKeyIndex) < howMany; i++) { keysToRemove.push(sortedKeys[i]); } var that = this; keysToRemove.forEach(function (key) { result.push(that._list._spliceFromKey(key, 1)[0]); }); } return result; } }, { supportedForProcessing: false, } ); // This projection sorts the underlying list by group key and within a group // respects the position of the item in the underlying list. It is built on top // of the SortedListProjection and has an intimate contract with // GroupsListProjection. // var GroupedSortedListProjection = WinJS.Class.derive(SortedListProjection, function (list, groupKeyOf, groupDataOf, groupSorter) { this._list = list; this._addListListener("itemchanged", this._listGroupedItemChanged); this._addListListener("iteminserted", this._listGroupedItemInserted); this._addListListener("itemmoved", this._listGroupedItemMoved); this._addListListener("itemmutated", this._listGroupedItemMutated); this._addListListener("itemremoved", this._listGroupedItemRemoved); this._addListListener("reload", this._listReload); this._sortFunction = function (l, r) { l = groupKeyOf(l); r = groupKeyOf(r); if (groupSorter) { return groupSorter(l, r); } else { return l < r ? -1 : l === r ? 0 : 1; } }; this._groupKeyOf = groupKeyOf; this._groupDataOf = groupDataOf; this._initSortedKeys(); this._initGroupedItems(); }, { _groupKeyOf: null, _groupDataOf: null, _groupedItems: null, _initGroupedItems: function () { var groupedItems = {}; var list = this._list; var groupKeyOf = this._groupKeyOf; for (var i = 0, len = list.length; i < len; i++) { var item = cloneItem(list.getItem(i)); item.groupKey = groupKeyOf(item.data); groupedItems[item.key] = item; } this._groupedItems = groupedItems; }, _groupsProjection: null, _listGroupedItemChanged: function (event) { var key = event.detail.key; var oldValue = event.detail.oldValue; var newValue = event.detail.newValue; var groupedItems = this._groupedItems; var oldGroupedItem = groupedItems[key]; var newGroupedItem = cloneItem(oldGroupedItem); newGroupedItem.data = newValue; newGroupedItem.groupKey = this._groupKeyOf(newValue); groupedItems[key] = newGroupedItem; var index; if (oldGroupedItem.groupKey === newGroupedItem.groupKey) { index = this.indexOfKey(key); this._notifyItemChanged(key, index, oldValue, newValue, oldGroupedItem, newGroupedItem); } else { index = event.detail.index; this._listItemChanged({ detail: { key: key, index: index, oldValue: oldValue, newValue: newValue, oldItem: oldGroupedItem, newItem: newGroupedItem } }); } }, _listGroupedItemInserted: function (event) { var key = event.detail.key; var value = event.detail.value; var groupKey = this._groupKeyOf(value); this._groupedItems[key] = { handle: key, key: key, data: value, groupKey: groupKey }; var groupMin, groupMax; if (this._groupsProjection) { var groupItem = this._groupsProjection._groupItems[groupKey]; if (groupItem) { groupMin = groupItem.firstItemIndexHint; groupMax = groupMin + groupItem.groupSize; } } this._listItemInserted(event, groupMin, groupMax); }, _listGroupedItemMoved: function (event) { var groupMin, groupMax; var groupKey = this._groupedItems[event.detail.key].groupKey; if (this._groupsProjection) { var groupItem = this._groupsProjection._groupItems[groupKey]; groupMin = groupItem.firstItemIndexHint; groupMax = groupMin + groupItem.groupSize; } this._listItemMoved(event, groupMin, groupMax); }, _listGroupedItemMutated: function (event) { var key = event.detail.key; var value = event.detail.value; var groupedItems = this._groupedItems; var oldGroupedItem = groupedItems[key]; var groupKey = this._groupKeyOf(value); if (oldGroupedItem.groupKey === groupKey) { this._notifyItemMutated(key, value, oldGroupedItem); } else { var newGroupedItem = cloneItem(oldGroupedItem); newGroupedItem.groupKey = groupKey; groupedItems[key] = newGroupedItem; var index = this._list.indexOfKey(key); this._listItemRemoved({ detail: { key: key, index: index, value: value, item: oldGroupedItem } }); this._listItemInserted({ detail: { key: key, index: index, value: value } }); } }, _listGroupedItemRemoved: function (event) { var key = event.detail.key; var index = event.detail.index; var value = event.detail.value; var groupedItems = this._groupedItems; var groupedItem = groupedItems[key]; delete groupedItems[key]; var groupMin, groupMax; if (this._groupsProjection) { var groupItem = this._groupsProjection._groupItems[groupedItem.groupKey]; groupMin = groupItem.firstItemIndexHint; groupMax = groupMin + groupItem.groupSize; } this._listItemRemoved({ detail: { key: key, index: index, value: value, item: groupedItem } }, groupMin, groupMax); }, // override _listReload _listReload: function () { this._initGroupedItems(); SortedListProjection.prototype._listReload.call(this); }, /// Gets a WinJS.Binding.List, which is a projection of the groups that were identified in this list. groups: { get: function () { if (this._groupsProjection === null) { this._groupsProjection = new GroupsListProjection(this, this._groupKeyOf, this._groupDataOf); } return this._groupsProjection; } }, // We have to implement this because we keep our own set of items so that we can // tag them with groupKey. // getItemFromKey: function (key) { /// /// /// Gets a key/data pair for the specified item key. /// /// The key of the value to retrieve. /// An object with .key and .data properties. /// return this._groupedItems[key]; } }, { supportedForProcessing: false, } ); // This is really an implementation detail of GroupedSortedListProjection and takes a // dependency on its internals and implementation details. // var GroupsListProjection = WinJS.Class.derive(ListBase, function (list, groupKeyOf, groupDataOf) { this._list = list; // itemchanged is handled by the GroupedSortedListProjection because if the item // changes groups it turns into a remove/insert this._addListListener("iteminserted", this._listItemInserted); this._addListListener("itemmoved", this._listItemMoved); // itemmutated is handled by the GroupedSortedListProjection because if the item // changes groups it turns into a remove/insert. this._addListListener("itemremoved", this._listItemRemoved); this._addListListener("reload", this._listReload); this._groupKeyOf = groupKeyOf; this._groupDataOf = groupDataOf; this._initGroupKeysAndItems(); }, { _list: null, _addListListener: function (name, func) { // interestingly, since GroupsListProjection has the same lifetime as the GroupedSortedListProjection // we don't have to worry about cleaning up the cycle here. this._list.addEventListener(name, func.bind(this)); }, _groupDataOf: null, _groupKeyOf: null, _groupOf: function (item) { return this.getItemFromKey(this._groupKeyOf(item.data)); }, _groupKeys: null, _groupItems: null, _initGroupKeysAndItems: function () { var groupDataOf = this._groupDataOf; var list = this._list; var groupItems = {}; var groupKeys = []; var currentGroupKey = null; var currentGroupItem = null; var groupCount; for (var i = 0, len = list.length; i < len; i++) { var item = list.getItem(i); var groupKey = item.groupKey; if (groupKey !== currentGroupKey) { // new group if (currentGroupItem) { currentGroupItem.groupSize = groupCount; } groupCount = 1; currentGroupKey = groupKey; currentGroupItem = { handle: groupKey, key: groupKey, data: groupDataOf(item.data), firstItemKey: item.key, firstItemIndexHint: i }; groupItems[groupKey] = currentGroupItem; groupKeys.push(groupKey); } else { // existing group groupCount++; } } if (currentGroupItem) { currentGroupItem.groupSize = groupCount; } this._groupKeys = groupKeys; this._groupItems = groupItems; }, _listItemInserted: function (event) { // iteminserted is only interesting if this is a new group, or is the first // item of the group at which point the group data is regenerated. It will // however always result in a +1 to all the following firstItemIndexHints // var key = event.detail.key; var index = event.detail.index; var value = event.detail.value; var list = this._list; var groupKey = list.getItemFromKey(key).groupKey; var groupItems = this._groupItems; var groupKeys = this._groupKeys; var groupItem = groupItems[groupKey]; var groupIndex; var oldGroupItem, newGroupItem; var i, len; if (!groupItem) { // we have a new group, add it for (i = 0, len = groupKeys.length; i < len; i++) { groupItem = groupItems[groupKeys[i]]; if (groupItem.firstItemIndexHint >= index) { break; } } groupIndex = i; groupItem = { handle: groupKey, key: groupKey, data: this._groupDataOf(value), groupSize: 1, firstItemKey: key, firstItemIndexHint: index }; groupKeys.splice(groupIndex, 0, groupKey); groupItems[groupKey] = groupItem; this._notifyItemInserted(groupKey, groupIndex, groupItem.data); } else { oldGroupItem = groupItem; newGroupItem = cloneItem(oldGroupItem); newGroupItem.groupSize++; if (oldGroupItem.firstItemIndexHint === index) { newGroupItem.groupData = this._groupDataOf(value); newGroupItem.firstItemKey = key; newGroupItem.firstItemIndexHint = index; } groupItems[groupKey] = newGroupItem; groupIndex = groupKeys.indexOf(groupKey); this._notifyItemChanged(groupKey, groupIndex, oldGroupItem.data, newGroupItem.data, oldGroupItem, newGroupItem); } // update the firstItemIndexHint on following groups for (i = groupIndex + 1, len = groupKeys.length; i < len; i++) { oldGroupItem = groupItems[groupKeys[i]]; newGroupItem = cloneItem(oldGroupItem); newGroupItem.firstItemIndexHint++; groupItems[newGroupItem.key] = newGroupItem; this._notifyItemChanged(newGroupItem.key, i, oldGroupItem.data, newGroupItem.data, oldGroupItem, newGroupItem); } }, _listItemMoved: function (event) { // itemmoved is not important to grouping unless the move resulted in a new // first item in the group at which point we will regenerate the group data // var key = event.detail.key; var oldIndex = event.detail.oldIndex; var newIndex = event.detail.newIndex; var list = this._list; var groupKey = list.getItemFromKey(key).groupKey; var groupItems = this._groupItems; var groupItem = groupItems[groupKey]; if (groupItem.firstItemIndexHint === newIndex || groupItem.firstItemIndexHint === oldIndex) { // the first item of the group has changed, update it. var item = list.getItem(groupItem.firstItemIndexHint); var newGroupItem = cloneItem(groupItem); newGroupItem.data = this._groupDataOf(item.data); newGroupItem.firstItemKey = item.key; groupItems[groupKey] = newGroupItem; this._notifyItemChanged(groupKey, this._groupKeys.indexOf(groupKey), groupItem.data, newGroupItem.data, groupItem, newGroupItem); } }, _listItemRemoved: function (event) { // itemremoved is only interesting if the group was of size 1 or was the // first item of the group at which point the group data is regenerated. // It will however always result in a -1 to all of the following // firstItemIndexHints. // var index = event.detail.index; var item = event.detail.item; var groupItems = this._groupItems; var groupKeys = this._groupKeys; // since the value is no longer in the list we can't ask for its item and // get the group key from there. var groupKey = item.groupKey; var groupItem = groupItems[groupKey]; var groupIndex = groupKeys.indexOf(groupKey); var oldGroupItem, newGroupItem; if (groupItem.groupSize === 1) { groupKeys.splice(groupIndex, 1); delete groupItems[groupKey]; this._notifyItemRemoved(groupKey, groupIndex, groupItem.data, groupItem); // after removing the group we need to decrement the index because it is used // for modifying subsequent group firstItemIndexHint's groupIndex--; } else { oldGroupItem = groupItem; newGroupItem = cloneItem(oldGroupItem); newGroupItem.groupSize--; if (oldGroupItem.firstItemIndexHint === index) { // find the next group item, it will be at the same index as the old // first group item. var newFirstItem = this._list.getItem(index); newGroupItem.data = this._groupDataOf(newFirstItem.data); newGroupItem.firstItemKey = newFirstItem.key; } groupItems[groupKey] = newGroupItem; this._notifyItemChanged(groupKey, groupIndex, oldGroupItem.data, newGroupItem.data, oldGroupItem, newGroupItem); } for (var i = groupIndex + 1, len = groupKeys.length; i < len; i++) { oldGroupItem = groupItems[groupKeys[i]]; newGroupItem = cloneItem(oldGroupItem); newGroupItem.firstItemIndexHint--; groupItems[newGroupItem.key] = newGroupItem; this._notifyItemChanged(newGroupItem.key, i, oldGroupItem.data, newGroupItem.data, oldGroupItem, newGroupItem); } }, _listReload: function () { this._initGroupKeysAndItems(); this._notifyReload(); }, /// Gets the length of the list. Returns an integer value one higher than the highest element defined in a list. length: { get: function () { return this._groupKeys.length; } }, getItem: function (index) { /// /// /// Gets a key/data pair for the specified index . /// /// The index of the value to retrieve. /// An object with .key and .data properties. /// index = asNumber(index); return this._groupItems[this._groupKeys[index]]; }, getItemFromKey: function (key) { /// /// /// Gets a key/data pair for the specified key. /// /// The key of the value to retrieve. /// An object with .key and .data properties. /// return this._groupItems[key]; }, indexOfKey: function (key) { /// /// /// Returns the index of the first occurrence of a key in a list. /// /// The key to locate in the list. /// The index of the first occurrence of a key in a list, or -1 if not found. /// return this._groupKeys.indexOf(key); } }, { supportedForProcessing: false, } ); WinJS.Namespace.define("WinJS.Binding", { List: List }); }(this)); (function resInit(WinJS, undefined) { "use strict"; var readyComplete = false; var resourceMap; var resourceLoader; var requireSupportedForProcessing = WinJS.Utilities.requireSupportedForProcessing function processAllImpl(rootElement, count) { rootElement = rootElement || document.body; var count = count || 0; if (count < 4) { // Only 3 depth is supported in the innerHTML if (count == 0) { if (rootElement.getAttribute) { // Fragment-loaded root element isn't caught by querySelectorAll var rootElementNode = rootElement.getAttribute('data-win-res'); if (rootElementNode) { var decls = WinJS.UI.optionsParser(rootElementNode); setMembers(rootElement, rootElement, decls, count); } } } var elements = rootElement.querySelectorAll('[data-win-res]'); if (elements.length === 0) { return WinJS.Promise.as(rootElement); } for (var i = 0, len = elements.length; i < len; i++) { var e = elements[i]; // Use optionsParser that accept string format // {name="value", name2="value2"} var decls = WinJS.UI.optionsParser(e.getAttribute('data-win-res')); setMembers(e, e, decls, count); } } else if (WinJS.validation) { throw new WinJS.ErrorFromName("WinJS.Res.NestingExceeded", WinJS.Resources._getWinJSString("base/nestingExceeded").value); } return WinJS.Promise.as(rootElement); }; function setAttributes(root, descriptor) { var names = Object.keys(descriptor); for (var k = 0, l = names.length ; k < l; k++) { var name = names[k]; var value = descriptor[name]; var data = WinJS.Resources.getString(value); if (!data || !data.empty) { root.setAttribute(name, data.value); if ((data.lang !== undefined) && (root.lang !== undefined) && (root.lang !== data.lang)) { root.lang = data.lang; } } else if (WinJS.validation) { notFound(value); } } } function notFound(name) { throw new WinJS.ErrorFromName("WinJS.Res.NotFound", WinJS.Resources._formatString(WinJS.Resources._getWinJSString("base/notFound").value, name)); } function setMembers(root, target, descriptor, count) { var names = Object.keys(descriptor); target = requireSupportedForProcessing(target); for (var k = 0, l = names.length ; k < l; k++) { var name = names[k]; var value = descriptor[name]; if (typeof value === "string") { var data = WinJS.Resources.getString(value); if (!data || !data.empty) { target[name] = data.value; if ((data.lang !== undefined) && (root.lang !== undefined) && (root.lang !== data.lang)) { // When lang property is different, we set the language with selected string's language root.lang = data.lang; } if (name === "innerHTML") { processAllImpl(target, count + 1); } } else if (WinJS.validation) { notFound(value); } } else if (root === target && name === "attributes") { //Exposing setAttribute for attributes that don't have HTML properties, like aria, through a fake 'attributes' property setAttributes(root, value); } else { setMembers(root, target[name], value, count); } } } WinJS.Namespace.define("WinJS.Resources", { processAll: function (rootElement) { /// /// /// Processes resources tag and replaces strings /// with localized strings. /// /// /// The DOM element at which to start processing. processAll processes the element and its child elements. /// If you don't specify root element, processAll processes the entire document. /// /// if (!readyComplete) { return WinJS.Utilities.ready().then(function () { readyComplete = true; return processAllImpl(rootElement); }); } else { try { return processAllImpl(rootElement); } catch (e) { return WinJS.Promise.wrapError(e); } } } }); })(WinJS);