GraphicModel.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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;
  50. // Set id and type after id assigned.
  51. newElOption.id = resultItem.keyInfo.id;
  52. !newElOption.type && existElOption && (newElOption.type = existElOption.type);
  53. // Set parent id if not specified
  54. if (newElOption.parentId == null) {
  55. var newElParentOption = newElOption.parentOption;
  56. if (newElParentOption) {
  57. newElOption.parentId = newElParentOption.id;
  58. } else if (existElOption) {
  59. newElOption.parentId = existElOption.parentId;
  60. }
  61. }
  62. // Clear
  63. newElOption.parentOption = null;
  64. }
  65. function isSetLoc(obj, props) {
  66. var isSet;
  67. zrUtil.each(props, function (prop) {
  68. obj[prop] != null && obj[prop] !== 'auto' && (isSet = true);
  69. });
  70. return isSet;
  71. }
  72. function mergeNewElOptionToExist(existList, index, newElOption) {
  73. // Update existing options, for `getOption` feature.
  74. var newElOptCopy = zrUtil.extend({}, newElOption);
  75. var existElOption = existList[index];
  76. var $action = newElOption.$action || 'merge';
  77. if ($action === 'merge') {
  78. if (existElOption) {
  79. if (process.env.NODE_ENV !== 'production') {
  80. var newType = newElOption.type;
  81. zrUtil.assert(!newType || existElOption.type === newType, 'Please set $action: "replace" to change `type`');
  82. }
  83. // We can ensure that newElOptCopy and existElOption are not
  84. // the same object, so `merge` will not change newElOptCopy.
  85. zrUtil.merge(existElOption, newElOptCopy, true);
  86. // Rigid body, use ignoreSize.
  87. mergeLayoutParam(existElOption, newElOptCopy, {
  88. ignoreSize: true
  89. });
  90. // Will be used in render.
  91. copyLayoutParams(newElOption, existElOption);
  92. // Copy transition info to new option so it can be used in the transition.
  93. // DO IT AFTER merge
  94. copyTransitionInfo(newElOption, existElOption);
  95. copyTransitionInfo(newElOption, existElOption, 'shape');
  96. copyTransitionInfo(newElOption, existElOption, 'style');
  97. copyTransitionInfo(newElOption, existElOption, 'extra');
  98. // Copy clipPath
  99. newElOption.clipPath = existElOption.clipPath;
  100. } else {
  101. existList[index] = newElOptCopy;
  102. }
  103. } else if ($action === 'replace') {
  104. existList[index] = newElOptCopy;
  105. } else if ($action === 'remove') {
  106. // null will be cleaned later.
  107. existElOption && (existList[index] = null);
  108. }
  109. }
  110. var TRANSITION_PROPS_TO_COPY = ['transition', 'enterFrom', 'leaveTo'];
  111. var ROOT_TRANSITION_PROPS_TO_COPY = TRANSITION_PROPS_TO_COPY.concat(['enterAnimation', 'updateAnimation', 'leaveAnimation']);
  112. function copyTransitionInfo(target, source, targetProp) {
  113. if (targetProp) {
  114. if (!target[targetProp] && source[targetProp]) {
  115. // TODO avoid creating this empty object when there is no transition configuration.
  116. target[targetProp] = {};
  117. }
  118. target = target[targetProp];
  119. source = source[targetProp];
  120. }
  121. if (!target || !source) {
  122. return;
  123. }
  124. var props = targetProp ? TRANSITION_PROPS_TO_COPY : ROOT_TRANSITION_PROPS_TO_COPY;
  125. for (var i = 0; i < props.length; i++) {
  126. var prop = props[i];
  127. if (target[prop] == null && source[prop] != null) {
  128. target[prop] = source[prop];
  129. }
  130. }
  131. }
  132. function setLayoutInfoToExist(existItem, newElOption) {
  133. if (!existItem) {
  134. return;
  135. }
  136. existItem.hv = newElOption.hv = [
  137. // Rigid body, don't care about `width`.
  138. isSetLoc(newElOption, ['left', 'right']),
  139. // Rigid body, don't care about `height`.
  140. isSetLoc(newElOption, ['top', 'bottom'])];
  141. // Give default group size. Otherwise layout error may occur.
  142. if (existItem.type === 'group') {
  143. var existingGroupOpt = existItem;
  144. var newGroupOpt = newElOption;
  145. existingGroupOpt.width == null && (existingGroupOpt.width = newGroupOpt.width = 0);
  146. existingGroupOpt.height == null && (existingGroupOpt.height = newGroupOpt.height = 0);
  147. }
  148. }
  149. var GraphicComponentModel = /** @class */function (_super) {
  150. __extends(GraphicComponentModel, _super);
  151. function GraphicComponentModel() {
  152. var _this = _super !== null && _super.apply(this, arguments) || this;
  153. _this.type = GraphicComponentModel.type;
  154. _this.preventAutoZ = true;
  155. return _this;
  156. }
  157. GraphicComponentModel.prototype.mergeOption = function (option, ecModel) {
  158. // Prevent default merge to elements
  159. var elements = this.option.elements;
  160. this.option.elements = null;
  161. _super.prototype.mergeOption.call(this, option, ecModel);
  162. this.option.elements = elements;
  163. };
  164. GraphicComponentModel.prototype.optionUpdated = function (newOption, isInit) {
  165. var thisOption = this.option;
  166. var newList = (isInit ? thisOption : newOption).elements;
  167. var existList = thisOption.elements = isInit ? [] : thisOption.elements;
  168. var flattenedList = [];
  169. this._flatten(newList, flattenedList, null);
  170. var mappingResult = modelUtil.mappingToExists(existList, flattenedList, 'normalMerge');
  171. // Clear elOptionsToUpdate
  172. var elOptionsToUpdate = this._elOptionsToUpdate = [];
  173. zrUtil.each(mappingResult, function (resultItem, index) {
  174. var newElOption = resultItem.newOption;
  175. if (process.env.NODE_ENV !== 'production') {
  176. zrUtil.assert(zrUtil.isObject(newElOption) || resultItem.existing, 'Empty graphic option definition');
  177. }
  178. if (!newElOption) {
  179. return;
  180. }
  181. elOptionsToUpdate.push(newElOption);
  182. setKeyInfoToNewElOption(resultItem, newElOption);
  183. mergeNewElOptionToExist(existList, index, newElOption);
  184. setLayoutInfoToExist(existList[index], newElOption);
  185. }, this);
  186. // Clean
  187. thisOption.elements = zrUtil.filter(existList, function (item) {
  188. // $action should be volatile, otherwise option gotten from
  189. // `getOption` will contain unexpected $action.
  190. item && delete item.$action;
  191. return item != null;
  192. });
  193. };
  194. /**
  195. * Convert
  196. * [{
  197. * type: 'group',
  198. * id: 'xx',
  199. * children: [{type: 'circle'}, {type: 'polygon'}]
  200. * }]
  201. * to
  202. * [
  203. * {type: 'group', id: 'xx'},
  204. * {type: 'circle', parentId: 'xx'},
  205. * {type: 'polygon', parentId: 'xx'}
  206. * ]
  207. */
  208. GraphicComponentModel.prototype._flatten = function (optionList, result, parentOption) {
  209. zrUtil.each(optionList, function (option) {
  210. if (!option) {
  211. return;
  212. }
  213. if (parentOption) {
  214. option.parentOption = parentOption;
  215. }
  216. result.push(option);
  217. var children = option.children;
  218. // here we don't judge if option.type is `group`
  219. // when new option doesn't provide `type`, it will cause that the children can't be updated.
  220. if (children && children.length) {
  221. this._flatten(children, result, option);
  222. }
  223. // Deleting for JSON output, and for not affecting group creation.
  224. delete option.children;
  225. }, this);
  226. };
  227. // FIXME
  228. // Pass to view using payload? setOption has a payload?
  229. GraphicComponentModel.prototype.useElOptionsToUpdate = function () {
  230. var els = this._elOptionsToUpdate;
  231. // Clear to avoid render duplicately when zooming.
  232. this._elOptionsToUpdate = null;
  233. return els;
  234. };
  235. GraphicComponentModel.type = 'graphic';
  236. GraphicComponentModel.defaultOption = {
  237. elements: []
  238. // parentId: null
  239. };
  240. return GraphicComponentModel;
  241. }(ComponentModel);
  242. export { GraphicComponentModel };