core.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { keys, map } from '../core/util.js';
  2. export var SVGNS = 'http://www.w3.org/2000/svg';
  3. export var XLINKNS = 'http://www.w3.org/1999/xlink';
  4. export var XMLNS = 'http://www.w3.org/2000/xmlns/';
  5. export var XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
  6. export function createElement(name) {
  7. return document.createElementNS(SVGNS, name);
  8. }
  9. ;
  10. export function createVNode(tag, key, attrs, children, text) {
  11. return {
  12. tag: tag,
  13. attrs: attrs || {},
  14. children: children,
  15. text: text,
  16. key: key
  17. };
  18. }
  19. function createElementOpen(name, attrs) {
  20. var attrsStr = [];
  21. if (attrs) {
  22. for (var key in attrs) {
  23. var val = attrs[key];
  24. var part = key;
  25. if (val === false) {
  26. continue;
  27. }
  28. else if (val !== true && val != null) {
  29. part += "=\"" + val + "\"";
  30. }
  31. attrsStr.push(part);
  32. }
  33. }
  34. return "<" + name + " " + attrsStr.join(' ') + ">";
  35. }
  36. function createElementClose(name) {
  37. return "</" + name + ">";
  38. }
  39. export function vNodeToString(el, opts) {
  40. opts = opts || {};
  41. var S = opts.newline ? '\n' : '';
  42. function convertElToString(el) {
  43. var children = el.children, tag = el.tag, attrs = el.attrs;
  44. return createElementOpen(tag, attrs)
  45. + (el.text || '')
  46. + (children ? "" + S + map(children, function (child) { return convertElToString(child); }).join(S) + S : '')
  47. + createElementClose(tag);
  48. }
  49. return convertElToString(el);
  50. }
  51. export function getCssString(selectorNodes, animationNodes, opts) {
  52. opts = opts || {};
  53. var S = opts.newline ? '\n' : '';
  54. var bracketBegin = " {" + S;
  55. var bracketEnd = S + "}";
  56. var selectors = map(keys(selectorNodes), function (className) {
  57. return className + bracketBegin + map(keys(selectorNodes[className]), function (attrName) {
  58. return attrName + ":" + selectorNodes[className][attrName] + ";";
  59. }).join(S) + bracketEnd;
  60. }).join(S);
  61. var animations = map(keys(animationNodes), function (animationName) {
  62. return "@keyframes " + animationName + bracketBegin + map(keys(animationNodes[animationName]), function (percent) {
  63. return percent + bracketBegin + map(keys(animationNodes[animationName][percent]), function (attrName) {
  64. var val = animationNodes[animationName][percent][attrName];
  65. if (attrName === 'd') {
  66. val = "path(\"" + val + "\")";
  67. }
  68. return attrName + ":" + val + ";";
  69. }).join(S) + bracketEnd;
  70. }).join(S) + bracketEnd;
  71. }).join(S);
  72. if (!selectors && !animations) {
  73. return '';
  74. }
  75. return ['<![CDATA[', selectors, animations, ']]>'].join(S);
  76. }
  77. export function createBrushScope(zrId) {
  78. return {
  79. zrId: zrId,
  80. shadowCache: {},
  81. patternCache: {},
  82. gradientCache: {},
  83. clipPathCache: {},
  84. defs: {},
  85. cssNodes: {},
  86. cssAnims: {},
  87. cssClassIdx: 0,
  88. cssAnimIdx: 0,
  89. shadowIdx: 0,
  90. gradientIdx: 0,
  91. patternIdx: 0,
  92. clipPathIdx: 0
  93. };
  94. }
  95. export function createSVGVNode(width, height, children, useViewBox) {
  96. return createVNode('svg', 'root', {
  97. 'width': width,
  98. 'height': height,
  99. 'xmlns': SVGNS,
  100. 'xmlns:xlink': XLINKNS,
  101. 'version': '1.1',
  102. 'baseProfile': 'full',
  103. 'viewBox': useViewBox ? "0 0 " + width + " " + height : false
  104. }, children);
  105. }