bytefw.js

Byte FrameWorkJavaScript Performances and Compatibility in a couple of bytes

Author

Andrea Giammarchi, http://www.3site.eu

License

Copyright (C) 2007 Andrea Giammarchi - www.3site.eu

Permission is hereby granted, free of charge,
to any person obtaining a copy of this software and associated
documentation files (the "Software"),
to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation Credits

Summary
The base object of the byte framework.
browser compatibility based on Internet Explorer versions.
browser version that can use every object properties and methods.
object, class, functions or namespaces dependencies
global objects modified/added prototypes
method to get generic browser version.
runtime JavaScript execution, used to evaluate code using global/window scope
add an extra utility, component or something else inside a dedicated bytefw namespace.
method to get Adobe flash player version.
method to initialize bytefw enviroment (add prototypes if these are not yet implemented by browser or other libraries)
returns used language
verify if a generica variable is an instance of a generic class or is a valid type.
Python range like function.
try to find the constructor name of a generic variable.
contains some primitive vars prototype and other useful method.
bytefw utilities object.
bytefw utilities object.

bytefw

bytefw

The base object of the byte framework.

Properties

Summary
browser compatibility based on Internet Explorer versions.
browser version that can use every object properties and methods.
object, class, functions or namespaces dependencies
global objects modified/added prototypes

COMPATIBILITY

browser compatibility based on Internet Explorer versions.

Returns

String”ie5”, “ie5.5”, “ie6” “ie5.5” means that Internet Explorer version 5 should include this script but can’t use them.  Every other browser (at least every last version) can read and use this object.

REALCOMPATIBILITY

browser version that can use every object properties and methods.

Returns

String . “ie5”, “ie5.5”, “ie6” if compatibility is “ie5” but REALCOMPATIBILITY is “ie5.5” it means that Internet Explorer version 5 should include this script but can use only some method or property.  Every other browser (at least every last version) should read and use this code without problems.

DEPENDENCIES

object, class, functions or namespaces dependencies

Returns

Objectevery other files dependencies ordered by priority.

Example

for(var        key in self.DEPENDENCIES)
alert("this object depends on " key + ": " + self.DEPENDENCIES[key].join(" - "));

PROTOTYPED

global objects modified/added prototypes

Returns

Objectevery key as description and relative value as prototyped function.

Example

for(var key in self.PROTOTYPED)
alert(key + " prototype has been modified with this function: " + self.PROTOTYPED[key]);

Methods

Summary
method to get generic browser version.
runtime JavaScript execution, used to evaluate code using global/window scope
add an extra utility, component or something else inside a dedicated bytefw namespace.
method to get Adobe flash player version.
method to initialize bytefw enviroment (add prototypes if these are not yet implemented by browser or other libraries)
returns used language
verify if a generica variable is an instance of a generic class or is a valid type.
Python range like function.
try to find the constructor name of a generic variable.

browser

browser:function()

method to get generic browser version.

Returns

Stringsafari or khtml or gecko or operaN, where N is the version, or ieN, where N is the integer or float version

Example

var browser = bytefw.browser();
if(/ie/.test(browser)) {
// this browser seems to be IE (or IE compatible)
if(/ie5/.test(browser))
doie5();
else
// I have a dedicated function for every other ie
doie();
}
else
// common standard browser functions
(/opera9/.test(browser) || !/opera/.test(browser) ? domore() : doOldOpera());

exec

exec:window.execScript || function(code)

runtime JavaScript execution, used to evaluate code using global/window scope

Arguments

StringJavaScript valid source code to execute

Example

// simple function with its private scope
function evalScript(code){

// evaluation doesn't use a global scope
// while exec method will be evaluated as global
bytefw.exec(code);
};

evalScript("function Global(){alert(arguments[0])}");
Global("Hello"); // Hello

extend

extend:function(namespace,
func,
init)

add an extra utility, component or something else inside a dedicated bytefw namespace.  With this method You could extend core objects, create your own components or create your dedicated namespace.

Arguments

Stringrelative namespace path starting automatically from bytefw.  It can contain object name if second argument isn’t a function.
Objecta new object or a new function.  In first case first argument must contain object name too.
Functionoptional callback to call on extend operation complete

Example

// how to add a regexp object inside core
bytefw.extend("core.regexp", {
$:function(re){
return re.value;
}
});

// how to add an utility called DoStuff
bytefw.extend("utils", function DoStuff(p1, p2, pN) {
function calc(){
return Math.round(p1/p2) * pN;
};
this.doSomething = function(value){
return calc() + value;
};
});
// Note: if you add a function you don't need to specify
// its name on first parameter. It will be find automatically
// from bytefw.create method

