element.js
来自「php绿色服务器,让大家试用greenamp」· JavaScript 代码 · 共 1,746 行 · 第 1/5 页
JS
1,746 行
/*
* Ext JS Library 1.0.1
* Copyright(c) 2006-2007, Ext JS, LLC.
* licensing@extjs.com
*
* http://www.extjs.com/license
*/
/**
* @class Ext.Element
* Represents an Element in the DOM.<br><br>
* Usage:<br>
<pre><code>
var el = Ext.get("my-div");
// or with getEl
var el = getEl("my-div");
// or with a DOM element
var el = Ext.get(myDivElement);
</code></pre>
* Using Ext.get() or getEl() instead of calling the constructor directly ensures you get the same object
* each call instead of constructing a new one.<br><br>
* <b>Animations</b><br />
* Many of the functions for manipulating an element have an optional "animate" parameter. The animate parameter
* should either be a boolean (true) or an object literal with animation options. The animation options are:
<pre>
Option Default Description
--------- -------- ---------------------------------------------
duration .35 The duration of the animation in seconds
easing easeOut The YUI easing method
callback none A function to execute when the anim completes
scope this The scope (this) of the callback function
</pre>
* Also, the Anim object being used for the animation will be set on your options object as "anim", which allows you to stop or
* manipulate the animation. Here's an example:
<pre><code>
var el = Ext.get("my-div");
// no animation
el.setWidth(100);
// default animation
el.setWidth(100, true);
// animation with some options set
el.setWidth(100, {
duration: 1,
callback: this.foo,
scope: this
});
// using the "anim" property to get the Anim object
var opt = {
duration: 1,
callback: this.foo,
scope: this
};
el.setWidth(100, opt);
...
if(opt.anim.isAnimated()){
opt.anim.stop();
}
</code></pre>
* <b> Composite (Collections of) Elements</b><br />
* For working with collections of Elements, see <a href="Ext.CompositeElement.html">Ext.CompositeElement</a>
* @constructor Create a new Element directly.
* @param {String/HTMLElement} element
* @param {Boolean} forceNew (optional) By default the constructor checks to see if there is already an instance of this element in the cache and if there is it returns the same instance. This will skip that check (useful for extending this class).
*/
(function(){
var D = Ext.lib.Dom;
var E = Ext.lib.Event;
var A = Ext.lib.Anim;
// local style camelizing for speed
var propCache = {};
var camelRe = /(-[a-z])/gi;
var camelFn = function(m, a){ return a.charAt(1).toUpperCase(); };
var view = document.defaultView;
Ext.Element = function(element, forceNew){
var dom = typeof element == "string" ?
document.getElementById(element) : element;
if(!dom){ // invalid id/element
return null;
}
if(!forceNew && Ext.Element.cache[dom.id]){ // element object already exists
return Ext.Element.cache[dom.id];
}
/**
* The DOM element
* @type HTMLElement
*/
this.dom = dom;
/**
* The DOM element ID
* @type String
*/
this.id = dom.id || Ext.id(dom);
};
var El = Ext.Element;
El.prototype = {
/**
* The element's default display mode @type String
*/
originalDisplay : "",
visibilityMode : 1,
/**
* The default unit to append to CSS values where a unit isn't provided (Defaults to px).
* @type String
*/
defaultUnit : "px",
/**
* Sets the elements visibility mode. When setVisible() is called it
* will use this to determine whether to set the visibility or the display property.
* @param visMode Element.VISIBILITY or Element.DISPLAY
* @return {Ext.Element} this
*/
setVisibilityMode : function(visMode){
this.visibilityMode = visMode;
return this;
},
/**
* Convenience method for setVisibilityMode(Element.DISPLAY)
* @param {String} display (optional) What to set display to when visible
* @return {Ext.Element} this
*/
enableDisplayMode : function(display){
this.setVisibilityMode(El.DISPLAY);
if(typeof display != "undefined") this.originalDisplay = display;
return this;
},
/**
* Looks at this node and then at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)
* @param {String} ss The simple selector to test
* @param {Number/String/HTMLElement/Element} maxDepth (optional) The max depth to
search as a number or element (defaults to 10 || document.body)
* @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node
* @return {HTMLElement}
*/
findParent : function(simpleSelector, maxDepth, returnEl){
var p = this.dom, b = document.body, depth = 0, dq = Ext.DomQuery, stopEl;
maxDepth = maxDepth || 50;
if(typeof maxDepth != "number"){
stopEl = Ext.getDom(maxDepth);
maxDepth = 10;
}
while(p && p.nodeType == 1 && depth < maxDepth && p != b && p != stopEl){
if(dq.is(p, simpleSelector)){
return returnEl ? Ext.get(p) : p;
}
depth++;
p = p.parentNode;
}
return null;
},
/**
* Looks at parent nodes for a match of the passed simple selector (e.g. div.some-class or span:first-child)
* @param {String} ss The simple selector to test
* @param {Number/String/HTMLElement/Element} maxDepth (optional) The max depth to
search as a number or element (defaults to 10 || document.body)
* @param {Boolean} returnEl (optional) True to return a Ext.Element object instead of DOM node
* @return {HTMLElement}
*/
findParentNode : function(simpleSelector, maxDepth, returnEl){
var p = Ext.fly(this.dom.parentNode, '_internal');
return p ? p.findParent(simpleSelector, maxDepth, returnEl) : null;
},
/**
* Walks up the dom looking for a parent node that matches the passed simple selector (e.g. div.some-class or span:first-child).
* This is a shortcut for findParentNode() that always returns an Ext.Element.
* @param {String} ss The simple selector to test
* @param {Number/String/HTMLElement/Element} maxDepth (optional) The max depth to
search as a number or element (defaults to 10 || document.body)
* @return {Ext.Element}
*/
up : function(simpleSelector, maxDepth){
return this.findParentNode(simpleSelector, maxDepth, true);
},
/**
* Returns true if this element matches the passed simple selector (e.g. div.some-class or span:first-child)
* @param {String} ss The simple selector to test
* @return {Boolean}
*/
is : function(simpleSelector){
return Ext.DomQuery.is(this.dom, simpleSelector);
},
/**
* Perform animation on this element.
* @param {Object} args The YUI animation control args
* @param {Float} duration (optional) How long the animation lasts. (Defaults to .35 seconds)
* @param {Function} onComplete (optional) Function to call when animation completes.
* @param {String} easing (optional) Easing method to use. (Defaults to 'easeOut')
* @param {String} animType (optional) 'run' is the default. Can be 'color', 'motion', or 'scroll'
* @return {Ext.Element} this
*/
animate : function(args, duration, onComplete, easing, animType){
this.anim(args, {duration: duration, callback: onComplete, easing: easing}, animType);
return this;
},
/*
* @private Internal animation call
*/
anim : function(args, opt, animType, defaultDur, defaultEase, cb){
animType = animType || 'run';
opt = opt || {};
var anim = Ext.lib.Anim[animType](
this.dom, args,
(opt.duration || defaultDur) || .35,
(opt.easing || defaultEase) || 'easeOut',
function(){
Ext.callback(cb, this);
Ext.callback(opt.callback, opt.scope || this, [this, opt]);
},
this
);
opt.anim = anim;
return anim;
},
// private legacy anim prep
preanim : function(a, i){
return !a[i] ? false : (typeof a[i] == "object" ? a[i]: {duration: a[i+1], callback: a[i+2], easing: a[i+3]});
},
/**
* Removes worthless text nodes
* @param {Boolean} forceReclean (optional) By default the element
* keeps track if it has been cleaned already so
* you can call this over and over. However, if you update the element and
* need to force a reclean, you can pass true.
*/
clean : function(forceReclean){
if(this.isCleaned && forceReclean !== true){
return this;
}
var ns = /\S/;
var d = this.dom, n = d.firstChild, ni = -1;
while(n){
var nx = n.nextSibling;
if(n.nodeType == 3 && !ns.test(n.nodeValue)){
d.removeChild(n);
}else{
n.nodeIndex = ++ni;
}
n = nx;
}
this.isCleaned = true;
return this;
},
calcOffsetsTo : function(el){
el = Ext.get(el), d = el.dom;
var restorePos = false;
if(el.getStyle('position') == 'static'){
el.position('relative');
restorePos = true;
}
var x = 0, y =0;
var op = this.dom;
while(op && op != d && op.tagName != 'HTML'){
x+= op.offsetLeft;
y+= op.offsetTop;
op = op.offsetParent;
}
if(restorePos){
el.position('static');
}
return [x, y];
},
/**
* Scrolls this element into view within the passed container.
* @param {String/HTMLElement/Element} container (optional) The container element to scroll (defaults to document.body)
* @param {Boolean} hscroll (optional) false to disable horizontal scroll
* @return {Ext.Element} this
*/
scrollIntoView : function(container, hscroll){
var c = Ext.getDom(container) || document.body;
var el = this.dom;
var o = this.calcOffsetsTo(c),
l = o[0],
t = o[1],
b = t+el.offsetHeight,
r = l+el.offsetWidth;
var ch = c.clientHeight;
var ct = parseInt(c.scrollTop, 10);
var cl = parseInt(c.scrollLeft, 10);
var cb = ct + ch;
var cr = cl + c.clientWidth;
if(t < ct){
c.scrollTop = t;
}else if(b > cb){
c.scrollTop = b-ch;
}
if(hscroll !== false){
if(l < cl){
c.scrollLeft = l;
}else if(r > cr){
c.scrollLeft = r-c.clientWidth;
}
}
return this;
},
scrollChildIntoView : function(child){
Ext.fly(child, '_scrollChildIntoView').scrollIntoView(this);
},
/**
* Measures the elements content height and updates height to match. Note, this function uses setTimeout and
* the new height may not be available immediately.
* @param {Boolean} animate (optional) Animate the transition (Default is false)
* @param {Float} duration (optional) Length of the animation. (Defaults to .35 seconds)
* @param {Function} onComplete (optional) Function to call when animation completes.
* @param {String} easing (optional) Easing method to use.
* @return {Ext.Element} this
*/
autoHeight : function(animate, duration, onComplete, easing){
var oldHeight = this.getHeight();
this.clip();
this.setHeight(1); // force clipping
setTimeout(function(){
var height = parseInt(this.dom.scrollHeight, 10); // parseInt for Safari
if(!animate){
this.setHeight(height);
this.unclip();
if(typeof onComplete == "function"){
onComplete();
}
}else{
this.setHeight(oldHeight); // restore original height
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?