Subversion Repositories wimsdev

Rev

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