Element.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. import Transformable, { TRANSFORMABLE_PROPS } from './core/Transformable.js';
  2. import Animator, { cloneValue } from './animation/Animator.js';
  3. import BoundingRect from './core/BoundingRect.js';
  4. import Eventful from './core/Eventful.js';
  5. import { calculateTextPosition, parsePercent } from './contain/text.js';
  6. import { guid, isObject, keys, extend, indexOf, logError, mixin, isArrayLike, isTypedArray, isGradientObject, filter, reduce } from './core/util.js';
  7. import { LIGHT_LABEL_COLOR, DARK_LABEL_COLOR } from './config.js';
  8. import { parse, stringify } from './tool/color.js';
  9. import { REDRAW_BIT } from './graphic/constants.js';
  10. import { invert } from './core/matrix.js';
  11. export var PRESERVED_NORMAL_STATE = '__zr_normal__';
  12. var PRIMARY_STATES_KEYS = TRANSFORMABLE_PROPS.concat(['ignore']);
  13. var DEFAULT_ANIMATABLE_MAP = reduce(TRANSFORMABLE_PROPS, function (obj, key) {
  14. obj[key] = true;
  15. return obj;
  16. }, { ignore: false });
  17. var tmpTextPosCalcRes = {};
  18. var tmpBoundingRect = new BoundingRect(0, 0, 0, 0);
  19. var tmpInnerTextTrans = [];
  20. var Element = (function () {
  21. function Element(props) {
  22. this.id = guid();
  23. this.animators = [];
  24. this.currentStates = [];
  25. this.states = {};
  26. this._init(props);
  27. }
  28. Element.prototype._init = function (props) {
  29. this.attr(props);
  30. };
  31. Element.prototype.drift = function (dx, dy, e) {
  32. switch (this.draggable) {
  33. case 'horizontal':
  34. dy = 0;
  35. break;
  36. case 'vertical':
  37. dx = 0;
  38. break;
  39. }
  40. var m = this.transform;
  41. if (!m) {
  42. m = this.transform = [1, 0, 0, 1, 0, 0];
  43. }
  44. m[4] += dx;
  45. m[5] += dy;
  46. this.decomposeTransform();
  47. this.markRedraw();
  48. };
  49. Element.prototype.beforeUpdate = function () { };
  50. Element.prototype.afterUpdate = function () { };
  51. Element.prototype.update = function () {
  52. this.updateTransform();
  53. if (this.__dirty) {
  54. this.updateInnerText();
  55. }
  56. };
  57. Element.prototype.updateInnerText = function (forceUpdate) {
  58. var textEl = this._textContent;
  59. if (textEl && (!textEl.ignore || forceUpdate)) {
  60. if (!this.textConfig) {
  61. this.textConfig = {};
  62. }
  63. var textConfig = this.textConfig;
  64. var isLocal = textConfig.local;
  65. var innerTransformable = textEl.innerTransformable;
  66. var textAlign = void 0;
  67. var textVerticalAlign = void 0;
  68. var textStyleChanged = false;
  69. innerTransformable.parent = isLocal ? this : null;
  70. var innerOrigin = false;
  71. innerTransformable.copyTransform(textEl);
  72. var hasPosition = textConfig.position != null;
  73. var autoOverflowArea = textConfig.autoOverflowArea;
  74. var layoutRect = void 0;
  75. if (autoOverflowArea || hasPosition) {
  76. layoutRect = tmpBoundingRect;
  77. if (textConfig.layoutRect) {
  78. layoutRect.copy(textConfig.layoutRect);
  79. }
  80. else {
  81. layoutRect.copy(this.getBoundingRect());
  82. }
  83. if (!isLocal) {
  84. layoutRect.applyTransform(this.transform);
  85. }
  86. }
  87. if (hasPosition) {
  88. if (this.calculateTextPosition) {
  89. this.calculateTextPosition(tmpTextPosCalcRes, textConfig, layoutRect);
  90. }
  91. else {
  92. calculateTextPosition(tmpTextPosCalcRes, textConfig, layoutRect);
  93. }
  94. innerTransformable.x = tmpTextPosCalcRes.x;
  95. innerTransformable.y = tmpTextPosCalcRes.y;
  96. textAlign = tmpTextPosCalcRes.align;
  97. textVerticalAlign = tmpTextPosCalcRes.verticalAlign;
  98. var textOrigin = textConfig.origin;
  99. if (textOrigin && textConfig.rotation != null) {
  100. var relOriginX = void 0;
  101. var relOriginY = void 0;
  102. if (textOrigin === 'center') {
  103. relOriginX = layoutRect.width * 0.5;
  104. relOriginY = layoutRect.height * 0.5;
  105. }
  106. else {
  107. relOriginX = parsePercent(textOrigin[0], layoutRect.width);
  108. relOriginY = parsePercent(textOrigin[1], layoutRect.height);
  109. }
  110. innerOrigin = true;
  111. innerTransformable.originX = -innerTransformable.x + relOriginX + (isLocal ? 0 : layoutRect.x);
  112. innerTransformable.originY = -innerTransformable.y + relOriginY + (isLocal ? 0 : layoutRect.y);
  113. }
  114. }
  115. if (textConfig.rotation != null) {
  116. innerTransformable.rotation = textConfig.rotation;
  117. }
  118. var textOffset = textConfig.offset;
  119. if (textOffset) {
  120. innerTransformable.x += textOffset[0];
  121. innerTransformable.y += textOffset[1];
  122. if (!innerOrigin) {
  123. innerTransformable.originX = -textOffset[0];
  124. innerTransformable.originY = -textOffset[1];
  125. }
  126. }
  127. var innerTextDefaultStyle = this._innerTextDefaultStyle || (this._innerTextDefaultStyle = {});
  128. if (autoOverflowArea) {
  129. var overflowRect = innerTextDefaultStyle.overflowRect =
  130. innerTextDefaultStyle.overflowRect || new BoundingRect(0, 0, 0, 0);
  131. innerTransformable.getLocalTransform(tmpInnerTextTrans);
  132. invert(tmpInnerTextTrans, tmpInnerTextTrans);
  133. BoundingRect.copy(overflowRect, layoutRect);
  134. overflowRect.applyTransform(tmpInnerTextTrans);
  135. }
  136. else {
  137. innerTextDefaultStyle.overflowRect = null;
  138. }
  139. var isInside = textConfig.inside == null
  140. ? (typeof textConfig.position === 'string' && textConfig.position.indexOf('inside') >= 0)
  141. : textConfig.inside;
  142. var textFill = void 0;
  143. var textStroke = void 0;
  144. var autoStroke = void 0;
  145. if (isInside && this.canBeInsideText()) {
  146. textFill = textConfig.insideFill;
  147. textStroke = textConfig.insideStroke;
  148. if (textFill == null || textFill === 'auto') {
  149. textFill = this.getInsideTextFill();
  150. }
  151. if (textStroke == null || textStroke === 'auto') {
  152. textStroke = this.getInsideTextStroke(textFill);
  153. autoStroke = true;
  154. }
  155. }
  156. else {
  157. textFill = textConfig.outsideFill;
  158. textStroke = textConfig.outsideStroke;
  159. if (textFill == null || textFill === 'auto') {
  160. textFill = this.getOutsideFill();
  161. }
  162. if (textStroke == null || textStroke === 'auto') {
  163. textStroke = this.getOutsideStroke(textFill);
  164. autoStroke = true;
  165. }
  166. }
  167. textFill = textFill || '#000';
  168. if (textFill !== innerTextDefaultStyle.fill
  169. || textStroke !== innerTextDefaultStyle.stroke
  170. || autoStroke !== innerTextDefaultStyle.autoStroke
  171. || textAlign !== innerTextDefaultStyle.align
  172. || textVerticalAlign !== innerTextDefaultStyle.verticalAlign) {
  173. textStyleChanged = true;
  174. innerTextDefaultStyle.fill = textFill;
  175. innerTextDefaultStyle.stroke = textStroke;
  176. innerTextDefaultStyle.autoStroke = autoStroke;
  177. innerTextDefaultStyle.align = textAlign;
  178. innerTextDefaultStyle.verticalAlign = textVerticalAlign;
  179. textEl.setDefaultTextStyle(innerTextDefaultStyle);
  180. }
  181. textEl.__dirty |= REDRAW_BIT;
  182. if (textStyleChanged) {
  183. textEl.dirtyStyle(true);
  184. }
  185. }
  186. };
  187. Element.prototype.canBeInsideText = function () {
  188. return true;
  189. };
  190. Element.prototype.getInsideTextFill = function () {
  191. return '#fff';
  192. };
  193. Element.prototype.getInsideTextStroke = function (textFill) {
  194. return '#000';
  195. };
  196. Element.prototype.getOutsideFill = function () {
  197. return this.__zr && this.__zr.isDarkMode() ? LIGHT_LABEL_COLOR : DARK_LABEL_COLOR;
  198. };
  199. Element.prototype.getOutsideStroke = function (textFill) {
  200. var backgroundColor = this.__zr && this.__zr.getBackgroundColor();
  201. var colorArr = typeof backgroundColor === 'string' && parse(backgroundColor);
  202. if (!colorArr) {
  203. colorArr = [255, 255, 255, 1];
  204. }
  205. var alpha = colorArr[3];
  206. var isDark = this.__zr.isDarkMode();
  207. for (var i = 0; i < 3; i++) {
  208. colorArr[i] = colorArr[i] * alpha + (isDark ? 0 : 255) * (1 - alpha);
  209. }
  210. colorArr[3] = 1;
  211. return stringify(colorArr, 'rgba');
  212. };
  213. Element.prototype.traverse = function (cb, context) { };
  214. Element.prototype.attrKV = function (key, value) {
  215. if (key === 'textConfig') {
  216. this.setTextConfig(value);
  217. }
  218. else if (key === 'textContent') {
  219. this.setTextContent(value);
  220. }
  221. else if (key === 'clipPath') {
  222. this.setClipPath(value);
  223. }
  224. else if (key === 'extra') {
  225. this.extra = this.extra || {};
  226. extend(this.extra, value);
  227. }
  228. else {
  229. this[key] = value;
  230. }
  231. };
  232. Element.prototype.hide = function () {
  233. this.ignore = true;
  234. this.markRedraw();
  235. };
  236. Element.prototype.show = function () {
  237. this.ignore = false;
  238. this.markRedraw();
  239. };
  240. Element.prototype.attr = function (keyOrObj, value) {
  241. if (typeof keyOrObj === 'string') {
  242. this.attrKV(keyOrObj, value);
  243. }
  244. else if (isObject(keyOrObj)) {
  245. var obj = keyOrObj;
  246. var keysArr = keys(obj);
  247. for (var i = 0; i < keysArr.length; i++) {
  248. var key = keysArr[i];
  249. this.attrKV(key, keyOrObj[key]);
  250. }
  251. }
  252. this.markRedraw();
  253. return this;
  254. };
  255. Element.prototype.saveCurrentToNormalState = function (toState) {
  256. this._innerSaveToNormal(toState);
  257. var normalState = this._normalState;
  258. for (var i = 0; i < this.animators.length; i++) {
  259. var animator = this.animators[i];
  260. var fromStateTransition = animator.__fromStateTransition;
  261. if (animator.getLoop() || fromStateTransition && fromStateTransition !== PRESERVED_NORMAL_STATE) {
  262. continue;
  263. }
  264. var targetName = animator.targetName;
  265. var target = targetName
  266. ? normalState[targetName] : normalState;
  267. animator.saveTo(target);
  268. }
  269. };
  270. Element.prototype._innerSaveToNormal = function (toState) {
  271. var normalState = this._normalState;
  272. if (!normalState) {
  273. normalState = this._normalState = {};
  274. }
  275. if (toState.textConfig && !normalState.textConfig) {
  276. normalState.textConfig = this.textConfig;
  277. }
  278. this._savePrimaryToNormal(toState, normalState, PRIMARY_STATES_KEYS);
  279. };
  280. Element.prototype._savePrimaryToNormal = function (toState, normalState, primaryKeys) {
  281. for (var i = 0; i < primaryKeys.length; i++) {
  282. var key = primaryKeys[i];
  283. if (toState[key] != null && !(key in normalState)) {
  284. normalState[key] = this[key];
  285. }
  286. }
  287. };
  288. Element.prototype.hasState = function () {
  289. return this.currentStates.length > 0;
  290. };
  291. Element.prototype.getState = function (name) {
  292. return this.states[name];
  293. };
  294. Element.prototype.ensureState = function (name) {
  295. var states = this.states;
  296. if (!states[name]) {
  297. states[name] = {};
  298. }
  299. return states[name];
  300. };
  301. Element.prototype.clearStates = function (noAnimation) {
  302. this.useState(PRESERVED_NORMAL_STATE, false, noAnimation);
  303. };
  304. Element.prototype.useState = function (stateName, keepCurrentStates, noAnimation, forceUseHoverLayer) {
  305. var toNormalState = stateName === PRESERVED_NORMAL_STATE;
  306. var hasStates = this.hasState();
  307. if (!hasStates && toNormalState) {
  308. return;
  309. }
  310. var currentStates = this.currentStates;
  311. var animationCfg = this.stateTransition;
  312. if (indexOf(currentStates, stateName) >= 0 && (keepCurrentStates || currentStates.length === 1)) {
  313. return;
  314. }
  315. var state;
  316. if (this.stateProxy && !toNormalState) {
  317. state = this.stateProxy(stateName);
  318. }
  319. if (!state) {
  320. state = (this.states && this.states[stateName]);
  321. }
  322. if (!state && !toNormalState) {
  323. logError("State " + stateName + " not exists.");
  324. return;
  325. }
  326. if (!toNormalState) {
  327. this.saveCurrentToNormalState(state);
  328. }
  329. var useHoverLayer = !!((state && state.hoverLayer) || forceUseHoverLayer);
  330. if (useHoverLayer) {
  331. this._toggleHoverLayerFlag(true);
  332. }
  333. this._applyStateObj(stateName, state, this._normalState, keepCurrentStates, !noAnimation && !this.__inHover && animationCfg && animationCfg.duration > 0, animationCfg);
  334. var textContent = this._textContent;
  335. var textGuide = this._textGuide;
  336. if (textContent) {
  337. textContent.useState(stateName, keepCurrentStates, noAnimation, useHoverLayer);
  338. }
  339. if (textGuide) {
  340. textGuide.useState(stateName, keepCurrentStates, noAnimation, useHoverLayer);
  341. }
  342. if (toNormalState) {
  343. this.currentStates = [];
  344. this._normalState = {};
  345. }
  346. else {
  347. if (!keepCurrentStates) {
  348. this.currentStates = [stateName];
  349. }
  350. else {
  351. this.currentStates.push(stateName);
  352. }
  353. }
  354. this._updateAnimationTargets();
  355. this.markRedraw();
  356. if (!useHoverLayer && this.__inHover) {
  357. this._toggleHoverLayerFlag(false);
  358. this.__dirty &= ~REDRAW_BIT;
  359. }
  360. return state;
  361. };
  362. Element.prototype.useStates = function (states, noAnimation, forceUseHoverLayer) {
  363. if (!states.length) {
  364. this.clearStates();
  365. }
  366. else {
  367. var stateObjects = [];
  368. var currentStates = this.currentStates;
  369. var len = states.length;
  370. var notChange = len === currentStates.length;
  371. if (notChange) {
  372. for (var i = 0; i < len; i++) {
  373. if (states[i] !== currentStates[i]) {
  374. notChange = false;
  375. break;
  376. }
  377. }
  378. }
  379. if (notChange) {
  380. return;
  381. }
  382. for (var i = 0; i < len; i++) {
  383. var stateName = states[i];
  384. var stateObj = void 0;
  385. if (this.stateProxy) {
  386. stateObj = this.stateProxy(stateName, states);
  387. }
  388. if (!stateObj) {
  389. stateObj = this.states[stateName];
  390. }
  391. if (stateObj) {
  392. stateObjects.push(stateObj);
  393. }
  394. }
  395. var lastStateObj = stateObjects[len - 1];
  396. var useHoverLayer = !!((lastStateObj && lastStateObj.hoverLayer) || forceUseHoverLayer);
  397. if (useHoverLayer) {
  398. this._toggleHoverLayerFlag(true);
  399. }
  400. var mergedState = this._mergeStates(stateObjects);
  401. var animationCfg = this.stateTransition;
  402. this.saveCurrentToNormalState(mergedState);
  403. this._applyStateObj(states.join(','), mergedState, this._normalState, false, !noAnimation && !this.__inHover && animationCfg && animationCfg.duration > 0, animationCfg);
  404. var textContent = this._textContent;
  405. var textGuide = this._textGuide;
  406. if (textContent) {
  407. textContent.useStates(states, noAnimation, useHoverLayer);
  408. }
  409. if (textGuide) {
  410. textGuide.useStates(states, noAnimation, useHoverLayer);
  411. }
  412. this._updateAnimationTargets();
  413. this.currentStates = states.slice();
  414. this.markRedraw();
  415. if (!useHoverLayer && this.__inHover) {
  416. this._toggleHoverLayerFlag(false);
  417. this.__dirty &= ~REDRAW_BIT;
  418. }
  419. }
  420. };
  421. Element.prototype.isSilent = function () {
  422. var el = this;
  423. while (el) {
  424. if (el.silent) {
  425. return true;
  426. }
  427. var hostEl = el.__hostTarget;
  428. el = hostEl ? (el.ignoreHostSilent ? null : hostEl) : el.parent;
  429. }
  430. return false;
  431. };
  432. Element.prototype._updateAnimationTargets = function () {
  433. for (var i = 0; i < this.animators.length; i++) {
  434. var animator = this.animators[i];
  435. if (animator.targetName) {
  436. animator.changeTarget(this[animator.targetName]);
  437. }
  438. }
  439. };
  440. Element.prototype.removeState = function (state) {
  441. var idx = indexOf(this.currentStates, state);
  442. if (idx >= 0) {
  443. var currentStates = this.currentStates.slice();
  444. currentStates.splice(idx, 1);
  445. this.useStates(currentStates);
  446. }
  447. };
  448. Element.prototype.replaceState = function (oldState, newState, forceAdd) {
  449. var currentStates = this.currentStates.slice();
  450. var idx = indexOf(currentStates, oldState);
  451. var newStateExists = indexOf(currentStates, newState) >= 0;
  452. if (idx >= 0) {
  453. if (!newStateExists) {
  454. currentStates[idx] = newState;
  455. }
  456. else {
  457. currentStates.splice(idx, 1);
  458. }
  459. }
  460. else if (forceAdd && !newStateExists) {
  461. currentStates.push(newState);
  462. }
  463. this.useStates(currentStates);
  464. };
  465. Element.prototype.toggleState = function (state, enable) {
  466. if (enable) {
  467. this.useState(state, true);
  468. }
  469. else {
  470. this.removeState(state);
  471. }
  472. };
  473. Element.prototype._mergeStates = function (states) {
  474. var mergedState = {};
  475. var mergedTextConfig;
  476. for (var i = 0; i < states.length; i++) {
  477. var state = states[i];
  478. extend(mergedState, state);
  479. if (state.textConfig) {
  480. mergedTextConfig = mergedTextConfig || {};
  481. extend(mergedTextConfig, state.textConfig);
  482. }
  483. }
  484. if (mergedTextConfig) {
  485. mergedState.textConfig = mergedTextConfig;
  486. }
  487. return mergedState;
  488. };
  489. Element.prototype._applyStateObj = function (stateName, state, normalState, keepCurrentStates, transition, animationCfg) {
  490. var needsRestoreToNormal = !(state && keepCurrentStates);
  491. if (state && state.textConfig) {
  492. this.textConfig = extend({}, keepCurrentStates ? this.textConfig : normalState.textConfig);
  493. extend(this.textConfig, state.textConfig);
  494. }
  495. else if (needsRestoreToNormal) {
  496. if (normalState.textConfig) {
  497. this.textConfig = normalState.textConfig;
  498. }
  499. }
  500. var transitionTarget = {};
  501. var hasTransition = false;
  502. for (var i = 0; i < PRIMARY_STATES_KEYS.length; i++) {
  503. var key = PRIMARY_STATES_KEYS[i];
  504. var propNeedsTransition = transition && DEFAULT_ANIMATABLE_MAP[key];
  505. if (state && state[key] != null) {
  506. if (propNeedsTransition) {
  507. hasTransition = true;
  508. transitionTarget[key] = state[key];
  509. }
  510. else {
  511. this[key] = state[key];
  512. }
  513. }
  514. else if (needsRestoreToNormal) {
  515. if (normalState[key] != null) {
  516. if (propNeedsTransition) {
  517. hasTransition = true;
  518. transitionTarget[key] = normalState[key];
  519. }
  520. else {
  521. this[key] = normalState[key];
  522. }
  523. }
  524. }
  525. }
  526. if (!transition) {
  527. for (var i = 0; i < this.animators.length; i++) {
  528. var animator = this.animators[i];
  529. var targetName = animator.targetName;
  530. if (!animator.getLoop()) {
  531. animator.__changeFinalValue(targetName
  532. ? (state || normalState)[targetName]
  533. : (state || normalState));
  534. }
  535. }
  536. }
  537. if (hasTransition) {
  538. this._transitionState(stateName, transitionTarget, animationCfg);
  539. }
  540. };
  541. Element.prototype._attachComponent = function (componentEl) {
  542. if (componentEl.__zr && !componentEl.__hostTarget) {
  543. if (process.env.NODE_ENV !== 'production') {
  544. throw new Error('Text element has been added to zrender.');
  545. }
  546. return;
  547. }
  548. if (componentEl === this) {
  549. if (process.env.NODE_ENV !== 'production') {
  550. throw new Error('Recursive component attachment.');
  551. }
  552. return;
  553. }
  554. var zr = this.__zr;
  555. if (zr) {
  556. componentEl.addSelfToZr(zr);
  557. }
  558. componentEl.__zr = zr;
  559. componentEl.__hostTarget = this;
  560. };
  561. Element.prototype._detachComponent = function (componentEl) {
  562. if (componentEl.__zr) {
  563. componentEl.removeSelfFromZr(componentEl.__zr);
  564. }
  565. componentEl.__zr = null;
  566. componentEl.__hostTarget = null;
  567. };
  568. Element.prototype.getClipPath = function () {
  569. return this._clipPath;
  570. };
  571. Element.prototype.setClipPath = function (clipPath) {
  572. if (this._clipPath && this._clipPath !== clipPath) {
  573. this.removeClipPath();
  574. }
  575. this._attachComponent(clipPath);
  576. this._clipPath = clipPath;
  577. this.markRedraw();
  578. };
  579. Element.prototype.removeClipPath = function () {
  580. var clipPath = this._clipPath;
  581. if (clipPath) {
  582. this._detachComponent(clipPath);
  583. this._clipPath = null;
  584. this.markRedraw();
  585. }
  586. };
  587. Element.prototype.getTextContent = function () {
  588. return this._textContent;
  589. };
  590. Element.prototype.setTextContent = function (textEl) {
  591. var previousTextContent = this._textContent;
  592. if (previousTextContent === textEl) {
  593. return;
  594. }
  595. if (previousTextContent && previousTextContent !== textEl) {
  596. this.removeTextContent();
  597. }
  598. if (process.env.NODE_ENV !== 'production') {
  599. if (textEl.__zr && !textEl.__hostTarget) {
  600. throw new Error('Text element has been added to zrender.');
  601. }
  602. }
  603. textEl.innerTransformable = new Transformable();
  604. this._attachComponent(textEl);
  605. this._textContent = textEl;
  606. this.markRedraw();
  607. };
  608. Element.prototype.setTextConfig = function (cfg) {
  609. if (!this.textConfig) {
  610. this.textConfig = {};
  611. }
  612. extend(this.textConfig, cfg);
  613. this.markRedraw();
  614. };
  615. Element.prototype.removeTextConfig = function () {
  616. this.textConfig = null;
  617. this.markRedraw();
  618. };
  619. Element.prototype.removeTextContent = function () {
  620. var textEl = this._textContent;
  621. if (textEl) {
  622. textEl.innerTransformable = null;
  623. this._detachComponent(textEl);
  624. this._textContent = null;
  625. this._innerTextDefaultStyle = null;
  626. this.markRedraw();
  627. }
  628. };
  629. Element.prototype.getTextGuideLine = function () {
  630. return this._textGuide;
  631. };
  632. Element.prototype.setTextGuideLine = function (guideLine) {
  633. if (this._textGuide && this._textGuide !== guideLine) {
  634. this.removeTextGuideLine();
  635. }
  636. this._attachComponent(guideLine);
  637. this._textGuide = guideLine;
  638. this.markRedraw();
  639. };
  640. Element.prototype.removeTextGuideLine = function () {
  641. var textGuide = this._textGuide;
  642. if (textGuide) {
  643. this._detachComponent(textGuide);
  644. this._textGuide = null;
  645. this.markRedraw();
  646. }
  647. };
  648. Element.prototype.markRedraw = function () {
  649. this.__dirty |= REDRAW_BIT;
  650. var zr = this.__zr;
  651. if (zr) {
  652. if (this.__inHover) {
  653. zr.refreshHover();
  654. }
  655. else {
  656. zr.refresh();
  657. }
  658. }
  659. if (this.__hostTarget) {
  660. this.__hostTarget.markRedraw();
  661. }
  662. };
  663. Element.prototype.dirty = function () {
  664. this.markRedraw();
  665. };
  666. Element.prototype._toggleHoverLayerFlag = function (inHover) {
  667. this.__inHover = inHover;
  668. var textContent = this._textContent;
  669. var textGuide = this._textGuide;
  670. if (textContent) {
  671. textContent.__inHover = inHover;
  672. }
  673. if (textGuide) {
  674. textGuide.__inHover = inHover;
  675. }
  676. };
  677. Element.prototype.addSelfToZr = function (zr) {
  678. if (this.__zr === zr) {
  679. return;
  680. }
  681. this.__zr = zr;
  682. var animators = this.animators;
  683. if (animators) {
  684. for (var i = 0; i < animators.length; i++) {
  685. zr.animation.addAnimator(animators[i]);
  686. }
  687. }
  688. if (this._clipPath) {
  689. this._clipPath.addSelfToZr(zr);
  690. }
  691. if (this._textContent) {
  692. this._textContent.addSelfToZr(zr);
  693. }
  694. if (this._textGuide) {
  695. this._textGuide.addSelfToZr(zr);
  696. }
  697. };
  698. Element.prototype.removeSelfFromZr = function (zr) {
  699. if (!this.__zr) {
  700. return;
  701. }
  702. this.__zr = null;
  703. var animators = this.animators;
  704. if (animators) {
  705. for (var i = 0; i < animators.length; i++) {
  706. zr.animation.removeAnimator(animators[i]);
  707. }
  708. }
  709. if (this._clipPath) {
  710. this._clipPath.removeSelfFromZr(zr);
  711. }
  712. if (this._textContent) {
  713. this._textContent.removeSelfFromZr(zr);
  714. }
  715. if (this._textGuide) {
  716. this._textGuide.removeSelfFromZr(zr);
  717. }
  718. };
  719. Element.prototype.animate = function (key, loop, allowDiscreteAnimation) {
  720. var target = key ? this[key] : this;
  721. if (process.env.NODE_ENV !== 'production') {
  722. if (!target) {
  723. logError('Property "'
  724. + key
  725. + '" is not existed in element '
  726. + this.id);
  727. return;
  728. }
  729. }
  730. var animator = new Animator(target, loop, allowDiscreteAnimation);
  731. key && (animator.targetName = key);
  732. this.addAnimator(animator, key);
  733. return animator;
  734. };
  735. Element.prototype.addAnimator = function (animator, key) {
  736. var zr = this.__zr;
  737. var el = this;
  738. animator.during(function () {
  739. el.updateDuringAnimation(key);
  740. }).done(function () {
  741. var animators = el.animators;
  742. var idx = indexOf(animators, animator);
  743. if (idx >= 0) {
  744. animators.splice(idx, 1);
  745. }
  746. });
  747. this.animators.push(animator);
  748. if (zr) {
  749. zr.animation.addAnimator(animator);
  750. }
  751. zr && zr.wakeUp();
  752. };
  753. Element.prototype.updateDuringAnimation = function (key) {
  754. this.markRedraw();
  755. };
  756. Element.prototype.stopAnimation = function (scope, forwardToLast) {
  757. var animators = this.animators;
  758. var len = animators.length;
  759. var leftAnimators = [];
  760. for (var i = 0; i < len; i++) {
  761. var animator = animators[i];
  762. if (!scope || scope === animator.scope) {
  763. animator.stop(forwardToLast);
  764. }
  765. else {
  766. leftAnimators.push(animator);
  767. }
  768. }
  769. this.animators = leftAnimators;
  770. return this;
  771. };
  772. Element.prototype.animateTo = function (target, cfg, animationProps) {
  773. animateTo(this, target, cfg, animationProps);
  774. };
  775. Element.prototype.animateFrom = function (target, cfg, animationProps) {
  776. animateTo(this, target, cfg, animationProps, true);
  777. };
  778. Element.prototype._transitionState = function (stateName, target, cfg, animationProps) {
  779. var animators = animateTo(this, target, cfg, animationProps);
  780. for (var i = 0; i < animators.length; i++) {
  781. animators[i].__fromStateTransition = stateName;
  782. }
  783. };
  784. Element.prototype.getBoundingRect = function () {
  785. return null;
  786. };
  787. Element.prototype.getPaintRect = function () {
  788. return null;
  789. };
  790. Element.initDefaultProps = (function () {
  791. var elProto = Element.prototype;
  792. elProto.type = 'element';
  793. elProto.name = '';
  794. elProto.ignore =
  795. elProto.silent =
  796. elProto.ignoreHostSilent =
  797. elProto.isGroup =
  798. elProto.draggable =
  799. elProto.dragging =
  800. elProto.ignoreClip =
  801. elProto.__inHover = false;
  802. elProto.__dirty = REDRAW_BIT;
  803. var logs = {};
  804. function logDeprecatedError(key, xKey, yKey) {
  805. if (!logs[key + xKey + yKey]) {
  806. console.warn("DEPRECATED: '" + key + "' has been deprecated. use '" + xKey + "', '" + yKey + "' instead");
  807. logs[key + xKey + yKey] = true;
  808. }
  809. }
  810. function createLegacyProperty(key, privateKey, xKey, yKey) {
  811. Object.defineProperty(elProto, key, {
  812. get: function () {
  813. if (process.env.NODE_ENV !== 'production') {
  814. logDeprecatedError(key, xKey, yKey);
  815. }
  816. if (!this[privateKey]) {
  817. var pos = this[privateKey] = [];
  818. enhanceArray(this, pos);
  819. }
  820. return this[privateKey];
  821. },
  822. set: function (pos) {
  823. if (process.env.NODE_ENV !== 'production') {
  824. logDeprecatedError(key, xKey, yKey);
  825. }
  826. this[xKey] = pos[0];
  827. this[yKey] = pos[1];
  828. this[privateKey] = pos;
  829. enhanceArray(this, pos);
  830. }
  831. });
  832. function enhanceArray(self, pos) {
  833. Object.defineProperty(pos, 0, {
  834. get: function () {
  835. return self[xKey];
  836. },
  837. set: function (val) {
  838. self[xKey] = val;
  839. }
  840. });
  841. Object.defineProperty(pos, 1, {
  842. get: function () {
  843. return self[yKey];
  844. },
  845. set: function (val) {
  846. self[yKey] = val;
  847. }
  848. });
  849. }
  850. }
  851. if (Object.defineProperty) {
  852. createLegacyProperty('position', '_legacyPos', 'x', 'y');
  853. createLegacyProperty('scale', '_legacyScale', 'scaleX', 'scaleY');
  854. createLegacyProperty('origin', '_legacyOrigin', 'originX', 'originY');
  855. }
  856. })();
  857. return Element;
  858. }());
  859. mixin(Element, Eventful);
  860. mixin(Element, Transformable);
  861. function animateTo(animatable, target, cfg, animationProps, reverse) {
  862. cfg = cfg || {};
  863. var animators = [];
  864. animateToShallow(animatable, '', animatable, target, cfg, animationProps, animators, reverse);
  865. var finishCount = animators.length;
  866. var doneHappened = false;
  867. var cfgDone = cfg.done;
  868. var cfgAborted = cfg.aborted;
  869. var doneCb = function () {
  870. doneHappened = true;
  871. finishCount--;
  872. if (finishCount <= 0) {
  873. doneHappened
  874. ? (cfgDone && cfgDone())
  875. : (cfgAborted && cfgAborted());
  876. }
  877. };
  878. var abortedCb = function () {
  879. finishCount--;
  880. if (finishCount <= 0) {
  881. doneHappened
  882. ? (cfgDone && cfgDone())
  883. : (cfgAborted && cfgAborted());
  884. }
  885. };
  886. if (!finishCount) {
  887. cfgDone && cfgDone();
  888. }
  889. if (animators.length > 0 && cfg.during) {
  890. animators[0].during(function (target, percent) {
  891. cfg.during(percent);
  892. });
  893. }
  894. for (var i = 0; i < animators.length; i++) {
  895. var animator = animators[i];
  896. if (doneCb) {
  897. animator.done(doneCb);
  898. }
  899. if (abortedCb) {
  900. animator.aborted(abortedCb);
  901. }
  902. if (cfg.force) {
  903. animator.duration(cfg.duration);
  904. }
  905. animator.start(cfg.easing);
  906. }
  907. return animators;
  908. }
  909. function copyArrShallow(source, target, len) {
  910. for (var i = 0; i < len; i++) {
  911. source[i] = target[i];
  912. }
  913. }
  914. function is2DArray(value) {
  915. return isArrayLike(value[0]);
  916. }
  917. function copyValue(target, source, key) {
  918. if (isArrayLike(source[key])) {
  919. if (!isArrayLike(target[key])) {
  920. target[key] = [];
  921. }
  922. if (isTypedArray(source[key])) {
  923. var len = source[key].length;
  924. if (target[key].length !== len) {
  925. target[key] = new (source[key].constructor)(len);
  926. copyArrShallow(target[key], source[key], len);
  927. }
  928. }
  929. else {
  930. var sourceArr = source[key];
  931. var targetArr = target[key];
  932. var len0 = sourceArr.length;
  933. if (is2DArray(sourceArr)) {
  934. var len1 = sourceArr[0].length;
  935. for (var i = 0; i < len0; i++) {
  936. if (!targetArr[i]) {
  937. targetArr[i] = Array.prototype.slice.call(sourceArr[i]);
  938. }
  939. else {
  940. copyArrShallow(targetArr[i], sourceArr[i], len1);
  941. }
  942. }
  943. }
  944. else {
  945. copyArrShallow(targetArr, sourceArr, len0);
  946. }
  947. targetArr.length = sourceArr.length;
  948. }
  949. }
  950. else {
  951. target[key] = source[key];
  952. }
  953. }
  954. function isValueSame(val1, val2) {
  955. return val1 === val2
  956. || isArrayLike(val1) && isArrayLike(val2) && is1DArraySame(val1, val2);
  957. }
  958. function is1DArraySame(arr0, arr1) {
  959. var len = arr0.length;
  960. if (len !== arr1.length) {
  961. return false;
  962. }
  963. for (var i = 0; i < len; i++) {
  964. if (arr0[i] !== arr1[i]) {
  965. return false;
  966. }
  967. }
  968. return true;
  969. }
  970. function animateToShallow(animatable, topKey, animateObj, target, cfg, animationProps, animators, reverse) {
  971. var targetKeys = keys(target);
  972. var duration = cfg.duration;
  973. var delay = cfg.delay;
  974. var additive = cfg.additive;
  975. var setToFinal = cfg.setToFinal;
  976. var animateAll = !isObject(animationProps);
  977. var existsAnimators = animatable.animators;
  978. var animationKeys = [];
  979. for (var k = 0; k < targetKeys.length; k++) {
  980. var innerKey = targetKeys[k];
  981. var targetVal = target[innerKey];
  982. if (targetVal != null && animateObj[innerKey] != null
  983. && (animateAll || animationProps[innerKey])) {
  984. if (isObject(targetVal)
  985. && !isArrayLike(targetVal)
  986. && !isGradientObject(targetVal)) {
  987. if (topKey) {
  988. if (!reverse) {
  989. animateObj[innerKey] = targetVal;
  990. animatable.updateDuringAnimation(topKey);
  991. }
  992. continue;
  993. }
  994. animateToShallow(animatable, innerKey, animateObj[innerKey], targetVal, cfg, animationProps && animationProps[innerKey], animators, reverse);
  995. }
  996. else {
  997. animationKeys.push(innerKey);
  998. }
  999. }
  1000. else if (!reverse) {
  1001. animateObj[innerKey] = targetVal;
  1002. animatable.updateDuringAnimation(topKey);
  1003. animationKeys.push(innerKey);
  1004. }
  1005. }
  1006. var keyLen = animationKeys.length;
  1007. if (!additive && keyLen) {
  1008. for (var i = 0; i < existsAnimators.length; i++) {
  1009. var animator = existsAnimators[i];
  1010. if (animator.targetName === topKey) {
  1011. var allAborted = animator.stopTracks(animationKeys);
  1012. if (allAborted) {
  1013. var idx = indexOf(existsAnimators, animator);
  1014. existsAnimators.splice(idx, 1);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. if (!cfg.force) {
  1020. animationKeys = filter(animationKeys, function (key) { return !isValueSame(target[key], animateObj[key]); });
  1021. keyLen = animationKeys.length;
  1022. }
  1023. if (keyLen > 0
  1024. || (cfg.force && !animators.length)) {
  1025. var revertedSource = void 0;
  1026. var reversedTarget = void 0;
  1027. var sourceClone = void 0;
  1028. if (reverse) {
  1029. reversedTarget = {};
  1030. if (setToFinal) {
  1031. revertedSource = {};
  1032. }
  1033. for (var i = 0; i < keyLen; i++) {
  1034. var innerKey = animationKeys[i];
  1035. reversedTarget[innerKey] = animateObj[innerKey];
  1036. if (setToFinal) {
  1037. revertedSource[innerKey] = target[innerKey];
  1038. }
  1039. else {
  1040. animateObj[innerKey] = target[innerKey];
  1041. }
  1042. }
  1043. }
  1044. else if (setToFinal) {
  1045. sourceClone = {};
  1046. for (var i = 0; i < keyLen; i++) {
  1047. var innerKey = animationKeys[i];
  1048. sourceClone[innerKey] = cloneValue(animateObj[innerKey]);
  1049. copyValue(animateObj, target, innerKey);
  1050. }
  1051. }
  1052. var animator = new Animator(animateObj, false, false, additive ? filter(existsAnimators, function (animator) { return animator.targetName === topKey; }) : null);
  1053. animator.targetName = topKey;
  1054. if (cfg.scope) {
  1055. animator.scope = cfg.scope;
  1056. }
  1057. if (setToFinal && revertedSource) {
  1058. animator.whenWithKeys(0, revertedSource, animationKeys);
  1059. }
  1060. if (sourceClone) {
  1061. animator.whenWithKeys(0, sourceClone, animationKeys);
  1062. }
  1063. animator.whenWithKeys(duration == null ? 500 : duration, reverse ? reversedTarget : target, animationKeys).delay(delay || 0);
  1064. animatable.addAnimator(animator, topKey);
  1065. animators.push(animator);
  1066. }
  1067. }
  1068. export default Element;