GraphicModel.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import * as modelUtil from '../../util/model.js';
  43. import ComponentModel from '../../model/Component.js';
  44. import { copyLayoutParams, mergeLayoutParam } from '../../util/layout.js';
  45. ;
  46. ;
  47. ;
  48. export function setKeyInfoToNewElOption(resultItem, newElOption) {
  49. var existElOption = resultItem.existing; // Set id and type after id assigned.
  50. newElOption.id = resultItem.keyInfo.id;
  51. !newElOption.type && existElOption && (newElOption.type = existElOption.type); // Set parent id if not specified
  52. if (newElOption.parentId == null) {
  53. var newElParentOption = newElOption.parentOption;
  54. if (newElParentOption) {
  55. newElOption.parentId = newElParentOption.id;
  56. } else if (existElOption) {
  57. newElOption.parentId = existElOption.parentId;
  58. }
  59. } // Clear
  60. newElOption.parentOption = null;
  61. }
  62. function isSetLoc(obj, props) {
  63. var isSet;
  64. zrUtil.each(props, function (prop) {
  65. obj[prop] != null && obj[prop] !== 'auto' && (isSet = true);
  66. });
  67. return isSet;
  68. }
  69. function mergeNewElOptionToExist(existList, index, newElOption) {
  70. // Update existing options, for `getOption` feature.
  71. var newElOptCopy = zrUtil.extend({}, newElOption);
  72. var existElOption = existList[index];
  73. var $action = newElOption.$action || 'merge';
  74. if ($action === 'merge') {
  75. if (existElOption) {
  76. if (process.env.NODE_ENV !== 'production') {
  77. var newType = newElOption.type;
  78. zrUtil.assert(!newType || existElOption.type === newType, 'Please set $action: "replace" to change `type`');
  79. } // We can ensure that newElOptCopy and existElOption are not
  80. // the same object, so `merge` will not change newElOptCopy.
  81. zrUtil.merge(existElOption, newElOptCopy, true); // Rigid body, use ignoreSize.
  82. mergeLayoutParam(existElOption, newElOptCopy, {
  83. ignoreSize: true
  84. }); // Will be used in render.
  85. copyLayoutParams(newElOption, existElOption); // Copy transition info to new option so it can be used in the transition.
  86. // DO IT AFTER merge
  87. copyTransitionInfo(newElOption, existElOption);
  88. copyTransitionInfo(newElOption, existElOption, 'shape');
  89. copyTransitionInfo(newElOption, existElOption, 'style');
  90. copyTransitionInfo(newElOption, existElOption, 'extra'); // Copy clipPath
  91. newElOption.clipPath = existElOption.clipPath;
  92. } else {
  93. existList[index] = newElOptCopy;
  94. }
  95. } else if ($action === 'replace') {
  96. existList[index] = newElOptCopy;
  97. } else if ($action === 'remove') {
  98. // null will be cleaned later.
  99. existElOption && (existList[index] = null);
  100. }
  101. }
  102. var TRANSITION_PROPS_TO_COPY = ['transition', 'enterFrom', 'leaveTo'];
  103. var ROOT_TRANSITION_PROPS_TO_COPY = TRANSITION_PROPS_TO_COPY.concat(['enterAnimation', 'updateAnimation', 'leaveAnimation']);
  104. function copyTransitionInfo(target, source, targetProp) {
  105. if (targetProp) {
  106. if (!target[targetProp] && source[targetProp]) {
  107. // TODO avoid creating this empty object when there is no transition configuration.
  108. target[targetProp] = {};
  109. }
  110. target = target[targetProp];
  111. source = source[targetProp];
  112. }
  113. if (!target || !source) {
  114. return;
  115. }
  116. var props = targetProp ? TRANSITION_PROPS_TO_COPY : ROOT_TRANSITION_PROPS_TO_COPY;
  117. for (var i = 0; i < props.length; i++) {
  118. var prop = props[i];
  119. if (target[prop] == null && source[prop] != null) {
  120. target[prop] = source[prop];
  121. }
  122. }
  123. }
  124. function setLayoutInfoToExist(existItem, newElOption) {
  125. if (!existItem) {
  126. return;
  127. }
  128. existItem.hv = newElOption.hv = [// Rigid body, don't care about `width`.
  129. isSetLoc(newElOption, ['left', 'right']), // Rigid body, don't care about `height`.
  130. isSetLoc(newElOption, ['top', 'bottom'])]; // Give default group size. Otherwise layout error may occur.
  131. if (existItem.type === 'group') {
  132. var existingGroupOpt = existItem;
  133. var newGroupOpt = newElOption;
  134. existingGroupOpt.width == null && (existingGroupOpt.width = newGroupOpt.width = 0);
  135. existingGroupOpt.height == null && (existingGroupOpt.height = newGroupOpt.height = 0);
  136. }
  137. }
  138. var GraphicComponentModel =
  139. /** @class */
  140. function (_super) {
  141. __extends(GraphicComponentModel, _super);
  142. function GraphicComponentModel() {
  143. var _this = _super !== null && _super.apply(this, arguments) || this;
  144. _this.type = GraphicComponentModel.type;
  145. _this.preventAutoZ = true;
  146. return _this;
  147. }
  148. GraphicComponentModel.prototype.mergeOption = function (option, ecModel) {
  149. // Prevent default merge to elements
  150. var elements = this.option.elements;
  151. this.option.elements = null;
  152. _super.prototype.mergeOption.call(this, option, ecModel);
  153. this.option.elements = elements;
  154. };
  155. GraphicComponentModel.prototype.optionUpdated = function (newOption, isInit) {
  156. var thisOption = this.option;
  157. var newList = (isInit ? thisOption : newOption).elements;
  158. var existList = thisOption.elements = isInit ? [] : thisOption.elements;
  159. var flattenedList = [];
  160. this._flatten(newList, flattenedList, null);
  161. var mappingResult = modelUtil.mappingToExists(existList, flattenedList, 'normalMerge'); // Clear elOptionsToUpdate
  162. var elOptionsToUpdate = this._elOptionsToUpdate = [];
  163. zrUtil.each(mappingResult, function (resultItem, index) {
  164. var newElOption = resultItem.newOption;
  165. if (process.env.NODE_ENV !== 'production') {
  166. zrUtil.assert(zrUtil.isObject(newElOption) || resultItem.existing, 'Empty graphic option definition');
  167. }
  168. if (!newElOption) {
  169. return;
  170. }
  171. elOptionsToUpdate.push(newElOption);
  172. setKeyInfoToNewElOption(resultItem, newElOption);
  173. mergeNewElOptionToExist(existList, index, newElOption);
  174. setLayoutInfoToExist(existList[index], newElOption);
  175. }, this); // Clean
  176. thisOption.elements = zrUtil.filter(existList, function (item) {
  177. // $action should be volatile, otherwise option gotten from
  178. // `getOption` will contain unexpected $action.
  179. item && delete item.$action;
  180. return item != null;
  181. });
  182. };
  183. /**
  184. * Convert
  185. * [{
  186. * type: 'group',
  187. * id: 'xx',
  188. * children: [{type: 'circle'}, {type: 'polygon'}]
  189. * }]
  190. * to
  191. * [
  192. * {type: 'group', id: 'xx'},
  193. * {type: 'circle', parentId: 'xx'},
  194. * {type: 'polygon', parentId: 'xx'}
  195. * ]
  196. */
  197. GraphicComponentModel.prototype._flatten = function (optionList, result, parentOption) {
  198. zrUtil.each(optionList, function (option) {
  199. if (!option) {
  200. return;
  201. }
  202. if (parentOption) {
  203. option.parentOption = parentOption;
  204. }
  205. result.push(option);
  206. var children = option.children; // here we don't judge if option.type is `group`
  207. // when new option doesn't provide `type`, it will cause that the children can't be updated.
  208. if (children && children.length) {
  209. this._flatten(children, result, option);
  210. } // Deleting for JSON output, and for not affecting group creation.
  211. delete option.children;
  212. }, this);
  213. }; // FIXME
  214. // Pass to view using payload? setOption has a payload?
  215. GraphicComponentModel.prototype.useElOptionsToUpdate = function () {
  216. var els = this._elOptionsToUpdate; // Clear to avoid render duplicately when zooming.
  217. this._elOptionsToUpdate = null;
  218. return els;
  219. };
  220. GraphicComponentModel.type = 'graphic';
  221. GraphicComponentModel.defaultOption = {
  222. elements: [] // parentId: null
  223. };
  224. return GraphicComponentModel;
  225. }(ComponentModel);
  226. export { GraphicComponentModel };