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