scripts/domutils.js
changeset 2 860ba7141641
parent 1 cddc2ba706d6
child 11 0faea3a6c881
equal deleted inserted replaced
1:cddc2ba706d6 2:860ba7141641
     9 function DNobj(id)
     9 function DNobj(id)
    10 {
    10 {
    11   this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
    11   this.object = ( typeof(id) == 'object' ) ? id : document.getElementById(id);
    12   if ( !this.object )
    12   if ( !this.object )
    13   {
    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;
    14     this.object = false;
    18     return this;
    15     return this;
    19   }
    16   }
    20   this.height = __DNObjGetHeight(this.object);
    17   this.height = __DNObjGetHeight(this.object);
    21   this.width = __DNObjGetWidth(this.object);
    18   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 }
    19 }
    32 function __DNObjGetHeight(o) {
    20 function __DNObjGetHeight(o) {
    33   return o.offsetHeight;
    21   return o.offsetHeight;
    34 }
    22 }
    35 
    23 
    89     left_offset += obj.offsetTop;
    77     left_offset += obj.offsetTop;
    90   }
    78   }
    91   return left_offset;
    79   return left_offset;
    92 }
    80 }
    93 
    81 
    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; };
    82 DNobj.prototype.addClass = function(clsname) { addClass(this.object, clsname); return this; };
   203 DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
    83 DNobj.prototype.rmClass  = function(clsname) { rmClass( this.object, clsname); return this; };
   204 DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
    84 DNobj.prototype.hasClass = function(clsname) { return hasClass(this.object, clsname); };
   205 DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
    85 DNobj.prototype.Height   = function()        { return __DNObjGetHeight(this.object); }
   206 DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
    86 DNobj.prototype.Width    = function()        { return __DNObjGetWidth( this.object); }
   207 DNobj.prototype.Left     = function()        { /* return this.object.offsetLeft; */ return __DNObjGetLeft(this.object); }
    87 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); }
    88 DNobj.prototype.Top      = function()        { /* return this.object.offsetTop;  */ return __DNObjGetTop( this.object); }
   209 
    89 
       
    90 // Equivalent to PHP trim() function
       
    91 function trim(text)
       
    92 {
       
    93   text = text.replace(/^([\s]+)/, '');
       
    94   text = text.replace(/([\s]+)$/, '');
       
    95   return text;
       
    96 }
       
    97