yqb
Version:
Static Package Manager
684 lines (611 loc) • 20.9 kB
JavaScript
/*
* PAF 框架
* author: <%= author %>
* version: <%= version %>
* website: http://www.431103.com
*/
( function ( window, undefined ) {
// ----------------------
/* <><><><><><><><><><><><> */
Number.prototype.fixLen = String.prototype.fixLen = function ( _str )
{
var _this = this.toString( );
var _iLen = _str.toString( ).length | 0;
_this = "" + _str + _this;
var iLength = _this.length;
_this = _this.substr( iLength - _iLen );
return _this;
};
String.prototype.trim = function ( P )
{
var _this = this.toString( );
switch ( P )
{
case 'left':
return _this.replace( /^(\s|\u00A0)+/, "" );
case 'right':
return _this.replace( /(\s|\u00A0)+$/, "" );
default:
return _this.replace( /^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" );
}
};
/* <><><><><><><><><><><><> */
var
version = '1.1.3 bate',
_$ = window.$,
undefined = undefined,
noop = new Function (),
userAgent = navigator.userAgent,
slice = Array.prototype.slice,
toString = Object.prototype.toString,
indexOf = Array.prototype.indexOf,
Expr = {
isSimple: /^.[^:#\[\.,\s>]*$/,
isId : /#((?:[\w\u00c0-\uFFFF-]|\\.)+)/,
isClass : /\.((?:[\w\u00c0-\uFFFF-]|\\.)+)/,
isTag : /^((?:[\w\u00c0-\uFFFF\*-]|\\.)+)/
},
$ = PAF = function ( _selector, _context )
{
return new PAF.plu.selector ( _selector, _context );
};
PAF.plu =
{
PAF: version,
constructor : PAF,
selector : function ( _selector, _context )
{
if ( !_selector ) return this;
var _context = ( !_context || !(typeof _context == "string") ? document : ( _context.nodeType ? _context : document.getElementById(_context.substring(1) ) ) ) || document;
if ( _selector.nodeType )
{
this.context = this[ 0 ] = _selector;
this.length = 1;
return this;
}
if ( typeof _selector === "string" )
{
var HTMLElement = [];
if ( document.querySelectorAll && 0 )
HTMLElement = document.querySelectorAll( _selector );
else
{
var sRules = _selector = _selector.trim().replace(/\s+(,|>|\+)\s*|\s+/g, "$1 ");
HTMLElement = HandleRule( sRules.replace(/>\s/g, ">"), _context );
}
this.extend( HTMLElement );
this.context = _context;
this.selector = _selector;
this.length = HTMLElement.length;
return this;
}
},
each : function ( _callback, args )
{
return PAF.each( this, _callback, args );
},
length : 0,
push : [].push,
sort : [].sort,
splice : [].splice,
index : function ( _ser )
{
var ret = null, self = this;
if ( self.nodeType == 1 )
{
self.each( function ( index )
{
if ( self[ index ] == _ser ) ret = index;
} );
return parseInt( ret );
}
if ( _ser != undefined && ( 0 <= _ser && _ser < self.length ) ) return PAF( self[ _ser ] );
return self;
}
};
PAF.plu.selector.prototype = PAF.plu;
PAF.extend = PAF.plu.extend = function ( _Object, _Target )
{
var _Target = _Target || this;
if ( !_Object ) return false;
for ( var name in _Object ) _Target[ name ] = _Object[ name ];
return _Target;
};
PAF.each = function ( _object, _callback, _args )
{
var _args = _args || [ ], _object = _object || this, _callback = _callback || noop;
var idx = 0, length = _object.length, argLen = _args.length;
if ( length > 0 )
{
for ( ; idx < length; idx++ )
{
_args[ argLen ] = idx;
_callback.apply( _object[ idx ], _args );
}
}
return _object;
}
PAF.extend({
isArray : function ( _obj )
{
return toString.call( _obj ) === "[object Array]";
},
isFunction: function( obj )
{
return toString.call( obj ) === "[object Function]";
},
isNullObject : function ( _obj )
{
for ( var m in _obj ) return !1;
return !0;
},
inArray: function ( _E, _A )
{
if ( _A.indexOf ) return _A.indexOf( _E );
for ( var i = 0, length = _A.length; i < length; i++ ) if ( _A[ i ] === _E ) return i;
return -1;
},
toArray : function( _obj )
{
return slice.call( _obj, 0 );
},
mergeArray : function ( _A, _T )
{
var self = this, _T = _T || [];
if ( self.isArray( _A ) ) for ( var i in _A ) if ( self.inArray(_A[i], _T ) == -1 ) _T.push( _A[i] );
return _T;
},
get : function ( _url, _callback, _type )
{
return PAF.ajax({ type: "GET", url: _url, success: _callback, dataType: _type || "text" });
},
post : function ( _url, _callback, _data, _type )
{
return PAF.ajax({ type: "POST", url: _url, success: _callback, data: _data || null, dataType: _type || "text" });
},
ajax : function ( _config )
{
var _default = PAF.extend(
_config || {},
{ "async" : true, "cache" : true, "data" : null, "type" : "GET", "url" : null, "dataType" : 'text', "complete" : noop, "success" : noop, "error" : noop}
);
_default.dataType = _default.dataType.toLowerCase();
_default.type = _default.type.toUpperCase();
if ( _default.dataType == 'jsonp' ) return this.getJSONP( _default.url, _default.complete, 'utf-8' );
var xmlHttpRequest;
try {
xmlHttpRequest = new XMLHttpRequest ();
} catch( a ) {
try {
xmlHttpRequest = new ActiveXObject ( "Msxml2.XMLHTTP" );
} catch( b ) {
try {
xmlHttpRequest = new ActiveXObject ( "Microsoft.XMLHTTP" );
} catch( c ) {
console.error( "XMLHttpRequest 对象创建失败, 您的浏览器似乎不支持异步获取数据!");
return;
}
}
}
if ( !xmlHttpRequest || !_default.url ) return;
if ( _default.cache ) _default.url += ( ( _default.url.indexOf("?") == -1 ) ? "?" : "&" ) + ( "PAF_" + new Date().getTime() + PAF.random(1000, 9999) );
xmlHttpRequest.open( _default.type, _default.url, _default.async );
if ( _default.type == "POST")
{
xmlHttpRequest.setRequestHeader( "If-Modified-Since", "0" );
xmlHttpRequest.setRequestHeader( "Cache-Control", "no-cache" );
xmlHttpRequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
}
xmlHttpRequest.onreadystatechange = function ( )
{
if ( xmlHttpRequest.readyState == 4 )
{
_default.complete.call( this, xmlHttpRequest );
if ( xmlHttpRequest.status == 200 )
{
_default.success.call( this, PAF.parseJSON( xmlHttpRequest.responseText ) );
}
else if ( xmlHttpRequest.status >= 400 )
{
_default.error.call( this, { "errCode": xmlHttpRequest.status, "errInfo" : xmlHttpRequest.statusText } );
}
}
};
xmlHttpRequest.send( _default.data );
},
getJSONP : function ( _url, _callback, _charset )
{
if ( !_url ) return;
var caller = "callback";
var iStart = _url.indexOf( "caller=" );
if ( iStart > -1 )
{
var callerName = _url.substring(iStart).split("=")[ 1 ];
caller = ( callerName || callerName != "?" ) ? callerName : "callback";
}
var box = document.getElementsByTagName("head")[ 0 ];
var jsonScripBox = document.createElement( "script" );
with ( jsonScripBox )
{
type = "text/javascript";
charset = _charset || "utf-8";
src = _url;
onload = function ( )
{
box.removeChild( jsonScripBox );
};
}
window[ caller ] = this.typeOf( _callback, "function" ) ? _callback : new Function ( );
box.appendChild( jsonScripBox );
},
random : function ( a, b )
{
var a = a | 0, b = b | 10;
return ( a + Math.round( Math.random( ) * Math.abs( b - a ) ) );
},
parseJSON : function ( D )
{
var R;
try {
R = (new Function("return " + D) )();
} catch(E) {
try{
R = eval( "("+ D +")" );
} catch ( e ) {
R = D;
}
}
return R;
},
getElementsByClassName: function ( _classNames, _parentElement )
{
var item = ( _parentElement || document.body ).getElementsByTagName("*"), ret=[];
try {
var _classItem = _classNames.trim().replace(/\s+/g, " ").replace(/\./, "").split(" ");
} catch ( e ) {
return [];
}
for ( var x = 0; name = _classItem[x]; x++ )
{
if ( ret.length > 0 ) item = ret, ret=[];
for (var i = 0; e = item[i]; i++) if ( (' ' + ( e.className.trim().replace(/\s+/, ' ') ) + ' ').indexOf( (' ' + name + ' ') ) != -1 ) ret.push( e );
}
return ret;
},
});
var
_isSimple = function ( _tRule, _context )
{
var Or = Expr.isId.test( _tRule ) ? 1 : Expr.isTag.test( _tRule ) ? 2 : Expr.isClass.test( _tRule ) ? 3 : 0 ;
return Or==1 ? [ _context.getElementById( _tRule.substring(1) ) ]:
Or==2 ? PAF.toArray( _context.getElementsByTagName( _tRule ) ):
Or==3 ? slice.call( PAF.getElementsByClassName( _tRule.substring(1), _context ) ) : [];
},
HandleRule = function( _rule, _context )
{
var gRules = _rule.split(', '), result = [];
for ( var i = 0; i < gRules.length; i++ )
{
var CR = gRules[ i ];
if ( CR.indexOf( ' ' ) == -1 && CR.indexOf( '>' ) == -1 )
{
var newSimple = _isSimple( CR, _context );
if ( !result.length )
result = newSimple;
else
for ( var n=0; n < newSimple.length; n++ )
if ( PAF.inArray( newSimple[ n ] , result ) == -1 ) result.push( newSimple[ n ] );
}
else
{
if ( gRules[ i ].indexOf( ' ' ) > -1 )
{
var nParent = [ _context ];
for ( var i=0, gRulesChilds = CR.split(" "), iLen = gRulesChilds.length; i < iLen; i++ )
{
var curRuld = gRulesChilds[i], rParents = [];
for (var p in nParent)
{
var tmpRuleResult = arguments.callee( curRuld, nParent[p] );
if ( tmpRuleResult[0] ) for ( var x in tmpRuleResult ) if ( PAF.inArray( tmpRuleResult[x], rParents ) == -1 ) rParents.push( tmpRuleResult[x] );
}
nParent = rParents;
}
result = nParent;
}
else if ( gRules[ i ].indexOf( '>' ) > -1 )
{
var gRuleNode = gRules[ i ].split('>').reverse();
var target = arguments.callee( gRuleNode.shift().toString(), _context);
for ( var t in target )
{
var tParent = target[ t ].parentElement, isResult = 0;
for ( var r in gRuleNode )
{
var nParents = arguments.callee( gRuleNode[r], _context );
var inArrayIndex = PAF.inArray( tParent, nParents );
if ( inArrayIndex != -1 )
{
tParent = nParents[ inArrayIndex ].parentElement;
isResult = 1;
}
else
{
isResult = 0;
break;
}
}
if ( isResult ) result.push( target[ t ] );
}
}
}
}
return result.length == 0 ? [null] : result ;
},
_runJS = function ( _E )
{
var
C = document.createElement('div'),
S = C.getElementsByTagName('script');
C.innerHTML = _E;
var box = document.getElementsByTagName("head")[0];
for (var i=0, iLen = S.length; i< iLen; i++)
{
var J = document.createElement('script');
J.type = "text/javascript";
if ( S[i].src )
S[i].src = J.src;
else
J.text = S[i].text;
box.removeChild( box.appendChild( J ) );
}
return _E;
}
/* ********************************** */
if ( !document.getElementsByClassName ) document.getElementsByClassName = PAF.getElementsByClassName;
window[ ( _$ != undefined ? "wm" : "P" ) ] = PAF;
/*
* *************************************************************
*
* 这以下的东东东可以根据您自己的需要选择是保留还是删除
*
* *************************************************************
**/
PAF.plu.extend( {
css : function ( _key, _value )
{
var _key = _key, _value = _value, self = this;
if ( !self[0] ) return self;
if ( typeof _value == "string" )
{
self.each( function ( _key, _value )
{
this.style[ _key ] = _value;
}, [ _key, _value ] );
return self;
}
if ( typeof _key == "string" ) return window.getComputedStyle(self[0], null)[ _key ];
if ( typeof _key == "object" )
{
return slef.each( function ( _obj )
{
var styleCss = "";
for ( var k in _obj ) styleCss += ( k + ': ' + _obj[ k ] + '; ' );
this.style.cssText = styleCss;
}, [ _key ] );
}
},
width: function ( _v )
{
return this.css( "width", ( typeof _v == "string" ) ? _v : undefined );
},
height : function ( _v )
{
return this.css( "height",( typeof _v == "string" ) ? _v : undefined );
},
top : function ( _v )
{
return this.css( "top", ( typeof _v == "string" ) ? _v : undefined );
},
left : function ( _v )
{
return this.css( "left", ( typeof _v == "string" ) ? _v : undefined );
},
hide : function ( _T )
{
return this.css( "display", "none" );
},
show : function ( _T )
{
return this.css( "display", "" );
}
} );
PAF.plu.extend( {
addClass: function ( _N )
{
var _N = _N.trim().replace( /\s+/g, " " );
if ( _N )
{
_N = _N.split(" ");
for (var i in _N)
this.each( function ( _N )
{
if ( PAF.inArray( _N, slice.call( this.classList )) == -1 ) this.className += (" " + _N);
}, [ _N[i] ] );
}
return this;
},
className : function ( _N )
{
if ( !_N ) return this[ 0 ].className;
return this.each( function ( _N )
{
this.className = _N;
}, [ _N ] );
},
hasClass : function ( C )
{
if ( C ) for ( var i =0, len=this.length; i<len; i++ ) if ( PAF.inArray( C.replace(/\s+/g, ""), slice.call( this[i].classList ) ) != -1 ) return true;
return false;
},
removeClass : function ( _N )
{
var _N = _N.trim( ).replace( /\s+/g, " " );
if ( _N )
{
_N = _N.split( ' ' );
this.each( function ( _N )
{
var cEle = this;
for ( var n in _N )
{
if ( !cEle.className ) break;
var target = " " + slice.call( cEle.classList ).join(" ") + " ";
for ( var i in _N ) if ( target.indexOf( (" " + _N[ i ] + " ") ) > -1 ) target = target.replace( _N[ i ], "" );
cEle.className = target.trim();
}
}, [ _N ] );
}
return this;
}
} );
PAF.plu.extend({
src : function ( _S )
{
if ( _S != undefined )
{
return this.each( function ( _S )
{
if ( _S != this.src ) this.src = _S;
}, [ _S ] );
}
return this[ 0 ].src;
},
html : function ( E )
{
if ( E != undefined )
{
this.each( function ( E )
{
if ( E != this.innerHTML ) this.innerHTML = E;
}, [ E ] );
_runJS( E );
return this;
}
return this[ 0 ].innerHTML;
},
text : function ( t )
{
if ( t != undefined )
{
this.each( function ( t )
{
if ( t != this.innerText ) this.innerText = t;
}, [ t ] );
return this;
}
return this[ 0 ].innerText;
},
append : function ( E )
{
if ( E != undefined )
{
this.each( function ( E )
{
this.innerHTML += E;
}, [ E ] );
_runJS( E );
}
return this;
},
prepend : function ( E )
{
if ( E != undefined )
{
this.each( function ( E )
{
this.innerHTML = E + this.innerHTML;
}, [ E ] );
_runJS( E );
}
return this;
},
after : function ( E )
{
if ( E != undefined )
{
this.each( function ( E )
{
this.outerHTML = this.outerHTML + E;
}, [ E ] );
_runJS( E );
}
return this;
},
before : function ( E )
{
if ( E != undefined )
{
this.each( function ( E )
{
this.outerHTML = E + this.outerHTML;
}, [ E ] );
_runJS( E );
}
return this;
}
});
PAF.extend(
{
preloadImage : function ( _imageURL, imgElement )
{
var imgObject;
if ( typeof ( _imageURL ) == "string" )
{
imgObject = new Image ( );
imgObject.src = _imageURL;
}
else
{
for ( var i = 0; i < _imageURL.length; i++ )
{
imgObject = imgElement ? imgElement[ i ] : new Image ( );
imgObject.src = _imageURL[ i ];
}
}
},
getRequest : function ( S, K )
{
if ( !S || !K || S.indexOf( K ) == -1 ) return "";
var KLen = K.length, KStart = S.indexOf( K ) + 1;
var KEnd = S.indexOf( "&", KStart )
var P = ( KEnd == -1 ) ? S.substring( KStart ) : S.substring( KStart, KEnd );
return ( P.split('=')[ 1 ] );
},
typeOf : function ( _obj, _type )
{
if ( typeof _type === "string" )
{
var type = _type.toLowerCase( );
var tyEmpty = ( type === "" && _obj === "" ), tyNull = ( type === "null" && _obj === null ), tyUnder = ( type === "undefined" && _obj === undefined ), tyNumb = ( type === "number" && isFinite( _obj ) ), tyObjArr = ( type === Object.prototype.toString.call( _obj ).slice( 8, -1 ).toLowerCase( ) );
return tyEmpty || tyNull || tyUnder || tyNumb || tyObjArr;
}
else
{
if ( _obj === "" ) return "empty";
return Object.prototype.toString.call( _obj ).slice( 8, -1 ).toLowerCase( );
}
},
escapeParam : function ( _href )
{
var filePath = ( _href.match( /^((http|https):\/+((\w+\.)+\w+\/))(\w+(\/|\.\w+))+[\?\#]/g ).toString( ) );
_h = _href.replace( filePath, '' ).replace( /#.*$/, '' );
_h = _h.match( /[^\?\&\#]+=[^\&]*/g );
var ret = '';
for ( var i = 0; i < _h.length; i++ )
ret += _h[i].split("=")[ 0 ] + "=" + escape( _h[i].split("=")[ 1 ] ) + "&";
return filePath + ret.replace( /&$/g, '' );
},
} );
//----------------------
})( window );