web/drupal/modules/fckeditor/fckeditor.utils.js
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 // $Id: fckeditor.utils.js,v 1.2.2.32 2009/03/11 11:53:52 wwalc Exp $
       
     2 var fckIsRunning = new Array;
       
     3 var fckIsLaunching = new Array;
       
     4 var fckLaunchedTextareaId = new Array;
       
     5 var fckLaunchedJsId = new Array;
       
     6 var fckFirstrun = new Array;
       
     7 var fckIsIE = ( /*@cc_on!@*/false ) ? true : false ;
       
     8 
       
     9 function Toggle(js_id, textareaID, textTextarea, TextRTE, xss_check)
       
    10 {
       
    11   var eFCKeditorDiv  = document.getElementById( 'fck_' + js_id ) ;
       
    12   var teaser = false;
       
    13   var teaserCheckbox = false;
       
    14 
       
    15   for (var i in Drupal.settings.teaser) {
       
    16     if (Drupal.settings.teaser[i] == textareaID)
       
    17       teaser = i;
       
    18       teaserCheckbox = Drupal.settings.teaserCheckbox[i];
       
    19   }
       
    20 
       
    21   if (!fckIsRunning[js_id])
       
    22   {
       
    23     if (!fckIsLaunching[js_id])
       
    24     {
       
    25       //display is set to '' at this stage because of IE 800a025e bug
       
    26       if (fckIsIE)
       
    27         eFCKeditorDiv.style.display = '' ;
       
    28       fckIsLaunching[js_id] = true;
       
    29       $(".img_assist-button").hide();
       
    30       if (xss_check && $('#' + textareaID).attr('class').indexOf("filterxss2") != -1) {
       
    31         $.post(Drupal.settings.basePath + 'index.php?q=fckeditor/xss', {
       
    32             text: $('#' + textareaID).val(),
       
    33             'filters[]': Drupal.settings.fckeditor_filters[js_id]
       
    34           },
       
    35           function(text) {
       
    36             $('#' + textareaID).val(text);
       
    37             $('#' + js_id).val(text);
       
    38             window[js_id].ReplaceTextarea();
       
    39           }
       
    40         );
       
    41       }
       
    42       else {
       
    43         eval(js_id + '.ReplaceTextarea();');
       
    44       }
       
    45       $('#img_assist-link-' + textareaID).hide();
       
    46     }
       
    47     setTimeout("Toggle('" + js_id + "','" + textareaID + "','" + textTextarea + "','" + TextRTE + "'," + xss_check + ");",1000);
       
    48     return ;
       
    49   }
       
    50 
       
    51   var oEditor ;
       
    52   if ( typeof( FCKeditorAPI ) != 'undefined' )
       
    53     oEditor = FCKeditorAPI.GetInstance( js_id );
       
    54 
       
    55   // Get the _Textarea and _FCKeditor DIVs.
       
    56   var eTextarea  = document.getElementById( textareaID );
       
    57   var eFCKeditor  = document.getElementById( js_id );
       
    58   var text;
       
    59 
       
    60   // If the _Textarea DIV is visible, switch to FCKeditor.
       
    61   if ( eTextarea.style.display != 'none' )
       
    62   {
       
    63     if (typeof( imceInitiateFCK ) != 'undefined')
       
    64       imceInitiateFCK();
       
    65     document.getElementById('switch_' + js_id).innerHTML = textTextarea;
       
    66 
       
    67     // Switch the DIVs display.
       
    68     eFCKeditorDiv.style.display = '';
       
    69 
       
    70     text = eTextarea.value;
       
    71     if (teaser && $('input[@class=teaser-button]').attr('value') == Drupal.t('Join summary')) {
       
    72       var val = $('#' + teaser).val();
       
    73       if (val && val.length) {
       
    74         text = val + '<!--break-->' + text;
       
    75       }
       
    76     }
       
    77 
       
    78     // This is a hack for Gecko 1.0.x ... it stops editing when the editor is hidden.
       
    79     if (oEditor && !document.all)
       
    80     {
       
    81       if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG)
       
    82       oEditor.MakeEditable() ;
       
    83     }
       
    84 
       
    85     if ( text.length ) {
       
    86       oEditor.SetHTML( text, false);
       
    87     }
       
    88     eTextarea.style.display = 'none';
       
    89     $('#img_assist-link-' + textareaID).hide();
       
    90     $(".img_assist-button").hide();
       
    91 
       
    92     if (teaser) {
       
    93       $('div[@class=teaser-button-wrapper]').hide();
       
    94       $('#' + teaser).parent().hide();
       
    95       $('#' + teaserCheckbox).parent().show();
       
    96     }
       
    97   }
       
    98   else
       
    99   {
       
   100     if (fckFirstrun[js_id]) {
       
   101       fckFirstrun[js_id] = false;
       
   102     }
       
   103     if (document.getElementById('switch_' + js_id)) {
       
   104       document.getElementById('switch_' + js_id).innerHTML = TextRTE;
       
   105     }
       
   106 
       
   107     var text = oEditor.GetHTML(true);
       
   108     // #372150 and #374386
       
   109     if (text == '<br />' || text == '<p>&#160;</p>' || text == '<div>&#160;</div>') {
       
   110       text = '';
       
   111     }
       
   112 
       
   113     if (teaser) {
       
   114       var t = text.indexOf('<!--break-->');
       
   115       if (t != -1) {
       
   116         $('#' + teaser).val(text.slice(0,t));
       
   117         $('#' + textareaID).val(text.slice(t+12));
       
   118         $('#' + teaser).parent().show();
       
   119         $('#' + teaser).attr('disabled', '');
       
   120         if ($('input[@class=teaser-button]').attr('value') != Drupal.t('Join summary')) {
       
   121           try {$('input[@class=teaser-button]').click();} catch(e) {$('input[@class=teaser-button]').val(Drupal.t('Join summary'));}
       
   122         }
       
   123       }
       
   124       else {
       
   125         $('#' + teaser).attr('disabled', 'disabled');
       
   126         if ($('input[@class=teaser-button]').attr('value') != Drupal.t('Split summary at cursor')) {
       
   127           try {$('input[@class=teaser-button]').click();} catch(e) {$('input[@class=teaser-button]').val(Drupal.t('Split summary at cursor'));}
       
   128         }
       
   129         // Set the textarea value to the editor value.
       
   130         $('#' + textareaID).val(text);
       
   131       }
       
   132     }
       
   133     else {
       
   134       // Set the textarea value to the editor value.
       
   135       $('#' + textareaID).val(text);
       
   136     }
       
   137 
       
   138     $('#img_assist-link-' + textareaID).show();
       
   139     $(".img_assist-button").show();
       
   140     // Switch the DIVs display.
       
   141     eTextarea.style.display = '';
       
   142     eFCKeditorDiv.style.display = 'none';
       
   143     if (teaser) {
       
   144       $('div[@class=teaser-button-wrapper]').show();
       
   145     }
       
   146   }
       
   147 }
       
   148 
       
   149 function CreateToggle(elId, jsId, fckeditorOn)
       
   150 {
       
   151   var ta = document.getElementById(elId);
       
   152   var ta2 = document.getElementById('fck_' + jsId);
       
   153 
       
   154   if (!ta || !ta2)
       
   155     return ;
       
   156 
       
   157   ta2.value = ta.value;
       
   158   ta.parentNode.insertBefore(ta2, ta);
       
   159   if (fckeditorOn) {
       
   160     ta.style.display = 'none';
       
   161     $('#img_assist-link-' + elId).hide();
       
   162   }
       
   163   else
       
   164     ta2.style.display = 'none';
       
   165 }
       
   166 
       
   167 function doFCKeditorSave(){
       
   168   DoFCKeditorTeaserStuff();
       
   169   return true; //continue submitting
       
   170 }
       
   171 
       
   172 function DoFCKeditorTeaserStuff()
       
   173 {
       
   174   //bad hack for #248146
       
   175   if ($('#ahah-progress-edit-attach').length) {
       
   176     return false;
       
   177   }
       
   178     for( var i = 0 ; i < fckLaunchedJsId.length ; i++ ) {
       
   179       if ( document.getElementById( fckLaunchedTextareaId[i] ).style.display == 'none' )
       
   180       {
       
   181         var text = FCKeditorAPI.GetInstance( fckLaunchedJsId[i] ).GetXHTML(true);
       
   182         var teaser = false;
       
   183 
       
   184         // #372150 and #374386
       
   185         if (text == '<br />' || text == '<p>&#160;</p>' || text == '<div>&#160;</div>') {
       
   186           text = '';
       
   187         }
       
   188 
       
   189         for (var k in Drupal.settings.teaser) {
       
   190           if (Drupal.settings.teaser[k] == fckLaunchedTextareaId[i])
       
   191             teaser = k;
       
   192         }
       
   193 
       
   194         if (teaser) {
       
   195           var t = text.indexOf('<!--break-->');
       
   196           if (t != -1) {
       
   197             $('#' + teaser).val(text.slice(0,t));
       
   198             document.getElementById( fckLaunchedTextareaId[i] ).value = text.slice(t+12);
       
   199           }
       
   200           else {
       
   201             $('#' + teaser).val('');
       
   202             $('#' + teaser).attr('disabled', 'disabled');
       
   203             document.getElementById( fckLaunchedTextareaId[i] ).value = text;
       
   204             if ($('input[@class=teaser-button]').attr('value') == Drupal.t('Join summary')) {
       
   205               try {$('input[@class=teaser-button]').click();} catch(e) {$('input[@class=teaser-button]').val(Drupal.t('Join summary'));}
       
   206             }
       
   207           }
       
   208         }
       
   209         else {
       
   210           document.getElementById( fckLaunchedTextareaId[i] ).value = text;
       
   211         }
       
   212       }
       
   213     }
       
   214 }
       
   215 // The FCKeditor_OnComplete function is a special function called everytime an
       
   216 // editor instance is completely loaded and available for API interactions.
       
   217 function FCKeditor_OnComplete( editorInstance )
       
   218 {
       
   219   fckIsRunning[editorInstance.Name] = true ;
       
   220   fckLaunchedTextareaId.push(editorInstance.Config['TextareaID']) ;
       
   221   fckLaunchedJsId.push(editorInstance.Name) ;
       
   222   fckFirstrun[editorInstance.Name] = true;
       
   223 
       
   224   // Enable the switch button. It is disabled at startup, waiting the editor to be loaded.
       
   225   var oElem = document.getElementById('switch_' + editorInstance.Name);
       
   226   if (oElem != null) {
       
   227     oElem.style.display = '';
       
   228   }
       
   229 
       
   230   // If the textarea isn't visible update the content from the editor.
       
   231   $(editorInstance.LinkedField.form).submit(DoFCKeditorTeaserStuff);
       
   232 
       
   233   editorInstance.Events.AttachEvent( 'OnAfterLinkedFieldUpdate', DoFCKeditorTeaserStuff ) ;
       
   234 
       
   235   var teaser = false;
       
   236   var teaserCheckbox = false;
       
   237 
       
   238   for (var k in Drupal.settings.teaser) {
       
   239     if (Drupal.settings.teaser[k] == editorInstance.Config['TextareaID']) {
       
   240       teaser = k;
       
   241       teaserCheckbox = Drupal.settings.teaserCheckbox[k];
       
   242     }
       
   243   }
       
   244 
       
   245   if (teaser) {
       
   246     $('#' + teaser).attr('disabled', '');
       
   247     $('div[@class=teaser-button-wrapper]').hide();
       
   248     $('#' + teaser).parent().hide();
       
   249     $('#' + teaserCheckbox).parent().show();
       
   250   }
       
   251 
       
   252   //Img_Assist integration
       
   253   IntegrateWithImgAssist();
       
   254 }
       
   255 
       
   256 function FCKeditorReplaceTextarea(textarea_id, oFCKeditor, xss_check)
       
   257 {
       
   258   if ($('#' + oFCKeditor.Config['TextareaID']).length === 0) {
       
   259     return;
       
   260   }
       
   261   $(".img_assist-button").hide();
       
   262   if (xss_check && $('#' + oFCKeditor.Config['TextareaID']).attr('class').indexOf("filterxss") != -1) {
       
   263     $.post(Drupal.settings.basePath + 'index.php?q=fckeditor/xss', {
       
   264       text: $('#' + textarea_id).val(),
       
   265       'filters[]': Drupal.settings.fckeditor_filters[textarea_id]
       
   266       },
       
   267       function(text) {
       
   268         $('#' + textarea_id).val(text);
       
   269         oFCKeditor.ReplaceTextarea();
       
   270       }
       
   271     );
       
   272   }
       
   273   else {
       
   274     oFCKeditor.ReplaceTextarea();
       
   275   }
       
   276 }
       
   277 
       
   278 function IntegrateWithImgAssist()
       
   279 {
       
   280   var link = document.getElementsByTagName("a");
       
   281   for (var i = 0; i < link.length; i++) {
       
   282     cl = link[i].className;
       
   283     if ( cl == "img_assist-link") {
       
   284       link[i].href = link[i].href.replace("/load/textarea", "/load/fckeditor");
       
   285     }
       
   286   }
       
   287 }
       
   288 
       
   289 // Probably JsMin was used to compress the code.
       
   290 // In such case, in IE FCKeditor_IsCompatibleBrowser() will always return false.
       
   291 if (typeof(FCKeditor_IsCompatibleBrowser) == 'function' && !FCKeditor_IsCompatibleBrowser()) {
       
   292   var FCKeditor_IsCompatibleBrowser = function() {
       
   293     var sAgent = navigator.userAgent.toLowerCase() ;
       
   294     // Internet Explorer 5.5+
       
   295     if ( sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 && navigator.appVersion.match( /MSIE (.\..)/ ) )
       
   296     {
       
   297       var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
       
   298       return ( sBrowserVersion >= 5.5 ) ;
       
   299     }
       
   300     return false;
       
   301   }
       
   302 }