[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/fckeditor/ -> fckeditor.utils.js (source)

   1  // $Id: fckeditor.utils.js,v 1.2.2.8.2.32 2009/10/04 16:47:50 jorrit Exp $
   2  // map of instancename -> FCKeditor object
   3  var fckInstances = {};
   4  var fckActiveId = false;
   5  // this object will store teaser information
   6  var fckTeaser = { lookup : {}, lookupSetup : false, cache : {} };
   7  
   8  /**
   9   * Drupal behavior that adds FCKeditors to textareas
  10   */
  11  Drupal.behaviors.fckeditor = function(context) {
  12    // make sure the textarea behavior is run first, to get a correctly sized grippie
  13    // the textarea behavior requires the teaser behavior, so load that one as well
  14    if (Drupal.behaviors.teaser && Drupal.behaviors.textarea) {
  15      Drupal.behaviors.teaser(context);
  16      Drupal.behaviors.textarea(context);
  17    }
  18    
  19    $('textarea.fckeditor:not(.fckeditor-processed)', context).each(function() {
  20      var textarea = $(this).addClass('fckeditor-processed');
  21  
  22      var taid = textarea.attr('id');
  23      if (fckInstances[taid]) {
  24        var editorInstance = fckInstances[taid];
  25  
  26        if (editorInstance.defaultState == 1) {
  27          if (textarea.attr('class').indexOf("filterxss1") != -1 || textarea.attr('class').indexOf("filterxss2") != -1) {
  28            $.post(Drupal.settings.basePath + 'index.php?q=fckeditor/xss', {
  29              text: $('#' + taid).val(),
  30              'filters[]': Drupal.settings.fckeditor_filters[fckInstances[taid].DrupalId]
  31              },
  32              function(text) {
  33                textarea.val(text);
  34                $('#img_assist-link-' + taid).hide();
  35                $(".img_assist-button").hide();
  36                editorInstance.ReplaceTextarea();
  37              }
  38            );
  39          }
  40          else {
  41            editorInstance.ReplaceTextarea();
  42            $('#img_assist-link-' + taid).hide();
  43            $(".img_assist-button").hide();
  44          }
  45        }
  46      }
  47    });
  48  }
  49  
  50  /**
  51   * This method takes care of replacing a textarea with an FCKeditor
  52   * and vice versa.
  53   */
  54  function Toggle(textareaID, TextTextarea, TextRTE, xss_check)
  55  {
  56    var swtch = $('#switch_'+textareaID);
  57  
  58    // check if this FCKeditor was initially disabled
  59    if (fckInstances[textareaID].defaultState == 0) {
  60      fckInstances[textareaID].defaultState = 2;
  61      if ($('#' + textareaID).attr('class').indexOf("filterxss2") != -1) {
  62        $.post(Drupal.settings.basePath + 'index.php?q=fckeditor/xss', {
  63          text: $('#' + textareaID).val(),
  64          'filters[]': Drupal.settings.fckeditor_filters[fckInstances[textareaID].DrupalId]
  65          },
  66          function(text) {
  67            $('#' + textareaID).val(text);
  68            fckInstances[textareaID].ReplaceTextarea();
  69          }
  70        );
  71      }
  72      else {
  73        fckInstances[textareaID].ReplaceTextarea();
  74      }
  75      swtch.text(TextTextarea);
  76      $(".img_assist-button").hide();
  77      // simply return: ReplaceTextarea will take the contents of the textarea for us
  78      return;
  79    }
  80  
  81    var textArea = $('#'+textareaID);
  82    var textAreaContainer = textArea.parents('.resizable-textarea');
  83    var editorFrame = $('#'+textareaID+'___Frame');
  84    var editorInstance = FCKeditorAPI.GetInstance(textareaID);
  85    var text;
  86  
  87    // execute the switch
  88    if (textArea.is(':hidden')) {
  89      // switch from fck to textarea
  90      swtch.text(TextRTE);
  91  
  92      text = editorInstance.GetData(true);
  93      // #372150 and #374386
  94      if (text == '<br />' || text == '<p>&#160;</p>' || text == '<div>&#160;</div>') {
  95          text = '';
  96      }
  97  
  98      // check if we have to take care of teasers
  99      var teaser = FCKeditor_TeaserInfo(textareaID);
 100      if (teaser) {
 101        var t = text.indexOf('<!--break-->');
 102        if (t != -1) {
 103          teaser.textarea.val(FCKeditor_trim(text.slice(0,t)));
 104          text = FCKeditor_trim(text.slice(t+12));
 105  
 106          teaser.textareaContainer.show();
 107          teaser.textarea.attr('disabled', '');
 108          if (teaser.button.attr('value') != Drupal.t('Join summary')) {
 109            try {teaser.button.click();} catch(e) {teaser.button.val(Drupal.t('Join summary'));}
 110          }
 111        } else {
 112          teaser.textarea.attr('disabled', 'disabled');
 113          if (teaser.button.attr('value') != Drupal.t('Split summary at cursor')) {
 114            try {teaser.button.click();} catch(e) {teaser.button.val(Drupal.t('Split summary at cursor'));}
 115          }
 116        }
 117  
 118        teaser.buttonContainer.show();
 119      }
 120      textArea.val(text);
 121  
 122      textArea.show();
 123      textAreaContainer.show();
 124      editorFrame.hide();
 125      $('#img_assist-link-' + textareaID).show();
 126      $(".img_assist-button").show();
 127      $(textArea).parent().children(".grippie").show();
 128    } else {
 129      // switch from textarea to fck
 130      swtch.text(TextTextarea);
 131  
 132      // check if we have to take care of teasers
 133      var teaser = FCKeditor_TeaserInfo(textareaID);
 134  
 135      if (teaser) {
 136        if (teaser.textarea.val().length > 0) {
 137          text = teaser.textarea.val() + '\n<!--break-->\n' + textArea.val();
 138        } else {
 139          text = textArea.val();
 140        }
 141        teaser.textarea.attr('disabled', '');
 142        teaser.buttonContainer.hide();
 143        teaser.textareaContainer.hide();
 144        teaser.checkboxContainer.show();
 145      } else {
 146        text = textArea.val();
 147      }
 148  
 149      editorInstance.SetData(text, true);
 150  
 151      // Switch the DIVs display.
 152      textArea.hide();
 153      textAreaContainer.show();
 154      textArea.parent().children('.grippie').hide();
 155      editorFrame.show();
 156      $('#img_assist-link-' + textareaID).hide();
 157      $(".img_assist-button").hide();
 158    }
 159  }
 160  
 161  // Update a global variable containing the active FCKeditor ID.
 162  function DoFCKeditorUpdateId(editorInstance) {
 163    fckActiveId = editorInstance.Name;
 164  }
 165  
 166  /**
 167   * The FCKeditor_OnComplete function is a special function called everytime an
 168   * editor instance is completely loaded and available for API interactions.
 169   */
 170  function FCKeditor_OnComplete(editorInstance) {
 171    // Enable the switch button. It is disabled at startup, waiting the editor to be loaded.
 172    $('#switch_' + editorInstance.Name).show();
 173    editorInstance.Events.AttachEvent('OnAfterLinkedFieldUpdate', FCKeditor_OnAfterLinkedFieldUpdate);
 174    editorInstance.Events.AttachEvent('OnFocus', DoFCKeditorUpdateId);
 175  
 176    var teaser = FCKeditor_TeaserInfo(editorInstance.Name);
 177  
 178    if (teaser) {
 179      // if there is a teaser, prepend it to the text, only when switched to FCKeditor using toggle
 180      //if (fckInstances[editorInstance.Name].defaultState == 2) {
 181        if (teaser.textarea.val().length > 0 && editorInstance.GetData(true).indexOf('<!--break-->') == -1 ) {
 182          var text = teaser.textarea.val() + '\n<!--break-->\n' + editorInstance.GetData(true);
 183          editorInstance.SetData(text);
 184        }
 185      //}
 186      // hide the teaser
 187      teaser.textarea.attr('disabled', '');
 188      teaser.buttonContainer.hide();
 189      teaser.textareaContainer.hide();
 190      teaser.checkboxContainer.show();
 191    }
 192  
 193    // jQuery's hide() does not work when the field is not visible, for instance because it is in a collapsed field set
 194    $(editorInstance.LinkedField).parent().children('.grippie').each(function() {
 195      this.style.display = 'none';
 196    });
 197  
 198    // very ugly hack to circumvent FCKeditor from re-updating textareas on submission. We do that ourselves
 199    // FCKeditor will happily update the fake textarea while we will use the proper one
 200    editorInstance.LinkedField2 = editorInstance.LinkedField;
 201    editorInstance.LinkedField = $('<textarea></textarea>');
 202  
 203    // Img_Assist integration
 204    IntegrateWithImgAssist();
 205  }
 206  
 207  /**
 208   * This method is executed for every FCKeditor instance just after the underlying text field is updated
 209   * before the form is submitted.
 210   */
 211  function FCKeditor_OnAfterLinkedFieldUpdate(editorInstance) {
 212    var textArea = editorInstance.LinkedField2;
 213    var taid = textArea.id;
 214  
 215    var teaser = FCKeditor_TeaserInfo(taid);
 216  
 217    // when textArea is hidden, FCKeditor is visible
 218    if ($(textArea).is(':hidden')) {
 219      var text = editorInstance.GetData(true);
 220      // #372150 and #374386
 221      if (text == '<br />' || text == '<p>&#160;</p>' || text == '<div>&#160;</div>') {
 222          text = '';
 223      }
 224      textArea.value = text;
 225      // only update the teaser field if this field is associated with a teaser field
 226      if (teaser) {
 227        var t = text.indexOf('<!--break-->');
 228        if (t != -1) {
 229          teaser.textarea.val(FCKeditor_trim(text.slice(0,t)));
 230          textArea.value = FCKeditor_trim(text.slice(t+12));
 231        } else {
 232          teaser.textarea.val('');
 233          teaser.textarea.attr('disabled', 'disabled');
 234  
 235          var teaserbuttontxt = Drupal.t('Join summary');
 236  
 237          if (teaser.button.attr('value') == teaserbuttontxt) {
 238            try {
 239              teaser.button.click();
 240            } catch(e) {
 241              teaserbutton.val(teaserbuttontxt);
 242            }
 243          }
 244        }
 245      }
 246    }
 247  }
 248  
 249  function IntegrateWithImgAssist()
 250  {
 251    var link = document.getElementsByTagName("a");
 252    for (var i = 0; i < link.length; i++) {
 253      cl = link[i].className;
 254      if ( cl == "img_assist-link") {
 255        link[i].href = link[i].href.replace("/load/textarea", "/load/fckeditor");
 256      }
 257    }
 258  }
 259  
 260  /**
 261   * Removes leading and trailing whitespace from the input
 262   */
 263  function FCKeditor_trim(text) {
 264    return text.replace(/^\s+/g, '').replace(/\s+$/g, '');
 265  }
 266  
 267  /**
 268   * This function retrieves information about a possible teaser field
 269   * associated with the mentioned field.
 270   *
 271   * @param taid string HTML id of the main text area
 272   */
 273  function FCKeditor_TeaserInfo(taid) {
 274    // if the result is cached, return it
 275    if (fckTeaser.cache[taid]) {
 276      return fckTeaser.cache[taid];
 277    }
 278  
 279    // build a lookup table
 280    if (!fckTeaser.lookupSetup) {
 281      fckTeaser.lookupSetup = true;
 282      for(var x in Drupal.settings.teaser) {
 283        fckTeaser.lookup[Drupal.settings.teaser[x]] = x;
 284      }
 285    }
 286  
 287    // find the elements
 288    if (fckTeaser.lookup[taid]) {
 289      var obj = {
 290        textarea : $('#'+fckTeaser.lookup[taid]),
 291        checkbox : $('#'+Drupal.settings.teaserCheckbox[fckTeaser.lookup[taid]])
 292      };
 293  
 294      obj.textareaContainer = obj.textarea.parent();
 295      obj.checkboxContainer = obj.checkbox.parent();
 296  
 297      obj.button = $('input.teaser-button', obj.checkbox.parents('div.teaser-checkbox').get(0));
 298      obj.buttonContainer = obj.button.parent();
 299  
 300      fckTeaser.cache[taid] = obj;
 301    } else {
 302      fckTeaser.cache[taid] = null;
 303    }
 304  
 305    return fckTeaser.cache[taid];
 306  }
 307  
 308  /**
 309   * Creates a screen wide popup window containing an FCKeditor
 310   */
 311  function FCKeditor_OpenPopup(popupUrl, jsID, textareaID, width) {
 312    popupUrl = popupUrl + '?var='+ jsID + '&el=' + textareaID;
 313  
 314    var teaser = FCKeditor_TeaserInfo(textareaID);
 315    if (teaser) {
 316      popupUrl = popupUrl + '&teaser=' + teaser.textarea.attr('id');
 317    }
 318  
 319    var percentPos = width.indexOf('%');
 320    if (percentPos != -1) {
 321      width = width.substr(0, percentPos);
 322      width = width / 100 * screen.width;
 323    }
 324  
 325    window.open(popupUrl, null, 'width=' + width + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=1,dependent=yes');
 326  }
 327  
 328  // Probably JsMin was used to compress the code.
 329  // In such case, in IE FCKeditor_IsCompatibleBrowser() will always return false.
 330  if (typeof(FCKeditor_IsCompatibleBrowser) == 'function' && !FCKeditor_IsCompatibleBrowser()) {
 331    var FCKeditor_IsCompatibleBrowser = function() {
 332      var sAgent = navigator.userAgent.toLowerCase() ;
 333      // Internet Explorer 5.5+
 334      if ( sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 && navigator.appVersion.match( /MSIE (.\..)/ ) )
 335      {
 336        var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
 337        return ( sBrowserVersion >= 5.5 ) ;
 338      }
 339      return false;
 340    }
 341  }
 342  
 343  /**
 344   * Integration for ajax.module
 345   */
 346  function doFCKeditorSave() {
 347    for(var textareaid in fckInstances) {
 348      FCKeditor_OnAfterLinkedFieldUpdate(FCKeditorAPI.GetInstance(textareaid));
 349    }
 350  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7