web/drupal/modules/imce/js/imce_set_inline.js
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 // $Id: imce_set_inline.js,v 1.3.2.4 2009/02/20 21:17:58 ufku Exp $
       
     2 
       
     3 var imceInline = {};
       
     4 
       
     5 imceInline.initiate = function() {
       
     6   $('div.imce-inline-wrapper').show().find('a').click(function() {
       
     7     var i = this.name.indexOf('-IMCE-');
       
     8     imceInline.activeTextarea = $('#'+ this.name.substr(0, i)).get(0);
       
     9     imceInline.activeType = this.name.substr(i+6);
       
    10  
       
    11     if (typeof imceInline.pop == 'undefined' || imceInline.pop.closed) {
       
    12       imceInline.pop = window.open(this.href + (this.href.indexOf('?') < 0 ? '?' : '&') +'app=nomatter|onload@imceInlineImceLoad', '', 'width='+ 760 +',height='+ 560 +',resizable=1');
       
    13     }
       
    14 
       
    15     imceInline.pop.focus();
       
    16     return false;
       
    17   });
       
    18 };
       
    19 
       
    20 //function to be executed when imce loads.
       
    21 function imceInlineImceLoad(win) {
       
    22   win.imce.setSendTo(Drupal.t('Send to @app', {'@app': Drupal.t('textarea')}), imceInline.insert);
       
    23   $(window).unload(function() {
       
    24     if (imceInline.pop && !imceInline.pop.closed) imceInline.pop.close();
       
    25   });
       
    26 };
       
    27 
       
    28 //insert html at cursor position
       
    29 imceInline.insertAtCursor = function (field, txt, type) {
       
    30   field.focus();
       
    31   if ('undefined' != typeof(field.selectionStart)) {
       
    32     if (type == 'link' && (field.selectionEnd-field.selectionStart)) {
       
    33       txt = txt.split('">')[0] +'">'+ field.value.substring(field.selectionStart, field.selectionEnd) +'</a>';
       
    34     }
       
    35     field.value = field.value.substring(0, field.selectionStart) + txt + field.value.substring(field.selectionEnd, field.value.length);
       
    36   }
       
    37   else if (document.selection) {
       
    38     if (type == 'link' && document.selection.createRange().text.length) {
       
    39       txt = txt.split('">')[0] +'">'+ document.selection.createRange().text +'</a>';
       
    40     }
       
    41     document.selection.createRange().text = txt;
       
    42   }
       
    43   else {
       
    44     field.value += txt;
       
    45   }
       
    46 };
       
    47 
       
    48 //sendTo function
       
    49 imceInline.insert = function (file, win) {
       
    50   var type = imceInline.activeType == 'link' ? 'link' : (file.width ? 'image' : 'link');
       
    51   var html = type == 'image' ? ('<img src="'+ file.url +'" width="'+ file.width +'" height="'+ file.height +'" alt="'+ file.name +'" />') : ('<a href="'+ file.url +'">'+ file.name +' ('+ file.size +')</a>');
       
    52   imceInline.activeType = null;
       
    53   win.blur();
       
    54   imceInline.insertAtCursor(imceInline.activeTextarea, html, type);
       
    55 };
       
    56 
       
    57 $(document).ready(imceInline.initiate);