Updates and miscellaneous enhancements and optimizations

master
Steve L. Nyemba 10 years ago
parent 1bfbcbe7f5
commit 39ff5c7e1b

@ -14,51 +14,7 @@
if(!jx){
var jx = {}
}
//jx.ajax = {
// /**
// * In order to make the ajax requests domain specific we allow users to get an instance while making a global implementation available
// * This is useful for oauth applications that require domain specific headers to be set upon issuing a request
// */
// getInstance:function(){
// var obj = {} ;
// obj.headers = [] ;
// obj.setHeader = function(key,value){
// this.headers.push({'key':key,'value':value}) ;
// }
// obj.async = true;
// obj.send = function(url,callback,method){
// var xmlhttp = new XMLHttpRequest() ;
// method = (method==null)?'GET':method ;
// xmlhttp.onreadystatechange = function(){
// if(xmlhttp.readyState == 4){
// callback(xmlhttp) ;
// }
// }//-- end of Inline function
// var key,value ;
// //
// // async=false has been deprecated (browsers complain a lot)
// xmlhttp.open(method,url,this.async) ;
//
// if(obj.headers.length > 0){
// for(var i=0; i < obj.headers.length; i++){
// key = obj.headers[i]['key'] ;
// value= obj.headers[i]['value'];
// //air.trace(key+'='+value)
//
// if(key != null && value != null){
// xmlhttp.setRequestHeader(key,value) ;
// }
// }
// }
// xmlhttp.send(null) ;
// }
// return obj;
//
// },//-- end jx.ajax.getInstance
// parser:null
// }//--end jx.ajax
/**
/**
* These are a few parsers that can come in handy:
* urlparser: This parser is intended to break down a url parameter string in key,value pairs
*/
@ -92,7 +48,6 @@ function urlparser(url){
*/
jx.ajax = {}
jx.ajax.get = {} ;
jx.ajax.get.instance = jx.ajax.getInstance ;
jx.ajax.debug = null;
jx.ajax.get.instance = function(){
var factory = function(){
@ -160,3 +115,7 @@ jx.ajax.get.instance = function(){
}//-- end of the factory method
return new factory() ;
}
//
// backward compatibility
jx.ajax.getInstance = jx.ajax.get.instance ;

@ -1,10 +1,18 @@
/**
* Steve L. Nyemba <steve@the-phi.com>
* jxf version 0.9
* This file contains the compilation of utilities for miscellaneous/unsorted operations:
* casting
* vector extraction from associative array
* list of keys from an associative array
* Simple Javascript eXtension - 1.0
* (c) 2014 - 2015 Steve L. Nyemba, steve@the-phi.com
* License GPL version 3.0
*
* Implementation of miscellaneous utilities commonly used, These functions are reusable and simple:
* jx.utils.vector extracts a vector from an array of objects (or a matrix)
* jx.utils.keys extract keys from an associative array
* jx.utils.unique returns unique objects in an array, including array of objects (provided an key function)
*
* jx.utils.patterns:
* Implementation of design patterns defined by the GOF http://en.wikipedia.org/wiki/Software_design_pattern
* jx.utils.patterns.visitor The visitor design pattern
* jx.utils.patterns.iterator The iterator design pattern
* jx.utils.patterns.observer The observer design pattern
*/
if(!jx){
@ -58,49 +66,43 @@ jx.utils.keys=function(rec){
* This function will returnt he unique elements of a list
* @param list list of elements (duplicates are expected)
*/
jx.utils.unique = function (list,key){
jx.utils.unique = function (list,getKey){
var obj = {}
for(var i=0; i < list.length; i++){
if(list[i].constructor.name == Object){
id = list[i][key] ;
obj[id] = list[i] ;
if(list[i].constructor == Object && getKey != null){
var key = getKey(list[i]) ;
obj[key] = list[i] ;
}else{
obj[list[i]]= 1 ;
}
}
if (list[0].constructor.name == Object){
if(getKey == null){
return jx.utils.keys(obj);
}else{
//
// In case we have a complex object we can use a utility function and a design pattern to address the issue
// @TODO: This function can be used in the math library considering either a key or a function to return some form of representation of the record ... perhaps a hash?
// This will return the unique list of objects, provided the user has given a key extraction function
// The key extraction function is analogous to the equal operator in C++
//
return jx.utils.patterns.visitor(jx.utils.keys(obj),function(id){
return obj[id] ;
}) ;
}else{
return jx.utils.keys(obj);
})
}
}
/**
* Implementation of a few standard design patterns. Their use is user/dependent
* For more information on how/when to use a design pattern please use google/wikipedia ;-)
* We have implemented:
* - Iterator design pattern
* - Visitor design pattern
* - Observer design pattern
* @TODO: Consider adding these patterns to events associated with DOMS (Other kind of utility function)
*/
jx.utils.patterns = {}
jx.utils.patterns.visitor = function(list,pointer){
rlist = [] ;
var rlist = [] ;
for(var i=0; i < list.length; i++){
value = pointer(list[i]) ;
if(value != null){
rlist.push(value) ;
}
}
return (rlist.length > 0)?rlist:list;
return (rlist.length > 0)?rlist:[];
}
/**
* Implementation of an iterator design pattern: i.e we use a sequence of objects and call a given method on them
@ -118,7 +120,7 @@ jx.utils.patterns.iterator = function(list,pointer){
* @param init pointer to be called on each observer to trigger it
*/
jx.utils.patterns.observer = function(lobservers,init){
p = {} ; //-- specification of the subject
var p = {} ; //-- specification of the subject
p.index = 0;
p.nodes = lobservers ;
p.pointer = init;
@ -131,10 +133,18 @@ jx.utils.patterns.observer = function(lobservers,init){
}
}
p.start = function(){
observer = this.nodes[this.index];
var observer = this.nodes[this.index];
try{
observer[this.pointer](this) ;
++this.index;
if(this.pointer.constructor == String){
observer[this.pointer](this) ;
}else{
this.pointer(observer);
this.notify() ;
}
}catch(e){
//
// if an exception was thrown, chances are were unable to increment and call notify
@ -159,46 +169,3 @@ jx.utils.patterns.observer = function(lobservers,init){
* @return array containing casted type
*/
jx.utils.cast = jx.utils.patterns.visitor ;
/**
* Print a dom object to system defined printer (Physical; Network or File)
* @param id id of a DOM object
*/
jx.utils.print = function(id){}
/**
* The following namespace handles searches in depth & in breath.
*/
jx.utils.search = function(id,keywords,attrib){
var lattr = null;
attrib = (attrib == null)?['innerHTML','value']:attrib;
if(attrib.constructor == Array){
lattr = attrib;
}else{
lattr = [attrib] ;
}
regex = keywords.toLowerCase();
ldoms = jx.dom.get.children(id) ;
//ldoms.push(document.getElementById(id)) ;
var imatch = function(_dom){
obj = null;
for(var j=0; j < lattr.length; j++){
id = lattr[j] ;
str = _dom[id] ;
str = (str != null)?str.toLowerCase():str;
if(str == null){
continue;
}else if(str.match(regex) != null){
obj = _dom.cloneNode(true) ;
}
}
return obj;
}
lmatches = jx.utils.patterns.visitor(ldoms,imatch)
return lmatches ;
}
//module.exports.utils = jx.utils ;

Loading…
Cancel
Save