ESOE Version: 0.1
Version : 0.2
By Feng WeiGuo / forxm@21cn.com
2006-6
This article describes an implementation for ESOE V0.1, in javascript language.
ns
|
name space string, ex. “MyPkg.sub.Class3”
|
nsx
|
name space string or a constructor/function
|
func
|
abbr. function/constructor
|
pkg
|
abbr. package
|
name
|
a String object
|
ESOE layout include 3 parts,
1. Load ESOE engine, load parameters.
2. If any libraries are required, load them. It's optional for lv2.
3. If any libraries and classes are required, initialize all of them.
NOTE: ESOE layout is strongly required to be independent from other user codes, although it's not necessary in some environment.
Example for javasript in html page:
......
<script src="../esoe.js" level="2" debug="1"></script>
//load ESOE engine
<script src="../net/eae/webfx/xtree/xtree.js"></script>
//load libraries
<script>
//initialize packages and classes
_packagepath("com","../");
_import("net.eae.webfx.xtree.WebFXTree");
_import("net.eae.webfx.xtree.WebFXTreeItem");
_import("com.jsvm.js.lang.StringBuffer" , "myapp.StringBuffer");
_update ();
</script>
//ESOE layout end here
<script>......</script>
//separated from other user code
......
<script>
//user code
......
var obTree= new WebFXTree('name1');
var sb= new myapp.StringBuffer;
......
</script >
Attribute
|
Description
|
src
|
ESOE engine lv1 file
|
level
|
Default 1
|
debug
|
Default 0
|
nokey
|
Default ""; separate by ","; if "*" all keywords forbid
|
libpath (lv2)
|
Default library path, only one path can be set. If not set, use _path as _libpath
|
libmode (lv2)
|
0: do not automatically load library or class file (default).
1: load package library firstly, then class file.
2: load class file firstly, then package library.
3: load package library only
4: load class file only
|
1. Prepare package namespace.
2. Create constructor and initializing process for the instance.
3. Create constructor’s _declare() process.
3.1 Import external classes
3.2 Call _normalize() for class without base class, or call _derive().
3.3 Add class property and method to the prototype of the class.
3.4 If _declare success, return true, else return false.
Example:
_package("MyPkg.sub");
// Prepare package namespace
$esoe.ns.MyPkg.sub.Class3= function(){}
// Create constructor
$esoe.ns.MyPkg.sub.Class3._declare= function()
// Create constructor’s _declare()
{
var ns={}; // Import external classes
if( !_import("MyPkg.Class5","Class5",ns) ) return false;
if( !_derive("MyPkg.sub.Class3","MyPkg.Class2") ) //derive or normalize
return false;
with($esoe.ns.MyPkg.sub.Class3) //add property to the prototype
{
prototype.p1= new ns.Class5;
prototype.func1= function() {}
}
return true; //return
}
1. Add wrap header function to enclose the original source.
2. Prepare the external non-esoe class and global object.
3. Replace quoting "...[class/object name]..." to "...$esoe.ns.[packages].[ class/object name]..."
4. Add wrap end, normalize class and global objects.
5. Call the wrap function.
Note 1:
Normalizing non-esoe class can not automatically load any classes. It's mainly a level 1 implementation. So when preparing an external non-esoe class, create a variable with the same name of the external class in the wrap function.
Note 2:
Replacing the quoting item is mostly for normalizing non-esoe global object.
Example:
function _wrap_webfx_WebFXCheckBoxTreeItem()
// wrap header function
{
//prepare external class and global object
var WebFXTreeItem= $esoe.ns.webfx.xtree.WebFXTreeItem;
var webFXTreeConfig= $esoe.ns.webfx.xtree.webFXTreeConfig;
//wrap-header end
//original source start
...
//replace the quoting string for global object
str += "οndblclick=/"$esoe.ns.webfx.xtree.webFXTreeConfig.set(this)/";";
...
//original source end
//wrap-end start, normalize function and object
$esoe.normalizeobject( " webfx.xtree.webFXTreeHandler", webFXTreeHandler );
_normalize( " webfx.xtree.WebFXCheckBoxTreeItem", WebFXCheckBoxTreeItem, null, 1 );
}
_wrap_webfx_WebFXCheckBoxTreeItem();
//call wrap function
_package= function( ns )
Define/create a package namespace.
ns: name space string
Ex:
_package (“MyPkg.sub.Class3”)
_normalize= function( ns, func, nsxBase, bNonEsoe, bConflict )
Load a constructor to a name space item
ns: name space string
func: constructor/function, can be omitted if the func has mapped to the ns.
nsxBase: constructor/function for base class, optional.
bNonEsoe: set 1 if normalizing a non-esoe function.
bConflict: set 1 if any keyword of “_pkg” or “_base” conflict.
Ex:
If( !_normalize("MyPkg.Class1") ) return false; // normalize esoe style class
_normalize ( CodeUtil, "com.homolo.ide.tools.CodeUtil",null,1 );
// normalize non-esoe style class
$esoe.normalizeobject= function( ns, obj, bConflict )
Normalize a non-esoe object
ns: name space string
obj: an object
bConflict: set 1 if any keyword of “_pkg” or “_base” conflict.
Ex:
$esoe.normalizeobject ( webFXTreeConfig, "net.eae.webfx.xtree.webFXTreeConfig" );
_derive= function( ns, nsBase, argument1, ..., argumentn )
Derive a class from a base class
ns: name space string
nsBase: name space string for base class
arguments: arguments for base class
Ex:
if( !_derive("MyPkg.sub.Class3","MyPkg.Class2") ) return false;
_import= function( ns, nsMap, obj )
Let constructor create its class and map it to a name space.
ns: name space string
nsMap: name space string for mapping to.
obj: the object to map into, if not set, map to the GLOBAL object/window.
Note:
If nsMap is set to null and obj is not set, just load the ns and don’t map it to a name space item.
If nsMap is not set or set to "", use the last item of ns as nsMap.
Ex:
_import("MyPkg.sub.Class3"); //map to window.Class3
_import("MyPkg.sub.Class3","MyClass"); //map to window.MyClass
_import("MyPkg.sub.Class3","myapp.MyClass"); //map to window.myapp.MyClass
var ns={};
_import("MyPkg.sub.Class3","",ns); //map to ns.Class3
_packagepath= function( ns, strPath )
Set the package path
ns: name space string
strPath: path name string
Ex:
_packagepath("com","../lib/" );
_packagepath("com.mylib","../lib2/" );
// "com.xtree.WebFXTree" will be loaded from ../lib/com/xtree/WebFXTree
// "com.mylib.class1" will be loaded from ../lib2/ mylib/class1
_update= function()
If package loading need recursive process, call this to active it.
Note:
_update must be at the last place of a <script> tag enclosure.
Ex:
<script>
...
_update();
</script>
_base property is the prototype of the base class.
An esoe class/constructor or its instance can use _base to get the base class property.
Ex:
alert( obj1._base.p1 );
obj1._base.func1();
_pkg property is the nearest upper package name space item.
An esoe constructor or its instance can use _pkg to get the reference to its nearest upper package name space item.
Ex:
var a=new this._pkg.Class5;
$esoe.GetPkgPath= function( ns )
Get package path.
ns: name space string
Ex:
WebFXTree.prototype.setXTreePath($esoe.GetPkgPath("net.eae.webfx.xtree"));
$esoe.Include= function( src )
Include external javascript file
src: external javascript file path
Ex:
$esoe.Include($esoe.GetPkgPath("net.eae.webfx.xtree")+"xtree.js");
$esoe.AddCss= function( src )
Add CSS stylesheet file
src: CSS stylesheet file path
Ex:
$esoe.AddCss($esoe.GetPkgPath("net.eae.webfx.xtree")+"xtree.css");