mirror of
https://github.com/modernw/App-Installer-For-Windows-8.x-Reset.git
synced 2026-06-19 05:40:12 +10:00
Update Shell.
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
var AnimationKeyFrames = {
|
||||
// 弹出(从边缘)
|
||||
Flyout: new(function Flyout() {
|
||||
if (this._instance) { return this._instance; }
|
||||
this._instance = this;
|
||||
// 向顶端
|
||||
this.toTop = "WinJS-showFlyoutTop";
|
||||
// 向底端
|
||||
this.toBottom = "WinJS-showFlyoutBottom";
|
||||
// 向左
|
||||
this.toLeft = "WinJS-showFlyoutLeft";
|
||||
// 向右
|
||||
this.toRight = "WinJS-showFlyoutRight";
|
||||
// 从底端(别名,向顶端)
|
||||
this.fromBottom = this.toTop;
|
||||
// 从顶端(别名,向底端)
|
||||
this.fromTop = this.toBottom;
|
||||
// 从左侧
|
||||
this.fromLeft = this.toRight;
|
||||
// 从右侧
|
||||
this.fromRight = this.toLeft;
|
||||
})(),
|
||||
// WinJS 内部使用,建议不用这个
|
||||
Progress: {
|
||||
fadeOut: "win-progress-fade-out"
|
||||
},
|
||||
// WinJS 内部使用,对于搜索框弹出搜索推荐
|
||||
SearchBox: {
|
||||
// 显示搜索推荐
|
||||
showPopup: {
|
||||
flyoutBelow: "WinJS-flyoutBelowSearchBox-showPopup",
|
||||
flyoutAbove: "WinJS-flyoutAboveSearchBox-showPopup"
|
||||
}
|
||||
},
|
||||
// 渐变
|
||||
Opacity: {
|
||||
// 显示
|
||||
visible: "WinJS-opacity-in",
|
||||
// 消失
|
||||
hidden: "WinJS-opacity-out"
|
||||
},
|
||||
// 缩放
|
||||
Scale: {
|
||||
// 放大一点
|
||||
up: "WinJS-scale-up",
|
||||
// 缩小一点
|
||||
down: "WinJS-scale-down"
|
||||
},
|
||||
// 默认分类
|
||||
Default: {
|
||||
// 从右返回
|
||||
remove: "WinJS-default-remove",
|
||||
// 从左返回
|
||||
removertl: "WinJS-default-remove-rtl",
|
||||
// 向右移动
|
||||
apply: "WinJS-default-apply",
|
||||
// 向左移动
|
||||
applyrtl: "WinJS-default-apply-rtl"
|
||||
},
|
||||
// 从边缘
|
||||
Edge: {
|
||||
// 从顶部
|
||||
show: "WinJS-showEdgeUI",
|
||||
// 到顶部
|
||||
hide: "WinJS-hideEdgeUI"
|
||||
},
|
||||
Panel: {
|
||||
// 从右侧
|
||||
show: "WinJS-showPanel",
|
||||
// 从左侧
|
||||
showrtl: "WinJS-showPanel-rtl",
|
||||
// 到右侧
|
||||
hide: "WinJS-hidePanel",
|
||||
// 到左侧
|
||||
hidertl: "WinJS-hidePanel-rtl"
|
||||
},
|
||||
Popup: {
|
||||
show: "WinJS-showPopup"
|
||||
},
|
||||
// 对元素的拖放
|
||||
Drag: {
|
||||
// 从右复位
|
||||
sourceEnd: "WinJS-dragSourceEnd",
|
||||
// 从左复位
|
||||
sourceEndRtl: "WinJS-dragSourceEnd-rtl"
|
||||
},
|
||||
// 进入内容
|
||||
Content: {
|
||||
// 从右进入
|
||||
enter: "WinJS-enterContent",
|
||||
// 从左进入
|
||||
enterrtl: "WinJS-enterContent-rtl"
|
||||
},
|
||||
Page: {
|
||||
// 从右进入
|
||||
enter: "WinJS-enterPage",
|
||||
// 从左进入
|
||||
enterrtl: "WinJS-enterPage-rtl"
|
||||
},
|
||||
Exit: "WinJS-exit",
|
||||
UpdateBadge: "WinJS-updateBadge"
|
||||
};
|
||||
Object.freeze(AnimationKeyFrames);
|
||||
/**
|
||||
* 生成用于 element.style.animation 的字符串
|
||||
* @param {string} swKeyFrames - 动画关键帧名称
|
||||
* @param {number} uMillisecond - 动画持续时间(毫秒)
|
||||
* @param {string} [swTimingFunc] - 缓动函数,默认 cubic-bezier(0.1, 0.9, 0.2, 1)
|
||||
* @param {number} [uDelayMs] - 延迟时间(毫秒),默认 0
|
||||
* @param {string} [swIteration] - 播放次数,默认 "1"
|
||||
* @param {string} [swDirection] - 播放方向,默认 "normal"
|
||||
* @param {string} [swFillMode] - 填充模式,默认 "forwards"
|
||||
* @param {string} [swPlayState] - 播放状态,默认 ""
|
||||
* @returns {string} 可直接赋给 element.style.animation
|
||||
*/
|
||||
function generateAnimeString(swKeyFrames, uMillisecond, swTimingFunc, uDelayMs, swIteration, swDirection, swFillMode, swPlayState) {
|
||||
// 默认参数
|
||||
if (!swTimingFunc) { swTimingFunc = "cubic-bezier(0.1, 0.9, 0.2, 1)"; }
|
||||
if (!uDelayMs) { uDelayMs = 0; }
|
||||
if (!swIteration) { swIteration = "1"; }
|
||||
if (!swDirection) { swDirection = "normal"; }
|
||||
if (!swFillMode) { swFillMode = "forwards"; }
|
||||
if (!swPlayState) { swPlayState = ""; }
|
||||
if (!uMillisecond) { uMillisecond = 500; }
|
||||
if (!swKeyFrames) { swKeyFrames = AnimationKeyFrames.Flyout.toLeft; }
|
||||
// 毫秒转秒
|
||||
var swDuration = (uMillisecond * 0.001) + "s";
|
||||
var swDelay = (uDelayMs * 0.001) + "s";
|
||||
// 拼接函数
|
||||
function buildOne(name) {
|
||||
return name + " " +
|
||||
swDuration + " " +
|
||||
swTimingFunc + " " +
|
||||
swDelay + " " +
|
||||
swIteration + " " +
|
||||
swDirection + " " +
|
||||
swFillMode +
|
||||
(swPlayState ? (" " + swPlayState) : "");
|
||||
}
|
||||
var swResult = "";
|
||||
if (typeof swKeyFrames === "string") {
|
||||
swResult = buildOne(swKeyFrames);
|
||||
} else if (swKeyFrames instanceof Array) {
|
||||
var parts = [];
|
||||
for (var i = 0; i < swKeyFrames.length; i++) {
|
||||
parts.push(buildOne(swKeyFrames[i]));
|
||||
}
|
||||
swResult = parts.join(", ");
|
||||
}
|
||||
return swResult;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Windows: {
|
||||
UI: {
|
||||
Animation: {
|
||||
Keyframes: AnimationKeyFrames,
|
||||
Animation: generateAnimeString,
|
||||
/**
|
||||
* 异步设置动画,返回一个 Promise 对象,动画结束后会执行
|
||||
* @param {HTMLElement} element 元素节点
|
||||
* @param {string|Array <string>} swKeyFrames 动画关键帧名称
|
||||
* @param {number} uMillisecond 动画持续时间(毫秒)
|
||||
* @param {string} [swTimingFunc] 缓动函数,默认 cubic-bezier(0.1, 0.9, 0.2, 1)
|
||||
* @param {number} [uDelayMs] 延迟时间(毫秒),默认 0
|
||||
* @param {string} [swIteration] 播放次数,默认 "1"
|
||||
* @param {string} [swDirection] 播放方向,默认 "normal"
|
||||
* @param {string} [swFillMode] 填充模式,默认 "forwards"
|
||||
* @param {string} [swPlayState] 播放状态,默认 ""
|
||||
* @returns {Promise}
|
||||
*/
|
||||
RunAsync: function(element, swKeyFrames, uMillisecond, swTimingFunc, uDelayMs, swIteration, swDirection, swFillMode, swPlayState) {
|
||||
return new WinJS.Promise(function(complete) {
|
||||
element.style.animation = generateAnimeString(swKeyFrames, uMillisecond, swTimingFunc, uDelayMs, swIteration, swDirection, swFillMode, swPlayState);
|
||||
element.addEventListener("animationend", function() {
|
||||
element.style.animation = "";
|
||||
complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
@@ -0,0 +1,41 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
var ext = global.external;
|
||||
|
||||
function blankFunc(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) {
|
||||
console.log("blankFunc called with arguments: " + arg1 + ", " + arg2 + ", " + arg3 + ", " + arg4 + ", " + arg5 + ", " + arg6 + ", " + arg7 + ", " + arg8 + ", " + arg9 + ", " + arg10);
|
||||
}
|
||||
|
||||
global.Bridge = {
|
||||
External: ext,
|
||||
Frame: {
|
||||
isIe10: function() { return ext.IEFrame.Version === 10; },
|
||||
isIe11: function() { return ext.IEFrame.Version === 11; }
|
||||
},
|
||||
UI: {},
|
||||
String: {
|
||||
trim: function(str) { return ext.String.Trim(str); },
|
||||
tolower: function(str) { return ext.String.ToLower(str); },
|
||||
toupper: function(str) { return ext.String.ToUpper(str); },
|
||||
},
|
||||
NString: {
|
||||
equals: function(str1, str2) { return ext.String.NString.NEquals(str1, str2); },
|
||||
compare: function(str1, str2) { return ext.String.NString.Compare(str1, str2); },
|
||||
empty: function(str) { return ext.String.NString.Empty(str); },
|
||||
length: function(str) { return ext.String.NString.Length(str); },
|
||||
}
|
||||
}
|
||||
Object.defineProperty(global.Bridge.Frame, "scale", {
|
||||
get: function() { return ext.IEFrame.Scale; },
|
||||
set: function(value) { ext.IEFrame.Scale = value; return ext.IEFrame.Scale; }
|
||||
});
|
||||
Object.defineProperty(global.Bridge.Frame, "version", {
|
||||
get: function() { return ext.IEFrame.Version; },
|
||||
});
|
||||
Object.defineProperty(global.Bridge.UI, "dpiPercent", {
|
||||
get: function() { return ext.System.UI.DPIPercent; }
|
||||
});
|
||||
Object.defineProperty(global.Bridge.UI, "dpi", {
|
||||
get: function() { return ext.System.UI.DPI; }
|
||||
});
|
||||
})(this);
|
||||
@@ -0,0 +1,463 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
|
||||
function RGB(parent) {
|
||||
Object.defineProperty(this, "red", {
|
||||
get: function() { return parent.red; },
|
||||
set: function(value) { parent.red = value; }
|
||||
});
|
||||
Object.defineProperty(this, "green", {
|
||||
get: function() { return parent.green; },
|
||||
set: function(value) { parent.green = value; }
|
||||
});
|
||||
Object.defineProperty(this, "blue", {
|
||||
get: function() { return parent.blue; },
|
||||
set: function(value) { parent.blue = value; }
|
||||
});
|
||||
this.toString = function() { return "rgb(" + parent.red + "," + parent.green + "," + parent.blue + ")"; };
|
||||
this.valueOf = function() { return parent.valueOf(); };
|
||||
this.convert = function(type) { return new type(parent); };
|
||||
Object.defineProperty(this, "hex", {
|
||||
get: function() { return parent.hex; },
|
||||
set: function(value) { parent.hex = value; }
|
||||
});
|
||||
Object.defineProperty(this, "color", {
|
||||
get: function() { return parent; }
|
||||
});
|
||||
}
|
||||
|
||||
function RGBA(parent) {
|
||||
parent.RGB.constructor.call(this, parent);
|
||||
Object.defineProperty(this, "alpha", {
|
||||
get: function() { return parent.alpha; },
|
||||
set: function(value) { parent.alpha = value; }
|
||||
});
|
||||
this.toString = function() { return "rgba(" + parent.red + "," + parent.green + "," + parent.blue + "," + (parent.alpha / 255).toFixed(2) + ")"; };
|
||||
this.valueOf = function() { return parent.valueOf(); };
|
||||
}
|
||||
|
||||
function HSL(parent) {
|
||||
parent.RGB.constructor.call(this, parent);
|
||||
/**
|
||||
* @type {number} 色调 0 - 360
|
||||
*/
|
||||
Object.defineProperty(this, "hue", {
|
||||
get: function() {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h, s, l = (max + min) / 2;
|
||||
if (max == min) {
|
||||
h = s = 0; // achromatic
|
||||
} else {
|
||||
var d = max - min;
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
switch (max) {
|
||||
case r:
|
||||
h = (g - b) / d + (g < b ? 6 : 0);
|
||||
break;
|
||||
case g:
|
||||
h = (b - r) / d + 2;
|
||||
break;
|
||||
case b:
|
||||
h = (r - g) / d + 4;
|
||||
break;
|
||||
}
|
||||
h /= 6;
|
||||
}
|
||||
return h * 360;
|
||||
},
|
||||
set: function(value) {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h = value / 360,
|
||||
s = (max == 0 ? 0 : (max - min) / (max + min)),
|
||||
l = (max + min) / 2;
|
||||
if (s == 0) {
|
||||
r = g = b = l; // achromatic
|
||||
} else {
|
||||
var hue2rgb = function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
var p = 2 * l - q;
|
||||
r = hue2rgb(p, q, h + 1 / 3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
parent.red = Math.round(r * 255);
|
||||
parent.green = Math.round(g * 255);
|
||||
parent.blue = Math.round(b * 255);
|
||||
}
|
||||
});
|
||||
/**
|
||||
* @type {number} 饱和度 0 - 1
|
||||
*/
|
||||
Object.defineProperty(this, "saturation", {
|
||||
get: function() {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h, s, l = (max + min) / 2;
|
||||
if (max == min) {
|
||||
s = 0; // achromatic
|
||||
} else {
|
||||
var d = max - min;
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
}
|
||||
return s;
|
||||
},
|
||||
set: function(value) {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h = parent.hue / 360,
|
||||
s = value,
|
||||
l = (max + min) / 2;
|
||||
if (s == 0) {
|
||||
r = g = b = l; // achromatic
|
||||
} else {
|
||||
var hue2rgb = function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
var p = 2 * l - q;
|
||||
r = hue2rgb(p, q, h + 1 / 3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
parent.red = Math.round(r * 255);
|
||||
parent.green = Math.round(g * 255);
|
||||
parent.blue = Math.round(b * 255);
|
||||
}
|
||||
});
|
||||
/**
|
||||
* @type {number} 亮度 0 - 1
|
||||
*/
|
||||
Object.defineProperty(this, "lightness", {
|
||||
get: function() {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h, s, l = (max + min) / 2;
|
||||
return l;
|
||||
},
|
||||
set: function(value) {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h = parent.hue / 360,
|
||||
s = parent.saturation,
|
||||
l = value;
|
||||
if (s == 0) {
|
||||
r = g = b = l; // achromatic
|
||||
} else {
|
||||
var hue2rgb = function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
var p = 2 * l - q;
|
||||
r = hue2rgb(p, q, h + 1 / 3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
parent.red = Math.round(r * 255);
|
||||
parent.green = Math.round(g * 255);
|
||||
parent.blue = Math.round(b * 255);
|
||||
}
|
||||
});
|
||||
this.toString = function() { return "hsl(" + this.hue + "," + (this.saturation * 100).toFixed(2) + "%," + (this.lightness * 100).toFixed(2) + "%)"; };
|
||||
this.valueOf = function() { return parent.valueOf(); };
|
||||
}
|
||||
|
||||
function HSLA(parent) {
|
||||
parent.HSL.constructor.call(this, parent);
|
||||
Object.defineProperty(this, "alpha", {
|
||||
get: function() { return parent.alpha; },
|
||||
set: function(value) { parent.alpha = value; }
|
||||
});
|
||||
this.toString = function() { return "hsla(" + this.hue + "," + (this.saturation * 100).toFixed(2) + "%," + (this.lightness * 100).toFixed(2) + "%," + (parent.alpha / 255).toFixed(2) + ")"; };
|
||||
this.valueOf = function() { return parent.valueOf(); };
|
||||
}
|
||||
|
||||
function HWB(parent) {
|
||||
parent.RGB.constructor.call(this, parent);
|
||||
/**
|
||||
* @type {number} 色调 0 - 360
|
||||
*/
|
||||
Object.defineProperty(this, "hue", {
|
||||
get: function() {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h, w, b = (max + min) / 2;
|
||||
if (max == min) {
|
||||
h = w = 0; // achromatic
|
||||
} else {
|
||||
var d = max - min;
|
||||
w = (max == 0 ? 0 : d / max);
|
||||
switch (max) {
|
||||
case r:
|
||||
h = (g - b) / d + (g < b ? 6 : 0);
|
||||
break;
|
||||
case g:
|
||||
h = (b - r) / d + 2;
|
||||
break;
|
||||
case b:
|
||||
h = (r - g) / d + 4;
|
||||
break;
|
||||
}
|
||||
h /= 6;
|
||||
}
|
||||
return h * 360;
|
||||
},
|
||||
set: function(value) {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h = value / 360,
|
||||
w = parent.white,
|
||||
b = (1 - w) * max;
|
||||
if (max == min) {
|
||||
r = g = b = max; // achromatic
|
||||
} else {
|
||||
var hue2rgb = function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
var q = b < 0.5 ? b * (1 + w) : b + w - b * w;
|
||||
var p = 2 * b - q;
|
||||
r = hue2rgb(p, q, h + 1 / 3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
parent.red = Math.round(r * 255);
|
||||
parent.green = Math.round(g * 255);
|
||||
parent.blue = Math.round(b * 255);
|
||||
}
|
||||
});
|
||||
/**
|
||||
* @type {number} 白色分量 0 - 1
|
||||
*/
|
||||
Object.defineProperty(this, "white", {
|
||||
get: function() {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h, w, b = (max + min) / 2;
|
||||
if (max == min) {
|
||||
w = 0; // achromatic
|
||||
} else {
|
||||
var d = max - min;
|
||||
w = (max == 0 ? 0 : d / max);
|
||||
}
|
||||
return w;
|
||||
},
|
||||
set: function(value) {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h = parent.hue / 360,
|
||||
w = value,
|
||||
b = (1 - w) * max;
|
||||
if (max == min) {
|
||||
r = g = b = max; // achromatic
|
||||
} else {
|
||||
var hue2rgb = function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
var q = b < 0.5 ? b * (1 + w) : b + w - b * w;
|
||||
var p = 2 * b - q;
|
||||
r = hue2rgb(p, q, h + 1 / 3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
parent.red = Math.round(r * 255);
|
||||
parent.green = Math.round(g * 255);
|
||||
parent.blue = Math.round(b * 255);
|
||||
}
|
||||
});
|
||||
Object.defineProperty(this, "black", {
|
||||
get: function() {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h, w, b = (max + min) / 2;
|
||||
if (max == min) {
|
||||
w = 0; // achromatic
|
||||
} else {
|
||||
var d = max - min;
|
||||
w = (max == 0 ? 0 : d / max);
|
||||
}
|
||||
return 1 - w;
|
||||
},
|
||||
set: function(value) {
|
||||
var r = parent.red / 255,
|
||||
g = parent.green / 255,
|
||||
b = parent.blue / 255;
|
||||
var max = Math.max(r, g, b),
|
||||
min = Math.min(r, g, b);
|
||||
var h = parent.hue / 360,
|
||||
w = 1 - value,
|
||||
b = (1 - w) * max;
|
||||
if (max == min) {
|
||||
r = g = b = max; // achromatic
|
||||
} else {
|
||||
var hue2rgb = function hue2rgb(p, q, t) {
|
||||
if (t < 0) t += 1;
|
||||
if (t > 1) t -= 1;
|
||||
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if (t < 1 / 2) return q;
|
||||
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
};
|
||||
var q = b < 0.5 ? b * (1 + w) : b + w - b * w;
|
||||
var p = 2 * b - q;
|
||||
r = hue2rgb(p, q, h + 1 / 3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1 / 3);
|
||||
}
|
||||
parent.red = Math.round(r * 255);
|
||||
parent.green = Math.round(g * 255);
|
||||
parent.blue = Math.round(b * 255);
|
||||
}
|
||||
});
|
||||
Object.defineProperty(this, "alpha", {
|
||||
get: function() { return parent.alpha; },
|
||||
set: function(value) { parent.alpha = value; }
|
||||
});
|
||||
this.toString = function() {
|
||||
if (parent.alpha == 255) {
|
||||
return "hwb(" + this.hue + "," + (this.white * 100).toFixed(2) + "%," + (this.black * 100).toFixed(2) + "%)";
|
||||
} else {
|
||||
return "hwb(" + this.hue + "," + (this.white * 100).toFixed(2) + "%," + (this.black * 100).toFixed(2) + "% / " + (parent.alpha / 255).toFixed(2) + ")";
|
||||
}
|
||||
};
|
||||
this.valueOf = function() { return parent.valueOf(); };
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {number} red 红色通道值 0-255
|
||||
* @param {number} green 绿色通道值 0-255
|
||||
* @param {number} blue 蓝色通道值 0-255
|
||||
* @param {number} alpha 透明度通道值 0-255
|
||||
*/
|
||||
function Color(red, green, blue, alpha) {
|
||||
red = red & 0xFF;
|
||||
green = green & 0xFF;
|
||||
blue = blue & 0xFF;
|
||||
alpha = (typeof alpha === "undefined") ? 255 : (alpha & 0xFF);
|
||||
this.rgbData = (red << 16) | (green << 8) | blue;
|
||||
this.alpha = alpha;
|
||||
// 红色通道
|
||||
Object.defineProperty(this, "red", {
|
||||
get: function() { return (this.rgbData >>> 16) & 0xFF; },
|
||||
set: function(value) { this.rgbData = ((value & 0xFF) << 16) | (this.rgbData & 0x00FFFF); }
|
||||
});
|
||||
// 绿色通道
|
||||
Object.defineProperty(this, "green", {
|
||||
get: function() { return (this.rgbData >>> 8) & 0xFF; },
|
||||
set: function(value) { this.rgbData = (this.rgbData & 0xFF00FF) | ((value & 0xFF) << 8); }
|
||||
});
|
||||
// 蓝色通道
|
||||
Object.defineProperty(this, "blue", {
|
||||
get: function() { return this.rgbData & 0xFF; },
|
||||
set: function(value) { this.rgbData = (this.rgbData & 0xFFFF00) | (value & 0xFF); }
|
||||
});
|
||||
// Alpha 通道单独存储
|
||||
Object.defineProperty(this, "alpha", {
|
||||
get: function() { return this._alpha; },
|
||||
set: function(value) { this._alpha = value & 0xFF; }
|
||||
});
|
||||
// hex 属性
|
||||
Object.defineProperty(this, "hex", {
|
||||
get: function() {
|
||||
function padZero(str, length) {
|
||||
while (str.length < length) str = "0" + str;
|
||||
return str;
|
||||
}
|
||||
var r = padZero(this.red.toString(16), 2),
|
||||
g = padZero(this.green.toString(16), 2),
|
||||
b = padZero(this.blue.toString(16), 2),
|
||||
a = padZero(this.alpha.toString(16), 2);
|
||||
return this.alpha === 255 ? "#" + r + g + b : "#" + r + g + b + a;
|
||||
},
|
||||
set: function(value) {
|
||||
var hex = value.replace(/^#/, "");
|
||||
if (hex.length === 3) hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
|
||||
if (hex.length === 6) hex = hex + "ff";
|
||||
if (hex.length === 8) {
|
||||
this.red = parseInt(hex.substr(0, 2), 16);
|
||||
this.green = parseInt(hex.substr(2, 2), 16);
|
||||
this.blue = parseInt(hex.substr(4, 2), 16);
|
||||
this.alpha = parseInt(hex.substr(6, 2), 16);
|
||||
}
|
||||
}
|
||||
});
|
||||
this.toString = function() { return this.hex; };
|
||||
this.valueOf = function() {
|
||||
function padZero(str, length) {
|
||||
while (str.length < length) str = "0" + str;
|
||||
return str;
|
||||
}
|
||||
var r = padZero(this.red.toString(16), 2),
|
||||
g = padZero(this.green.toString(16), 2),
|
||||
b = padZero(this.blue.toString(16), 2),
|
||||
a = padZero(this.alpha.toString(16), 2);
|
||||
return "#" + r + g + b + a;
|
||||
};
|
||||
this.RGB = new RGB(this);
|
||||
this.RGBA = new RGBA(this);
|
||||
this.HSL = new HSL(this);
|
||||
this.HSLA = new HSLA(this);
|
||||
this.HWB = new HWB(this);
|
||||
}
|
||||
module.exports = { Color: Color };
|
||||
})(this);
|
||||
@@ -0,0 +1,178 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
var highDpiMode = 0; // 默认不进行转换
|
||||
var dpiValue = 1.0; // 默认 100%
|
||||
|
||||
function IsIeVersionElder() {
|
||||
return Bridge.Frame.version < 11;
|
||||
}
|
||||
// 0 - 不使用
|
||||
// 1 - 转换为 px
|
||||
// 2 - 转换为 pt
|
||||
function setHighDpiMode(modeType) {
|
||||
highDpiMode = modeType;
|
||||
refreshModeDisplay();
|
||||
}
|
||||
|
||||
function refreshModeDisplay() {
|
||||
switch (highDpiMode) {
|
||||
case 1:
|
||||
convertAllPtToPx();
|
||||
break;
|
||||
case 2:
|
||||
convertAllPxToPt();
|
||||
break;
|
||||
}
|
||||
var images = document.getElementsByTagName('img');
|
||||
for (var i = 0; i < images.length; i++) {
|
||||
var img = images[i];
|
||||
var parent = img.parentElement;
|
||||
if (!parent) continue;
|
||||
var parentWidth = parent.offsetWidth;
|
||||
var parentHeight = parent.offsetHeight;
|
||||
var scaledWidth = img.naturalWidth * dpiValue;
|
||||
var scaledHeight = img.naturalHeight * dpiValue;
|
||||
if (scaledWidth > parentWidth || scaledHeight > parentHeight) {
|
||||
img.style.transform = 'none';
|
||||
} else {
|
||||
img.style.transform = 'scale(' + dpiValue + ')';
|
||||
}
|
||||
// console.log('Image:', img, 'Parent Size:', parentWidth, parentHeight, 'Scaled:', scaledWidth, scaledHeight, 'Transform:', img.style.transform);
|
||||
}
|
||||
if (IsIeVersionElder && IsIeVersionElder()) {
|
||||
var contentElements = document.getElementsByClassName('content');
|
||||
var controlElements = document.getElementsByClassName('control-column-bottom');
|
||||
if (controlElements.length === 0 || contentElements.length === 0) {
|
||||
console.error('未找到相应元素');
|
||||
} else {
|
||||
var controlElement = controlElements[0];
|
||||
var vheight = controlElement.currentStyle ?
|
||||
controlElement.currentStyle.height :
|
||||
window.getComputedStyle(controlElement).height;
|
||||
for (var i = 0; i < contentElements.length; i++) {
|
||||
var element = contentElements[i];
|
||||
element.style.height = 'calc(100% - ' + vheight + ')';
|
||||
// console.log('Set height of', element, 'to', 'calc(100% - ' + vheight + ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function convertAllPtToPx() {
|
||||
var conversionFactor = 96 / 72;
|
||||
var allElements = document.getElementsByTagName("*");
|
||||
for (var i = 0; i < allElements.length; i++) {
|
||||
var el = allElements[i];
|
||||
if (el.style && el.style.cssText) {
|
||||
el.style.cssText = el.style.cssText.replace(/(\d+(\.\d+)?)pt/g, function(match, p1) {
|
||||
var pxValue = parseFloat(p1) * conversionFactor;
|
||||
return pxValue + "px";
|
||||
});
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < document.styleSheets.length; i++) {
|
||||
var styleSheet = document.styleSheets[i];
|
||||
try {
|
||||
var rules = styleSheet.cssRules || styleSheet.rules;
|
||||
if (rules) {
|
||||
for (var j = 0; j < rules.length; j++) {
|
||||
var rule = rules[j];
|
||||
if (rule.style) {
|
||||
for (var k = 0; k < rule.style.length; k++) {
|
||||
var propertyName = rule.style[k];
|
||||
var value = rule.style.getPropertyValue(propertyName);
|
||||
if (value.indexOf("pt") !== -1) {
|
||||
var newValue = value.replace(/(\d+(\.\d+)?)pt/g, function(match, p1) {
|
||||
var pxValue = parseFloat(p1) * conversionFactor;
|
||||
return pxValue + "px";
|
||||
});
|
||||
var priority = (rule.style.getPropertyPriority) ? rule.style.getPropertyPriority(propertyName) : "";
|
||||
rule.style.setProperty(propertyName, newValue, priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Could not access stylesheet", styleSheet.href, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function convertAllPxToPt() {
|
||||
var conversionFactor = 72 / 96;
|
||||
var allElements = document.getElementsByTagName("*");
|
||||
for (var i = 0; i < allElements.length; i++) {
|
||||
var el = allElements[i];
|
||||
if (el.style && el.style.cssText) {
|
||||
el.style.cssText = el.style.cssText.replace(/(\d+(\.\d+)?)px/g, function(match, p1) {
|
||||
var pxValue = parseFloat(p1) * conversionFactor;
|
||||
return pxValue + "pt";
|
||||
});
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < document.styleSheets.length; i++) {
|
||||
var styleSheet = document.styleSheets[i];
|
||||
try {
|
||||
var rules = styleSheet.cssRules || styleSheet.rules;
|
||||
if (rules) {
|
||||
for (var j = 0; j < rules.length; j++) {
|
||||
var rule = rules[j];
|
||||
if (rule.style) {
|
||||
for (var k = 0; k < rule.style.length; k++) {
|
||||
var propertyName = rule.style[k];
|
||||
var value = rule.style.getPropertyValue(propertyName);
|
||||
if (value.indexOf("px") !== -1) {
|
||||
var newValue = value.replace(/(\d+(\.\d+)?)px/g, function(match, p1) {
|
||||
var pxValue = parseFloat(p1) * conversionFactor;
|
||||
return pxValue + "pt";
|
||||
});
|
||||
var priority = (rule.style.getPropertyPriority) ? rule.style.getPropertyPriority(propertyName) : "";
|
||||
rule.style.setProperty(propertyName, newValue, priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Could not access stylesheet", styleSheet.href, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setPageZoom(zoomLevel) {
|
||||
// document.body.style.zoom = zoomLevel;
|
||||
dpiValue = zoomLevel;
|
||||
refreshModeDisplay();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Windows: {
|
||||
UI: {
|
||||
DPI: new function() {
|
||||
Object.defineProperty(this, "mode", {
|
||||
get: function() {
|
||||
return highDpiMode;
|
||||
},
|
||||
set: function(modeType) {
|
||||
setHighDpiMode(modeType);
|
||||
return highDpiMode;
|
||||
}
|
||||
});
|
||||
this.refresh = refreshModeDisplay;
|
||||
Object.defineProperty(this, "zoom", {
|
||||
get: function() {
|
||||
return dpiValue;
|
||||
},
|
||||
set: function(zoomLevel) {
|
||||
setPageZoom(zoomLevel);
|
||||
return dpiValue;
|
||||
}
|
||||
});
|
||||
this.ptToPx = convertAllPtToPx;
|
||||
this.pxToPt = convertAllPxToPt;
|
||||
}()
|
||||
}
|
||||
}
|
||||
};
|
||||
})(this);
|
||||
@@ -0,0 +1,338 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
|
||||
if (!global.Windows) global.Windows = {};
|
||||
if (!global.Windows.UI) global.Windows.UI = {};
|
||||
if (!global.Windows.UI.Event) global.Windows.UI.Event = {};
|
||||
|
||||
var Monitor = (function() {
|
||||
var _sIdAttr = "data-monitor-id";
|
||||
var _idCounter = 1;
|
||||
var _aRegistry = {};
|
||||
var _typeRegistry = {}; // 按事件类型分类缓存
|
||||
var _polling = false;
|
||||
var _loopHandle = null;
|
||||
var _cleanupThreshold = 30000; // 30秒清理一次
|
||||
var _lastCleanup = Date.now();
|
||||
var _checkInterval = 200; // 节流时间
|
||||
var _eventTypes = [
|
||||
"resize",
|
||||
"position",
|
||||
"attribute",
|
||||
"child"
|
||||
];
|
||||
|
||||
// 缓存 DOM 元素引用
|
||||
var _elementCache = {};
|
||||
|
||||
function _ensureId(el) {
|
||||
if (!el.getAttribute(_sIdAttr)) {
|
||||
el.setAttribute(_sIdAttr, "monitor_" + (_idCounter++));
|
||||
}
|
||||
return el.getAttribute(_sIdAttr);
|
||||
}
|
||||
|
||||
function _getElementById(id) {
|
||||
if (_elementCache[id] && _elementCache[id].parentNode) {
|
||||
return _elementCache[id];
|
||||
}
|
||||
var el = document.querySelector("[" + _sIdAttr + "=\"" + id + "\"]");
|
||||
if (el) _elementCache[id] = el;
|
||||
return el;
|
||||
}
|
||||
|
||||
function _getAttrSnapshot(el) {
|
||||
var attrs = {};
|
||||
for (var i = 0; i < el.attributes.length; i++) {
|
||||
var attr = el.attributes[i];
|
||||
attrs[attr.name] = attr.value;
|
||||
}
|
||||
attrs["_rect"] = el.getBoundingClientRect();
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function _hasChanged(snapshotA, snapshotB) {
|
||||
for (var key in snapshotA) {
|
||||
if (snapshotA.hasOwnProperty(key)) {
|
||||
if (key === "_rect") {
|
||||
var a = snapshotA[key],
|
||||
b = snapshotB[key];
|
||||
if (!b || a.top !== b.top || a.left !== b.left || a.width !== b.width || a.height !== b.height) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (snapshotA[key] !== snapshotB[key]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function _pollOnce() {
|
||||
var now = Date.now();
|
||||
|
||||
// 按事件类型遍历,减少不必要检查
|
||||
for (var type in _typeRegistry) {
|
||||
if (!_typeRegistry.hasOwnProperty(type)) continue;
|
||||
|
||||
var list = _typeRegistry[type];
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var item = list[i];
|
||||
var el = _getElementById(item.id);
|
||||
if (!el) {
|
||||
list.splice(i--, 1);
|
||||
delete _elementCache[item.id];
|
||||
continue;
|
||||
}
|
||||
|
||||
var newSnapshot = _getAttrSnapshot(el);
|
||||
if (_hasChanged(item.snapshot, newSnapshot)) {
|
||||
item.snapshot = newSnapshot;
|
||||
try {
|
||||
item.callback.call(el, { type: type });
|
||||
} catch (ex) {
|
||||
console.error("Monitor callback error:", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 清理过期节点
|
||||
if (now - _lastCleanup > _cleanupThreshold) {
|
||||
_cleanup();
|
||||
_lastCleanup = now;
|
||||
}
|
||||
}
|
||||
|
||||
function _startLoop() {
|
||||
if (_polling) return;
|
||||
_polling = true;
|
||||
|
||||
function loop() {
|
||||
_pollOnce();
|
||||
_loopHandle = global.requestAnimationFrame ? requestAnimationFrame(loop) : setTimeout(loop, _checkInterval);
|
||||
}
|
||||
loop();
|
||||
}
|
||||
|
||||
function _stopLoop() {
|
||||
_polling = false;
|
||||
if (_loopHandle) {
|
||||
if (global.cancelAnimationFrame) cancelAnimationFrame(_loopHandle);
|
||||
else clearTimeout(_loopHandle);
|
||||
_loopHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
function _cleanup() {
|
||||
for (var type in _typeRegistry) {
|
||||
if (!_typeRegistry.hasOwnProperty(type)) continue;
|
||||
var list = _typeRegistry[type];
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
if (!_getElementById(list[i].id)) {
|
||||
list.splice(i--, 1);
|
||||
delete _elementCache[list[i].id];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function observe(el, type, callback) {
|
||||
if (_eventTypes.indexOf(type) < 0) throw new Error("Unsupported event type: " + type);
|
||||
var id = _ensureId(el);
|
||||
if (!_typeRegistry[type]) _typeRegistry[type] = [];
|
||||
_typeRegistry[type].push({
|
||||
id: id,
|
||||
callback: callback,
|
||||
snapshot: _getAttrSnapshot(el)
|
||||
});
|
||||
_startLoop();
|
||||
}
|
||||
|
||||
function detach(el, type, callback) {
|
||||
if (!_typeRegistry[type]) return;
|
||||
var id = el.getAttribute(_sIdAttr);
|
||||
if (!id) return;
|
||||
var list = _typeRegistry[type];
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
if (list[i].id === id && (!callback || list[i].callback === callback)) {
|
||||
list.splice(i--, 1);
|
||||
delete _elementCache[id];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
_typeRegistry = {};
|
||||
_elementCache = {};
|
||||
_stopLoop();
|
||||
}
|
||||
|
||||
return {
|
||||
observe: observe,
|
||||
detach: detach,
|
||||
clearAll: clearAll,
|
||||
EventType: {
|
||||
resize: "resize",
|
||||
position: "position",
|
||||
attribute: "attribute",
|
||||
child: "child"
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
global.Windows.UI.Event.Monitor = Monitor;
|
||||
|
||||
})(window);
|
||||
|
||||
/*
|
||||
|
||||
// 1) 监听元素尺寸变化
|
||||
var el = document.getElementById("box");
|
||||
Windows.UI.Event.Monitor.observe(el, "resize", function (e) {
|
||||
console.log("resized", e.oldValue, e.newValue, e.rect);
|
||||
});
|
||||
|
||||
// 2) 监听属性变化
|
||||
Windows.UI.Event.Monitor.observe(el, "attributeChange", function (e) {
|
||||
console.log("attrs changed", e.detail); // detail.added / removed / changed
|
||||
});
|
||||
|
||||
// 3) 监听附着/分离
|
||||
Windows.UI.Event.Monitor.observe(el, "attach", function (e) {
|
||||
console.log("attached to doc");
|
||||
});
|
||||
Windows.UI.Event.Monitor.observe(el, "detach", function (e) {
|
||||
console.log("detached from doc");
|
||||
});
|
||||
|
||||
// 4) 取消监听
|
||||
Windows.UI.Event.Monitor.unobserve(el, "resize", handler);
|
||||
|
||||
*/
|
||||
|
||||
(function(global) {
|
||||
"use strict";
|
||||
|
||||
var EventUtil = {};
|
||||
|
||||
/**
|
||||
* 添加事件,兼容 IE10/IE11
|
||||
* @param {Element|Window|Document} el 目标元素
|
||||
* @param {string} sType 事件类型,如 "click", "resize", "scroll"
|
||||
* @param {function} pfHandler 回调函数
|
||||
* @param {boolean} [bUseCapture] 是否捕获阶段,默认 false
|
||||
*/
|
||||
EventUtil.addEvent = function(el, sType, pfHandler, bUseCapture) {
|
||||
if (!el || typeof sType !== "string" || typeof pfHandler !== "function") return;
|
||||
|
||||
bUseCapture = !!bUseCapture;
|
||||
|
||||
if (el.addEventListener) {
|
||||
// 标准方式
|
||||
el.addEventListener(sType, pfHandler, bUseCapture);
|
||||
} else if (el.attachEvent) {
|
||||
// IE8-9 fallback
|
||||
el.attachEvent("on" + sType, pfHandler);
|
||||
} else {
|
||||
// 最原始方式
|
||||
var oldHandler = el["on" + sType];
|
||||
el["on" + sType] = function(e) {
|
||||
if (oldHandler) oldHandler(e || window.event);
|
||||
pfHandler(e || window.event);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 移除事件,兼容 IE10/IE11
|
||||
* @param {Element|Window|Document} el 目标元素
|
||||
* @param {string} sType 事件类型,如 "click", "resize", "scroll"
|
||||
* @param {function} pfHandler 回调函数
|
||||
* @param {boolean} [bUseCapture] 是否捕获阶段,默认 false
|
||||
*/
|
||||
EventUtil.removeEvent = function(el, sType, pfHandler, bUseCapture) {
|
||||
if (!el || typeof sType !== "string" || typeof pfHandler !== "function") return;
|
||||
|
||||
bUseCapture = !!bUseCapture;
|
||||
|
||||
if (el.removeEventListener) {
|
||||
el.removeEventListener(sType, pfHandler, bUseCapture);
|
||||
} else if (el.detachEvent) {
|
||||
el.detachEvent("on" + sType, pfHandler);
|
||||
} else {
|
||||
var oldHandler = el["on" + sType];
|
||||
if (oldHandler === pfHandler) {
|
||||
el["on" + sType] = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 暴露到全局命名空间
|
||||
if (typeof module !== "undefined" && module.exports) {
|
||||
module.exports = {
|
||||
Windows: {
|
||||
UI: {
|
||||
Event: {
|
||||
Util: EventUtil
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
} else {
|
||||
global.Windows = global.Windows || {};
|
||||
global.Windows.UI = global.Windows.UI || {};
|
||||
global.Windows.UI.Event = global.Windows.UI.Event || {};
|
||||
global.Windows.UI.Event.Util = EventUtil;
|
||||
}
|
||||
|
||||
})(this);
|
||||
/*
|
||||
使用示例:
|
||||
var handler = function (e) {
|
||||
console.log("事件触发", e.type);
|
||||
};
|
||||
|
||||
// 添加事件
|
||||
Windows.UI.Event.Util.addEvent(window, "resize", handler);
|
||||
|
||||
// 删除事件
|
||||
Windows.UI.Event.Util.removeEvent(window, "resize", handler);
|
||||
|
||||
*/
|
||||
(function(global) {
|
||||
"use strict";
|
||||
/**
|
||||
*
|
||||
* @param {function} fn
|
||||
* @param {number} delay
|
||||
* @param {boolean} immediate 是否在第一次立即执行(可选,默认 false)
|
||||
* @returns {function} 返回一个新的函数,该函数在 delay 时间后执行 fn 函数,如果在 delay 时间内再次调用该函数,则会重新计时。
|
||||
*/
|
||||
function debounce(fn, delay, immediate) {
|
||||
var timer = null;
|
||||
var lastCall = 0;
|
||||
return function() {
|
||||
var context = this;
|
||||
var args = arguments;
|
||||
var now = +new Date();
|
||||
var callNow = immediate && !timer;
|
||||
if (now - lastCall >= delay) {
|
||||
lastCall = now;
|
||||
if (callNow) {
|
||||
fn.apply(context, args);
|
||||
}
|
||||
}
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(function() {
|
||||
lastCall = +new Date();
|
||||
if (!immediate) {
|
||||
fn.apply(context, args);
|
||||
}
|
||||
}, delay);
|
||||
};
|
||||
}
|
||||
module.exports = { debounce: debounce };
|
||||
})(this);
|
||||
@@ -0,0 +1,187 @@
|
||||
/**
|
||||
* @module module
|
||||
* @description 模块化相关的函数。
|
||||
|
||||
* 将模块化相关的函数封装在一个对象中,方便使用。
|
||||
* 1. directexports(cObject, cDirection):直接将对象或函数公开到指定方向。
|
||||
* 2. exports:公开的对象或函数。
|
||||
* 3. imports:已加载的脚本。
|
||||
* 4. unload:卸载已加载的脚本。
|
||||
* 5. namespace(swNameSpace, cDirection):创建命名空间。
|
||||
* 6. export(cObject, cDirection):兼容旧版本的函数。
|
||||
* 7. import(aswSrc):兼容旧版本的函数。
|
||||
* 兼容的旧版本函数需要引用预处理模块,否则不会兼容。
|
||||
*/
|
||||
(function(global) {
|
||||
"use strict";
|
||||
// 是否为通过构造函数创建的对象实例
|
||||
function isNewFunctionInstance(obj) {
|
||||
return typeof obj === "object" && Object.getPrototypeOf(obj) !== Object.prototype;
|
||||
}
|
||||
var module = new function() {
|
||||
if (this._instance) return this._instance;
|
||||
else this._instance = this;
|
||||
/**
|
||||
* 便于从代码块中公开。
|
||||
* @param {Object} cObject 需要公开的内容,以对象的形式分配好命名空间和别名。如:
|
||||
* {
|
||||
* myFunc: function() { },
|
||||
* myObj: function () {
|
||||
* // object constructor
|
||||
* }
|
||||
* }
|
||||
* 然后默认公开到全局。则公开后可以直接通过 myFunc() 或 myObj() 调用。
|
||||
* @param {global | Object} cDirection 公开方向
|
||||
* @returns {void}
|
||||
*/
|
||||
this.directexports = function(cObject, cDirection) {
|
||||
if (!cDirection) cDirection = global;
|
||||
if (typeof cObject === "function") {
|
||||
cDirection[cObject.name] = cObject;
|
||||
return;
|
||||
} else if (typeof cObject === "object") {
|
||||
var keys = Object.keys(cObject);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
if (typeof cDirection[keys[i]] === "undefined") {
|
||||
cDirection[keys[i]] = {};
|
||||
}
|
||||
if (isNewFunctionInstance(cObject[keys[i]])) {
|
||||
cDirection[keys[i]] = cObject[keys[i]];
|
||||
} else if (typeof cObject[keys[i]] === "object") {
|
||||
this.directexports(cObject[keys[i]], cDirection[keys[i]]);
|
||||
} else {
|
||||
cDirection[keys[i]] = cObject[keys[i]];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cDirection = cObject;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* @property {global | Object} exports 欲公开的内容以对象的形式分配好命名空间和别名。如:
|
||||
*/
|
||||
Object.defineProperty(this, "exports", {
|
||||
get: function() {
|
||||
return global;
|
||||
},
|
||||
set: function(cObject, cDirection) {
|
||||
this.directexports(cObject, cDirection);
|
||||
}
|
||||
});
|
||||
if (typeof Array.isArray === "undefined") {
|
||||
/**
|
||||
* 判断是否为数组。这是函数补充。
|
||||
* @param {*} arg 需要判断的对象
|
||||
* @returns {boolean}
|
||||
*/
|
||||
Array.isArray = function(arg) {
|
||||
return Object.prototype.toString.call(arg) === "[object Array]" || arg instanceof Array || arg instanceof HTMLCollection || arg instanceof NodeList;
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @property {HTMLCollection | NodeList | null} imports 当前页面已加载的脚本。会以 NodeList 形式返回。如果为设置,则是追加脚本,而不是覆盖。传入脚本路径或其数组。
|
||||
*/
|
||||
Object.defineProperty(this, "imports", {
|
||||
get: function() {
|
||||
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
||||
var scripts = document.querySelectorAll("script[src]");
|
||||
return scripts;
|
||||
} else return null;
|
||||
},
|
||||
set: function(aswSrc) {
|
||||
if (typeof aswSrc === "string") {
|
||||
var scripts = this.imports;
|
||||
if (scripts && scripts.length > 0) {
|
||||
for (var i = 0; i < scripts.length; i++) {
|
||||
if (scripts[i].src === aswSrc) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
var script = document.createElement("script");
|
||||
script.src = aswSrc;
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
} else if (Array.isArray(aswSrc) || aswSrc instanceof Array || aswSrc instanceof HTMLCollection || aswSrc instanceof NodeList) {
|
||||
for (var i = 0; i < aswSrc.length; i++) {
|
||||
this.imports = aswSrc[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
/**
|
||||
* @property {HTMLCollection | NodeList | null} unload 卸载当前页面已加载的脚本。传入脚本路径或其数组。无法只读
|
||||
*/
|
||||
Object.defineProperty(this, "unload", {
|
||||
set: function(aswSrc) {
|
||||
if (typeof aswSrc === "string") {
|
||||
var scripts = this.imports;
|
||||
if (scripts && scripts.length > 0) {
|
||||
for (var i = 0; i < scripts.length; i++) {
|
||||
if (scripts[i].src === aswSrc) {
|
||||
scripts[i].remove();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (Array.isArray(aswSrc) || aswSrc instanceof Array || aswSrc instanceof HTMLCollection || aswSrc instanceof NodeList) {
|
||||
for (var i = 0; i < aswSrc.length; i++) {
|
||||
this.unload = aswSrc[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Object.defineProperty(this, "global", {
|
||||
get: function() {
|
||||
return global;
|
||||
}
|
||||
});
|
||||
/**
|
||||
* 命名空间,其实为一种字典。
|
||||
* @param {string} swNameSpace 命名空间路径。如:"WinJS.Namespace"。不能为空。命名空间之间用"."分隔。
|
||||
* @param {*} cDirection 创建的路径,默认创建到全局
|
||||
* @returns
|
||||
*/
|
||||
this.namespace = function(swNameSpace, cDirection) {
|
||||
if (!cDirection) cDirection = global;
|
||||
var strarr = (swNameSpace || "").split(".");
|
||||
if (!strarr.length) strarr.push(swns);
|
||||
|
||||
function setNs(strarr, direct) {
|
||||
var newdirect = null;
|
||||
if (!strarr.length) return direct;
|
||||
else {
|
||||
if (typeof direct[strarr[0]] === "undefined") {
|
||||
direct[strarr[0]] = {};
|
||||
}
|
||||
newdirect = direct[strarr[0]];
|
||||
}
|
||||
strarr.shift();
|
||||
return setNs((strarr || []), newdirect);
|
||||
}
|
||||
return setNs(strarr, cDirection);
|
||||
};
|
||||
if (typeof preprocess !== "undefined" && preprocess.ifdef("COMPATIBLE")) {
|
||||
/**
|
||||
* 兼容旧版本的函数。
|
||||
* @param {Object | Function} cObject 欲公开的对象或函数
|
||||
* @param {global | Object} cDirection 公开方向
|
||||
*/
|
||||
this.export = function(cObject, cDirection) {
|
||||
this.directexports(cObject, cDirection);
|
||||
};
|
||||
/**
|
||||
*
|
||||
* @param {Array <string> | string} aswSrc 脚本路径或其数组
|
||||
* @returns {boolean} 是否成功添加脚本。在兼容模式下只会返回真。
|
||||
*/
|
||||
this.import = function(aswSrc) {
|
||||
this.imports = aswSrc;
|
||||
return true;
|
||||
};
|
||||
}
|
||||
};
|
||||
module.exports = {
|
||||
module: module,
|
||||
namespace: module.namespace,
|
||||
};
|
||||
})(this);
|
||||
@@ -0,0 +1,74 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
Object.defineProperty(global, "pagecontainer", {
|
||||
get: function() { return document.querySelector(".pagecontainer"); }
|
||||
});
|
||||
|
||||
function getPage() {
|
||||
return pagecontainer.querySelector(".page");
|
||||
}
|
||||
var supportPageList = [
|
||||
"splash",
|
||||
"select",
|
||||
"preinstall",
|
||||
"installing",
|
||||
"installsuccess",
|
||||
"installfailed"
|
||||
];
|
||||
var supportMulPageList = [
|
||||
"preinstall",
|
||||
"installing",
|
||||
"installsuccess",
|
||||
"installfailed"
|
||||
];
|
||||
|
||||
function setPage(swPageLabel, bIsMulti) {
|
||||
var page = getPage();
|
||||
swPageLabel = ("" + (swPageLabel || ""));
|
||||
for (var i = 0; i < supportPageList.length; i++) {
|
||||
if (Bridge.NString.equals(swPageLabel, supportPageList[i])) {
|
||||
page.classList.add(supportPageList[i]);
|
||||
} else {
|
||||
if (page.classList.contains(supportPageList[i])) page.classList.remove(supportPageList[i]);
|
||||
}
|
||||
}
|
||||
if (page.classList.contains("multiple")) page.classList.remove("multiple");
|
||||
for (var j = 0; j < supportMulPageList.length; j++) {
|
||||
if (Bridge.NString.equals(swPageLabel, supportMulPageList[j]) && bIsMulti) {
|
||||
page.classList.add("multiple");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getPageLabel() {
|
||||
var page = getPage();
|
||||
for (var i = 0; i < supportPageList.length; i++) {
|
||||
if (page.classList.contains(supportPageList[i])) {
|
||||
return supportPageList[i];
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function isMultiPage() {
|
||||
var page = getPage();
|
||||
return page.classList.contains("multiple");
|
||||
}
|
||||
|
||||
function setPageMultiple(bIsMulti) {
|
||||
setPage(getPageLabel(), bIsMulti);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Page: {}
|
||||
};
|
||||
Object.defineProperty(Page, "current", {
|
||||
get: function() { return getPageLabel(); },
|
||||
set: function(swPageLabel) { setPage(swPageLabel, isMultiPage()); }
|
||||
});
|
||||
Object.defineProperty(Page, "multiple", {
|
||||
get: function() { return isMultiPage(); },
|
||||
set: function(bIsMulti) { setPage(getPageLabel(), bIsMulti); }
|
||||
});
|
||||
})(this);
|
||||
@@ -0,0 +1,130 @@
|
||||
(function(global) {
|
||||
if (typeof msWriteProfilerMark === "undefined") {
|
||||
function msWriteProfilerMark(swMark) {
|
||||
if (typeof performance !== "undefined" && typeof performance.mark === "function") {
|
||||
return performance.mark(swMark);
|
||||
} else if (typeof console !== "undefined" && typeof console.log === "function") {
|
||||
return console.log(swMark);
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
msWriteProfilerMark: msWriteProfilerMark
|
||||
};
|
||||
}
|
||||
})(this);
|
||||
(function(global) {
|
||||
|
||||
if (typeof global.Debug === "undefined") {
|
||||
var fakeDebug = {};
|
||||
|
||||
// 基本属性
|
||||
fakeDebug.debuggerEnabled = true;
|
||||
fakeDebug.setNonUserCodeExceptions = false;
|
||||
|
||||
// 常量
|
||||
fakeDebug.MS_ASYNC_CALLBACK_STATUS_ASSIGN_DELEGATE = 0;
|
||||
fakeDebug.MS_ASYNC_CALLBACK_STATUS_JOIN = 1;
|
||||
fakeDebug.MS_ASYNC_CALLBACK_STATUS_CHOOSEANY = 2;
|
||||
fakeDebug.MS_ASYNC_CALLBACK_STATUS_CANCEL = 3;
|
||||
fakeDebug.MS_ASYNC_CALLBACK_STATUS_ERROR = 4;
|
||||
|
||||
fakeDebug.MS_ASYNC_OP_STATUS_SUCCESS = 1;
|
||||
fakeDebug.MS_ASYNC_OP_STATUS_CANCELED = 2;
|
||||
fakeDebug.MS_ASYNC_OP_STATUS_ERROR = 3;
|
||||
|
||||
// 方法:输出
|
||||
fakeDebug.write = function(msg) {
|
||||
if (console && console.log) console.log("[Debug.write] " + msg);
|
||||
};
|
||||
fakeDebug.writeln = function(msg) {
|
||||
if (console && console.log) console.log("[Debug.writeln] " + msg);
|
||||
};
|
||||
|
||||
// 方法:断言 / 中断
|
||||
fakeDebug.assert = function(cond, msg) {
|
||||
if (!cond) {
|
||||
if (console && console.error) {
|
||||
console.error("[Debug.assert] Assertion failed: " + (msg || ""));
|
||||
}
|
||||
// 可选触发断点
|
||||
// debugger;
|
||||
}
|
||||
};
|
||||
fakeDebug.break = function() {
|
||||
debugger;
|
||||
};
|
||||
|
||||
// 方法:异步跟踪(空实现)
|
||||
fakeDebug.msTraceAsyncCallbackCompleted = function() {};
|
||||
fakeDebug.msTraceAsyncCallbackStarting = function() {};
|
||||
fakeDebug.msTraceAsyncOperationCompleted = function() {};
|
||||
fakeDebug.msTraceAsyncOperationStarting = function() {};
|
||||
fakeDebug.msUpdateAsyncCallbackRelation = function() {};
|
||||
|
||||
global.Debug = fakeDebug;
|
||||
}
|
||||
|
||||
})(this);
|
||||
(function(global) {
|
||||
|
||||
if (typeof global.setImmediate === "undefined") {
|
||||
var nextHandle = 1; // 唯一任务 id
|
||||
var tasksByHandle = {};
|
||||
var currentlyRunning = false;
|
||||
|
||||
function addTask(fn, args) {
|
||||
tasksByHandle[nextHandle] = function() {
|
||||
fn.apply(undefined, args);
|
||||
};
|
||||
return nextHandle++;
|
||||
}
|
||||
|
||||
function run(handle) {
|
||||
if (currentlyRunning) {
|
||||
// 如果已经在运行,延迟一下
|
||||
setTimeout(run, 0, handle);
|
||||
} else {
|
||||
var task = tasksByHandle[handle];
|
||||
if (task) {
|
||||
currentlyRunning = true;
|
||||
try {
|
||||
task();
|
||||
} finally {
|
||||
delete tasksByHandle[handle];
|
||||
currentlyRunning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function installSetImmediate() {
|
||||
if (typeof MessageChannel !== "undefined") {
|
||||
var channel = new MessageChannel();
|
||||
channel.port1.onmessage = function(event) {
|
||||
run(event.data);
|
||||
};
|
||||
return function() {
|
||||
var handle = addTask(arguments[0], Array.prototype.slice.call(arguments, 1));
|
||||
channel.port2.postMessage(handle);
|
||||
return handle;
|
||||
};
|
||||
} else {
|
||||
// fallback: setTimeout
|
||||
return function() {
|
||||
var handle = addTask(arguments[0], Array.prototype.slice.call(arguments, 1));
|
||||
setTimeout(run, 0, handle);
|
||||
return handle;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
global.setImmediate = installSetImmediate();
|
||||
|
||||
// 对应 clearImmediate
|
||||
if (typeof global.clearImmediate === "undefined") {
|
||||
global.clearImmediate = function(handle) {
|
||||
delete tasksByHandle[handle];
|
||||
};
|
||||
}
|
||||
}
|
||||
})(this);
|
||||
@@ -0,0 +1,60 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
var flags = {
|
||||
HIDE_WHEN_CLICK_OUTSIDE: 0x00000001,
|
||||
SHOW_CONTROL_BUTTONS: 0x00000002,
|
||||
};
|
||||
|
||||
function Popup(content, options) {
|
||||
var _content = content;
|
||||
var _flags = options["flags"] || 0;
|
||||
var _element = null;
|
||||
_element = document.createElement("div");
|
||||
_element.className = "popup";
|
||||
_element.style.position = "absolute";
|
||||
_element.style.display = "none";
|
||||
document.body.appendChild(_element);
|
||||
if (_content instanceof HTMLElement) {
|
||||
_element.appendChild(_content);
|
||||
} else if (typeof _content === "string") {
|
||||
_element.textContent = _content;
|
||||
}
|
||||
Object.defineProperty(this, "content", {
|
||||
get: function() { return _content; },
|
||||
set: function(value) {
|
||||
_element.innerHTML = "";
|
||||
if (value instanceof HTMLElement) {
|
||||
_element.appendChild(value);
|
||||
} else if (typeof value === "string") {
|
||||
_element.textContent = value;
|
||||
}
|
||||
_content = value;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(this, "", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
Popup.Options = function() {
|
||||
this.flags = 0;
|
||||
this.position = {
|
||||
left: null,
|
||||
top: null
|
||||
};
|
||||
this.size = {
|
||||
width: null,
|
||||
height: null
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
Windows: {
|
||||
UI: {
|
||||
Popups: {
|
||||
DisplayBlock: Popup
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
})(this);
|
||||
@@ -0,0 +1,241 @@
|
||||
(function(global) {
|
||||
"use strict";
|
||||
|
||||
function PromisePolyfill(pfExecutor) {
|
||||
var swState = "pending"; // "fulfilled" | "rejected"
|
||||
var vValue = undefined;
|
||||
var aHandlers = [];
|
||||
var pfOnCancel = null;
|
||||
|
||||
function invokeHandlers() {
|
||||
if (swState === "pending") return;
|
||||
for (var i = 0; i < aHandlers.length; i++) {
|
||||
handle(aHandlers[i]);
|
||||
}
|
||||
aHandlers = [];
|
||||
}
|
||||
|
||||
function handle(hHandler) {
|
||||
if (swState === "pending") {
|
||||
aHandlers.push(hHandler);
|
||||
return;
|
||||
}
|
||||
var pfCallback = swState === "fulfilled" ? hHandler.onFulfilled : hHandler.onRejected;
|
||||
if (!pfCallback) {
|
||||
if (swState === "fulfilled") {
|
||||
hHandler.resolve(vValue);
|
||||
} else {
|
||||
hHandler.reject(vValue);
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var vResult = pfCallback(vValue);
|
||||
hHandler.resolve(vResult);
|
||||
} catch (ex) {
|
||||
hHandler.reject(ex);
|
||||
}
|
||||
}
|
||||
|
||||
function resolve(vResult) {
|
||||
try {
|
||||
if (vResult === self) throw new TypeError("A promise cannot be resolved with itself.");
|
||||
if (vResult && (typeof vResult === "object" || typeof vResult === "function")) {
|
||||
var pfThen = vResult.then;
|
||||
if (typeof pfThen === "function") {
|
||||
pfThen.call(vResult, resolve, reject);
|
||||
return;
|
||||
}
|
||||
}
|
||||
swState = "fulfilled";
|
||||
vValue = vResult;
|
||||
invokeHandlers();
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
}
|
||||
|
||||
function reject(vReason) {
|
||||
swState = "rejected";
|
||||
vValue = vReason;
|
||||
if (typeof PromisePolyfill.onerror === "function") {
|
||||
PromisePolyfill.onerror(vReason);
|
||||
}
|
||||
invokeHandlers();
|
||||
}
|
||||
var self = this;
|
||||
try {
|
||||
pfExecutor(resolve, reject, function(pfCancel) {
|
||||
pfOnCancel = pfCancel;
|
||||
});
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
this.then = function(pfOnFulfilled, pfOnRejected) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
handle({
|
||||
onFulfilled: pfOnFulfilled,
|
||||
onRejected: pfOnRejected,
|
||||
resolve: resolve,
|
||||
reject: reject
|
||||
});
|
||||
});
|
||||
};
|
||||
this["catch"] = function(pfOnRejected) {
|
||||
return this.then(null, pfOnRejected);
|
||||
};
|
||||
this.done = function(pfOnFulfilled, pfOnRejected) {
|
||||
this.then(pfOnFulfilled, pfOnRejected)["catch"](function(ex) {
|
||||
setTimeout(function() { throw ex; }, 0);
|
||||
});
|
||||
};
|
||||
this.cancel = function() {
|
||||
if (pfOnCancel) {
|
||||
try { pfOnCancel(); } catch (ex) {}
|
||||
}
|
||||
reject(new Error("Promise was canceled"));
|
||||
};
|
||||
this._oncancel = pfOnCancel;
|
||||
this._state = swState;
|
||||
this._value = vValue;
|
||||
}
|
||||
PromisePolyfill.is = function(vObj) {
|
||||
return vObj instanceof PromisePolyfill;
|
||||
};
|
||||
PromisePolyfill.resolve = function(vValue) {
|
||||
return new PromisePolyfill(function(resolve) { resolve(vValue); });
|
||||
};
|
||||
PromisePolyfill.reject = function(vReason) {
|
||||
return new PromisePolyfill(function(resolve, reject) { reject(vReason); });
|
||||
};
|
||||
PromisePolyfill.all = function(aPromises) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
var nRemaining = aPromises.length;
|
||||
var aResults = new Array(nRemaining);
|
||||
if (nRemaining === 0) resolve([]);
|
||||
|
||||
function resolver(iIndex) {
|
||||
return function(vValue) {
|
||||
aResults[iIndex] = vValue;
|
||||
nRemaining--;
|
||||
if (nRemaining === 0) resolve(aResults);
|
||||
};
|
||||
}
|
||||
for (var i = 0; i < aPromises.length; i++) {
|
||||
PromisePolyfill.resolve(aPromises[i]).then(resolver(i), reject);
|
||||
}
|
||||
});
|
||||
};
|
||||
PromisePolyfill.race = function(aPromises) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
for (var i = 0; i < aPromises.length; i++) {
|
||||
PromisePolyfill.resolve(aPromises[i]).then(resolve, reject);
|
||||
}
|
||||
});
|
||||
};
|
||||
PromisePolyfill.join = function(aPromises) {
|
||||
return PromisePolyfill.all(aPromises);
|
||||
};
|
||||
PromisePolyfill.any = function(aPromises) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
var nRemaining = aPromises.length;
|
||||
var aErrors = new Array(nRemaining);
|
||||
if (nRemaining === 0) reject(new Error("No promises provided."));
|
||||
|
||||
function resolver(vValue) { resolve(vValue); }
|
||||
|
||||
function rejecter(iIndex) {
|
||||
return function(ex) {
|
||||
aErrors[iIndex] = ex;
|
||||
nRemaining--;
|
||||
if (nRemaining === 0) reject(aErrors);
|
||||
};
|
||||
}
|
||||
for (var i = 0; i < aPromises.length; i++) {
|
||||
PromisePolyfill.resolve(aPromises[i]).then(resolver, rejecter(i));
|
||||
}
|
||||
});
|
||||
};
|
||||
PromisePolyfill.timeout = function(pPromise, nMilliseconds) {
|
||||
return new PromisePolyfill(function(resolve, reject) {
|
||||
var hTimer = setTimeout(function() {
|
||||
reject(new Error("Promise timed out after " + nMilliseconds + "ms"));
|
||||
}, nMilliseconds);
|
||||
PromisePolyfill.resolve(pPromise).then(function(vValue) {
|
||||
clearTimeout(hTimer);
|
||||
resolve(vValue);
|
||||
}, function(ex) {
|
||||
clearTimeout(hTimer);
|
||||
reject(ex);
|
||||
});
|
||||
});
|
||||
};
|
||||
PromisePolyfill.as = function(vValue) {
|
||||
return PromisePolyfill.resolve(vValue);
|
||||
};
|
||||
PromisePolyfill.wrap = function(vValue) {
|
||||
return PromisePolyfill.resolve(vValue);
|
||||
};
|
||||
PromisePolyfill.wrapError = function(vError) {
|
||||
return PromisePolyfill.reject(vError);
|
||||
};
|
||||
PromisePolyfill.thenEach = function(aValues, pfCallback) {
|
||||
var aPromises = [];
|
||||
for (var i = 0; i < aValues.length; i++) {
|
||||
aPromises.push(PromisePolyfill.resolve(aValues[i]).then(pfCallback));
|
||||
}
|
||||
return PromisePolyfill.all(aPromises);
|
||||
};
|
||||
var hListeners = {};
|
||||
PromisePolyfill.addEventListener = function(sType, pfHandler) {
|
||||
if (!hListeners[sType]) hListeners[sType] = [];
|
||||
hListeners[sType].push(pfHandler);
|
||||
};
|
||||
PromisePolyfill.removeEventListener = function(sType, pfHandler) {
|
||||
if (!hListeners[sType]) return;
|
||||
var aList = hListeners[sType];
|
||||
for (var i = 0; i < aList.length; i++) {
|
||||
if (aList[i] === pfHandler) {
|
||||
aList.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
PromisePolyfill.dispatchEvent = function(sType, vDetail) {
|
||||
if (!hListeners[sType]) return;
|
||||
var aList = hListeners[sType].slice();
|
||||
for (var i = 0; i < aList.length; i++) {
|
||||
try { aList[i](vDetail); } catch (ex) {}
|
||||
}
|
||||
};
|
||||
PromisePolyfill.supportedForProcessing = true;
|
||||
PromisePolyfill.onerror = null;
|
||||
if (typeof global.Promise !== "undefined") {
|
||||
var p = global.Promise;
|
||||
if (!p.join) p.join = p.all;
|
||||
if (!p.any) p.any = PromisePolyfill.any;
|
||||
if (!p.timeout) p.timeout = PromisePolyfill.timeout;
|
||||
if (!p.as) p.as = p.resolve;
|
||||
if (!p.wrap) p.wrap = p.resolve;
|
||||
if (!p.wrapError) p.wrapError = p.reject;
|
||||
if (!p.thenEach) p.thenEach = PromisePolyfill.thenEach;
|
||||
if (!p.is) p.is = function(vObj) { return vObj instanceof p; };
|
||||
if (!p.supportedForProcessing) p.supportedForProcessing = true;
|
||||
if (!p.addEventListener) p.addEventListener = PromisePolyfill.addEventListener;
|
||||
if (!p.removeEventListener) p.removeEventListener = PromisePolyfill.removeEventListener;
|
||||
if (!p.dispatchEvent) p.dispatchEvent = PromisePolyfill.dispatchEvent;
|
||||
if (!p.onerror) p.onerror = null;
|
||||
}
|
||||
if (typeof global.WinJS !== "undefined" && typeof global.WinJS.Promise !== "undefined") {
|
||||
var wp = global.WinJS.Promise;
|
||||
if (!wp.resolve) wp.resolve = function(vValue) { return new wp(function(c) { c(vValue); }); };
|
||||
if (!wp.reject) wp.reject = function(vReason) { return new wp(function(c, e) { e(vReason); }); };
|
||||
if (!wp.all) wp.all = function(aPromises) { return wp.join(aPromises); };
|
||||
if (!wp.race) wp.race = PromisePolyfill.race;
|
||||
global.Promise = wp;
|
||||
if (typeof global.Promise === "undefined") global.Promise = wp;
|
||||
}
|
||||
if (typeof global.Promise === "undefined" && typeof global.WinJS === "undefined") {
|
||||
global.Promise = PromisePolyfill;
|
||||
}
|
||||
})(this);
|
||||
Reference in New Issue
Block a user