JAVASCRIPT   44

otp js

Guest on 28th July 2022 06:48:24 PM

  1. /* Copyright (c) by OpenLayers Contributors (see authors.txt for
  2.  * full list of contributors). Published under the 2-clause BSD license.
  3.  * See license.txt in the otp distribution or repository for the
  4.  * full text of the license. */
  5.  
  6.  
  7. // define otp, if not already done so...
  8. if(otp == null)
  9.     var otp = {};
  10.  
  11. otp.CLASS_NAME = "otp";
  12.  
  13.  
  14. /**
  15.  * Function: namespace
  16.  * Create a namespace given a string, eg: otp.namespace("otp.widget.ui");
  17.  *
  18.  * Parameters:
  19.  * ns      - {String || Array} A string representing a namespace or an array of strings representing multiple namespaces.  E.g. "some.name.space".
  20.  * context - {Object} Optional object to which additional names will be added.  Default is the window object.
  21.  */
  22. otp.namespace = function(ns, context)
  23. {
  24.     ns = (typeof ns == 'string') ? [ns] : ns;
  25.     context = context || window;
  26.     var num   = ns.length;
  27.     for(var i=0; i<num; ++i)
  28.     {
  29.         var parts = ns[i].split('.');
  30.         var base  = parts.shift();
  31.         if(typeof context[base] == 'undefined') {
  32.             context[base] = {};
  33.         }
  34.         if(parts.length > 0) {
  35.             otp.namespace([parts.join('.')], context[base]);
  36.         }
  37.     }
  38. };
  39.  
  40.  
  41. /**
  42.  * Constructor: otp.Class
  43.  * Base class used to construct all other classes. Includes support for
  44.  *     multiple inheritance.
  45.  
  46.  * To create a new otp-style class, use the following syntax:
  47.  * (code)
  48.  *     var MyClass = otp.Class(prototype);
  49.  * (end)
  50.  *
  51.  * To create a new otp-style class with multiple inheritance, use the
  52.  *     following syntax:
  53.  * (code)
  54.  *     var MyClass = otp.Class(Class1, Class2, prototype);
  55.  * (end)
  56.  *
  57.  * Note that instanceof reflection will only reveal Class1 as superclass.
  58.  *
  59.  */
  60.  
  61.  
  62. otp.Class = function() {
  63.     var len = arguments.length;
  64.     var P = arguments[0];
  65.     var F = arguments[len-1];
  66.  
  67.     var C = typeof F.initialize == "function" ?
  68.         F.initialize :
  69.         function(){ P.prototype.initialize.apply(this, arguments); };
  70.  
  71.     if (len > 1) {
  72.         var newArgs = [C, P].concat(
  73.                 Array.prototype.slice.call(arguments).slice(1, len-1), F);
  74.         otp.inherit.apply(null, newArgs);
  75.     } else {
  76.         C.prototype = F;
  77.     }
  78.     return C;
  79. };
  80.  
  81.  
  82. /**
  83.  * set member variables in the destination object, as long as that same element is defined in the destination
  84.  * (and the source has a value other than null).  It might overwrite an existing variable.
  85.  *
  86.  * @param {Object} destination
  87.  * @param {Object} source
  88.  * @param {Object} getAll == true, we'll assign the value to the desination regarless if desination has an existing slot
  89.  */
  90. otp.configure = function(destination, source, getAll) {
  91.  
  92.     destination = destination || {};
  93.     if(source)
  94.     {
  95.         for(var property in source)
  96.         {
  97.             if(property == "CLASS_NAME") continue;  // don't override the name property
  98.  
  99.             var value = source[property];
  100.             var exist = destination[property];
  101.  
  102.             // copy the variable to the destination as long as it's not null, and the source is either a simple  object or an Ext Template
  103.             if(value != null && typeof value !== 'function' && (exist !== undefined || getAll == true))
  104.             {
  105.                 destination[property] = value;
  106.             }
  107.  
  108.             // copy null value'd attriubtes if we want to 'getAll'
  109.             if(value == null && getAll == true)
  110.             {
  111.                 destination[property] = value;
  112.             }
  113.         }
  114.  
  115.         /**
  116.          * IE doesn't include the toString property when iterating over an object's
  117.          * properties with the for(property in object) syntax.  Explicitly check if
  118.          * the source has its own toString property.
  119.          */
  120.  
  121.         /*
  122.          * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
  123.          * prototype object" when calling hawOwnProperty if the source object
  124.          * is an instance of window.Event.
  125.          */
  126.  
  127.         var sourceIsEvt = typeof window.Event == "function" && source instanceof window.Event;
  128.         if(!sourceIsEvt && source.hasOwnProperty && source.hasOwnProperty('toString'))
  129.         {
  130.             destination.toString = source.toString;
  131.         }
  132.     }
  133.     return destination;
  134. };
  135.  
  136.  
  137. /**
  138.  * Function: otp.inherit
  139.  *
  140.  * Parameters:
  141.  * C - {Object} the class that inherits
  142.  * P - {Object} the superclass to inherit from
  143.  *
  144.  * In addition to the mandatory C and P parameters, an arbitrary number of
  145.  * objects can be passed, which will extend C.
  146.  */
  147. otp.inherit = function(C, P) {
  148.    var F = function() {};
  149.    F.prototype = P.prototype;
  150.    C.prototype = new F;
  151.    var i, l, o;
  152.    for(i=2, l=arguments.length; i<l; i++) {
  153.        o = arguments[i];
  154.        if(typeof o === "function") {
  155.            o = o.prototype;
  156.        }
  157.        otp.Util.extend(C.prototype, o);
  158.    }
  159. };
  160.  
  161. /**
  162.  * APIFunction: extend
  163.  * Copy all properties of a source object to a destination object.  Modifies
  164.  *     the passed in destination object.  Any properties on the source object
  165.  *     that are set to undefined will not be (re)set on the destination object.
  166.  *
  167.  * Parameters:
  168.  * destination - {Object} The object that will be modified
  169.  * source - {Object} The object with properties to be set on the destination
  170.  *
  171.  * Returns:
  172.  * {Object} The destination object.
  173.  */
  174. otp.Util = otp.Util || {};
  175. otp.Util.extend = function(destination, source) {
  176.     destination = destination || {};
  177.     if (source) {
  178.         for (var property in source) {
  179.             var value = source[property];
  180.             if (value !== undefined) {
  181.                 destination[property] = value;
  182.             }
  183.         }
  184.  
  185.         /**
  186.          * IE doesn't include the toString property when iterating over an object's
  187.          * properties with the for(property in object) syntax.  Explicitly check if
  188.          * the source has its own toString property.
  189.          */
  190.  
  191.         /*
  192.          * FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
  193.          * prototype object" when calling hawOwnProperty if the source object
  194.          * is an instance of window.Event.
  195.          */
  196.  
  197.         var sourceIsEvt = typeof window.Event == "function"
  198.                           && source instanceof window.Event;
  199.  
  200.         if (!sourceIsEvt
  201.            && source.hasOwnProperty && source.hasOwnProperty("toString")) {
  202.             destination.toString = source.toString;
  203.         }
  204.     }
  205.     return destination;
  206. };

Raw Paste


Login or Register to edit or fork this paste. It's free.