Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import pc1 from '../../../lib/ui/ProductClasses/svg/1.svg';
  2.  
  3. // todo: webpack imports it as b64, better just load it as string
  4. function b64DecodeUnicode(str) {
  5. return decodeURIComponent(Array.prototype.map.call(window.atob(str), function(c) {
  6. return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  7. }).join(''));
  8. }
  9.  
  10. const pc1String = b64DecodeUnicode(pc1.split(',')[1]);
  11.  
  12. function createSElement(s) {
  13. const container = document.createElement('div');
  14. container.innerHTML = s;
  15. return container.children[0];
  16. }
  17.  
  18. // store all svgs as node elements, hmm
  19. const cache = {
  20. productClass1: createSElement(pc1String)
  21. };
  22.  
  23. function onProps(callerProps, props, tagName, name, value, index) {
  24. switch (name) {
  25. case 'fill':
  26. props[name] = callerProps.color || value;
  27. break;
  28. default:
  29. props[name] = value;
  30. }
  31. props.key = props.key || `k-${tagName}-${index || 0}`;
  32. // todo: add other cases like style, className, onClick on tagName svg
  33. return props;
  34. }
  35.  
  36. function buildS(el, callerProps, index) {
  37. let props = {};
  38. for (let att, i = 0, atts = el.attributes, n = atts.length; i < n; i++) {
  39. att = atts[i];
  40. props = onProps(callerProps, props, el.tagName, att.nodeName, att.nodeValue, index);
  41. }
  42. if (el.children.length > 0) {
  43. return React.createElement(el.tagName, props, _.map(el.children, (e, i) => buildS(e, callerProps, i)));
  44. } else {
  45. return React.createElement(el.tagName, props);
  46. }
  47. }
  48.  
  49. const DynamicSvg = React.createClass({
  50. render() {
  51. const {name, ...rest} = this.props;
  52. const s = cache[name];
  53. if (s) {
  54. // memoize buildS
  55. return buildS(s, rest);
  56. } else {
  57. return null;
  58. }
  59. }
  60. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement