scripts/domutils.js
changeset 1 cddc2ba706d6
child 2 860ba7141641
equal deleted inserted replaced
0:c63de9eb7045 1:cddc2ba706d6
       
     1 // The "Dynano" Javascript framework. Similar in syntax to JQuery but only has what Enano needs.
       
     2 // License = GPLv2
       
     3 
       
     4 var $ = function(id)
       
     5 {
       
     6   return new DNobj(id);
       
     7 }
       
     8 var $dynano = $;
       
     9 function DNobj(id)
       
    10 {
       
    11   this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
       
    12   if ( !this.object )
       
    13   {
       
    14     console.warn('Dynano: requested object is bad. id parameter follows.');
       
    15     console.debug(id);
       
    16     console.debug(tinyMCE.getInstanceById(id));
       
    17     this.object = false;
       
    18     return this;
       
    19   }
       
    20   this.height = __DNObjGetHeight(this.object);
       
    21   this.width = __DNObjGetWidth(this.object);
       
    22   
       
    23   if ( this.object.tagName == 'TEXTAREA' && typeof(tinyMCE) == 'object' )
       
    24   {
       
    25     this.object.dnIsMCE = 'no';
       
    26     this.switchToMCE = DN_switchToMCE;
       
    27     this.destroyMCE = DN_destroyMCE;
       
    28     this.getContent = DN_mceFetchContent;
       
    29     this.setContent = DN_mceSetContent;
       
    30   }
       
    31 }
       
    32 function __DNObjGetHeight(o) {
       
    33   return o.offsetHeight;
       
    34 }
       
    35 
       
    36 function __DNObjGetWidth(o) {
       
    37   return o.offsetWidth;
       
    38 }
       
    39 
       
    40 function addClass(obj, clsname)
       
    41 {
       
    42   var cnt = obj.className;
       
    43   var space = ( (cnt + '').length > 0 ) ? ' ' : '';
       
    44   var cls = cnt + space + clsname;
       
    45   obj.className = cls;
       
    46 }
       
    47 
       
    48 function rmClass(obj, clsname)
       
    49 {
       
    50   var cnt = obj.className;
       
    51   if ( cnt == clsname )
       
    52   {
       
    53     obj.className = '';
       
    54   }
       
    55   else
       
    56   {
       
    57     cnt = cnt.replace(clsname, '');
       
    58     cnt = trim(cnt);
       
    59     obj.className = cnt;
       
    60   }
       
    61 }
       
    62 
       
    63 function hasClass(obj, clsname)
       
    64 {
       
    65   var cnt = obj.className;
       
    66   if ( !cnt )
       
    67     return false;
       
    68   if ( cnt == clsname )
       
    69     return true;
       
    70   cnt = cnt.split(' ');
       
    71   
       
    72   for ( var i in cnt )
       
    73     if ( cnt[i] == clsname )
       
    74       return true;
       
    75     
       
    76   return false;
       
    77 }
       
    78 function __DNObjGetLeft(obj) {
       
    79   var left_offset = obj.offsetLeft;
       
    80   while ((obj = obj.offsetParent) != null) {
       
    81     left_offset += obj.offsetLeft;
       
    82   }
       
    83   return left_offset;
       
    84 }
       
    85 
       
    86 function __DNObjGetTop(obj) {
       
    87   var left_offset = obj.offsetTop;
       
    88   while ((obj = obj.offsetParent) != null) {
       
    89     left_offset += obj.offsetTop;
       
    90   }
       
    91   return left_offset;
       
    92 }
       
    93 
       
    94 function DN_switchToMCE(performWikiTransform)
       
    95 {
       
    96   if ( !this.object.id )
       
    97     this.object.id = 'textarea_' + Math.floor(Math.random() * 1000000);
       
    98   if ( !this.object.name )
       
    99     this.object.name = 'textarea_' + Math.floor(Math.random() * 1000000);
       
   100   // Updated for TinyMCE 3.x
       
   101   if ( performWikiTransform )
       
   102   {
       
   103     this.object.value = DN_WikitextToXHTML(this.object.value);
       
   104   }
       
   105   // If tinyMCE init hasn't been called yet, do it now.
       
   106   if ( !tinymce_initted )
       
   107   {
       
   108     enano_tinymce_options.mode = 'exact';
       
   109     enano_tinymce_options.elements = this.object.id;
       
   110     initTinyMCE();
       
   111     this.object.dnIsMCE = 'yes';
       
   112     return true;
       
   113   }
       
   114   else
       
   115   {
       
   116     tinyMCE.execCommand("mceAddControl", true, this.object.id);
       
   117     this.object.dnIsMCE = 'yes';
       
   118   }
       
   119   return this;
       
   120 }
       
   121 
       
   122 function DN_destroyMCE(performWikiTransform)
       
   123 {
       
   124   //if ( !this.object.dn_is_mce )
       
   125   //  return this;
       
   126   if ( this.object.id )
       
   127   {
       
   128     // TinyMCE 2.x
       
   129     // tinyMCE.removeMCEControl(this.object.name);
       
   130     // TinyMCE 3.x
       
   131     var ed = tinyMCE.getInstanceById(this.object.id);
       
   132     if ( ed )
       
   133     {
       
   134       if ( !tinyMCE.execCommand("mceRemoveEditor", false, this.object.id) )
       
   135         alert('could not destroy editor');
       
   136       if ( performWikiTransform )
       
   137       {
       
   138         this.object.value = DN_XHTMLToWikitext(this.object.value);
       
   139       }
       
   140     }
       
   141   }
       
   142   this.object.dnIsMCE = 'no';
       
   143   return this;
       
   144 }
       
   145 
       
   146 function DN_mceFetchContent()
       
   147 {
       
   148   if ( this.object.name )
       
   149   {
       
   150     var text = this.object.value;
       
   151     if ( tinyMCE.get(this.object.id) )
       
   152     {
       
   153       var editor = tinyMCE.get(this.object.id);
       
   154       text = editor.getContent();
       
   155     }
       
   156     return text;
       
   157   }
       
   158   else
       
   159   {
       
   160     return this.object.value;
       
   161   }
       
   162 }
       
   163 
       
   164 function DN_mceSetContent(text)
       
   165 {
       
   166   if ( this.object.name )
       
   167   {
       
   168     this.object.value = text;
       
   169     if ( tinyMCE.get(this.object.id) )
       
   170     {
       
   171       var editor = tinyMCE.get(this.object.id);
       
   172       editor.setContent(text);
       
   173     }
       
   174   }
       
   175   else
       
   176   {
       
   177     this.object.value = text;
       
   178   }
       
   179 }
       
   180 
       
   181 // A basic Wikitext to XHTML converter
       
   182 function DN_WikitextToXHTML(text)
       
   183 {
       
   184   text = text.replace(/^===[\s]*(.+?)[\s]*===$/g, '<h3>$1</h3>');
       
   185   text = text.replace(/'''(.+?)'''/g, '<b>$1</b>');
       
   186   text = text.replace(/''(.+?)''/g, '<i>$1</i>');
       
   187   text = text.replace(/\[(http|ftp|irc|mailto):([^ \]])+ ([^\]]+?)\]/g, '<a href="$1:$2">$4</a>');
       
   188   return text;
       
   189 }
       
   190 
       
   191 // Inverse of the previous function
       
   192 function DN_XHTMLToWikitext(text)
       
   193 {
       
   194   text = text.replace(/<h3>(.+?)<\/h3>/g, '=== $1 ===');
       
   195   text = text.replace(/<(b|strong)>(.+?)<\/(b|strong)>/g, "'''$2'''");
       
   196   text = text.replace(/<(i|em)>(.+?)<\/(i|em)>/g, "''$2''");
       
   197   text = text.replace(/<a href="([^" ]+)">(.+?)<\/a>/g, '[$1 $2]');
       
   198   text = text.replace(/<\/?p>/g, '');
       
   199   return text;
       
   200 }
       
   201 
       
   202 DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
       
   203 DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
       
   204 DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
       
   205 DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
       
   206 DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
       
   207 DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); }
       
   208 DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); }
       
   209