core.js 3.6 KB

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