继承zepto的core,event和touch
This commit is contained in:
parent
f077bcc83a
commit
02b56cfcf3
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -149,56 +149,56 @@
|
|||
<div id="alertContainer"></div>
|
||||
<script src="../dist/nuclear.js"></script>
|
||||
<script type="text/javascript">
|
||||
var Button = Nuclear.create({
|
||||
var Button = $.create({
|
||||
install: function () {
|
||||
this.option.disable = false;
|
||||
},
|
||||
installed: function () {
|
||||
this.node.addEventListener("click", function (evt) {
|
||||
$(this.node).on("click", function (evt) {
|
||||
if (this.option.onClick) {
|
||||
this.option.onClick.call(this.node, evt);
|
||||
}
|
||||
}.bind(this), false);
|
||||
}.bind(this));
|
||||
},
|
||||
render: function () {
|
||||
return ' <a class="btn {{#disable}}disable{{/disable}}">{{text}}</a>';
|
||||
}
|
||||
})
|
||||
|
||||
var Alert = Nuclear.create({
|
||||
var Alert = $.create({
|
||||
installed: function () {
|
||||
this.ok.addEventListener("click", function (evt) {
|
||||
$(this.ok).on("click", function (evt) {
|
||||
this.option.display = false;
|
||||
evt.stopPropagation();
|
||||
}.bind(this), false);
|
||||
}.bind(this));
|
||||
},
|
||||
render: function () {
|
||||
if (!this.option.display) return;
|
||||
return '<div>\
|
||||
<div class="ui-mask" style="height:' + (Math.max(document.body.scrollHeight, document.body.clientHeight) - 1) + 'px;"></div>\
|
||||
<div class="ui-dialog">\
|
||||
<div class="ui-dialog-title">\
|
||||
<h3>{{title}}</h3>\
|
||||
<div class="ui-mask" style="height:' + (Math.max(document.body.scrollHeight, document.body.clientHeight) - 1) + 'px;"></div>\
|
||||
<div class="ui-dialog">\
|
||||
<div class="ui-dialog-title">\
|
||||
<h3>{{title}}</h3>\
|
||||
</div>\
|
||||
<div class="ui-dialog-content">\
|
||||
<div title="{{msg}}">\
|
||||
<p>{{msg}}</p></div>\
|
||||
</div>\
|
||||
<div class="ui-dialog-content">\
|
||||
<div title="{{msg}}">\
|
||||
<p>{{msg}}</p></div>\
|
||||
</div>\
|
||||
<div nc-id="ok" class="ui-dialog-btns"><a class="ui-btn ui-btn-1">好</a>\
|
||||
</div>\
|
||||
</div>\
|
||||
</div>';
|
||||
<div nc-id="ok" class="ui-dialog-btns"><a class="ui-btn ui-btn-1">好</a>\
|
||||
</div>\
|
||||
</div>\
|
||||
</div>';
|
||||
}
|
||||
});
|
||||
|
||||
Nuclear.alert = function (msg, title) {
|
||||
$.alert = function (msg, title) {
|
||||
new Alert({ msg: msg, title: title || "提示", display:true}, "body");
|
||||
}
|
||||
|
||||
new Button({
|
||||
text: "点我试试alert",
|
||||
onClick: function () {
|
||||
Nuclear.alert("Nuclear大法好");
|
||||
$.alert("Nuclear大法好");
|
||||
}
|
||||
}, "#alertContainer")
|
||||
</script>
|
||||
|
|
|
@ -36,16 +36,16 @@
|
|||
<script src="../dist/nuclear.js"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
var Button = Nuclear.create({
|
||||
var Button = $.create({
|
||||
install: function () {
|
||||
this.option.disable = false;
|
||||
},
|
||||
installed: function () {
|
||||
this.node.addEventListener("click", function (evt) {
|
||||
$(this.node).on("click", function (evt) {
|
||||
if (this.option.onClick) {
|
||||
this.option.onClick.call(this.node, evt);
|
||||
}
|
||||
}.bind(this), false);
|
||||
}.bind(this));
|
||||
},
|
||||
render: function () {
|
||||
return ' <a class="btn {{#disable}}disable{{/disable}}">{{text}}</a>';
|
||||
|
@ -54,14 +54,14 @@
|
|||
|
||||
var SwitchButton = Button.create({
|
||||
installed: function () {
|
||||
this.node.addEventListener("click", function (evt) {
|
||||
$(this.node).on("click", function (evt) {
|
||||
this.option.disable = !this.option.disable;
|
||||
if (this.option.disable) {
|
||||
this.option.close && this.option.close();
|
||||
} else {
|
||||
this.option.open && this.option.open();
|
||||
}
|
||||
}.bind(this), false);
|
||||
}.bind(this));
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -71,7 +71,7 @@
|
|||
// this._super();
|
||||
},
|
||||
installed: function () {
|
||||
this.node.addEventListener("click", function (evt) {
|
||||
$(this.node).on("click", function (evt) {
|
||||
if (!this.option.disable) {
|
||||
this.option.textBak = this.option.text;
|
||||
this.option.valueBak = this.option.value;
|
||||
|
@ -105,19 +105,15 @@
|
|||
|
||||
new SwitchButton({
|
||||
text: "开关按钮",
|
||||
open: function () {
|
||||
},
|
||||
close: function () {
|
||||
}
|
||||
open: function () {},
|
||||
close: function () {}
|
||||
},"#switchButtonContainer");
|
||||
|
||||
new CountdownButton({
|
||||
text: "获取短信验证码",
|
||||
waitingText: "{}秒后可以重试",
|
||||
value: 10,
|
||||
complete: function () {
|
||||
console.log(11);
|
||||
}
|
||||
text: "获取短信验证码",
|
||||
waitingText: "{}秒后可以重试",
|
||||
value: 10,
|
||||
complete: function () {}
|
||||
},"#countdownButtonContainer");
|
||||
|
||||
</script>
|
||||
|
|
|
@ -47,22 +47,21 @@
|
|||
<div id="carouselContainer"></div>
|
||||
<script src="../dist/nuclear.js"></script>
|
||||
<script>
|
||||
var Carousel = Nuclear.create({
|
||||
var Carousel = $.create({
|
||||
installed: function () {
|
||||
this.links = this.nav.querySelectorAll('a');
|
||||
this.$links = $(this.nav).find('a');
|
||||
var self = this;
|
||||
Nuclear.addEvent(this.links, "click", function () {
|
||||
this.$links.on("click", function () {
|
||||
self.option.index = this.getAttribute("data-index");
|
||||
});
|
||||
this.active();
|
||||
},
|
||||
active: function () {
|
||||
Nuclear.removeClass(this.links, "active");
|
||||
Nuclear.addClass(this.links[this.option.index], "active");
|
||||
this.$links.removeClass("active").eq(this.option.index).addClass("active");
|
||||
},
|
||||
onOptionChange: function (prop, value, oldValue, path) {
|
||||
if (prop === "index") {
|
||||
this.carouselScroll.style.left = value * -100 + "%";
|
||||
$(this.carouselScroll).css("left", value * -100 + "%");
|
||||
this.active();
|
||||
}
|
||||
},
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Drag</title>
|
||||
<link href="../asset/demo.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div id="box">
|
||||
<div class="dragger1" id="dragger1"></div>
|
||||
</div>
|
||||
|
||||
<div id="box1">
|
||||
<div class="dragger2" id="dragger2"></div>
|
||||
</div>
|
||||
<div id="box2">
|
||||
<div class="dragger3" id="dragger3"></div>
|
||||
</div>
|
||||
<script src="../dist/nuclear.js"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
var Drag = $.createAction({
|
||||
install: function () {
|
||||
var option = this.option;
|
||||
this.currentPosition = { x: 0, y: 0 };
|
||||
this.$moveElement = $(option.moveElement);
|
||||
this.$dragElement = $(option.dragElement)
|
||||
this.$moveElement.css("position", "absolute");
|
||||
this.$dragElement.css("cursor", option.cursor || "move");
|
||||
|
||||
this.$dragElement.on("mousedown", this._mousedown.bind(this));
|
||||
|
||||
if (this.$moveElement.css("left") === "auto") this.$moveElement.css("left", 0);
|
||||
if (this.$dragElement.css("top") === "auto") this.$dragElement.css("top", 0);
|
||||
|
||||
this._docMove = this._mousemove.bind(this);
|
||||
this._docUp = this._mouseup.bind(this);
|
||||
|
||||
this.stopped = false;
|
||||
},
|
||||
_mousedown: function (evt) {
|
||||
|
||||
this.currentPosition.x = evt.pageX;
|
||||
this.currentPosition.y = evt.pageY;
|
||||
$(document).on("mousemove", this._docMove);
|
||||
$(document).on("mouseup", this._docUp);
|
||||
evt.stopDrag = function () {
|
||||
|
||||
this.stopped = true;
|
||||
}.bind(this);
|
||||
if (this.option.start) {
|
||||
this.option.start(evt);
|
||||
}
|
||||
},
|
||||
_mousemove: function (evt) {
|
||||
if (this.stopped) return;
|
||||
var dx = evt.pageX - this.currentPosition.x;
|
||||
var dy = evt.pageY - this.currentPosition.y;
|
||||
if (this.option.direction) {
|
||||
if (this.option.direction.toUpperCase() == "X") dy = 0;
|
||||
if (this.option.direction.toUpperCase() == "Y") dx = 0;
|
||||
}
|
||||
this.$moveElement.css("left", (parseInt(this.$moveElement.css("left")) + dx) + "px");
|
||||
this.$moveElement.css("top", (parseInt(this.$moveElement.css("top")) + dy) + "px");
|
||||
if (this.option.move) {
|
||||
this.option.move(evt);
|
||||
}
|
||||
this.currentPosition.x = evt.pageX;
|
||||
this.currentPosition.y = evt.pageY;
|
||||
evt.preventDefault();
|
||||
|
||||
},
|
||||
_mouseup: function (evt) {
|
||||
$(document).off("mousemove", this._docMove);
|
||||
$(document).off("mouseup", this._docUp);
|
||||
if (this.option.end) {
|
||||
this.option.end(evt);
|
||||
}
|
||||
this.stopped = false;
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
new Drag({
|
||||
dragElement: "#dragger1",
|
||||
moveElement: "#dragger1",
|
||||
direction: "y",
|
||||
cursor: "n-resize"
|
||||
})
|
||||
|
||||
|
||||
new Drag({
|
||||
dragElement: "#dragger2",
|
||||
moveElement: "#dragger2",
|
||||
start: function (evt) {
|
||||
//evt.stopDrag()
|
||||
},
|
||||
move: function () {
|
||||
|
||||
},
|
||||
end: function () {
|
||||
|
||||
}
|
||||
})
|
||||
new Drag({
|
||||
dragElement: "#dragger3",
|
||||
moveElement: "#dragger3",
|
||||
cursor: "e-resize",
|
||||
direction: "x"
|
||||
})
|
||||
</script>
|
||||
<a href="https://github.com/AlloyTeam/Nuclear" target="_blank" style="position: absolute; right: 0; top: 0;">
|
||||
<img src="http://alloyteam.github.io/AlloyRenderingEngine/asset/img/github.png" alt="" />
|
||||
</a>
|
||||
</body>
|
||||
</html>
|
|
@ -17,6 +17,7 @@ gulp.task('build', function () {
|
|||
'src/intro.js',
|
||||
|
||||
'src/nuclear.js',
|
||||
'src/core.js',
|
||||
'src/tpl.js',
|
||||
'src/observe.js',
|
||||
'src/class.js',
|
||||
|
|
|
@ -0,0 +1,692 @@
|
|||
Nuclear.create = function (obj) {
|
||||
Nuclear._mixObj(obj);
|
||||
if (!obj.statics) obj.statics = {};
|
||||
obj.statics.create = function (obj) {
|
||||
Nuclear._mixObj(obj);
|
||||
return this.extend(obj);
|
||||
}
|
||||
return Nuclear.Class.extend(obj);
|
||||
}
|
||||
|
||||
Nuclear._mixObj = function (obj) {
|
||||
obj.ctor = function (option, selector) {
|
||||
this._nuclearParentEmpty = !selector;
|
||||
this.HTML = "";
|
||||
this.option = option;
|
||||
if (!this._nuclearParentEmpty) {
|
||||
this.parent = typeof selector === "string" ? document.querySelector(selector) : selector;
|
||||
} else {
|
||||
this.parent = document.createElement("div");
|
||||
}
|
||||
if (this.install) {
|
||||
this.install();
|
||||
}
|
||||
this._nuclearRef = [];
|
||||
for (var key in this) {
|
||||
if (this.hasOwnProperty(key)) {
|
||||
if (this[key] && this[key]["_nuclearLocalRefresh"]) {
|
||||
this[key]._nuclearParent = this;
|
||||
this._nuclearRef.push(this[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
this._nuclearTimer = null;
|
||||
this._preNuclearTime = new Date();
|
||||
|
||||
if (this.option) {
|
||||
Nuclear.observe(this.option, function (prop, value, oldValue, path) {
|
||||
if (!this.onOptionChange||(this.onOptionChange && this.onOptionChange(prop, value, oldValue, path))) {
|
||||
clearTimeout(this._nuclearTimer);
|
||||
if (new Date() - this._preNuclearTime > 40) {
|
||||
this._nuclearLocalRefresh();
|
||||
this._preNuclearTime = new Date();
|
||||
} else {
|
||||
this._nuclearTimer = setTimeout(function () {
|
||||
this._nuclearLocalRefresh();
|
||||
}.bind(this), 40);
|
||||
}
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
this._nuclearRenderInfo = {
|
||||
tpl: this._nuclearTplGenerator(),
|
||||
data: this.option,
|
||||
parent: this.parent
|
||||
};
|
||||
this.option._nuclearIndex = function () {
|
||||
return ++window['_nuclearIndex'] || (window['_nuclearIndex'] = 0);
|
||||
}
|
||||
this.option._resetNuclearIndex=function() {
|
||||
window['_nuclearIndex'] = -1;
|
||||
return;
|
||||
}
|
||||
this._nuclearRender(this._nuclearRenderInfo);
|
||||
if (this.installed) this.installed();
|
||||
}
|
||||
|
||||
obj.refresh = function () {
|
||||
this._nuclearLocalRefresh();
|
||||
}
|
||||
|
||||
//加if防止子类赋值undefined,丢失父类方法
|
||||
if (obj.render) {
|
||||
obj._nuclearTplGenerator = obj.render;
|
||||
}
|
||||
|
||||
obj.render = function () {
|
||||
if (this._nuclearParentEmpty) {
|
||||
|
||||
return this.HTML;
|
||||
//var len=this._nuclearRef.length;
|
||||
////嵌套的render逻辑
|
||||
////子节点下再无子节点
|
||||
//if (len === 0) {
|
||||
// return this.HTML;
|
||||
//} else {//子节点下又有子节点
|
||||
// var i=0;
|
||||
// for (; i < len; i++) {
|
||||
// var ref = this._nuclearRef[i];
|
||||
// return ref.render();
|
||||
// }
|
||||
//}
|
||||
} else {
|
||||
return this._nuclearTplGenerator();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
obj._nuclearRender = function (item) {
|
||||
if (this.node) {
|
||||
//this.node.parentNode&&this.node.parentNode.removeChild(this.node);
|
||||
// item.parent.removeChild(this.node);
|
||||
if (Nuclear.isUndefined(item.tpl)) {
|
||||
item.parent.removeChild(this.node);
|
||||
this.node = null;
|
||||
this.HTML = "";
|
||||
} else {
|
||||
var newNode = Nuclear.str2Dom(Nuclear.Tpl.render(Nuclear._fixTplIndex(item.tpl), item.data));
|
||||
item.parent.replaceChild(newNode, this.node);
|
||||
this.node = newNode;
|
||||
}
|
||||
} else {
|
||||
item.parent.insertAdjacentHTML("beforeEnd", Nuclear.Tpl.render(Nuclear._fixTplIndex(item.tpl), item.data));
|
||||
this.node = item.parent.lastChild;
|
||||
}
|
||||
window["_nuclearIndex"] = null;
|
||||
if (this.node) {
|
||||
this._nuclearId = Nuclear.getId();
|
||||
this.node.setAttribute("data-nuclearId", this._nuclearId);
|
||||
|
||||
this._mixNode();
|
||||
|
||||
if (this.onRefresh) this.onRefresh();
|
||||
item.refreshPart = this.node.querySelectorAll('*[nc-refresh]');
|
||||
this.HTML = this.node.outerHTML;
|
||||
|
||||
|
||||
this._nuclearFix();
|
||||
}
|
||||
}
|
||||
|
||||
obj._mixNode = function () {
|
||||
var nodes = this.node.querySelectorAll('*[nc-id]'),len=nodes.length;
|
||||
if (len > 0) {
|
||||
var i=0;
|
||||
for (; i < len; i++) {
|
||||
var node=nodes[i];
|
||||
this[node.getAttribute("nc-id")] = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//从最顶部组件向内fix
|
||||
obj._nuclearFix = function () {
|
||||
if (this._nuclearParent) return;
|
||||
this._nuclearFixOne(this)
|
||||
}
|
||||
|
||||
obj._nuclearFixOne = function (one) {
|
||||
var refLen = one._nuclearRef.length;
|
||||
if (refLen > 0) {
|
||||
var i = 0;
|
||||
for (; i < refLen; i++) {
|
||||
var ref = one._nuclearRef[i];
|
||||
ref.node = one.node.querySelector('*[data-nuclearId="' + ref._nuclearId + '"]');
|
||||
if (ref.node) {
|
||||
ref._mixNode();
|
||||
ref._nuclearRenderInfo.refreshPart = ref.node.querySelectorAll('*[nc-refresh]');
|
||||
ref._nuclearRenderInfo.parent = ref.node.parentNode;
|
||||
if (ref.onRefresh) ref.onRefresh();
|
||||
if (ref.installed) ref.installed();
|
||||
this._nuclearFixOne(ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
obj._nuclearLocalRefresh = function () {
|
||||
var item = this._nuclearRenderInfo, rpLen = item.refreshPart.length;
|
||||
item.tpl = this._nuclearTplGenerator();
|
||||
if (rpLen > 0) {
|
||||
var parts = Nuclear.str2Dom(Nuclear.Tpl.render(Nuclear._fixTplIndex(item.tpl), item.data)).querySelectorAll('*[nc-refresh]');
|
||||
window["_nuclearIndex"] = null;
|
||||
for (var j = 0; j < rpLen; j++) {
|
||||
var part = item.refreshPart[j];
|
||||
//part.parentNode为null,代表其已经被子节点替换掉了
|
||||
part.parentNode&&part.parentNode.replaceChild(parts[j], part);
|
||||
|
||||
}
|
||||
item.refreshPart = parts;
|
||||
this._mixNode();
|
||||
if (this.onRefresh) this.onRefresh();
|
||||
this.HTML = this.node.outerHTML;
|
||||
|
||||
this._nuclearFix();
|
||||
//var refLen = this._nuclearRef.length;
|
||||
//if (refLen > 0) {
|
||||
// var i = 0;
|
||||
// for (; i < refLen; i++) {
|
||||
// var ref = this._nuclearRef[i];
|
||||
// ref.node = this.node.querySelector('*[data-nuclearId="' + ref._nuclearId + '"]');
|
||||
|
||||
// if (ref.onRefresh) ref.onRefresh();
|
||||
// if (ref.installed) ref.installed();
|
||||
// }
|
||||
//}
|
||||
} else {
|
||||
this._nuclearRender(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Nuclear._fixTplIndex = function (tpl) {
|
||||
//"{{_nuclearIndex}}"
|
||||
return tpl.replace(/{{#[\s\S]*?{{@index}}/g, function (str) {
|
||||
|
||||
return "{{_resetNuclearIndex}}" + str.replace("{{@index}}", "{{_nuclearIndex}}");
|
||||
});
|
||||
}
|
||||
|
||||
Nuclear._minActionObj = function (obj) {
|
||||
obj.ctor = function (option) {
|
||||
this.option = option;
|
||||
if (this.install) {
|
||||
this.install();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Nuclear.createAction = function (obj) {
|
||||
Nuclear._minActionObj(obj)
|
||||
if (!obj.statics) obj.statics = {};
|
||||
obj.statics.createAction = function (obj) {
|
||||
Nuclear._minActionObj(obj);
|
||||
return this.extend(obj);
|
||||
}
|
||||
return Nuclear.Class.extend(obj);
|
||||
|
||||
}
|
||||
|
||||
Nuclear.throttle = function (func, wait, options) {
|
||||
var context, args, result;
|
||||
var timeout = null;
|
||||
// 上次执行时间点
|
||||
var previous = 0;
|
||||
if (!options) options = {};
|
||||
// 延迟执行函数
|
||||
var later = function () {
|
||||
// 若设定了开始边界不执行选项,上次执行时间始终为0
|
||||
previous = options.leading === false ? 0 : Date.now();
|
||||
timeout = null;
|
||||
result = func.apply(context, args);
|
||||
if (!timeout) context = args = null;
|
||||
};
|
||||
return function () {
|
||||
var now = Date.now();
|
||||
// 首次执行时,如果设定了开始边界不执行选项,将上次执行时间设定为当前时间。
|
||||
if (!previous && options.leading === false) previous = now;
|
||||
// 延迟执行时间间隔
|
||||
var remaining = wait - (now - previous);
|
||||
context = this;
|
||||
args = arguments;
|
||||
// 延迟时间间隔remaining小于等于0,表示上次执行至此所间隔时间已经超过一个时间窗口
|
||||
// remaining大于时间窗口wait,表示客户端系统时间被调整过
|
||||
if (remaining <= 0 || remaining > wait) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
previous = now;
|
||||
result = func.apply(context, args);
|
||||
if (!timeout) context = args = null;
|
||||
//如果延迟执行不存在,且没有设定结尾边界不执行选项
|
||||
} else if (!timeout && options.trailing !== false) {
|
||||
timeout = setTimeout(later, remaining);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
Nuclear.isElement=function(o) {
|
||||
return (
|
||||
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
|
||||
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string"
|
||||
);
|
||||
}
|
||||
|
||||
Nuclear.str2Dom = function (html) {
|
||||
var wrapMap = {
|
||||
option: [1, "<select multiple='multiple'>", "</select>"],
|
||||
legend: [1, "<fieldset>", "</fieldset>"],
|
||||
area: [1, "<map>", "</map>"],
|
||||
param: [1, "<object>", "</object>"],
|
||||
thead: [1, "<table>", "</table>"],
|
||||
tr: [2, "<table><tbody>", "</tbody></table>"],
|
||||
col: [2, "<table><tbody></tbody><colgroup>", "</colgroup></table>"],
|
||||
td: [3, "<table><tbody><tr>", "</tr></tbody></table>"],
|
||||
body: [0, "", ""],
|
||||
_default: [1, "<div>", "</div>"]
|
||||
};
|
||||
wrapMap.optgroup = wrapMap.option;
|
||||
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
|
||||
wrapMap.th = wrapMap.td;
|
||||
var match = /<\s*\w.*?>/g.exec(html);
|
||||
var element = document.createElement('div');
|
||||
if (match != null) {
|
||||
var tag = match[0].replace(/</g, '').replace(/>/g, '').split(' ')[0];
|
||||
if (tag.toLowerCase() === 'body') {
|
||||
var dom = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
|
||||
var body = document.createElement("body");
|
||||
// keeping the attributes
|
||||
element.innerHTML = html.replace(/<body/g, '<div').replace(/<\/body>/g, '</div>');
|
||||
var attrs = element.firstChild.attributes;
|
||||
body.innerHTML = html;
|
||||
for (var i = 0; i < attrs.length; i++) {
|
||||
body.setAttribute(attrs[i].name, attrs[i].value);
|
||||
}
|
||||
return body;
|
||||
} else {
|
||||
var map = wrapMap[tag] || wrapMap._default, element;
|
||||
html = map[1] + html + map[2];
|
||||
element.innerHTML = html;
|
||||
// Descend through wrappers to the right content
|
||||
var j = map[0] + 1;
|
||||
while (j--) {
|
||||
element = element.lastChild;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
element.innerHTML = html;
|
||||
element = element.lastChild;
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
Nuclear.debounce=function (func, wait, immediate) {
|
||||
var timeout;
|
||||
return function () {
|
||||
var context = this, args = arguments;
|
||||
var later = function () {
|
||||
timeout = null;
|
||||
if (!immediate) func.apply(context, args);
|
||||
};
|
||||
var callNow = immediate && !timeout;
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
if (callNow) func.apply(context, args);
|
||||
};
|
||||
}
|
||||
|
||||
Nuclear.addEvent = (function () {
|
||||
return function (el, type, fn) {
|
||||
if (el && el.nodeName || el === window) {
|
||||
el.addEventListener(type, fn, false);
|
||||
} else if (el && el.length) {
|
||||
for (var i = 0; i < el.length; i++) {
|
||||
Nuclear.addEvent(el[i], type, fn);
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
Nuclear.removeEvent = (function () {
|
||||
return function (el, type, fn) {
|
||||
if (el && el.nodeName || el === window) {
|
||||
el.removeEventListener(type, fn, false);
|
||||
} else if (el && el.length) {
|
||||
for (var i = 0; i < el.length; i++) {
|
||||
Nuclear.removeEvent.removeEvent(el[i], type, fn);
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
||||
Nuclear.getNodeIndex = function (node) {
|
||||
var index = 0;
|
||||
while ((node = node.previousSibling)) {
|
||||
if (node.nodeType != 3 || !/^\s*$/.test(node.data)) {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
Nuclear.hasClass = function (ele, cls) {
|
||||
return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
|
||||
}
|
||||
|
||||
Nuclear.addClass = function (node, cls) {
|
||||
if (Nuclear.isNodeList(node)) {
|
||||
Nuclear._iteration("addClass", arguments);
|
||||
return;
|
||||
}
|
||||
if (!Nuclear.hasClass(node, cls)) node.className += " " + cls;
|
||||
|
||||
}
|
||||
|
||||
Nuclear.removeClass = function (node, cls) {
|
||||
if (Nuclear.isNodeList(node)) {
|
||||
Nuclear._iteration("removeClass", arguments);
|
||||
return;
|
||||
}
|
||||
if (Nuclear.hasClass(node, cls)) {
|
||||
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
|
||||
node.className = node.className.replace(reg, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
Nuclear.toggleClass = function (node, classStr) {
|
||||
if (Nuclear.isNodeList(node)) {
|
||||
Nuclear._iteration("toggleClass", arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
var cls = ' ' + node.className + ' ';
|
||||
if (cls.indexOf(' ' + Nuclear.trim(classStr) + ' ') >= 0) {
|
||||
Nuclear.removeClass(node, classStr);
|
||||
} else {
|
||||
Nuclear.addClass(node, classStr);
|
||||
}
|
||||
}
|
||||
|
||||
Nuclear.trim = function (str) {
|
||||
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
|
||||
};
|
||||
|
||||
Nuclear._iteration = function (method, args) {
|
||||
var args = Array.prototype.slice.call(args);
|
||||
Nuclear.foreach(args[0], function (index) {
|
||||
var temp = args[0];
|
||||
args[0] = args[0][index];
|
||||
Nuclear[method].apply(Nuclear, args);
|
||||
args[0] = temp;
|
||||
})
|
||||
}
|
||||
|
||||
Nuclear.css = function (node, styleName, value) {
|
||||
if (!node) return;
|
||||
|
||||
if (Nuclear.isNodeList(node)) {
|
||||
Nuclear._iteration("css", arguments);
|
||||
return;
|
||||
}
|
||||
|
||||
if (arguments.length == 3 && node.style) { //change by bizai
|
||||
node.style[styleName] = value;
|
||||
}
|
||||
//因为有transition的存在,所有这行代码需要注释掉
|
||||
//if (node.style[styleName]) return node.style[styleName];
|
||||
return window.getComputedStyle(node, null)[styleName];
|
||||
|
||||
},
|
||||
|
||||
|
||||
Nuclear.foreach = function (list, fn) {
|
||||
for (var i = 0, len = list.length; i < len ; i++) {
|
||||
fn.call(list[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
Nuclear.offset = function (el) {
|
||||
var _t = 0,
|
||||
_l = 0;
|
||||
if (document.documentElement.getBoundingClientRect && el.getBoundingClientRect) {
|
||||
var box = el.getBoundingClientRect();
|
||||
_l = box.left;
|
||||
_t = box.top;
|
||||
} else {
|
||||
while (el.offsetParent) {
|
||||
_t += el.offsetTop;
|
||||
_l += el.offsetLeft;
|
||||
el = el.offsetParent;
|
||||
}
|
||||
return { left: _l, top: _t };
|
||||
}
|
||||
return { left: _l + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft), top: _t + Math.max(document.documentElement.scrollTop, document.body.scrollTop) };
|
||||
}
|
||||
|
||||
Nuclear.getViewport = function () {
|
||||
var d = document.documentElement, b = document.body, w = window, div = document.createElement("div");
|
||||
div.innerHTML = " <link/>";
|
||||
var lt = !(div.firstChild.nodeType === 3) ?
|
||||
{ left: b.scrollLeft || d.scrollLeft, top: b.scrollTop || d.scrollTop } :
|
||||
{ left: w.pageXOffset, top: w.pageYOffset };
|
||||
var wh = w.innerWidth ?
|
||||
{ width: w.innerWidth, height: w.innerHeight } :
|
||||
(d && d.clientWidth && d.clientWidth != 0 ?
|
||||
{ width: d.clientWidth, height: d.clientHeight } :
|
||||
{ width: b.clientWidth, height: b.clientHeight });
|
||||
|
||||
return { left: lt.left, top: lt.top, width: wh.width, height: wh.height };
|
||||
}
|
||||
|
||||
//this.parent.lastChild代替
|
||||
Nuclear.getLastNode = function (parent, selector) {
|
||||
var childs = parent.querySelectorAll(selector), len = childs.length;
|
||||
return childs[len - 1];
|
||||
}
|
||||
|
||||
Nuclear.isUndefined = function (o) {
|
||||
return typeof (o) === "undefined";
|
||||
}
|
||||
|
||||
Nuclear.domready = (function () {
|
||||
|
||||
var fns = [], listener
|
||||
, doc = document
|
||||
, hack = doc.documentElement.doScroll
|
||||
, domContentLoaded = 'DOMContentLoaded'
|
||||
, loaded = (hack ? /^loaded|^c/ : /^loaded|^i|^c/).test(doc.readyState)
|
||||
|
||||
|
||||
if (!loaded)
|
||||
doc.addEventListener(domContentLoaded, listener = function () {
|
||||
doc.removeEventListener(domContentLoaded, listener)
|
||||
loaded = 1
|
||||
while (listener = fns.shift()) listener()
|
||||
})
|
||||
|
||||
return function (fn) {
|
||||
loaded ? setTimeout(fn, 0) : fns.push(fn)
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
; (function () {
|
||||
var class2type = {}, toString = Object.prototype.toString;
|
||||
"Boolean Number String Function Array Date RegExp Object Error NodeList".split(" ").forEach(function (name) {
|
||||
class2type["[object " + name + "]"] = name.toLowerCase();
|
||||
})
|
||||
|
||||
var type = function (obj) {
|
||||
if (obj == null) {
|
||||
return obj + "";
|
||||
}
|
||||
// Support: Android<4.0 (functionish RegExp)
|
||||
return typeof obj === "object" || typeof obj === "function" ?
|
||||
class2type[toString.call(obj)] || "object" :
|
||||
typeof obj;
|
||||
}
|
||||
|
||||
var isNodeList = function (obj) {
|
||||
return type(obj) === "nodelist";
|
||||
}
|
||||
|
||||
var isFunction = function (obj) {
|
||||
return type(obj) === "function";
|
||||
|
||||
}
|
||||
var isArray = function (obj) {
|
||||
return type(obj) === "array";
|
||||
}
|
||||
var isWindow = function (obj) {
|
||||
return obj != null && obj === obj.window;
|
||||
}
|
||||
|
||||
var isPlainObject = function (obj) {
|
||||
// Not plain objects:
|
||||
// - Any object or value whose internal [[Class]] property is not "[object Object]"
|
||||
// - DOM nodes
|
||||
// - window
|
||||
if (type(obj) !== "object" || obj.nodeType || isWindow(obj)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (obj.constructor &&
|
||||
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the function hasn't returned already, we're confident that
|
||||
// |obj| is a plain object, created by {} or constructed with new Object
|
||||
return true;
|
||||
}
|
||||
|
||||
Nuclear.type = type;
|
||||
Nuclear.isFunction = isFunction;
|
||||
Nuclear.isArray = isArray;
|
||||
Nuclear.isWindow = isWindow;
|
||||
Nuclear.isPlainObject = isPlainObject;
|
||||
Nuclear.isNodeList = isNodeList;
|
||||
})();
|
||||
|
||||
Nuclear.merge = function () {
|
||||
var options, name, src, copy, copyIsArray, clone,
|
||||
target = arguments[0] || {},
|
||||
i = 1,
|
||||
length = arguments.length,
|
||||
deep = false;
|
||||
|
||||
// Handle a deep copy situation
|
||||
if (typeof target === "boolean") {
|
||||
deep = target;
|
||||
|
||||
// Skip the boolean and the target
|
||||
target = arguments[i] || {};
|
||||
i++;
|
||||
}
|
||||
|
||||
// Handle case when target is a string or something (possible in deep copy)
|
||||
if (typeof target !== "object" && !Nuclear.isFunction(target)) {
|
||||
target = {};
|
||||
}
|
||||
|
||||
// Extend jQuery itself if only one argument is passed
|
||||
if (i === length) {
|
||||
target = this;
|
||||
i--;
|
||||
}
|
||||
|
||||
for (; i < length; i++) {
|
||||
// Only deal with non-null/undefined values
|
||||
if ((options = arguments[i]) != null) {
|
||||
// Extend the base object
|
||||
for (name in options) {
|
||||
src = target[name];
|
||||
copy = options[name];
|
||||
|
||||
// Prevent never-ending loop
|
||||
if (target === copy) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Recurse if we're merging plain objects or arrays
|
||||
if (deep && copy && (Nuclear.isPlainObject(copy) ||
|
||||
(copyIsArray = Nuclear.isArray(copy)))) {
|
||||
|
||||
if (copyIsArray) {
|
||||
copyIsArray = false;
|
||||
clone = src && Nuclear.isArray(src) ? src : [];
|
||||
|
||||
} else {
|
||||
clone = src && Nuclear.isPlainObject(src) ? src : {};
|
||||
}
|
||||
|
||||
// Never move original objects, clone them
|
||||
target[name] = Nuclear.merge(deep, clone, copy);
|
||||
|
||||
// Don't bring in undefined values
|
||||
} else if (copy !== undefined) {
|
||||
target[name] = copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return the modified object
|
||||
return target;
|
||||
};
|
||||
|
||||
//因为js单线程的关系,所以就算同时执行两次Nuclear.uuid,得到的uuid也不可能相同
|
||||
Nuclear.uuid = function () {
|
||||
var d = new Date().getTime();
|
||||
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
||||
var r = (d + Math.random() * 16) % 16 | 0;
|
||||
d = Math.floor(d / 16);
|
||||
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
||||
});
|
||||
return uuid;
|
||||
};
|
||||
|
||||
Nuclear.createCanvas = function (obj) {
|
||||
Nuclear._minCanvasObj(obj);
|
||||
if (!obj.statics) obj.statics = {};
|
||||
obj.statics.create = function (obj) {
|
||||
Nuclear._minCanvasObj(obj);
|
||||
return this.extend(obj);
|
||||
}
|
||||
return Nuclear.Class.extend(obj);
|
||||
};
|
||||
|
||||
|
||||
Nuclear._minCanvasObj = function (obj) {
|
||||
obj.ctor = function (width, height, option, selector) {
|
||||
this.canvas = document.createElement("canvas");
|
||||
this.ctx = this.canvas.getContext("2d");
|
||||
this.canvas.width = width;
|
||||
this.canvas.height = height;
|
||||
this.parent = typeof selector === "string" ? document.querySelector(selector) : selector;
|
||||
this.option = option;
|
||||
if (this.install) {
|
||||
this.install();
|
||||
}
|
||||
if (this.option) {
|
||||
Nuclear.observe(this.option, Nuclear.throttle(this._nuclearRender.bind(this), 15));
|
||||
}
|
||||
this._nuclearRender();
|
||||
if (this.installed) this.installed();
|
||||
this.parent.appendChild(this.canvas);
|
||||
}
|
||||
|
||||
obj._nuclearRender = function (item) {
|
||||
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
this.render();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
Nuclear._nextID = 0;
|
||||
Nuclear.getId = function () {
|
||||
return Nuclear._nextID++;
|
||||
|
||||
}
|
|
@ -13,6 +13,6 @@
|
|||
module.exports=factory();
|
||||
});
|
||||
} else {
|
||||
root.Nuclear = factory();
|
||||
root.$ = root.Nuclear = factory();
|
||||
}
|
||||
}(this, function () {
|
||||
|
|
1894
src/nuclear.js
1894
src/nuclear.js
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue