Painter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { logError, each } from '../core/util.js';
  2. import * as vmlCore from './core.js';
  3. function parseInt10(val) {
  4. return parseInt(val, 10);
  5. }
  6. function VMLPainter(root, storage) {
  7. vmlCore.initVML();
  8. this.root = root;
  9. this.storage = storage;
  10. var vmlViewport = document.createElement('div');
  11. var vmlRoot = document.createElement('div');
  12. vmlViewport.style.cssText = 'display:inline-block;overflow:hidden;position:relative;width:300px;height:150px;';
  13. vmlRoot.style.cssText = 'position:absolute;left:0;top:0;';
  14. root.appendChild(vmlViewport);
  15. this._vmlRoot = vmlRoot;
  16. this._vmlViewport = vmlViewport;
  17. this.resize();
  18. var oldDelFromStorage = storage.delFromStorage;
  19. var oldAddToStorage = storage.addToStorage;
  20. storage.delFromStorage = function (el) {
  21. oldDelFromStorage.call(storage, el);
  22. if (el) {
  23. el.onRemove && el.onRemove(vmlRoot);
  24. }
  25. };
  26. storage.addToStorage = function (el) {
  27. el.onAdd && el.onAdd(vmlRoot);
  28. oldAddToStorage.call(storage, el);
  29. };
  30. this._firstPaint = true;
  31. }
  32. VMLPainter.prototype = {
  33. constructor: VMLPainter,
  34. getType: function () {
  35. return 'vml';
  36. },
  37. getViewportRoot: function () {
  38. return this._vmlViewport;
  39. },
  40. getViewportRootOffset: function () {
  41. var viewportRoot = this.getViewportRoot();
  42. if (viewportRoot) {
  43. return {
  44. offsetLeft: viewportRoot.offsetLeft || 0,
  45. offsetTop: viewportRoot.offsetTop || 0
  46. };
  47. }
  48. },
  49. refresh: function () {
  50. var list = this.storage.getDisplayList(true, true);
  51. this._paintList(list);
  52. },
  53. _paintList: function (list) {
  54. var vmlRoot = this._vmlRoot;
  55. for (var i = 0; i < list.length; i++) {
  56. var el = list[i];
  57. if (el.invisible || el.ignore) {
  58. if (!el.__alreadyNotVisible) {
  59. el.onRemove(vmlRoot);
  60. }
  61. el.__alreadyNotVisible = true;
  62. }
  63. else {
  64. if (el.__alreadyNotVisible) {
  65. el.onAdd(vmlRoot);
  66. }
  67. el.__alreadyNotVisible = false;
  68. if (el.__dirty) {
  69. el.beforeBrush && el.beforeBrush();
  70. (el.brushVML || el.brush).call(el, vmlRoot);
  71. el.afterBrush && el.afterBrush();
  72. }
  73. }
  74. el.__dirty = false;
  75. }
  76. if (this._firstPaint) {
  77. this._vmlViewport.appendChild(vmlRoot);
  78. this._firstPaint = false;
  79. }
  80. },
  81. resize: function (width, height) {
  82. var width = width == null ? this._getWidth() : width;
  83. var height = height == null ? this._getHeight() : height;
  84. if (this._width !== width || this._height !== height) {
  85. this._width = width;
  86. this._height = height;
  87. var vmlViewportStyle = this._vmlViewport.style;
  88. vmlViewportStyle.width = width + 'px';
  89. vmlViewportStyle.height = height + 'px';
  90. }
  91. },
  92. dispose: function () {
  93. this.root.innerHTML = '';
  94. this._vmlRoot =
  95. this._vmlViewport =
  96. this.storage = null;
  97. },
  98. getWidth: function () {
  99. return this._width;
  100. },
  101. getHeight: function () {
  102. return this._height;
  103. },
  104. clear: function () {
  105. if (this._vmlViewport) {
  106. this.root.removeChild(this._vmlViewport);
  107. }
  108. },
  109. _getWidth: function () {
  110. var root = this.root;
  111. var stl = root.currentStyle;
  112. return ((root.clientWidth || parseInt10(stl.width))
  113. - parseInt10(stl.paddingLeft)
  114. - parseInt10(stl.paddingRight)) | 0;
  115. },
  116. _getHeight: function () {
  117. var root = this.root;
  118. var stl = root.currentStyle;
  119. return ((root.clientHeight || parseInt10(stl.height))
  120. - parseInt10(stl.paddingTop)
  121. - parseInt10(stl.paddingBottom)) | 0;
  122. }
  123. };
  124. function createMethodNotSupport(method) {
  125. return function () {
  126. logError('In IE8.0 VML mode painter not support method "' + method + '"');
  127. };
  128. }
  129. each([
  130. 'getLayer', 'insertLayer', 'eachLayer', 'eachBuiltinLayer', 'eachOtherLayer', 'getLayers',
  131. 'modLayer', 'delLayer', 'clearLayer', 'toDataURL', 'pathToImage'
  132. ], function (name) {
  133. VMLPainter.prototype[name] = createMethodNotSupport(name);
  134. });
  135. export default VMLPainter;