'use strict';
import _WinJS = require('./_WinJS');
import _Global = require('./_Global');
import _BaseCoreUtils = require('./_BaseCoreUtils');
import _WriteProfilerMark = require('./_WriteProfilerMark');
var initializeProperties = (target?, members?, prefix?) => {
var keys = Object.keys(members);
var isArray = Array.isArray(target);
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;
}
if (prefix && member.setName && typeof member.setName === 'function') {
member.setName(prefix + "." + key);
}
properties = properties || {};
properties[key] = member;
continue;
}
}
if (!enumerable) {
properties = properties || {};
properties[key] = { value: member, enumerable: enumerable, configurable: true, writable: true };
continue;
}
if (isArray) {
target.forEach(function(target) {
target[key] = member;
});
} else {
target[key] = member;
}
}
if (properties) {
if (isArray) {
target.forEach(function(target) {
Object.defineProperties(target, properties);
});
} else {
Object.defineProperties(target, properties);
}
}
}
(function() {
var _rootNamespace = _WinJS;
if (!_rootNamespace.Namespace) {
_rootNamespace.Namespace = Object.create(Object.prototype);
}
function createNamespace(parentNamespace, name) {
var currentNamespace = parentNamespace || {};
if (name) {
var namespaceFragments = name.split(".");
if (currentNamespace === _Global && namespaceFragments[0] === "WinJS") {
currentNamespace = _WinJS;
namespaceFragments.splice(0, 1);
}
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];
}
}
return currentNamespace;
}
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 = createNamespace(parentNamespace, name);
if (members) {
initializeProperties(currentNamespace, members, name || "");
}
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);
}
var LazyStates = {
uninitialized: 1,
working: 2,
initialized: 3,
};
function lazy(f) {
var name;
var state = LazyStates.uninitialized;
var result;
return {
setName: function(value) {
name = value;
},
get: function() {
switch (state) {
case LazyStates.initialized:
return result;
case LazyStates.uninitialized:
state = LazyStates.working;
try {
_WriteProfilerMark("WinJS.Namespace._lazy:" + name + ",StartTM");
result = f();
} finally {
_WriteProfilerMark("WinJS.Namespace._lazy:" + name + ",StopTM");
state = LazyStates.uninitialized;
}
f = null;
state = LazyStates.initialized;
return result;
case LazyStates.working:
throw "Illegal: reentrancy on initialization";
default:
throw "Illegal";
}
},
set: function(value) {
switch (state) {
case LazyStates.working:
throw "Illegal: reentrancy on initialization";
default:
state = LazyStates.initialized;
result = value;
break;
}
},
enumerable: true,
configurable: true,
};
}
// helper for defining AMD module members
function moduleDefine(exports, name, members) {
var target = [];
if (exports) target = [exports];
var publicNS = null;
if (name) {
publicNS = createNamespace(_Global, name);
target.push(publicNS);
}
initializeProperties(target, members, name || "");
return publicNS;
}
// 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 },
_lazy: { value: lazy, writable: true, enumerable: true, configurable: true },
_moduleDefine: { value: moduleDefine, writable: true, enumerable: true, configurable: true }
});
})();
(function() {
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() { };
_BaseCoreUtils.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);
_BaseCoreUtils.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
});
})();
var _export = {
Namespace: _WinJS.Namespace,
Class: _WinJS.Class
};
export = _export;