⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfx.js

📁 Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是供中,大型企业来管理其发布在互连网
💻 JS
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////////////
//	Cfx.js
//
//	Description:
//		JavaScript Class Framework 
//		(c) 2003 Chris Waldron  -- All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////

var cfxJs = new _js();
var cfxClass = new _class();
var cfxDom = new _dom();
var cfxHtml = new _html();
var cfxTimer = new _stopwatch();

var Cfx = new Object();

Cfx.Js = cfxJs;
Cfx.Class = cfxClass;
Cfx.Dom = cfxDom;
Cfx.Html = cfxHtml;
Cfx.Timer = cfxTimer;

//////////////////////////////////////////////////////////////////////////////
// Function extensions.
//////////////////////////////////////////////////////////////////////////////
Function.prototype.className = "Function";
Function.prototype.baseClass = null;
Function.prototype.instances = false;
Function.prototype.Method = function( func )
{
    this.prototype[cfxJs.FunctionName( func )] = func;
    return this;
};

//////////////////////////////////////////////////////////////////////////////
// Object extensions.
//////////////////////////////////////////////////////////////////////////////
Object.prototype.className = "Object";
Object.prototype.baseClass = null;
Object.prototype.InstanceOf = cfxClass.InstanceOf;
Object.prototype.InitInstance = function()
{
	// Initialize the instance.
	for ( name in this.constructor )
	{
		var temp = name;
		var value = this.constructor[name];
		this[name] = this.constructor[name];
	}
	// Perserve instance if requested.
	if ( this.constructor.instances == true )
		this.constructor.instances = new Array();
	
	if ( Cfx.Js.IsArray( this.constructor.instances ) )
		this.constructor.instances[this.constructor.instances.length] = this;
};


//////////////////////////////////////////////////////////////////////////////
// Date extensions.
//////////////////////////////////////////////////////////////////////////////
Date.prototype.className = "Date";
Date.prototype.baseClass = Object;


//////////////////////////////////////////////////////////////////////////////
// Error extensions.
//////////////////////////////////////////////////////////////////////////////

Error.prototype.className = "Error";
Error.prototype.baseClass = Object;


//////////////////////////////////////////////////////////////////////////////
// RegExp extensions.
//////////////////////////////////////////////////////////////////////////////
RegExp.prototype.className = "RegExp";
RegExp.prototype.baseClass = Object;


//////////////////////////////////////////////////////////////////////////////
// String extensions.
//////////////////////////////////////////////////////////////////////////////
String.prototype.className = "String";
String.prototype.baseClass = Object;
String.prototype.trim = function()
{
	// Remove leading and trailing whitespace.
	return this.replace( /^\s+/, "" ).replace( /\s+$/, "" );
}
String.prototype.normalizeSpace = function() {
    // Replace repeated spaces, newlines and tabs with a single space
    return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}

String.prototype.startsWith = function(str) { 
  return this.substr(0, str.length) == str;
}

String.prototype.endsWith = function(str) {
  if(str.length > this.length) return false;
  return this.substring(this.length-str.length) == str;
}

