Make WordPress Core

Changeset 22569


Ignore:
Timestamp:
11/14/2012 09:31:53 AM (11 years ago)
Author:
koopersmith
Message:

Media: Declare wp.media.string methods in a single object. see #21390.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/js/media-upload.js

    r22568 r22569  
    109109    };
    110110
    111     wp.media.string = {};
    112 
    113     wp.media.string.link = function( attachment, props ) {
    114         var linkTo  = getUserSetting( 'urlbutton', 'post' ),
     111    wp.media.string = {
     112        link: function( attachment, props ) {
     113            var linkTo  = getUserSetting( 'urlbutton', 'post' ),
     114                options = {
     115                    tag:     'a',
     116                    content: attachment.get('title') || attachment.get('filename'),
     117                    attrs:   {
     118                        rel: 'attachment wp-att-' + attachment.id
     119                    }
     120                };
     121
     122            options.attrs.href = linkToUrl( attachment, props );
     123
     124            return wp.html.string( options );
     125        },
     126
     127        image: function( attachment, props ) {
     128            var classes, img, options, size, shortcode, html;
     129
     130            props = _.defaults( props || {}, {
     131                img:   {},
     132                align: getUserSetting( 'align', 'none' ),
     133                size:  getUserSetting( 'imgsize', 'medium' ),
     134                link:  getUserSetting( 'urlbutton', 'post' )
     135            });
     136
     137            props.linkUrl = linkToUrl( attachment, props );
     138
     139            attachment = attachment.toJSON();
     140
     141            img     = _.clone( props.img );
     142            classes = img['class'] ? img['class'].split(/\s+/) : [];
     143            size    = attachment.sizes ? attachment.sizes[ props.size ] : {};
     144
     145            if ( ! size ) {
     146                delete props.size;
     147                size = attachment;
     148            }
     149
     150            img.width  = size.width;
     151            img.height = size.height;
     152            img.src    = size.url;
     153
     154            // Only assign the align class to the image if we're not printing
     155            // a caption, since the alignment is sent to the shortcode.
     156            if ( props.align && ! attachment.caption )
     157                classes.push( 'align' + props.align );
     158
     159            if ( props.size )
     160                classes.push( 'size-' + props.size );
     161
     162            classes.push( 'wp-image-' + attachment.id );
     163
     164            img['class'] = _.compact( classes ).join(' ');
     165
     166            // Generate `img` tag options.
    115167            options = {
    116                 tag:     'a',
    117                 content: attachment.get('title') || attachment.get('filename'),
    118                 attrs:   {
    119                     rel: 'attachment wp-att-' + attachment.id
    120                 }
     168                tag:    'img',
     169                attrs:  img,
     170                single: true
    121171            };
    122172
    123         options.attrs.href = linkToUrl( attachment, props );
    124 
    125         return wp.html.string( options );
    126     };
    127 
    128     wp.media.string.image = function( attachment, props ) {
    129         var classes, img, options, size, shortcode, html;
    130 
    131         props = _.defaults( props || {}, {
    132             img:   {},
    133             align: getUserSetting( 'align', 'none' ),
    134             size:  getUserSetting( 'imgsize', 'medium' ),
    135             link:  getUserSetting( 'urlbutton', 'post' )
    136         });
    137 
    138         props.linkUrl = linkToUrl( attachment, props );
    139 
    140         attachment = attachment.toJSON();
    141 
    142         img     = _.clone( props.img );
    143         classes = img['class'] ? img['class'].split(/\s+/) : [];
    144         size    = attachment.sizes ? attachment.sizes[ props.size ] : {};
    145 
    146         if ( ! size ) {
    147             delete props.size;
    148             size = attachment;
     173            // Generate the `href` based on the `link` property.
     174            if ( props.linkUrl ) {
     175                props.anchor = props.anchor || {};
     176                props.anchor.href = props.linkUrl;
     177            }
     178
     179            // Generate the `a` element options, if they exist.
     180            if ( props.anchor ) {
     181                options = {
     182                    tag:     'a',
     183                    attrs:   props.anchor,
     184                    content: options
     185                };
     186            }
     187
     188            html = wp.html.string( options );
     189
     190            // Generate the caption shortcode.
     191            if ( attachment.caption ) {
     192                shortcode = {
     193                    id:    'attachment_' + attachment.id,
     194                    width: img.width
     195                };
     196
     197                if ( props.align )
     198                    shortcode.align = 'align' + props.align;
     199
     200                html = wp.shortcode.string({
     201                    tag:     'caption',
     202                    attrs:   shortcode,
     203                    content: html + ' ' + attachment.caption
     204                });
     205            }
     206
     207            return html;
    149208        }
    150 
    151         img.width  = size.width;
    152         img.height = size.height;
    153         img.src    = size.url;
    154 
    155         // Only assign the align class to the image if we're not printing
    156         // a caption, since the alignment is sent to the shortcode.
    157         if ( props.align && ! attachment.caption )
    158             classes.push( 'align' + props.align );
    159 
    160         if ( props.size )
    161             classes.push( 'size-' + props.size );
    162 
    163         classes.push( 'wp-image-' + attachment.id );
    164 
    165         img['class'] = _.compact( classes ).join(' ');
    166 
    167         // Generate `img` tag options.
    168         options = {
    169             tag:    'img',
    170             attrs:  img,
    171             single: true
    172         };
    173 
    174         // Generate the `href` based on the `link` property.
    175         if ( props.linkUrl ) {
    176             props.anchor = props.anchor || {};
    177             props.anchor.href = props.linkUrl;
    178         }
    179 
    180         // Generate the `a` element options, if they exist.
    181         if ( props.anchor ) {
    182             options = {
    183                 tag:     'a',
    184                 attrs:   props.anchor,
    185                 content: options
    186             };
    187         }
    188 
    189         html = wp.html.string( options );
    190 
    191         // Generate the caption shortcode.
    192         if ( attachment.caption ) {
    193             shortcode = {
    194                 id:    'attachment_' + attachment.id,
    195                 width: img.width
    196             };
    197 
    198             if ( props.align )
    199                 shortcode.align = 'align' + props.align;
    200 
    201             html = wp.shortcode.string({
    202                 tag:     'caption',
    203                 attrs:   shortcode,
    204                 content: html + ' ' + attachment.caption
    205             });
    206         }
    207 
    208         return html;
    209209    };
    210210
Note: See TracChangeset for help on using the changeset viewer.