// how to add some class and use them after extend operation
bytefw.extend("utils",
function PersonalFX(name){
this.name = name;
this.moveSoft = function(element){
new Tween(element, "x", 100, 500, 2500);
};
},
function(self){
self.prototype = new bytefw.utils.Fx;
self.COMPATIBILITY = "all";
self.REALCOMPATIBILITY = "ie5.5";
self.DEPENDENCIES = ["Dom", "Fx", "Transitions", "Tween"];
self.PROTOTYPED = {};
self.VERSION = "0.1";
}
);

// now You could use them, for exampe, in this way
var PersonalFX = bytefw.utils.PersonalFX;
var MyFX = new PersonalFX("my personal bytefw component");


// if You're not sure about namespace ...
bytefw.extend("mx" bytefw.mx || {});
bytefw.extend("mx", function LoadVars(){
this.load = function(uri){
var request = new XMLHttpRequest;
request.open("get", uri, false);
return request.responseText;
};
});

// in another script ...
var lv = new bytefw.mx.LoadVars;
alert(lv.load("mypge.html"));

flash

flash:function()

method to get Adobe flash player version.

Returns

Numberan unsigned integer with generic Adobe flash player version or 0.

Note

integer value is not full version, just the generic release (4 or 5 or 6 or 7 or 8 or 9 or others)

Example

var flash = bytefw.flash();
if(flash && flash > 6) {
// client supports swf created with flash Mx
createSWFContent();
};

init

init:function(callback)

method to initialize bytefw enviroment (add prototypes if these are not yet implemented by browser or other libraries)

Arguments

Functionoptional callback to call after byte framework initializzation.

Note

If You use one or more bytefw tilities this method will be called automatically.

Example

<script type="text/javascript" src="bytefw.js"><!--// byte framework [with core functions] //--></script>
<script type="text/javascript"><!--//
bytefw.init(function(){
[1,2,3].forEach(function(number){
alert(number);
});
});
// alert 1, then 2 and finally 3 with every browser
//--></script>

lang

lang:function()

returns used language

Returns

String”en”, “it” or used language

is_a

is_a:function(tmp,
type)

verify if a generica variable is an instance of a generic class or is a valid type.

Arguments

Objectgeneric object variable
Stringgeneric class name or mail or uri to verify if first argument (in these case requires a string) is a valid email or a valid uri.

Returns

Booleantrue if variable is specified type

Example

if(bytefw.is_a("test@me.it", "mail"))
alert("string is a valid email address");
else if(bytefw.is_a("www.3site.eu", "uri") || bytefw.is_a("http://3site.eu", "uri"))
alert("string is a valid uri");
else if(bytefw.is_a("", "String"))
alert("param is a string");
else if(bytefw.is_a(new Date, "Date"))
alert("param is a Date");

range

range:function(min,
max)

Python range like function.

Arguments

Numbermax range integer value, not included, or min value, included, if second argument exists
[Number]optional second integer value as max value

Returns

Objectobject with integer keys (between min and max values - 1)

Example

range = bytefw.range;

// write 0,1,2,3,4,5,6,7,8,9
for(i in range(10))
document.write(i);

// looping array
arr = ["a", "b", "c"];
for(i in range(arr.length))
alert(arr[i]);
// alert a, then b and finally c

type

type:function(tmp)

try to find the constructor name of a generic variable.

Arguments

Objectgeneric object variable

Returns

Stringthe name of variable constructor

Note

returns “anonymous” if argument[0] is an anonymous function

Example

alert(bytefw.type(new Date)); // "Date"

Namespaces

Summary
contains some primitive vars prototype and other useful method.
bytefw utilities object.
bytefw utilities object.

core

contains some primitive vars prototype and other useful method.

Returns

Objectsome useful initializzation function

utils

bytefw utilities object.  Contains every extra bytefw utility.  An utility could be a common, useful function, object or class, based (or not) on core and other utilities (not components or widgets)

Returns

Objectnamespace for bytefw utiltities

comps

bytefw utilities object.  Contains every extra bytefw component.  A component could be, for example, a complete widget, based (or not) on utilities

Returns

Objectnamespace for bytefw complete components
bytefw
The base object of the byte framework.
browser:function()
method to get generic browser version.
exec:window.execScript || function(code)
runtime JavaScript execution, used to evaluate code using global/window scope
extend:function(namespace,
func,
init)
add an extra utility, component or something else inside a dedicated bytefw namespace.
flash:function()
method to get Adobe flash player version.
init:function(callback)
method to initialize bytefw enviroment (add prototypes if these are not yet implemented by browser or other libraries)
lang:function()
returns used language
is_a:function(tmp,
type)
verify if a generica variable is an instance of a generic class or is a valid type.
range:function(min,
max)
Python range like function.
type:function(tmp)
try to find the constructor name of a generic variable.