//////////////////////////////////////////////////////////////////////////////
//	Js
//////////////////////////////////////////////////////////////////////////////
function _js()
{
	// Naming functions.
	_js.prototype.FunctionName = FunctionName;
	_js.prototype.ObjectName = ObjectName;

	// Type identity function.
	_js.prototype.IsArray = IsArray;
	_js.prototype.IsDefined = IsDefined;
	_js.prototype.IsEmpty = IsEmpty;
	_js.prototype.IsFunction = IsFunction;
	_js.prototype.IsNull = IsNull;
	_js.prototype.IsNumeric = IsNumeric;
	_js.prototype.IsObject = IsObject;
	_js.prototype.IsString = IsString;
	_js.prototype.IsBoolean = IsBoolean;

	// Conversion functions.
	_js.prototype.ToFunction = ToFunction;
	
	//////////////////////////////////////////////////////////////////////////////
	//	IsArray( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsArray( obj )
	{
		return IsObject( obj ) && obj.constructor == Array;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	IsDefined( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsDefined( obj )
	{
	    return typeof( obj ) == 'undefined' ? false : true;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	IsEmpty( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsEmpty( obj )
	{
	    if(!IsDefined(obj) || IsNull(obj)) {
            return true;
        }
        else if((IsString(obj) && obj.normalizeSpace(obj).length > 0) || IsNumeric(obj)) {
            return false;
        }
        else if( IsObject( obj ) ) {
			    for ( var item in obj )
			    {
				    return false;
          }
        }
        return true;
	}

	//////////////////////////////////////////////////////////////////////////////
	//	IsFunction( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsFunction( obj )
	{
		return typeof( obj ) == 'function';
	}


	//////////////////////////////////////////////////////////////////////////////
	//	IsNull( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsNull( obj )
	{
		return IsObject( obj ) && ( obj == null );
	}


	//////////////////////////////////////////////////////////////////////////////
	//	IsNumeric( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsNumeric( obj )
	{
	    return typeof( obj ) == 'number' && isFinite( obj );
	}

  function IsBoolean(obj) {
      return typeof(obj) == 'boolean';
  }

	//////////////////////////////////////////////////////////////////////////////
	//	IsObject( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsObject( obj )
	{
		return typeof( obj ) == 'object';
	}


	//////////////////////////////////////////////////////////////////////////////
	//	IsString( obj )
	//////////////////////////////////////////////////////////////////////////////
	function IsString( obj )
	{
		if(typeof( obj ) == 'string') {
			return true;
		}
		else if (!obj) {
            return false;
		} 
		else if(IsObject(obj) && obj.toLowerCase) {
		    return true;
		}
		return false;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	ObjectName( obj )
	//////////////////////////////////////////////////////////////////////////////
	function ObjectName( obj )
	{
		var objName = null;
		
		if ( IsObject( obj ) )
		{
			var objName = null;
			var strFunc = obj.constructor.toString();
			var reFuncName = new RegExp( /function\s+(\w+).*/g );
			var result = reFuncName.exec( strFunc );
			objName = result[1];
		}		
		
		return objName;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	FunctionName( func )
	//////////////////////////////////////////////////////////////////////////////
	function FunctionName( func )
	{
		var funcName = null;
		if ( IsFunction( func ) )
		{
			var strFunc = func.toString();
			var reFuncName = new RegExp( /function\s+(\w+).*/g );
			var result = reFuncName.exec( strFunc );
			funcName = result[1];
		}
		
		return funcName;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	ToFunction( obj )
	//////////////////////////////////////////////////////////////////////////////
	function ToFunction( obj )
	{
		if ( IsObject( obj ) )
			return obj.constructor;
		else if ( IsFunction( obj ) )
			return obj;
		else
			return null;
	}
}


//////////////////////////////////////////////////////////////////////////////
//	Class
//////////////////////////////////////////////////////////////////////////////
function _class()
{
	_class.prototype.New = New;
	_class.prototype.GetName = GetName;
	_class.prototype.IsDefined = IsDefined;
	_class.prototype.IsInitializing = IsInitializing;
	_class.prototype.InstanceOf = InstanceOf;
	_class.prototype.Reflect = Reflect;
	_class.initializing = true;
	return;


	//////////////////////////////////////////////////////////////////////////////
	//	GetName( classArg )
	//////////////////////////////////////////////////////////////////////////////
	function GetName( classArg )
	{
		var className;

		if ( arguments.length == 0 )
			classArg = this;
	
		if ( cfxJs.IsObject( classArg ) )
			className = cfxJs.ObjectName( classArg );
		else if ( cfxJs.IsFunction( classArg ) )
			className = cfxJs.FunctionName( classArg );
		else
			className = classArg;
			
		return className;
	}
	

	//////////////////////////////////////////////////////////////////////////////
	//	IsDefined( classArg )
	//////////////////////////////////////////////////////////////////////////////
	function IsDefined( thisClass )
	{
		if ( thisClass == Object )
			return true;
		else if ( thisClass.baseClass == null )
			return false;
		else
			return thisClass.className != _class.initializing ? true : false; 
	}


	//////////////////////////////////////////////////////////////////////////////
	//	IsInitializing( thisClass )
	//////////////////////////////////////////////////////////////////////////////
	function IsInitializing( thisClass )
	{
		// A class is initializing when the associated prototype is set to true.
		return thisClass.className == _class.initializing ? true : false;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	InstanceOf( thisClass, targClass )
	//////////////////////////////////////////////////////////////////////////////
	function InstanceOf( thisClass, targClass )
	{
		var retval = false;

		// Set up arguments.
		if ( arguments.length == 1 )
		{
			targClass = thisClass;
			thisClass = cfxJs.ToFunction( this );
		}
		
		// Convert arguments to functions.
		thisClass = cfxJs.ToFunction( thisClass );
		targClass = cfxJs.ToFunction( targClass );

		// Validate arguments.
		if ( !cfxJs.IsDefined( thisClass ) )
			throw new Error( "Source class is undefined" );
		else if ( !cfxJs.IsDefined( targClass ) )
			throw new Error( "Target class is undefined" );

		// Find instance of.
		if ( thisClass.className == targClass.className )
			retval = true;
		else if ( targClass == Object )
			retval = true;
		else if ( thisClass.baseClass == Object )
			return false;
		else if ( !cfxJs.IsArray( thisClass.baseClass ) )
			retval = cfxClass.InstanceOf( thisClass.baseClass, targClass );
		else 
		{
			var baseClasses = thisClass.baseClass;
			for ( var ii = 0; ii < baseClasses.length; ++ii )
			{
				retval = cfxClass.InstanceOf( baseClasses[ii], targClass );
				if ( retval == true )
					break;
			}
		}
			
		// Return result.		
		return retval;
	}


	//////////////////////////////////////////////////////////////////////////////
	//	New( thisClass )
	//////////////////////////////////////////////////////////////////////////////
	function New( thisClass )
	{
		// Exit if class is initializing.
		if ( cfxClass.IsInitializing( thisClass ) == true )
			return;
			
			var c = cfxClass.GetName( thisClass );
			
			//thisClass.prototype = Object;
		// Inherit from base classes.
		thisClass.prototype['dummy']='dummy';
		for ( var ii = arguments.length - 1; ii > 0; --ii )
		{
			// Create base class object.
			var baseClass = new arguments[ii];
			
			// Inherit base class methods.
			for ( name in baseClass )
				thisClass.prototype[name] = baseClass[name];
	
			var proto = thisClass.prototype;
			// Inherit base class properties.
			for ( name in baseClass.constructor )
			{
				if ( !cfxJs.IsFunction( baseClass.constructor[name] ) )
					thisClass[name] = baseClass.constructor[name];
			}

			// Setup base class designator used by InstanceOf
			if ( ( arguments.length == 2 ) && ( ii == 1 ) )
			{
				// Set up single inheritance.
				thisClass.baseClass = baseClass;
			}
			else
			{
				// Setup multiple inheritance.
				if ( !cfxJs.IsArray( thisClass.baseClass ) )
					thisClass.baseClass = new Array();

				thisClass.baseClass[ii-1] = baseClass;
			}
			var proto2 = thisClass.prototype;
			thisClass.prototype=proto;
		}
		
		var TC_p = thisClass.prototype;
		if(thisClass.baseClass!=null)
			var TC_bc = cfxClass.GetName(thisClass.baseClass);
		
		for( var c_name in thisClass )
			var n_name = c_name;
		
		// Initialize the class and return.
		thisClass.className = _class.initializing;
		var self = new thisClass;
		thisClass.className = cfxClass.GetName( thisClass );
		if ( thisClass.baseClass == null ) thisClass.baseClass = Object;
		return;
	}
	
	
	//////////////////////////////////////////////////////////////////////////////
	//	Reflect( thisClass )
	//////////////////////////////////////////////////////////////////////////////
	function Reflect( thisClass )
	{
		var className = cfxJs.GetName( thisClass );

		for ( name in thisClass.prototype )
			alert( className + ".prototype." + name );

		for ( name in thisClass )
			alert( className + "." + name );

	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -