includes/clientside/static/dynano.js
changeset 1 fe660c52c48f
child 29 e5484a9e0818
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 // The "Dynano" Javascript framework. Similar in syntax to JQuery but only has what Enano needs.
       
     2 
       
     3 var $ = function(id)
       
     4 {
       
     5   return new DNobj(id);
       
     6 }
       
     7 var $dynano = $;
       
     8 function DNobj(id)
       
     9 {
       
    10   this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
       
    11   this.height = __DNObjGetHeight(this.object);
       
    12   this.width = __DNObjGetWidth(this.object);
       
    13   
       
    14   if ( this.object.tagName == 'TEXTAREA' && typeof(tinyMCE) == 'object' )
       
    15   {
       
    16     this.object.dnIsMCE = 'no';
       
    17     this.switchToMCE = DN_switchToMCE;
       
    18     this.destroyMCE = DN_destroyMCE;
       
    19     this.getContent = DN_mceFetchContent;
       
    20   }
       
    21 }
       
    22 function __DNObjGetHeight(o) {
       
    23   return o.offsetHeight;
       
    24 }
       
    25 
       
    26 function __DNObjGetWidth(o) {
       
    27   return o.offsetWidth;
       
    28 }
       
    29 
       
    30 function addClass(obj, clsname)
       
    31 {
       
    32   var cnt = obj.className;
       
    33   var space = ( (cnt + '').length > 0 ) ? ' ' : '';
       
    34   var cls = cnt + space + clsname;
       
    35   obj.className = cls;
       
    36 }
       
    37 
       
    38 function rmClass(obj, clsname)
       
    39 {
       
    40   var cnt = obj.className;
       
    41   if ( cnt == clsname )
       
    42   {
       
    43     obj.className = '';
       
    44   }
       
    45   else
       
    46   {
       
    47     cnt = cnt.replace(clsname, '');
       
    48     cnt = trim(cnt);
       
    49     obj.className = cnt;
       
    50   }
       
    51 }
       
    52 
       
    53 function hasClass(obj, clsname)
       
    54 {
       
    55   var cnt = obj.className;
       
    56   if ( !cnt )
       
    57     return false;
       
    58   if ( cnt == clsname )
       
    59     return true;
       
    60   cnt = cnt.split(' ');
       
    61   
       
    62   for ( var i in cnt )
       
    63     if ( cnt[i] == clsname )
       
    64       return true;
       
    65     
       
    66   return false;
       
    67 }
       
    68 function __DNObjGetLeft(obj) {
       
    69   var left_offset = obj.offsetLeft;
       
    70   while ((obj = obj.offsetParent) != null) {
       
    71     left_offset += obj.offsetLeft;
       
    72   }
       
    73   return left_offset;
       
    74 }
       
    75 
       
    76 function __DNObjGetTop(obj) {
       
    77   var left_offset = obj.offsetTop;
       
    78   while ((obj = obj.offsetParent) != null) {
       
    79     left_offset += obj.offsetTop;
       
    80   }
       
    81   return left_offset;
       
    82 }
       
    83 
       
    84 function DN_switchToMCE()
       
    85 {
       
    86   //if ( this.object.dn_is_mce )
       
    87   //  return this;
       
    88   if ( !this.object.id )
       
    89     this.object.id = 'textarea_' + Math.floor(Math.random() * 1000000);
       
    90   if ( !this.object.name )
       
    91     this.object.name = 'textarea_' + Math.floor(Math.random() * 1000000);
       
    92   tinyMCE.addMCEControl(this.object, this.object.name, document);
       
    93   this.object.dnIsMCE = 'yes';
       
    94   return this;
       
    95 }
       
    96 
       
    97 function DN_destroyMCE()
       
    98 {
       
    99   //if ( !this.object.dn_is_mce )
       
   100   //  return this;
       
   101   if ( this.object.name )
       
   102     tinyMCE.removeMCEControl(this.object.name);
       
   103   this.object.dnIsMCE = 'no';
       
   104   return this;
       
   105 }
       
   106 
       
   107 function DN_mceFetchContent()
       
   108 {
       
   109   if ( this.object.name )
       
   110   {
       
   111     var text = this.object.value;
       
   112     if ( tinyMCE.getInstanceById(this.object.name) )
       
   113       text = tinyMCE.getContent(this.object.name);
       
   114     return text;
       
   115   }
       
   116   else
       
   117   {
       
   118     return this.object.value;
       
   119   }
       
   120 }
       
   121 
       
   122 DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
       
   123 DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
       
   124 DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
       
   125 DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
       
   126 DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
       
   127 DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); }
       
   128 DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); }
       
   129