[转帖]Mootools源码分析-06 -- Window
原帖地址:http://space.flash8.net/space/?uid-18713-action-viewspace-itemid-400782
原作者:我佛山人
//对象实例唯一标识
Native.UID = 1;
/*
唯一标识生成器
三目中使用函数而不是函数中使用三目运算的原因在于舍空间求时间,不必每次执行方法时都判断
*/
var $uid = (Browser.Engine.trident) ? function(item) {
return (item.uid || (item.uid = [Native.UID++]))[0];
} : function(item) {
return item.uid || (item.uid = Native.UID++);
};
//Window对象,对window对象的扩展
var Window = new Native({
//族名,为$type提供精准类型判断
name: 'Window',
//扩展原型
legacy: (Browser.Engine.trident) ? null: window.Window,
initialize: function(win) {
//生成唯一标识
$uid(win);
if (!win.Element) {
win.Element = $empty;
if (Browser.Engine.webkit) win.document.createElement("iframe"); //fixes safari 2
win.Element.prototype = (Browser.Engine.webkit) ? window["[[DOMElement.prototype]]"] : {};
}
return $extend(win, Window.Prototype);
},
//同时对当前window对象进行扩展
afterImplement: function(property, value) {
window[property] = Window.Prototype[property] = value;
}
});
Window.Prototype = {$family: {name: 'window'}};
new Window(window);
//对Window对象的扩展
Window.implement({
//经典的美元符号,获取DOM对象的快捷方式,同时对所获取的对象进行Element化
$: function(el, notrash) {
if (el && el.$family && el.uid) return el;
var type = $type(el);
//将复杂逻辑优雅的分享,参数后面的$.string,$.element,$.object和$.textnode
return ($[type]) ? $[type](el, notrash, this.document) : null;
},
//根据类XPath的语法获取DOM,返回Elements对象
$$: function(selector) {
if (arguments.length == 1 && typeof selector == 'string') return this.document.getElements(selector);
var elements = [];
//参数降维处理
var args = Array.flatten(arguments);
for (var i = 0, l = args.length; i < l; i++) {
var item = args[i];
switch ($type(item)) {
case 'element': item = [item]; break;
case 'string': item = this.document.getElements(item, true); break;
default: item = false;
}
if (item) elements.extend(item);
}
return new Elements(elements);
},
getDocument: function() {
return this.document;
},
getWindow: function() {
return this;
}
});
//在$方法中,当参数为string时的处理
$.string = function(id, notrash, doc) {
id = doc.getElementById(id);
return (id) ? $.element(id, notrash) : null;
};
//当参数已经为DOM对象时的处理
$.element = function(el, notrash) {
$uid(el);
if (!notrash && !el.$family && !(/^object|embed$/i).test(el.tagName)) {
var proto = Element.Prototype;
for (var p in proto) el[p] = proto[p];
};
return el;
};
//当参数为object对象时的处理
$.object = function(obj, notrash, doc) {
if (obj.toElement) return $.element(obj.toElement(doc), notrash);
return null;
};
//当参数为文本节点时的处理,使用$arguments直接返回第一个参数
$.textnode = $.whitespace = $.window = $.document = $arguments(0);
Native.UID = 1;
/*
唯一标识生成器
三目中使用函数而不是函数中使用三目运算的原因在于舍空间求时间,不必每次执行方法时都判断
*/
var $uid = (Browser.Engine.trident) ? function(item) {
return (item.uid || (item.uid = [Native.UID++]))[0];
} : function(item) {
return item.uid || (item.uid = Native.UID++);
};
//Window对象,对window对象的扩展
var Window = new Native({
//族名,为$type提供精准类型判断
name: 'Window',
//扩展原型
legacy: (Browser.Engine.trident) ? null: window.Window,
initialize: function(win) {
//生成唯一标识
$uid(win);
if (!win.Element) {
win.Element = $empty;
if (Browser.Engine.webkit) win.document.createElement("iframe"); //fixes safari 2
win.Element.prototype = (Browser.Engine.webkit) ? window["[[DOMElement.prototype]]"] : {};
}
return $extend(win, Window.Prototype);
},
//同时对当前window对象进行扩展
afterImplement: function(property, value) {
window[property] = Window.Prototype[property] = value;
}
});
Window.Prototype = {$family: {name: 'window'}};
new Window(window);
//对Window对象的扩展
Window.implement({
//经典的美元符号,获取DOM对象的快捷方式,同时对所获取的对象进行Element化
$: function(el, notrash) {
if (el && el.$family && el.uid) return el;
var type = $type(el);
//将复杂逻辑优雅的分享,参数后面的$.string,$.element,$.object和$.textnode
return ($[type]) ? $[type](el, notrash, this.document) : null;
},
//根据类XPath的语法获取DOM,返回Elements对象
$$: function(selector) {
if (arguments.length == 1 && typeof selector == 'string') return this.document.getElements(selector);
var elements = [];
//参数降维处理
var args = Array.flatten(arguments);
for (var i = 0, l = args.length; i < l; i++) {
var item = args[i];
switch ($type(item)) {
case 'element': item = [item]; break;
case 'string': item = this.document.getElements(item, true); break;
default: item = false;
}
if (item) elements.extend(item);
}
return new Elements(elements);
},
getDocument: function() {
return this.document;
},
getWindow: function() {
return this;
}
});
//在$方法中,当参数为string时的处理
$.string = function(id, notrash, doc) {
id = doc.getElementById(id);
return (id) ? $.element(id, notrash) : null;
};
//当参数已经为DOM对象时的处理
$.element = function(el, notrash) {
$uid(el);
if (!notrash && !el.$family && !(/^object|embed$/i).test(el.tagName)) {
var proto = Element.Prototype;
for (var p in proto) el[p] = proto[p];
};
return el;
};
//当参数为object对象时的处理
$.object = function(obj, notrash, doc) {
if (obj.toElement) return $.element(obj.toElement(doc), notrash);
return null;
};
//当参数为文本节点时的处理,使用$arguments直接返回第一个参数
$.textnode = $.whitespace = $.window = $.document = $arguments(0);

浙公网安备 33010602011771号