container.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const Util = require('../util/common');
  2. const Shape = require('./shape');
  3. const SHAPE_MAP = {};
  4. const INDEX = '_INDEX';
  5. function getComparer(compare) {
  6. return function(left, right) {
  7. const result = compare(left, right);
  8. return result === 0 ? left[INDEX] - right[INDEX] : result;
  9. };
  10. }
  11. module.exports = {
  12. getGroupClass() {},
  13. getChildren() {
  14. return this.get('children');
  15. },
  16. addShape(type, cfg = {}) {
  17. const canvas = this.get('canvas');
  18. let shapeType = SHAPE_MAP[type];
  19. if (!shapeType) {
  20. shapeType = Util.upperFirst(type);
  21. SHAPE_MAP[type] = shapeType;
  22. }
  23. cfg.canvas = canvas;
  24. if (shapeType === 'Text' && canvas && canvas.get('fontFamily')) {
  25. cfg.attrs.fontFamily = cfg.attrs.fontFamily || canvas.get('fontFamily');
  26. }
  27. const shape = new Shape[shapeType](cfg);
  28. this.add(shape);
  29. return shape;
  30. },
  31. addGroup(cfg) {
  32. const canvas = this.get('canvas');
  33. const groupClass = this.getGroupClass();
  34. cfg = Util.mix({}, cfg);
  35. cfg.canvas = canvas;
  36. cfg.parent = this;
  37. const rst = new groupClass(cfg);
  38. this.add(rst);
  39. return rst;
  40. },
  41. contain(item) {
  42. const children = this.get('children');
  43. return children.indexOf(item) > -1;
  44. },
  45. sort() {
  46. const children = this.get('children');
  47. for (let i = 0, len = children.length; i < len; i++) {
  48. const child = children[i];
  49. child[INDEX] = i;
  50. }
  51. children.sort(getComparer(function(obj1, obj2) {
  52. return obj1.get('zIndex') - obj2.get('zIndex');
  53. }));
  54. return this;
  55. },
  56. clear() {
  57. const children = this.get('children');
  58. while (children.length !== 0) {
  59. children[children.length - 1].remove(true);
  60. }
  61. return this;
  62. },
  63. add(items) {
  64. const self = this;
  65. const children = self.get('children');
  66. if (!Util.isArray(items)) {
  67. items = [ items ];
  68. }
  69. for (let i = 0, len = items.length; i < len; i++) {
  70. const item = items[i];
  71. const parent = item.get('parent');
  72. if (parent) {
  73. const descendants = parent.get('children');
  74. Util.Array.remove(descendants, item);
  75. }
  76. self._setEvn(item);
  77. children.push(item);
  78. }
  79. return self;
  80. },
  81. _setEvn(item) {
  82. const self = this;
  83. item._attrs.parent = self;
  84. item._attrs.context = self._attrs.context;
  85. item._attrs.canvas = self._attrs.canvas;
  86. const clip = item._attrs.attrs.clip;
  87. if (clip) {
  88. clip.set('parent', self);
  89. clip.set('context', self.get('context'));
  90. }
  91. if (item._attrs.isGroup) {
  92. const children = item._attrs.children;
  93. for (let i = 0, len = children.length; i < len; i++) {
  94. item._setEvn(children[i]);
  95. }
  96. }
  97. }
  98. };