Make WordPress Core

Changeset 23851


Ignore:
Timestamp:
03/29/2013 06:42:54 AM (13 years ago)
Author:
markjaquith
Message:

Defer JS to document ready

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/post-formats.js

    r23850 r23851  
    33(function($) {
    44    var mediaFrame, lastMimeType, lastMenu, mediaPreview;
     5    $(function(){
     6        // Post formats selection
     7        $('.post-format-options a').on( 'click', function(e){
     8            var $this = $(this), editor, body,
     9                parent = $this.parent(),
     10                format = $this.data('wp-format'),
     11                container = $('#post-body-content'),
     12                description = $('.post-format-description');
    513
    6     // Post formats selection
    7     $('.post-format-options a').on( 'click', function(e){
    8         var $this = $(this), editor, body,
    9             parent = $this.parent(),
    10             format = $this.data('wp-format'),
    11             container = $('#post-body-content'),
    12             description = $('.post-format-description');
     14            parent.find('a.active').removeClass('active');
     15            $this.addClass('active');
     16            $('#icon-edit').removeClass(postFormats.currentPostFormat).addClass(format);
     17            $('#post_format').val(format);
    1318
    14         parent.find('a.active').removeClass('active');
    15         $this.addClass('active');
    16         $('#icon-edit').removeClass(postFormats.currentPostFormat).addClass(format);
    17         $('#post_format').val(format);
     19            container.get(0).className = container.get(0).className.replace( /\bwp-format-[^ ]+/, '' );
     20            container.addClass('wp-format-' + format);
     21            $('#title').focus();
    1822
    19         container.get(0).className = container.get(0).className.replace( /\bwp-format-[^ ]+/, '' );
    20         container.addClass('wp-format-' + format);
    21         $('#title').focus();
     23            // Update description line
     24            description.html($this.data('description'));
    2225
    23         // Update description line
    24         description.html($this.data('description'));
     26            if (description.not(':visible'))
     27                description.slideDown('fast');
    2528
    26         if (description.not(':visible'))
    27             description.slideDown('fast');
     29            if ( typeof tinymce != 'undefined' ) {
     30                editor = tinymce.get('content');
    2831
    29         if ( typeof tinymce != 'undefined' ) {
    30             editor = tinymce.get('content');
     32                if ( editor ) {
     33                    body = editor.getBody();
     34                    body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
     35                    editor.dom.addClass( body, 'post-format-' + format );
     36                }
     37            }
    3138
    32             if ( editor ) {
    33                 body = editor.getBody();
    34                 body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
    35                 editor.dom.addClass( body, 'post-format-' + format );
    36             }
    37         }
     39            postFormats.currentPostFormat = format;
    3840
    39         postFormats.currentPostFormat = format;
    40 
    41         e.preventDefault();
    42     }).on('mouseenter focusin', function () {
    43         $('.post-format-tip').html( $(this).prop('title') );
    44     }).on('mouseleave focusout', function () {
    45         $('.post-format-tip').html( $('.post-format-options a.active').prop('title') );
    46     });
    47 
    48     // Media selection
    49     $('.wp-format-media-select').click(function (event) {
    50         event.preventDefault();
    51         var $el = $(this), $holder, $field, mime = 'image', menu = '',
    52             $holder = $el.closest('.wp-format-media-holder'),
    53             $field = $( '#wp_format_' + $holder.data('format') );
    54 
    55         switch ( $holder.data('format') ) {
    56             case 'audio':
    57                 mime = 'audio';
    58                 break;
    59             case 'video':
    60                 mime = 'video';
    61                 break;
    62         }
    63 
    64         // If the media frame already exists, reopen it.
    65         if ( mediaFrame && lastMimeType === mime && lastMenu === menu ) {
    66             mediaFrame.open();
    67             return;
    68         }
    69 
    70         lastMimeType = mime;
    71         lastMenu = menu;
    72 
    73         // Create the media frame.
    74         mediaFrame = wp.media.frames.formatMedia = wp.media({
    75             // Set the title of the modal.
    76             title: $el.data('choose'),
    77 
    78             // Set the menu sidebar of the modal, if applicable
    79             toolbar: menu,
    80 
    81             // Tell the modal to show only items matching the current mime type.
    82             library: {
    83                 type: mime
    84             },
    85 
    86             // Customize the submit button.
    87             button: {
    88                 // Set the text of the button.
    89                 text: $el.data('update')
    90             }
     41            e.preventDefault();
     42        }).on('mouseenter focusin', function () {
     43            $('.post-format-tip').html( $(this).prop('title') );
     44        }).on('mouseleave focusout', function () {
     45            $('.post-format-tip').html( $('.post-format-options a.active').prop('title') );
    9146        });
    9247
    93         mediaPreview = function (format, url, mime) {
    94             $('#' + format + '-preview').remove();
    95             $holder.parent().prepend( '<div id="' + format + '-preview" class="wp-format-media-preview">' +
    96                 '<' + format + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' +
    97                     '<source type="' + mime + '" src="' + url + '" />' +
    98                 '</' + format + '></div>' );
    99             $('.wp-' + format + '-shortcode').mediaelementplayer();
    100         };
     48        // Media selection
     49        $('.wp-format-media-select').click(function (event) {
     50            event.preventDefault();
     51            var $el = $(this), $holder, $field, mime = 'image', menu = '',
     52                $holder = $el.closest('.wp-format-media-holder'),
     53                $field = $( '#wp_format_' + $holder.data('format') );
    10154
    102         // When an image is selected, run a callback.
    103         mediaFrame.on( 'select', function () {
    104             // Grab the selected attachment.
    105             var attachment = mediaFrame.state().get('selection').first(), mime, url, id;
     55            switch ( $holder.data('format') ) {
     56                case 'audio':
     57                    mime = 'audio';
     58                    break;
     59                case 'video':
     60                    mime = 'video';
     61                    break;
     62            }
    10663
    107             id = attachment.get('id');
    108             url = attachment.get('url');
    109             mime = attachment.get('mime');
     64            // If the media frame already exists, reopen it.
     65            if ( mediaFrame && lastMimeType === mime && lastMenu === menu ) {
     66                mediaFrame.open();
     67                return;
     68            }
    11069
    111             if ( 0 === mime.indexOf('audio') ) {
    112                 $field.val(url);
    113                 // show one preview at a time
    114                 mediaPreview('audio', url, mime);
    115             } else if ( 0 === mime.indexOf('video') ) {
    116                 $field.val(url);
    117                 // show one preview at a time
    118                 mediaPreview('video', url, mime);
    119             } else {
    120                 // set the hidden input's value
    121                 $field.val(id);
    122                 // Show the image in the placeholder
    123                 $el.html('<img src="' + url + '" />');
    124                 $holder.removeClass('empty').show();
    125             }
     70            lastMimeType = mime;
     71            lastMenu = menu;
     72
     73            // Create the media frame.
     74            mediaFrame = wp.media.frames.formatMedia = wp.media({
     75                // Set the title of the modal.
     76                title: $el.data('choose'),
     77
     78                // Set the menu sidebar of the modal, if applicable
     79                toolbar: menu,
     80
     81                // Tell the modal to show only items matching the current mime type.
     82                library: {
     83                    type: mime
     84                },
     85
     86                // Customize the submit button.
     87                button: {
     88                    // Set the text of the button.
     89                    text: $el.data('update')
     90                }
     91            });
     92
     93            mediaPreview = function (format, url, mime) {
     94                $('#' + format + '-preview').remove();
     95                $holder.parent().prepend( '<div id="' + format + '-preview" class="wp-format-media-preview">' +
     96                    '<' + format + ' class="wp-' + format + '-shortcode" controls="controls" preload="none">' +
     97                        '<source type="' + mime + '" src="' + url + '" />' +
     98                    '</' + format + '></div>' );
     99                $('.wp-' + format + '-shortcode').mediaelementplayer();
     100            };
     101
     102            // When an image is selected, run a callback.
     103            mediaFrame.on( 'select', function () {
     104                // Grab the selected attachment.
     105                var attachment = mediaFrame.state().get('selection').first(), mime, url, id;
     106
     107                id = attachment.get('id');
     108                url = attachment.get('url');
     109                mime = attachment.get('mime');
     110
     111                if ( 0 === mime.indexOf('audio') ) {
     112                    $field.val(url);
     113                    // show one preview at a time
     114                    mediaPreview('audio', url, mime);
     115                } else if ( 0 === mime.indexOf('video') ) {
     116                    $field.val(url);
     117                    // show one preview at a time
     118                    mediaPreview('video', url, mime);
     119                } else {
     120                    // set the hidden input's value
     121                    $field.val(id);
     122                    // Show the image in the placeholder
     123                    $el.html('<img src="' + url + '" />');
     124                    $holder.removeClass('empty').show();
     125                }
     126            });
     127
     128            mediaFrame.open();
    126129        });
    127 
    128         mediaFrame.open();
    129130    });
    130131})(jQuery);
Note: See TracChangeset for help on using the changeset viewer.