Subversion Repositories wimsdev

Rev

Rev 20 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.         DynAPI Distribution
  3.         DynObject, DynAPI Object, UserAgent, Library, Functions
  4.  
  5.         The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6. */
  7.  
  8. function DynObject() {
  9.         this.id = "DynObject"+DynObject._c++;
  10.         DynObject.all[this.id] = this;
  11. };
  12. var p = DynObject.prototype;
  13. p.getClassName = function() {return this._className};
  14. p.getClass = function() {return dynapi.frame[this._className]};
  15. p.isClass = function(n) {return DynObject.isClass(this._className,n)};
  16. p.addMethod = function(n,fn) {this[n] = fn};
  17. p.removeMethod = function(n) {this[n] = null};
  18. p.setID = function(id,isInline,noImports) {
  19.         if (this.id) delete DynObject.all[this.id];
  20.         this.id = id;
  21.         this.isInline=isInline;
  22.         this._noInlineValues=noImports;
  23.         DynObject.all[this.id] = this;
  24. };
  25. p.toString = function() {return "DynObject.all."+this.id};
  26. DynObject.all = [];
  27. DynObject._c = 0;
  28. DynObject.isClass = function(cn,n) {
  29.         if (cn == n) return true;
  30.         else {
  31.                 var c = dynapi.frame[cn];
  32.                 var p = c.prototype._pClassName;
  33.                 if (p) return DynObject.isClass(p,n);
  34.                 else return false;
  35.         }
  36. };
  37.  
  38. function _UserAgent() {
  39.         var b = navigator.appName;
  40.         var v = this.version = navigator.appVersion;
  41.         var ua = navigator.userAgent.toLowerCase();    
  42.         this.v = parseInt(v);
  43.         this.safari = ua.indexOf("safari")>-1;  // always check for safari & opera
  44.         this.opera = ua.indexOf("opera")>-1;    // before ns or ie
  45.         this.ns = !this.opera && !this.safari && (b=="Netscape");
  46.         this.ie = !this.opera && (b=="Microsoft Internet Explorer");
  47.         this.gecko = ua.indexOf('gecko')>-1; // check for gecko engine
  48.         if (this.ns) {
  49.                 this.ns4 = (this.v==4);
  50.                 this.ns6 = (this.v>=5);
  51.                 this.b = "Netscape";
  52.         }else if (this.ie) {
  53.                 this.ie4 = this.ie5 = this.ie55 = this.ie6 = false;
  54.                 if (v.indexOf('MSIE 4')>0) {this.ie4 = true; this.v = 4;}
  55.                 else if (v.indexOf('MSIE 5')>0) {this.ie5 = true; this.v = 5;}
  56.                 else if (v.indexOf('MSIE 5.5')>0) {this.ie55 = true; this.v = 5.5;}
  57.                 else if (v.indexOf('MSIE 6')>0) {this.ie6 = true; this.v = 6;}
  58.                 this.b = "MSIE";
  59.         }else if (this.opera) {
  60.                 this.v=parseInt(ua.substr(ua.indexOf("opera")+6,1)); // set opera version
  61.                 this.opera6=(this.v>=6);
  62.                 this.opera7=(this.v>=7);
  63.                 this.b = "Opera";
  64.         }else if (this.safari) {
  65.                 this.ns6 = (this.v>=5); // ns6 compatible correct?
  66.                 this.b = "Safari";
  67.         }
  68.         this.dom = (document.createElement && document.appendChild && document.getElementsByTagName)? true : false;
  69.         this.def = (this.ie||this.dom);
  70.         this.win32 = ua.indexOf("win")>-1;
  71.         this.mac = ua.indexOf("mac")>-1;
  72.         this.other = (!this.win32 && !this.mac);
  73.         this.supported = (this.def||this.ns4||this.ns6||this.opera)? true:false;
  74.         this.broadband=false;
  75.         this._bws=new Date; // bandwidth timer start
  76. };
  77.  
  78. function DynAPIObject() {
  79.         this.DynObject = DynObject;
  80.         this.DynObject();
  81.  
  82.         this.version = '3.0.0';
  83.         this.loaded = false;
  84.  
  85.         this.ua = new _UserAgent();
  86.  
  87.         this._loadfn = [];
  88.         this._unloadfn = [];
  89.         var f = this.frame = window;
  90.  
  91.         var url = f.document.location.href;
  92.         url = url.substring(0,url.lastIndexOf('/')+1);
  93.         this.documentPath = url;
  94.  
  95.         var o = this;
  96.  
  97.         this.library = {};
  98.         this.library.setPath = function(p) {o.library.path = p};
  99.  
  100.         f.onload = function() {
  101.                 o.loaded = true;
  102.                 if (!o.ua.supported) return alert('Unsupported Browser. Exiting.');
  103.                 if (o.library._create) o.library._create();  // calls dynapi._onLoad() after loading necessary files
  104.                 else setTimeout(o+'._onLoad()',1);
  105.         };
  106.         f.onunload = function() {
  107.                 for (var i=0;i<o._unloadfn.length;i++) o._unloadfn[i]();
  108.                 if (o.document) {
  109.                         o.document._destroy();
  110.                         o.document = null;
  111.                 }
  112.         };
  113. };
  114. p = DynAPIObject.prototype = new DynObject;
  115.  
  116. p.onLoad = function(f) {
  117.         if (typeof(f)=="function") {
  118.                 if (!this.loaded) this._loadfn[this._loadfn.length] = f;
  119.                 else f();
  120.         }
  121. };
  122. p._onLoad = function(f) {
  123.         for (var i=0;i<this._loadfn.length;i++) this._loadfn[i]();
  124. };
  125. p.onUnload = function(f) {
  126.         if (typeof(f)=="function") this._unloadfn[this._unloadfn.length] = f;
  127. };
  128. p.setPrototype = function(sC,sP) {
  129.         var c = this.frame[sC];
  130.         var p = this.frame[sP];
  131.         if ((!c || !p) && this.ua.ns4 && this.library && this.library.elm) {
  132.                 if (!c) c = this.library.elm[sC];
  133.                 if (!p) p = this.library.elm[sP];
  134.         }
  135.         if (!c || !p) return alert('Prototype Error');
  136.         c.prototype = new p();
  137.         c.prototype._className = sC;
  138.         c.prototype._pClassName = sP;
  139.         c.toString = function() {return '['+sC+']'};
  140.         return c.prototype;
  141. };
  142.  
  143. var dynapi = new DynAPIObject();
  144.  
  145. dynapi.ximages={'__xCnTer__':0}; // eXtensible Images
  146. p._imageGetHTML=function(){
  147.         t= '<img src="'+this.src+'"'
  148.         +((this.width)? ' width="'+this.width+'"':'')
  149.         +((this.height)? ' height="'+this.height+'"':'')
  150.         +' border="0" />';
  151.         return t;
  152. };
  153.  
  154. dynapi.functions = {
  155.         removeFromArray : function(array, index, id) {
  156.                 var which=(typeof(index)=="object")?index:array[index];
  157.                 if (id) delete array[which.id];
  158.         else for (var i=0; i<array.length; i++) {
  159.                         if (array[i]==which) {
  160.                                 if(array.splice) array.splice(i,1);
  161.                                 else { 
  162.                                         for(var x=i; x<array.length-1; x++) array[x]=array[x+1];
  163.                                 array.length -= 1;
  164.                         }
  165.                                 break;
  166.                         }
  167.                 }
  168.                 return array;
  169.         },
  170.         removeFromObject : function(object, id) {
  171.                 if(!dynapi.ua.opera) delete object[id];
  172.                 else {
  173.                         var o={};
  174.                         for (var i in object) if(id!=i) o[i]=object[i];
  175.                         object=o;
  176.                 }
  177.                 return object;
  178.         },
  179.         True : function() {return true},
  180.         False : function() {return false},
  181.         Null : function() {},
  182.         Zero : function() {return 0;},
  183.         Allow : function() {
  184.                 event.cancelBubble = true;
  185.                 return true;
  186.         },
  187.         Deny : function() {
  188.                 event.cancelBubble = false;
  189.                 return false;
  190.         },
  191.         getImage : function(src,w,h) {
  192.                 img=(w!=null&&h!=null)? new Image(w,h) : new Image();
  193.                 img.src=src;
  194.                 img.getHTML=dynapi._imageGetHTML;
  195.                 return img;
  196.         },
  197.         getURLArguments : function(o) {  // pass a string or frame/layer object
  198.                 var url,l={};
  199.                 if (typeof(o)=="string") url = o;
  200.                 else if (dynapi.ua.ns4 && o.src) url = o.src;
  201.                 else if (o.document) url = o.document.location.href;
  202.                 else return l;
  203.                 var s = url.substring(url.indexOf('?')+1);
  204.                 var a = s.split('&');
  205.                 for (var i=0;i<a.length;i++) {
  206.                         var b = a[i].split('=');
  207.                         l[b[0]] = unescape(b[1]);
  208.                 }
  209.                 return l;
  210.         },
  211.         getAnchorLocation : function(a,lyr){
  212.                 var o,x=0,y=0;
  213.                 if(lyr && !lyr.doc) lyr=null;
  214.                 lyr=(lyr)? lyr:{doc:document,elm:document};
  215.                 if(typeof(a)=='string') {
  216.                         if(lyr.doc.all) a=lyr.doc.all[a];
  217.                         else if(lyr.doc.getElementById) a=lyr.doc.getElementById(a);
  218.                         else if(lyr.doc.layers) a=lyr.doc.anchors[a];
  219.                 }
  220.                 if(a) o=a;
  221.                 else return;
  222.                 if(lyr.doc.layers) { y+=o.y; x+=o.x;}
  223.                 else if(lyr.doc.getElementById || lyr.doc.all){
  224.                         while (o.offsetParent && lyr.elm!=o){
  225.                                 x+= o.offsetLeft;y+= o.offsetTop;
  226.                                 o = o.offsetParent;
  227.                         }
  228.                 }
  229.                 return {x:x,y:y,anchor:a};
  230.         }
  231. };
  232.  
  233. dynapi.documentArgs = dynapi.functions.getURLArguments(dynapi.frame);
  234.  
  235. dynapi.debug = {};
  236. dynapi._debugBuffer = '';
  237. dPrint=function(s){var d=dynapi.debug; d.print(s)};
  238. dynapi.debug.print = function(s) {
  239.         //@IF:DEBUG[
  240.                 if(s==null) s='';
  241.                 dynapi._debugBuffer += s + '\n';
  242.         //]:DEBUG
  243. };
  244.  
  245. // The DynAPI library system is optional, this can be removed if you want to include other scripts manually
  246. function DynAPILibrary() {
  247.         this.DynObject = DynObject;
  248.         this.DynObject();
  249.  
  250.         // list of js files: this.scripts['../src/api/dynlayer_ie.js'] = {dep, objects, pkg, fn};
  251.         this.scripts = {};
  252.  
  253.         // list of package names: this.packages['dynapi.api'] = dynapi.api = {_objects,_path}
  254.         this.packages = {};
  255.  
  256.         // list of object names: this.objects['DynLayer'] = this.scripts['../src/api/dynlayer_ie.js']
  257.         this.objects = {};
  258.  
  259.         this._c = 0;
  260.         this.loadList = [];
  261.         this.loadIndex = -1;
  262.         this.path = null;
  263.         this.busy = true;
  264. };
  265. p = dynapi.setPrototype('DynAPILibrary','DynObject');
  266.  
  267. // can return a path specific to a package, eg. dynapi.library.getPath('dynapi.api') returns '/src/dynapi/api/'
  268. p.getPath = function(pkg) {
  269.         if (!pkg) pkg = 'dynapi';
  270.         if (this.packages[pkg]) return this.packages[pkg]._path;
  271.         return null;
  272. };
  273.  
  274. // set dynapi path
  275. p.setPath = function(p) {
  276.         this.path = p;
  277.  
  278.         // to-do: rearrange so add()'s can be done before setPath
  279.         //        full paths will then be determined when queued
  280.         //        need an extra argument on addPackage to specify whether the path is relative to this.path or not
  281.         // OR:    add functionality so that these package definitions can be loaded/included on the fly
  282.        
  283.         // load ext/packages.js
  284.         document.write('<script type="text/javascript" language="JavaScript" src="'+p+'ext/packages.js"><\/script>');
  285. };
  286.  
  287. // adds package(s) to the library
  288. p.addPackage = function(pkg, path) {
  289.         var ps;
  290.         if (pkg.indexOf('.')) ps = pkg.split('.');
  291.         else ps = [pkg];
  292.  
  293.         var p = dynapi.frame;
  294.         for (var i=0;i<ps.length;i++) {  // returns the package object (eg. dynapi.api), or creates it if non-existant
  295.                 if (!p[ps[i]]) p[ps[i]] = {};
  296.                 p = p[ps[i]];
  297.         }
  298.         this.packages[pkg] = p;
  299.         p._objects = [];
  300.         p._path = path;
  301.         return p;
  302. };
  303.  
  304. // add object(s) to the library
  305. p.add = function(name, src, dep, relSource) {
  306.         var objects = typeof(name)=="string"? [name] : name;
  307.         dep = (!dep)? [] : typeof(dep)=="string"? [dep] : dep;
  308.  
  309.         var s,p,pkg;
  310.         if (objects[0].indexOf('.')) {
  311.                 pkg = objects[0].substring(0,objects[0].lastIndexOf('.'));
  312.                 if (pkg && this.packages[pkg]) {
  313.                         p = this.packages[pkg];
  314.                         if (relSource!=false) src = p._path + src;
  315.                 }
  316.         }
  317.         if (!this.scripts[src]) s = this.scripts[src] = {};
  318.         else s = this.scripts[src];
  319.         s.objects = [];
  320.         s.dep = dep;
  321.         s.rdep = [];
  322.         s.src = src;
  323.         s.pkg = pkg;
  324.         s.loaded = false;
  325.         s.fn = null;
  326.  
  327.         var n;
  328.         for (var i=0;i<objects.length;i++) {
  329.                 n = objects[i];
  330.                 if (pkg) n = n.substring(n.lastIndexOf('.')+1);
  331.                 this.objects[n] = s;
  332.                 s.objects[s.objects.length] = n;
  333.                 if (p) p._objects[p._objects.length] = n;
  334.         }
  335.  
  336.         return s;
  337. };
  338. // adds a dependency, whenever object "n" is loaded it will load object "d" beforehand
  339. p.addBefore = function(n, d) {
  340.         var s = this.objects[n];
  341.         if (s && this.objects[d]) s.dep[s.dep.length] = d;
  342. };
  343. // adds a reverse dependency, whenever object "n" is loaded it will load object "r" afterword
  344. p.addAfter = function(n, r) {
  345.         var s = this.objects[n];
  346.         if (s && this.objects[r]) s.rdep[s.rdep.length] = r;
  347. };
  348.  
  349. // returns a list of js source filenames to load
  350. p._queue = function(n, list, force) {
  351.         var na=[], names=[],o;
  352.         if (list==null) list = [];
  353.         if (typeof(n)=="string") na = [n];
  354.         else na = n;
  355.  
  356.         for (var i=0;i<na.length;i++) {
  357.                 o = na[i];
  358.                 if (typeof(o)=="string") {
  359.                         if (this.packages[o])
  360.                                 for (var j in this.packages[o]._objects)
  361.                                         names[names.length] = this.packages[o]._objects[j];
  362.                         else names[names.length] = o;
  363.                 }
  364.                 else if (typeof(o)=="object" && o.length) {
  365.                         list = this._queue(o, list, force);
  366.                 }
  367.         }
  368.  
  369.         var s;
  370.         for (var j=0;j<names.length;j++) {
  371.                 s = this._queueObject(names[j], force);
  372.                 if (s) {
  373.                         if (s.dep)
  374.                                 for (var i=0;i<s.dep.length;i++)
  375.                                         list = this._queue(s.dep[i], list, force);
  376.                         list[list.length] = s.src;
  377.                         // also include reverse deps
  378.                         if (s.rdep.length) list = this._queue(s.rdep, list, force);
  379.                 }
  380.         }
  381.         return list;
  382. };
  383.  
  384. // determines whether to queue the script this object is in
  385. p._queueObject = function(n, f) {
  386.         if (n.indexOf('.')) {
  387.                 var pkg = n.substring(0,n.lastIndexOf('.'));
  388.                 if (this.packages[pkg]) n = n.substring(n.lastIndexOf('.')+1);
  389.         }
  390.         var s = this.objects[n];
  391.         if (s) {
  392.                 if (!s.queued) {
  393.                         if (f!=true && s.loaded) dynapi.debug.print('Library Warning: '+n+' is already loaded');
  394.                         else {
  395.                                 s.queued = true;
  396.                                 s.loaded = false;
  397.                                 return s;
  398.                         }
  399.                 }
  400.         }
  401.         else dynapi.debug.print('Library Error: no library map for '+n);
  402.         return false;
  403. };
  404.  
  405. // writes the <script> tag for the object
  406. p.include = function() {
  407.         var a = arguments;
  408.         if (a[0]==true) a=a[1]; // arguments used ONLY by packages.js
  409.         // buffer includes until packages(.js) are loaded
  410.         if (!this._pakLoaded) {
  411.                 if(!this._buffer) this._buffer=[];
  412.                 this._buffer[this._buffer.length]=a;
  413.                 return;
  414.         }
  415.         if (dynapi.loaded) this.load(a);
  416.         else {
  417.                 var list = this._queue(a);
  418.                 var src;
  419.                 for (var i=0;i<list.length;i++) {
  420.                         src = list[i];
  421.                         this.scripts[src].loaded = true;
  422.                         dynapi.frame.document.write('<script type="text/javascript" src="'+src+'"><\/script>');
  423.                 }
  424.         }
  425. };
  426. p.load = p.reload = p.loadScript = p.reloadScript = function(n) {
  427.         dynapi.debug.print('Warning: dynapi.library load extensions not included');
  428. };
  429. dynapi.library = new DynAPILibrary();
  430.  
  431. // deprecated
  432. var DynAPI = dynapi;
  433.