From 43ca132c43395762d8c32d27bff68e837448e44d Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Fri, 29 Mar 2013 06:42:54 +0000 Subject: [PATCH] Defer JS to document ready git-svn-id: http://core.svn.wordpress.org/trunk@23851 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/js/post-formats.js | 221 ++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 110 deletions(-) diff --git a/wp-admin/js/post-formats.js b/wp-admin/js/post-formats.js index fe6f66907b..d7b799c268 100644 --- a/wp-admin/js/post-formats.js +++ b/wp-admin/js/post-formats.js @@ -2,129 +2,130 @@ window.wp = window.wp || {}; (function($) { var mediaFrame, lastMimeType, lastMenu, mediaPreview; + $(function(){ + // Post formats selection + $('.post-format-options a').on( 'click', function(e){ + var $this = $(this), editor, body, + parent = $this.parent(), + format = $this.data('wp-format'), + container = $('#post-body-content'), + description = $('.post-format-description'); - // Post formats selection - $('.post-format-options a').on( 'click', function(e){ - var $this = $(this), editor, body, - parent = $this.parent(), - format = $this.data('wp-format'), - container = $('#post-body-content'), - description = $('.post-format-description'); + parent.find('a.active').removeClass('active'); + $this.addClass('active'); + $('#icon-edit').removeClass(postFormats.currentPostFormat).addClass(format); + $('#post_format').val(format); - parent.find('a.active').removeClass('active'); - $this.addClass('active'); - $('#icon-edit').removeClass(postFormats.currentPostFormat).addClass(format); - $('#post_format').val(format); + container.get(0).className = container.get(0).className.replace( /\bwp-format-[^ ]+/, '' ); + container.addClass('wp-format-' + format); + $('#title').focus(); - container.get(0).className = container.get(0).className.replace( /\bwp-format-[^ ]+/, '' ); - container.addClass('wp-format-' + format); - $('#title').focus(); + // Update description line + description.html($this.data('description')); - // Update description line - description.html($this.data('description')); + if (description.not(':visible')) + description.slideDown('fast'); - if (description.not(':visible')) - description.slideDown('fast'); + if ( typeof tinymce != 'undefined' ) { + editor = tinymce.get('content'); - if ( typeof tinymce != 'undefined' ) { - editor = tinymce.get('content'); - - if ( editor ) { - body = editor.getBody(); - body.className = body.className.replace( /\bpost-format-[^ ]+/, '' ); - editor.dom.addClass( body, 'post-format-' + format ); + if ( editor ) { + body = editor.getBody(); + body.className = body.className.replace( /\bpost-format-[^ ]+/, '' ); + editor.dom.addClass( body, 'post-format-' + format ); + } } - } - postFormats.currentPostFormat = format; + postFormats.currentPostFormat = format; - e.preventDefault(); - }).on('mouseenter focusin', function () { - $('.post-format-tip').html( $(this).prop('title') ); - }).on('mouseleave focusout', function () { - $('.post-format-tip').html( $('.post-format-options a.active').prop('title') ); - }); + e.preventDefault(); + }).on('mouseenter focusin', function () { + $('.post-format-tip').html( $(this).prop('title') ); + }).on('mouseleave focusout', function () { + $('.post-format-tip').html( $('.post-format-options a.active').prop('title') ); + }); - // Media selection - $('.wp-format-media-select').click(function (event) { - event.preventDefault(); - var $el = $(this), $holder, $field, mime = 'image', menu = '', - $holder = $el.closest('.wp-format-media-holder'), - $field = $( '#wp_format_' + $holder.data('format') ); + // Media selection + $('.wp-format-media-select').click(function (event) { + event.preventDefault(); + var $el = $(this), $holder, $field, mime = 'image', menu = '', + $holder = $el.closest('.wp-format-media-holder'), + $field = $( '#wp_format_' + $holder.data('format') ); - switch ( $holder.data('format') ) { - case 'audio': - mime = 'audio'; - break; - case 'video': - mime = 'video'; - break; - } + switch ( $holder.data('format') ) { + case 'audio': + mime = 'audio'; + break; + case 'video': + mime = 'video'; + break; + } + + // If the media frame already exists, reopen it. + if ( mediaFrame && lastMimeType === mime && lastMenu === menu ) { + mediaFrame.open(); + return; + } + + lastMimeType = mime; + lastMenu = menu; + + // Create the media frame. + mediaFrame = wp.media.frames.formatMedia = wp.media({ + // Set the title of the modal. + title: $el.data('choose'), + + // Set the menu sidebar of the modal, if applicable + toolbar: menu, + + // Tell the modal to show only items matching the current mime type. + library: { + type: mime + }, + + // Customize the submit button. + button: { + // Set the text of the button. + text: $el.data('update') + } + }); + + mediaPreview = function (format, url, mime) { + $('#' + format + '-preview').remove(); + $holder.parent().prepend( '
' + + '<' + format + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' + + '' + + '
' ); + $('.wp-' + format + '-shortcode').mediaelementplayer(); + }; + + // When an image is selected, run a callback. + mediaFrame.on( 'select', function () { + // Grab the selected attachment. + var attachment = mediaFrame.state().get('selection').first(), mime, url, id; + + id = attachment.get('id'); + url = attachment.get('url'); + mime = attachment.get('mime'); + + if ( 0 === mime.indexOf('audio') ) { + $field.val(url); + // show one preview at a time + mediaPreview('audio', url, mime); + } else if ( 0 === mime.indexOf('video') ) { + $field.val(url); + // show one preview at a time + mediaPreview('video', url, mime); + } else { + // set the hidden input's value + $field.val(id); + // Show the image in the placeholder + $el.html(''); + $holder.removeClass('empty').show(); + } + }); - // If the media frame already exists, reopen it. - if ( mediaFrame && lastMimeType === mime && lastMenu === menu ) { mediaFrame.open(); - return; - } - - lastMimeType = mime; - lastMenu = menu; - - // Create the media frame. - mediaFrame = wp.media.frames.formatMedia = wp.media({ - // Set the title of the modal. - title: $el.data('choose'), - - // Set the menu sidebar of the modal, if applicable - toolbar: menu, - - // Tell the modal to show only items matching the current mime type. - library: { - type: mime - }, - - // Customize the submit button. - button: { - // Set the text of the button. - text: $el.data('update') - } }); - - mediaPreview = function (format, url, mime) { - $('#' + format + '-preview').remove(); - $holder.parent().prepend( '
' + - '<' + format + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' + - '' + - '
' ); - $('.wp-' + format + '-shortcode').mediaelementplayer(); - }; - - // When an image is selected, run a callback. - mediaFrame.on( 'select', function () { - // Grab the selected attachment. - var attachment = mediaFrame.state().get('selection').first(), mime, url, id; - - id = attachment.get('id'); - url = attachment.get('url'); - mime = attachment.get('mime'); - - if ( 0 === mime.indexOf('audio') ) { - $field.val(url); - // show one preview at a time - mediaPreview('audio', url, mime); - } else if ( 0 === mime.indexOf('video') ) { - $field.val(url); - // show one preview at a time - mediaPreview('video', url, mime); - } else { - // set the hidden input's value - $field.val(id); - // Show the image in the placeholder - $el.html(''); - $holder.removeClass('empty').show(); - } - }); - - mediaFrame.open(); }); })(jQuery);