PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/js/svg-painter.js

http://github.com/wordpress/wordpress
JavaScript | 241 lines | 170 code | 45 blank | 26 comment | 34 complexity | 78b62a77d26a89151be971c140a1e382 MD5 | raw file
Possible License(s): 0BSD
  1. /**
  2. * Attempt to re-color SVG icons used in the admin menu or the toolbar
  3. *
  4. * @output wp-admin/js/svg-painter.js
  5. */
  6. window.wp = window.wp || {};
  7. wp.svgPainter = ( function( $, window, document, undefined ) {
  8. 'use strict';
  9. var selector, base64, painter,
  10. colorscheme = {},
  11. elements = [];
  12. $(document).ready( function() {
  13. // Detection for browser SVG capability.
  14. if ( document.implementation.hasFeature( 'http://www.w3.org/TR/SVG11/feature#Image', '1.1' ) ) {
  15. $( document.body ).removeClass( 'no-svg' ).addClass( 'svg' );
  16. wp.svgPainter.init();
  17. }
  18. });
  19. /**
  20. * Needed only for IE9
  21. *
  22. * Based on jquery.base64.js 0.0.3 - https://github.com/yckart/jquery.base64.js
  23. *
  24. * Based on: https://gist.github.com/Yaffle/1284012
  25. *
  26. * Copyright (c) 2012 Yannick Albert (http://yckart.com)
  27. * Licensed under the MIT license
  28. * http://www.opensource.org/licenses/mit-license.php
  29. */
  30. base64 = ( function() {
  31. var c,
  32. b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  33. a256 = '',
  34. r64 = [256],
  35. r256 = [256],
  36. i = 0;
  37. function init() {
  38. while( i < 256 ) {
  39. c = String.fromCharCode(i);
  40. a256 += c;
  41. r256[i] = i;
  42. r64[i] = b64.indexOf(c);
  43. ++i;
  44. }
  45. }
  46. function code( s, discard, alpha, beta, w1, w2 ) {
  47. var tmp, length,
  48. buffer = 0,
  49. i = 0,
  50. result = '',
  51. bitsInBuffer = 0;
  52. s = String(s);
  53. length = s.length;
  54. while( i < length ) {
  55. c = s.charCodeAt(i);
  56. c = c < 256 ? alpha[c] : -1;
  57. buffer = ( buffer << w1 ) + c;
  58. bitsInBuffer += w1;
  59. while( bitsInBuffer >= w2 ) {
  60. bitsInBuffer -= w2;
  61. tmp = buffer >> bitsInBuffer;
  62. result += beta.charAt(tmp);
  63. buffer ^= tmp << bitsInBuffer;
  64. }
  65. ++i;
  66. }
  67. if ( ! discard && bitsInBuffer > 0 ) {
  68. result += beta.charAt( buffer << ( w2 - bitsInBuffer ) );
  69. }
  70. return result;
  71. }
  72. function btoa( plain ) {
  73. if ( ! c ) {
  74. init();
  75. }
  76. plain = code( plain, false, r256, b64, 8, 6 );
  77. return plain + '===='.slice( ( plain.length % 4 ) || 4 );
  78. }
  79. function atob( coded ) {
  80. var i;
  81. if ( ! c ) {
  82. init();
  83. }
  84. coded = coded.replace( /[^A-Za-z0-9\+\/\=]/g, '' );
  85. coded = String(coded).split('=');
  86. i = coded.length;
  87. do {
  88. --i;
  89. coded[i] = code( coded[i], true, r64, a256, 6, 8 );
  90. } while ( i > 0 );
  91. coded = coded.join('');
  92. return coded;
  93. }
  94. return {
  95. atob: atob,
  96. btoa: btoa
  97. };
  98. })();
  99. return {
  100. init: function() {
  101. painter = this;
  102. selector = $( '#adminmenu .wp-menu-image, #wpadminbar .ab-item' );
  103. this.setColors();
  104. this.findElements();
  105. this.paint();
  106. },
  107. setColors: function( colors ) {
  108. if ( typeof colors === 'undefined' && typeof window._wpColorScheme !== 'undefined' ) {
  109. colors = window._wpColorScheme;
  110. }
  111. if ( colors && colors.icons && colors.icons.base && colors.icons.current && colors.icons.focus ) {
  112. colorscheme = colors.icons;
  113. }
  114. },
  115. findElements: function() {
  116. selector.each( function() {
  117. var $this = $(this), bgImage = $this.css( 'background-image' );
  118. if ( bgImage && bgImage.indexOf( 'data:image/svg+xml;base64' ) != -1 ) {
  119. elements.push( $this );
  120. }
  121. });
  122. },
  123. paint: function() {
  124. // Loop through all elements.
  125. $.each( elements, function( index, $element ) {
  126. var $menuitem = $element.parent().parent();
  127. if ( $menuitem.hasClass( 'current' ) || $menuitem.hasClass( 'wp-has-current-submenu' ) ) {
  128. // Paint icon in 'current' color.
  129. painter.paintElement( $element, 'current' );
  130. } else {
  131. // Paint icon in base color.
  132. painter.paintElement( $element, 'base' );
  133. // Set hover callbacks.
  134. $menuitem.hover(
  135. function() {
  136. painter.paintElement( $element, 'focus' );
  137. },
  138. function() {
  139. // Match the delay from hoverIntent.
  140. window.setTimeout( function() {
  141. painter.paintElement( $element, 'base' );
  142. }, 100 );
  143. }
  144. );
  145. }
  146. });
  147. },
  148. paintElement: function( $element, colorType ) {
  149. var xml, encoded, color;
  150. if ( ! colorType || ! colorscheme.hasOwnProperty( colorType ) ) {
  151. return;
  152. }
  153. color = colorscheme[ colorType ];
  154. // Only accept hex colors: #101 or #101010.
  155. if ( ! color.match( /^(#[0-9a-f]{3}|#[0-9a-f]{6})$/i ) ) {
  156. return;
  157. }
  158. xml = $element.data( 'wp-ui-svg-' + color );
  159. if ( xml === 'none' ) {
  160. return;
  161. }
  162. if ( ! xml ) {
  163. encoded = $element.css( 'background-image' ).match( /.+data:image\/svg\+xml;base64,([A-Za-z0-9\+\/\=]+)/ );
  164. if ( ! encoded || ! encoded[1] ) {
  165. $element.data( 'wp-ui-svg-' + color, 'none' );
  166. return;
  167. }
  168. try {
  169. if ( 'atob' in window ) {
  170. xml = window.atob( encoded[1] );
  171. } else {
  172. xml = base64.atob( encoded[1] );
  173. }
  174. } catch ( error ) {}
  175. if ( xml ) {
  176. // Replace `fill` attributes.
  177. xml = xml.replace( /fill="(.+?)"/g, 'fill="' + color + '"');
  178. // Replace `style` attributes.
  179. xml = xml.replace( /style="(.+?)"/g, 'style="fill:' + color + '"');
  180. // Replace `fill` properties in `<style>` tags.
  181. xml = xml.replace( /fill:.*?;/g, 'fill: ' + color + ';');
  182. if ( 'btoa' in window ) {
  183. xml = window.btoa( xml );
  184. } else {
  185. xml = base64.btoa( xml );
  186. }
  187. $element.data( 'wp-ui-svg-' + color, xml );
  188. } else {
  189. $element.data( 'wp-ui-svg-' + color, 'none' );
  190. return;
  191. }
  192. }
  193. $element.attr( 'style', 'background-image: url("data:image/svg+xml;base64,' + xml + '") !important;' );
  194. }
  195. };
  196. })( jQuery, window, document );