[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/js/jsolait/ -> init.js (source)

   1  /*
   2    Copyright (c) 2003 Jan-Klaas Kollhof
   3    
   4    This file is part of the JavaScript o lait library(jsolait).
   5    
   6    jsolait is free software; you can redistribute it and/or modify
   7    it under the terms of the GNU Lesser General Public License as published by
   8    the Free Software Foundation; either version 2.1 of the License, or
   9    (at your option) any later version.
  10   
  11    This software is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU Lesser General Public License for more details.
  15   
  16    You should have received a copy of the GNU Lesser General Public License
  17    along with this software; if not, write to the Free Software
  18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19  */
  20  
  21  
  22  /**    
  23      Evaluates script in a global scope.
  24      @param [0]  The code to evaluate.
  25  */
  26  globalEval=function(){
  27      return eval(arguments[0]);
  28  }
  29  
  30  
  31  /**
  32      Creates a new class object which inherits from superClass.
  33      @param className="anonymous"  The name of the new class.
  34                                                    If the created class is a public member of a module then 
  35                                                    the className is automatically set.
  36      @param superClass=Object        The class to inherit from (super class).
  37      @param classScope                  A function which is executed for class construction.
  38                                                  As 1st parameter it will get the new class' protptype for 
  39                                                  overrideing or extending the super class. As 2nd parameter it will get
  40                                                  the super class' wrapper for calling inherited methods.
  41  */
  42  Class = function(className, superClass, classScope){
  43      if(arguments.length == 2){
  44          classScope = superClass;
  45          if(typeof className != "string"){
  46              superClass = className;
  47              className = "anonymous";
  48          }else{
  49              superClass = Object;
  50          }
  51      }else if(arguments.length == 1){
  52          classScope = className;
  53          superClass = Object;
  54          className = "anonymous";
  55      }
  56      
  57      //this is the constructor for the new objects created from the new class.
  58      //if and only if it is NOT used for prototyping/subclassing the init method of the newly created object will be called.
  59      var NewClass = function(calledBy){
  60          if(calledBy !== Class){
  61              return this.init.apply(this, arguments);
  62          }
  63      }
  64      //This will create a new prototype object of the new class.
  65      NewClass.createPrototype = function(){
  66          return new NewClass(Class);
  67      }
  68      //setting class properties for the new class.
  69      NewClass.superClass = superClass;
  70      NewClass.className=className; 
  71      NewClass.toString = function(){
  72          return "[class %s]".format(NewClass.className);
  73      };
  74      if(superClass.createPrototype!=null){//see if the super class can create prototypes. (creating an object without calling init())
  75          NewClass.prototype = superClass.createPrototype();
  76      }else{//just create an object of the super class
  77          NewClass.prototype = new superClass();
  78      }
  79      //reset the constructor for new objects to the actual constructor.
  80      NewClass.prototype.constructor = NewClass;
  81      
  82      if(superClass == Object){//all other objects already have a nice toString method.
  83          NewClass.prototype.toString = function(){
  84              return "[object %s]".format(this.constructor.className);
  85          };
  86      }
  87      
  88      if(NewClass.prototype.init==null){
  89          NewClass.prototype.init=function(){
  90          }
  91      }
  92     
  93      
  94      //create a supr  function to be used to call methods of the super class
  95      var supr = function(self){
  96          //set up super class functionality  so a call to super(this) will return an object with all super class methods 
  97          //the methods can be called like super(this).foo and the this object will be bound to that method
  98          var wrapper = {};
  99          var superProto = superClass.prototype;
 100          for(var n in superProto){
 101              if(typeof superProto[n] == "function"){
 102                   wrapper[n] = function(){
 103                      var f = arguments.callee;
 104                      return superProto[f._name].apply(self, arguments);
 105                  }
 106                  wrapper[n]._name = n;
 107              }
 108          }
 109          return wrapper;
 110      }
 111          
 112      //execute the scope of the class
 113      classScope(NewClass.prototype, supr);
 114      
 115      return NewClass;
 116  }    
 117  Class.toString = function(){
 118      return "[object Class]";
 119  }
 120  Class.createPrototype=function(){ 
 121      throw "Can't use Class as a super class.";
 122  }
 123  
 124  /**
 125      Creates a new module and registers it.
 126      @param name              The name of the module.
 127      @param version            The version of a module.
 128      @param moduleScope    A function which is executed for module creation.
 129                                       As 1st parameter it will get the module variable.                                     
 130  */
 131  Module = function(name, version, moduleScope){
 132      var mod = new Object();
 133      mod.version = version;
 134      mod.name = name;
 135      mod.toString=function(){
 136          return "[module '%s' version: %s]".format(mod.name, mod.version);
 137      }
 138      
 139      /**
 140          Base class for all module-Exceptions.
 141      */
 142      mod.Exception=Class("Exception", function(publ){
 143          /**
 144              Initializes a new Exception.
 145              @param msg           The error message for the user.
 146              @param trace=null   The error causing this Exception if available.
 147          */
 148          publ.init=function(msg, trace){
 149              this.name = this.constructor.className;
 150              this.message = msg;
 151              this.trace = trace;
 152          }
 153          
 154          publ.toString=function(){
 155              var s = "%s %s\n\n".format(this.name, this.module);
 156              s += this.message;
 157              return s;
 158          }
 159          /**
 160              Returns the complete trace of the exception.
 161              @return The error trace.
 162          */
 163          publ.toTraceString=function(){
 164              var s = "%s %s:\n    ".format(this.name, this.module );
 165              s+="%s\n\n".format(this.message);
 166              if(this.trace){
 167                  if(this.trace.toTraceString){
 168                      s+= this.trace.toTraceString();
 169                  }else{
 170                      s+= this.trace;
 171                  }
 172              }
 173              return s;
 174          }
 175          ///The name of the Exception(className).
 176          publ.name;
 177          ///The error message.
 178          publ.message;
 179          ///The module the Exception belongs to.
 180          publ.module = mod;
 181          ///The error which caused the Exception or null.
 182          publ.trace;      
 183      })
 184      
 185      //execute the scope of the module
 186      moduleScope(mod);
 187      
 188      //todo: set classNames for anonymous classes.
 189      for(var n in mod){
 190          if(mod[n].className == "anonymous"){
 191              mod[n].className = n;
 192          }
 193      }
 194      
 195      if(name != "jsolait"){
 196          jsolait.registerModule(mod);
 197      }
 198      return mod;
 199  }
 200  Module.toString = function(){
 201      return "[object Module]";
 202  }
 203  Module.createPrototype=function(){ 
 204      throw "Can't use Module as a super class.";
 205  }
 206  
 207  //docstart
 208  /**
 209      The root module for jsolait.
 210      It provides some global functionality for loading modules,
 211      some String enhancements.
 212  */
 213  Module("jsolait", "0.1.0", function(mod){
 214      ///The global jsolait object.
 215      jsolait=mod;
 216      
 217      ///base url for user modules.
 218      mod.baseURL=".";
 219      ///The URL where jsolait is installed.
 220      /// viniciuscb 2004-11-18 - to work with eGroupWare
 221      ///mod.libURL ="./jsolait";
 222      mod.libURL =GLOBALS['serverRoot']+"/phpgwapi/js/jsolait";
 223      ///Collection of all loaded modules.(module cache)
 224      mod.modules = new Array();
 225      ///The URLs of there the modules, part of jsolait.
 226      mod.moduleURLs = {urllib:"%(libURL)s/lib/urllib.js",
 227                                        xml:"%(libURL)s/lib/xml.js",
 228                                        crypto:"%(libURL)s/lib/crypto.js",
 229                                        codecs:"%(libURL)s/lib/codecs.js",
 230                                        jsonrpc:"%(libURL)s/lib/jsonrpc.js",
 231                                        lang:"%(libURL)s/lib/lang.js",
 232                                        xmlrpc:"%(libURL)s/lib/xmlrpc.js"};
 233     
 234      mod.init=function(){
 235          //make jsolait work with WScript
 236          var ws = null;
 237          try{//see if WScript is available
 238              ws = WScript;
 239          }catch(e){
 240          }
 241          if(ws != null){
 242              initWS();
 243          }
 244      }
 245      
 246      ///initializes jsolait for using it with WScript
 247      var initWS = function(){
 248          print=function(msg){
 249              WScript.echo(msg);
 250          }
 251          alert=function(msg){
 252              print(msg);
 253          }
 254          var args = WScript.arguments;
 255          try{
 256              //get script to execute
 257              var url = args(0);
 258              url = url.replace(/\\/g, "/");
 259              url = url.split("/");
 260              url = url.slice(0, url.length-1);
 261              //set base for user module loading
 262              mod.baseURL = url.join("/");
 263          }catch(e){
 264              throw new mod.Exception("Missing script filename to be run.", e);
 265          }
 266          
 267          //location of jsolait/init.js
 268          url = WScript.ScriptFullName;
 269          
 270          if(args(0).replace("file://","").toLowerCase() == url.toLowerCase()){
 271              WScript.stderr.write("Can't run myself! exiting ... \n");
 272              return;
 273          }
 274          url = url.replace(/\\/g, "/");
 275          url = url.split("/");
 276          url = url.slice(0, url.length-1);
 277          mod.libURL = "file://" + url.join("/");
 278          try{
 279              mod.loadScript(args(0));
 280          }catch(e){
 281              WScript.stdErr.write("%s(1,1) jsolait runtime error:\n%s\n".format(args(0).replace("file://",""), e.toTraceString()));
 282          }
 283      }
 284      
 285      
 286      /**
 287         Imports a module given its name(someModule.someSubModule).
 288         A module's file location is determined by treating each module name as a directory.
 289         Only the last one points to a file.
 290         If the module's URL is not known to jsolait then it will be searched for in jsolait.baseURL which is "." by default.
 291         @param name   The name of the module to load.
 292         @return           The module object.
 293      */
 294      mod.importModule = function(name){
 295  
 296          if (mod.modules[name]){ //module already loaded
 297              return mod.modules[name];
 298          }else{
 299              var src,modURL;
 300              //check if jsolait already knows the url of the module(moduleURLs contains urls to modules)
 301              if(mod.moduleURLs[name]){
 302                  modURL = mod.moduleURLs[name].format(mod);
 303              }else{//assume it's a user module and located at baseURL
 304                  modURL = "%s/%s.js".format(mod.baseURL, name.split(".").join("/"));
 305              }  
 306              try{//to load module from location calculated above
 307                  src = getFile(modURL);
 308              }catch(e){//module could not be found at the location.
 309                  throw new mod.ModuleImportFailed(name, modURL, e);
 310              }
 311              
 312              try{//interpret the script
 313                  globalEval(src);
 314              }catch(e){
 315                  throw new mod.ModuleImportFailed(name, modURL, e);
 316              }
 317              //the module should have registered itself
 318              return mod.modules[name]; 
 319          }
 320      }
 321      //make it global
 322      importModule = mod.importModule;
 323      
 324      /**
 325          Loads and interprets a script file.
 326          @param url  The url of the script to load.
 327      */
 328      mod.loadScript=function(url){
 329          var src = getFile(url);
 330          try{//to interpret the source 
 331              globalEval(src);
 332          }catch(e){
 333              throw new mod.EvalFailed(url, e);
 334          }
 335      }
 336      /**
 337          Registers a new module. 
 338          Registered modules can be imported with importModule(...).
 339          @param module  The module to register.
 340      */
 341      mod.registerModule = function(module){
 342          this.modules[module.name] = module;
 343      }
 344      
 345      /**
 346          Creates an HTTP request object for retreiving files.
 347          @return HTTP request object.
 348      */
 349      var getHTTP=function() {
 350          var obj;
 351          try{ //to get the mozilla httprequest object
 352              obj = new XMLHttpRequest();
 353          }catch(e){
 354              try{ //to get MS HTTP request object
 355                  obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
 356              }catch(e){
 357                  try{ //to get MS HTTP request object
 358                      obj=new ActiveXObject("Msxml2.XMLHTTP");
 359                  }catch(e){
 360                      try{// to get the old MS HTTP request object
 361                          obj = new ActiveXObject("microsoft.XMLHTTP"); 
 362                      }catch(e){
 363                          throw new mod.Exception("Unable to get an HTTP request object.");
 364                      }
 365                  }    
 366              }
 367          }
 368          return obj;
 369      }
 370      /**
 371          Retrieves a file given its URL.
 372          @param url             The url to load.
 373          @param headers=[]  The headers to use.
 374          @return                 The content of the file.
 375      */
 376      var getFile=function(url, headers) { 
 377          //if callback is defined then the operation is done async
 378          headers = (headers != null) ? headers : [];
 379          //setup the request
 380          try{
 381              var xmlhttp= getHTTP();
 382              xmlhttp.open("GET", url, false);
 383              for(var i=0;i< headers.length;i++){
 384                  xmlhttp.setRequestHeader(headers[i][0], headers[i][1]);    
 385              }
 386              xmlhttp.send("");
 387          }catch(e){
 388              throw new mod.Exception("Unable to load URL: '%s'.".format(url), e);
 389          }
 390          if(xmlhttp.status == 200 || xmlhttp.status == 0){
 391              return xmlhttp.responseText;
 392          }else{
 393               throw new mod.Exception("File not loaded: '%s'.".format(url));
 394          }
 395      }
 396      
 397      Error.prototype.toTraceString = function(){
 398          if(this.message){
 399              return "%s\n".format(this.message);
 400          }
 401          if (this.description){
 402             return "%s\n".format(this.description);
 403          }
 404          return "unknown error\n"; 
 405      }
 406     
 407      
 408      /**
 409          Thrown when a module could not be found.
 410      */
 411      mod.ModuleImportFailed=Class(mod.Exception, function(publ, supr){
 412          /**
 413              Initializes a new ModuleImportFailed Exception.
 414              @param name      The name of the module.
 415              @param url          The url of the module.
 416              @param trace      The error cousing this Exception.
 417          */
 418          publ.init=function(moduleName, url, trace){
 419              supr(this).init("Failed to import module: '%s' from URL:'%s'".format(moduleName, url), trace);
 420              this.moduleName = moduleName;
 421              this.url = url;
 422          }
 423          ///The  name of the module that was not found.
 424          publ.moduleName;
 425          ///The url the module was expected to be found at.
 426          publ.url;
 427      })
 428      
 429      /**
 430          Thrown when a source could not be loaded due to an interpretation error.
 431      */
 432      mod.EvalFailed=Class(mod.Exception, function(publ, supr){
 433          /**
 434              Initializes a new EvalFailed exception.
 435              @param url                   The url of the module.
 436              @param trace               The exception that was thrown while interpreting the module's source code.
 437          */
 438          publ.init=function(url, trace){
 439              supr(this).init("File '%s' Eval of script failed.".format(url), trace);
 440              this.url = url;
 441          }
 442          ///The url the module was expected to be found at.
 443          publ.url;
 444      })
 445      
 446      /**
 447          Displays an exception and it's trace.
 448          This works better than alert(e) because traces are taken into account.
 449          @param exception  The exception to display.
 450      */
 451      mod.reportException=function(exception){
 452          if(exception.toTraceString){
 453              var s= exception.toTraceString();
 454          }else{
 455              var s = exception.toString();
 456          }
 457          var ws = null;
 458          try{//see if WScript is available
 459              ws = WScript;
 460          }catch(e){
 461          }
 462          if(ws != null){
 463              WScript.stderr.write(s);
 464          }else{
 465              alert(s);
 466          }
 467      }    
 468      ///The global exception report method;
 469      reportException = mod.reportException;
 470  })
 471  
 472  //stringmod
 473  /**
 474      String formatting module.
 475      It allows python like string formatting ("some text %s" % "something").
 476      Also similar to sprintf from C.
 477  */
 478  Module("stringformat", "0.1.0", function(mod){
 479      /**
 480          Creates a format specifier object. 
 481      */
 482      var FormatSpecifier=function(s){
 483          var s = s.match(/%(\(\w+\)){0,1}([ 0-]){0,1}(\+){0,1}(\d+){0,1}(\.\d+){0,1}(.)/);
 484          if(s[1]){
 485              this.key=s[1].slice(1,-1);
 486          }else{
 487              this.key = null;
 488          }
 489          this.paddingFlag = s[2];
 490          if(this.paddingFlag==""){
 491              this.paddingFlag =" " 
 492          }
 493          this.signed=(s[3] == "+");
 494          this.minLength = parseInt(s[4]);
 495          if(isNaN(this.minLength)){
 496              this.minLength=0;
 497          }
 498          if(s[5]){
 499              this.percision = parseInt(s[5].slice(1,s[5].length));
 500          }else{
 501              this.percision=-1;
 502          }
 503          this.type = s[6];
 504      }
 505  
 506      /**
 507          Formats a string replacing formatting specifiers with values provided as arguments
 508          which are formatted according to the specifier.
 509          This is an implementation of  python's % operator for strings and is similar to sprintf from C.
 510          Usage:
 511              resultString = formatString.format(value1, v2, ...);
 512          
 513          Each formatString can contain any number of formatting specifiers which are
 514          replaced with the formated values.
 515          
 516          specifier([...]-items are optional): 
 517              "%(key)[flag][sign][min][percision]typeOfValue"
 518              
 519              (key)  If specified the 1st argument is treated as an object/associative array and the formating values 
 520                       are retrieved from that object using the key.
 521                  
 522              flag:
 523                  0      Use 0s for padding.
 524                  -      Left justify result, padding it with spaces.
 525                          Use spaces for padding.
 526              sign:
 527                  +      Numeric values will contain a +|- infront of the number.
 528              min:
 529                  l      The string will be padded with the padding character until it has a minimum length of l. 
 530              percision:
 531                 .x     Where x is the percision for floating point numbers and the lenght for 0 padding for integers.
 532              typeOfValue:
 533                  d    Signed integer decimal.       
 534                  i     Signed integer decimal.      
 535                  b    Unsigned binary.                       //This does not exist in python!
 536                  o    Unsigned octal.     
 537                  u    Unsigned decimal.      
 538                  x    Unsigned hexidecimal (lowercase).     
 539                  X   Unsigned hexidecimal (uppercase).     
 540                  e   Floating point exponential format (lowercase).      
 541                  E   Floating point exponential format (uppercase).      
 542                  f    Floating point decimal format.      
 543                  F   Floating point decimal format.      
 544                  c   Single character (accepts byte or single character string).      
 545                  s   String (converts any object using object.toString()).     
 546          
 547          Examples:
 548              "%02d".format(8) == "08"
 549              "%05.2f".format(1.234) == "01.23"
 550              "123 in binary is: %08b".format(123) == "123 in binary is: 01111011"
 551              
 552          @param *  Each parameter is treated as a formating value. 
 553          @return The formated String.
 554      */
 555      String.prototype.format=function(){
 556          var sf = this.match(/(%(\(\w+\)){0,1}[ 0-]{0,1}(\+){0,1}(\d+){0,1}(\.\d+){0,1}[dibouxXeEfFgGcrs%])|([^%]+)/g);
 557          if(sf){
 558              if(sf.join("") != this){
 559                  throw new mod.Exception("Unsupported formating string.");
 560              }
 561          }else{
 562              throw new mod.Exception("Unsupported formating string.");
 563          }
 564          var rslt ="";
 565          var s;
 566          var obj;
 567          var cnt=0;
 568          var frmt;
 569          var sign="";
 570          
 571          for(var i=0;i<sf.length;i++){
 572              s=sf[i];
 573              if(s == "%%"){
 574                  s = "%";
 575              }else if(s.slice(0,1) == "%"){
 576                  frmt = new FormatSpecifier(s);//get the formating object
 577                  if(frmt.key){//an object was given as formating value
 578                      if((typeof arguments[0]) == "object" && arguments.length == 1){
 579                          obj = arguments[0][frmt.key];
 580                      }else{
 581                          throw new mod.Exception("Object or associative array expected as formating value.");
 582                      }
 583                  }else{//get the current value
 584                      if(cnt>=arguments.length){
 585                          throw new mod.Exception("Not enough arguments for format string");
 586                      }else{
 587                          obj=arguments[cnt];
 588                          cnt++;
 589                      }
 590                  }
 591                      
 592                  if(frmt.type == "s"){//String
 593                      if (obj == null){
 594                          obj = "null";
 595                      }
 596                      s=obj.toString().pad(frmt.paddingFlag, frmt.minLength);
 597                      
 598                  }else if(frmt.type == "c"){//Character
 599                      if(frmt.paddingFlag == "0"){
 600                          frmt.paddingFlag=" ";//padding only spaces
 601                      }
 602                      if(typeof obj == "number"){//get the character code
 603                          s = String.fromCharCode(obj).pad(frmt.paddingFlag , frmt.minLength) ;
 604                      }else if(typeof obj == "string"){
 605                          if(obj.length == 1){//make sure it's a single character
 606                              s=obj.pad(frmt.paddingFlag, frmt.minLength);
 607                          }else{
 608                              throw new mod.Exception("Character of length 1 required.");
 609                          }
 610                      }else{
 611                          throw new mod.Exception("Character or Byte required.");
 612                      }
 613                  }else if(typeof obj == "number"){
 614                      //get sign of the number
 615                      if(obj < 0){
 616                          obj = -obj;
 617                          sign = "-"; //negative signs are always needed
 618                      }else if(frmt.signed){
 619                          sign = "+"; // if sign is always wanted add it 
 620                      }else{
 621                          sign = "";
 622                      }
 623                      //do percision padding and number conversions
 624                      switch(frmt.type){
 625                          case "f": //floats
 626                          case "F":
 627                              if(frmt.percision > -1){
 628                                  s = obj.toFixed(frmt.percision).toString();
 629                              }else{
 630                                  s = obj.toString();
 631                              }
 632                              break;
 633                          case "E"://exponential
 634                          case "e":
 635                              if(frmt.percision > -1){
 636                                  s = obj.toExponential(frmt.percision);
 637                              }else{
 638                                  s = obj.toExponential();
 639                              }
 640                              s = s.replace("e", frmt.type);
 641                              break;
 642                          case "b"://binary
 643                              s = obj.toString(2);
 644                              s = s.pad("0", frmt.percision);
 645                              break;
 646                          case "o"://octal
 647                              s = obj.toString(8);
 648                              s = s.pad("0", frmt.percision);
 649                              break;
 650                          case "x"://hexadecimal
 651                              s = obj.toString(16).toLowerCase();
 652                              s = s.pad("0", frmt.percision);
 653                              break;
 654                          case "X"://hexadecimal
 655                              s = obj.toString(16).toUpperCase();
 656                              s = s.pad("0", frmt.percision);
 657                              break;
 658                          default://integers
 659                              s = parseInt(obj).toString();
 660                              s = s.pad("0", frmt.percision);
 661                              break;
 662                      }
 663                      if(frmt.paddingFlag == "0"){//do 0-padding
 664                          //make sure that the length of the possible sign is not ignored
 665                          s=s.pad("0", frmt.minLength - sign.length);
 666                      }
 667                      s=sign + s;//add sign
 668                      s=s.pad(frmt.paddingFlag, frmt.minLength);//do padding and justifiing
 669                  }else{
 670                      throw new mod.Exception("Number required.");
 671                  }
 672              }
 673              rslt += s;
 674          }
 675          return rslt;
 676      }
 677      
 678      /**
 679          Padds a String with a character to have a minimum length.
 680          
 681          @param flag   "-":      to padd with " " and left justify the string.
 682                              Other: the character to use for padding. 
 683          @param len    The minimum length of the resulting string.
 684      */
 685      String.prototype.pad = function(flag, len){
 686          var s = "";
 687          if(flag == "-"){
 688              var c = " ";
 689          }else{
 690              var c = flag;
 691          }
 692          for(var i=0;i<len-this.length;i++){
 693              s += c;
 694          }
 695          if(flag == "-"){
 696              s = this + s;
 697          }else{
 698              s += this;
 699          }
 700          return s;
 701      }
 702      
 703      /**
 704          Repeats a string.
 705          @param c  The count how often the string should be repeated.
 706      */
 707      String.prototype.mul = function(c){
 708          var a = new Array(this.length * c);
 709          var s=""+ this;
 710          for(var i=0;i<c;i++){
 711              a[i] = s;
 712          }
 713          return a.join("");
 714      }
 715  })
 716  
 717  //let jsolait do some startup initialization
 718  jsolait.init();
 719  


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7