core.js 3.6 KB

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