imce_set_inline.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function($) {
  2. var ii = window.imceInline = {};
  3. // Drupal behavior
  4. Drupal.behaviors.imceInline = {attach: function(context, settings) {
  5. $('div.imce-inline-wrapper', context).not('.processed').addClass('processed').show().find('a').click(function() {
  6. var i = this.name.indexOf('-IMCE-');
  7. ii.activeTextarea = $('#'+ this.name.substr(0, i)).get(0);
  8. ii.activeType = this.name.substr(i+6);
  9. if (typeof ii.pop == 'undefined' || ii.pop.closed) {
  10. ii.pop = window.open(this.href + (this.href.indexOf('?') < 0 ? '?' : '&') +'app=nomatter|imceload@imceInline.load', '', 'width='+ 760 +',height='+ 560 +',resizable=1');
  11. }
  12. ii.pop.focus();
  13. return false;
  14. });
  15. }};
  16. //function to be executed when imce loads.
  17. ii.load = function(win) {
  18. win.imce.setSendTo(Drupal.t('Insert file'), ii.insert);
  19. $(window).bind('unload', function() {
  20. if (ii.pop && !ii.pop.closed) ii.pop.close();
  21. });
  22. };
  23. //insert html at cursor position
  24. ii.insertAtCursor = function (field, txt, type) {
  25. field.focus();
  26. if ('undefined' != typeof(field.selectionStart)) {
  27. if (type == 'link' && (field.selectionEnd-field.selectionStart)) {
  28. txt = txt.split('">')[0] +'">'+ field.value.substring(field.selectionStart, field.selectionEnd) +'</a>';
  29. }
  30. field.value = field.value.substring(0, field.selectionStart) + txt + field.value.substring(field.selectionEnd, field.value.length);
  31. }
  32. else if (document.selection) {
  33. if (type == 'link' && document.selection.createRange().text.length) {
  34. txt = txt.split('">')[0] +'">'+ document.selection.createRange().text +'</a>';
  35. }
  36. document.selection.createRange().text = txt;
  37. }
  38. else {
  39. field.value += txt;
  40. }
  41. };
  42. //sendTo function
  43. ii.insert = function (file, win) {
  44. var type = ii.activeType == 'link' ? 'link' : (file.width ? 'image' : 'link');
  45. 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>');
  46. ii.activeType = null;
  47. win.blur();
  48. ii.insertAtCursor(ii.activeTextarea, html, type);
  49. };
  50. })(jQuery);