Make WordPress Core

Changeset 26693


Ignore:
Timestamp:
12/05/2013 10:00:17 PM (13 years ago)
Author:
nacin
Message:

Final SVG painter fixes.

  • wp.svgPainter and now moved to wp-admin.
  • Restore !important background-image handling.
  • Delay executing the IE9-specific base64 code if we don't need it.
  • Make painted icons lose their color after hover at the same speed as dashicons (100ms).

props azaozz.
fixes #26333.

Location:
trunk/src
Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/svg-painter.js

    r26690 r26693  
    33 *
    44 */
    5 var svgPainter = ( function( $, window, document, undefined ) {
     5
     6window.wp = window.wp || {};
     7
     8wp.svgPainter = ( function( $, window, document, undefined ) {
    69        'use strict';
    7         var selector, base64,
     10        var selector, base64, painter,
    811                colorscheme = {},
    912                elements = [];
    … …  
    1316                if ( document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' ) ) {
    1417                        $( document.body ).removeClass( 'no-svg' ).addClass( 'svg' );
    15                         svgPainter.init();
     18                        wp.svgPainter.init();
    1619                }
    1720        });
    … …  
    3639                        i = 0;
    3740
    38                 while( i < 256 ) {
    39                         c = String.fromCharCode(i);
    40                         a256 += c;
    41                         r256[i] = i;
    42                         r64[i] = b64.indexOf(c);
    43                         ++i;
     41                function init() {
     42                        while( i < 256 ) {
     43                                c = String.fromCharCode(i);
     44                                a256 += c;
     45                                r256[i] = i;
     46                                r64[i] = b64.indexOf(c);
     47                                ++i;
     48                        }
    4449                }
    4550
    … …  
    7883
    7984                function btoa( plain ) {
     85                        if ( ! c ) {
     86                                init();
     87                        }
     88
    8089                        plain = code( plain, false, r256, b64, 8, 6 );
    8190                        return plain + '===='.slice( ( plain.length % 4 ) || 4 );
    … …  
    8493                function atob( coded ) {
    8594                        var i;
     95
     96                        if ( ! c ) {
     97                                init();
     98                        }
    8699
    87100                        coded = coded.replace( /[^A-Za-z0-9\+\/\=]/g, '' );
    … …  
    106119        return {
    107120                init: function() {
     121                        painter = this;
    108122                        selector = $( '#adminmenu .wp-menu-image, #wpadminbar .ab-item' );
    109123
    … …  
    140154                                if ( $menuitem.hasClass( 'current' ) || $menuitem.hasClass( 'wp-has-current-submenu' ) ) {
    141155                                        // paint icon in 'current' color
    142                                         svgPainter.paintElement( $element, 'current' );
     156                                        painter.paintElement( $element, 'current' );
    143157                                } else {
    144158                                        // paint icon in base color
    145                                         svgPainter.paintElement( $element, 'base' );
     159                                        painter.paintElement( $element, 'base' );
    146160
    147161                                        // set hover callbacks
    148162                                        $menuitem.hover(
    149                                                 function() { svgPainter.paintElement( $element, 'focus' ); },
    150                                                 function() { svgPainter.paintElement( $element, 'base' ); }
     163                                                function() {
     164                                                        painter.paintElement( $element, 'focus' );
     165                                                },
     166                                                function() {
     167                                                        // Match the delay from hoverIntent
     168                                                        window.setTimeout( function() {
     169                                                                painter.paintElement( $element, 'base' );
     170                                                        }, 100 );
     171                                                }
    151172                                        );
    152173                                }
    … …  
    170191                        xml = $element.data( 'wp-ui-svg-' + color );
    171192
     193                        if ( xml === 'none' ) {
     194                                return;
     195                        }
     196
    172197                        if ( ! xml ) {
    173                                 encoded = $element.css( 'background-image' ).match( /.+data:image\/svg\+xml;base64,(.+?)['"]? ?\)/ );
     198                                encoded = $element.css( 'background-image' ).match( /.+data:image\/svg\+xml;base64,([A-Za-z0-9\+\/\=]+)/ );
    174199
    175200                                if ( ! encoded || ! encoded[1] ) {
     201                                        $element.data( 'wp-ui-svg-' + color, 'none' );
    176202                                        return;
    177203                                }
    178204
    179                                 if ( 'atob' in window ) {
    180                                         xml = window.atob( encoded[1] );
     205                                try {
     206                                        if ( 'atob' in window ) {
     207                                                xml = window.atob( encoded[1] );
     208                                        } else {
     209                                                xml = base64.atob( encoded[1] );
     210                                        }
     211                                } catch ( error ) {}
     212
     213                                if ( xml ) {
     214                                        // replace `fill` attributes
     215                                        xml = xml.replace( /fill="(.+?)"/g, 'fill="' + color + '"');
     216
     217                                        // replace `style` attributes
     218                                        xml = xml.replace( /style="(.+?)"/g, 'style="fill:' + color + '"');
     219
     220                                        // replace `fill` properties in `<style>` tags
     221                                        xml = xml.replace( /fill:.*?;/g, 'fill: ' + color + ';');
     222
     223                                        if ( 'btoa' in window ) {
     224                                                xml = window.btoa( xml );
     225                                        } else {
     226                                                xml = base64.btoa( xml );
     227                                        }
     228
     229                                        $element.data( 'wp-ui-svg-' + color, xml );
    181230                                } else {
    182                                         xml = base64.atob( encoded[1] );
    183                                 }
    184 
    185                                 // replace `fill` attributes
    186                                 xml = xml.replace( /fill="(.+?)"/g, 'fill="' + color + '"');
    187 
    188                                 // replace `style` attributes
    189                                 xml = xml.replace( /style="(.+?)"/g, 'style="fill:' + color + '"');
    190 
    191                                 // replace `fill` properties in `<style>` tags
    192                                 xml = xml.replace( /fill:.*?;/g, 'fill: ' + color + ';');
    193 
    194                                 if ( 'btoa' in window ) {
    195                                         xml = window.btoa( xml );
    196                                 } else {
    197                                         xml = base64.btoa( xml );
    198                                 }
    199 
    200                                 $element.data( 'wp-ui-svg-' + color, xml );
    201                         }
    202 
    203                         $element.css( 'background-image', 'url("data:image/svg+xml;base64,' + xml + '")' );
     231                                        $element.data( 'wp-ui-svg-' + color, 'none' );
     232                                        return;
     233                                }
     234                        }
     235
     236                        $element.attr( 'style', 'background-image: url("data:image/svg+xml;base64,' + xml + '") !important;' );
    204237                }
    205238        };
  • trunk/src/wp-admin/js/user-profile.js

    r26601 r26693  
    1 /* global ajaxurl, pwsL10n, svgPainter */
     1/* global ajaxurl, pwsL10n */
    22(function($){
    33
    … …  
    100100
    101101                                // repaint icons
    102                                 if ( typeof window.svgPainter !== 'undefined' ) {
     102                                if ( typeof wp !== 'undefined' && wp.svgPainter ) {
    103103                                        try {
    104104                                                colors = $.parseJSON( $this.children( '.icon_colors' ).val() );
    … …  
    106106
    107107                                        if ( colors ) {
    108                                                 svgPainter.setColors( colors );
    109                                                 svgPainter.paint();
     108                                                wp.svgPainter.setColors( colors );
     109                                                wp.svgPainter.paint();
    110110                                        }
    111111                                }
  • trunk/src/wp-includes/script-loader.php

    r26605 r26693  
    516516                $scripts->add( 'media-gallery', "/wp-admin/js/media-gallery$suffix.js", array('jquery'), false, 1 );
    517517
    518                 $scripts->add( 'svg-painter', '/wp-includes/js/svg-painter.js', array( 'jquery' ), false, 1 );
     518                $scripts->add( 'svg-painter', '/wp-admin/js/svg-painter.js', array( 'jquery' ), false, 1 );
    519519        }
    520520}
Note: See TracChangeset for help on using the changeset viewer.