media-upload.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Contains global functions for the media upload within the post edit screen.
  3. *
  4. * Updates the ThickBox anchor href and the ThickBox's own properties in order
  5. * to set the size and position on every resize event. Also adds a function to
  6. * send HTML or text to the currently active editor.
  7. *
  8. * @file
  9. * @since 2.5.0
  10. * @output wp-admin/js/media-upload.js
  11. *
  12. * @requires jQuery
  13. */
  14. /* global tinymce, QTags, wpActiveEditor, tb_position */
  15. /**
  16. * Sends the HTML passed in the parameters to TinyMCE.
  17. *
  18. * @since 2.5.0
  19. *
  20. * @global
  21. *
  22. * @param {string} html The HTML to be sent to the editor.
  23. * @return {void|boolean} Returns false when both TinyMCE and QTags instances
  24. * are unavailable. This means that the HTML was not
  25. * sent to the editor.
  26. */
  27. window.send_to_editor = function( html ) {
  28. var editor,
  29. hasTinymce = typeof tinymce !== 'undefined',
  30. hasQuicktags = typeof QTags !== 'undefined';
  31. // If no active editor is set, try to set it.
  32. if ( ! wpActiveEditor ) {
  33. if ( hasTinymce && tinymce.activeEditor ) {
  34. editor = tinymce.activeEditor;
  35. window.wpActiveEditor = editor.id;
  36. } else if ( ! hasQuicktags ) {
  37. return false;
  38. }
  39. } else if ( hasTinymce ) {
  40. editor = tinymce.get( wpActiveEditor );
  41. }
  42. // If the editor is set and not hidden,
  43. // insert the HTML into the content of the editor.
  44. if ( editor && ! editor.isHidden() ) {
  45. editor.execCommand( 'mceInsertContent', false, html );
  46. } else if ( hasQuicktags ) {
  47. // If quick tags are available, insert the HTML into its content.
  48. QTags.insertContent( html );
  49. } else {
  50. // If neither the TinyMCE editor and the quick tags are available,
  51. // add the HTML to the current active editor.
  52. document.getElementById( wpActiveEditor ).value += html;
  53. }
  54. // If the old thickbox remove function exists, call it.
  55. if ( window.tb_remove ) {
  56. try { window.tb_remove(); } catch( e ) {}
  57. }
  58. };
  59. (function($) {
  60. /**
  61. * Recalculates and applies the new ThickBox position based on the current
  62. * window size.
  63. *
  64. * @since 2.6.0
  65. *
  66. * @global
  67. *
  68. * @return {Object[]} Array containing jQuery objects for all the found
  69. * ThickBox anchors.
  70. */
  71. window.tb_position = function() {
  72. var tbWindow = $('#TB_window'),
  73. width = $(window).width(),
  74. H = $(window).height(),
  75. W = ( 833 < width ) ? 833 : width,
  76. adminbar_height = 0;
  77. if ( $('#wpadminbar').length ) {
  78. adminbar_height = parseInt( $('#wpadminbar').css('height'), 10 );
  79. }
  80. if ( tbWindow.length ) {
  81. tbWindow.width( W - 50 ).height( H - 45 - adminbar_height );
  82. $('#TB_iframeContent').width( W - 50 ).height( H - 75 - adminbar_height );
  83. tbWindow.css({'margin-left': '-' + parseInt( ( ( W - 50 ) / 2 ), 10 ) + 'px'});
  84. if ( typeof document.body.style.maxWidth !== 'undefined' )
  85. tbWindow.css({'top': 20 + adminbar_height + 'px', 'margin-top': '0'});
  86. }
  87. /**
  88. * Recalculates the new height and width for all links with a ThickBox class.
  89. *
  90. * @since 2.6.0
  91. */
  92. return $('a.thickbox').each( function() {
  93. var href = $(this).attr('href');
  94. if ( ! href ) return;
  95. href = href.replace(/&width=[0-9]+/g, '');
  96. href = href.replace(/&height=[0-9]+/g, '');
  97. $(this).attr( 'href', href + '&width=' + ( W - 80 ) + '&height=' + ( H - 85 - adminbar_height ) );
  98. });
  99. };
  100. // Add handler to recalculates the ThickBox position when the window is resized.
  101. $(window).on( 'resize', function(){ tb_position(); });
  102. })(jQuery);