Model.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * @module echarts/model/Model
  21. */
  22. import * as zrUtil from 'zrender/src/core/util';
  23. import env from 'zrender/src/core/env';
  24. import {makeInner} from '../util/model';
  25. import {enableClassExtend, enableClassCheck} from '../util/clazz';
  26. import lineStyleMixin from './mixin/lineStyle';
  27. import areaStyleMixin from './mixin/areaStyle';
  28. import textStyleMixin from './mixin/textStyle';
  29. import itemStyleMixin from './mixin/itemStyle';
  30. var mixin = zrUtil.mixin;
  31. var inner = makeInner();
  32. /**
  33. * @alias module:echarts/model/Model
  34. * @constructor
  35. * @param {Object} [option]
  36. * @param {module:echarts/model/Model} [parentModel]
  37. * @param {module:echarts/model/Global} [ecModel]
  38. */
  39. function Model(option, parentModel, ecModel) {
  40. /**
  41. * @type {module:echarts/model/Model}
  42. * @readOnly
  43. */
  44. this.parentModel = parentModel;
  45. /**
  46. * @type {module:echarts/model/Global}
  47. * @readOnly
  48. */
  49. this.ecModel = ecModel;
  50. /**
  51. * @type {Object}
  52. * @protected
  53. */
  54. this.option = option;
  55. // Simple optimization
  56. // if (this.init) {
  57. // if (arguments.length <= 4) {
  58. // this.init(option, parentModel, ecModel, extraOpt);
  59. // }
  60. // else {
  61. // this.init.apply(this, arguments);
  62. // }
  63. // }
  64. }
  65. Model.prototype = {
  66. constructor: Model,
  67. /**
  68. * Model 的初始化函数
  69. * @param {Object} option
  70. */
  71. init: null,
  72. /**
  73. * 从新的 Option merge
  74. */
  75. mergeOption: function (option) {
  76. zrUtil.merge(this.option, option, true);
  77. },
  78. /**
  79. * @param {string|Array.<string>} path
  80. * @param {boolean} [ignoreParent=false]
  81. * @return {*}
  82. */
  83. get: function (path, ignoreParent) {
  84. if (path == null) {
  85. return this.option;
  86. }
  87. return doGet(
  88. this.option,
  89. this.parsePath(path),
  90. !ignoreParent && getParent(this, path)
  91. );
  92. },
  93. /**
  94. * @param {string} key
  95. * @param {boolean} [ignoreParent=false]
  96. * @return {*}
  97. */
  98. getShallow: function (key, ignoreParent) {
  99. var option = this.option;
  100. var val = option == null ? option : option[key];
  101. var parentModel = !ignoreParent && getParent(this, key);
  102. if (val == null && parentModel) {
  103. val = parentModel.getShallow(key);
  104. }
  105. return val;
  106. },
  107. /**
  108. * @param {string|Array.<string>} [path]
  109. * @param {module:echarts/model/Model} [parentModel]
  110. * @return {module:echarts/model/Model}
  111. */
  112. getModel: function (path, parentModel) {
  113. var obj = path == null
  114. ? this.option
  115. : doGet(this.option, path = this.parsePath(path));
  116. var thisParentModel;
  117. parentModel = parentModel || (
  118. (thisParentModel = getParent(this, path))
  119. && thisParentModel.getModel(path)
  120. );
  121. return new Model(obj, parentModel, this.ecModel);
  122. },
  123. /**
  124. * If model has option
  125. */
  126. isEmpty: function () {
  127. return this.option == null;
  128. },
  129. restoreData: function () {},
  130. // Pending
  131. clone: function () {
  132. var Ctor = this.constructor;
  133. return new Ctor(zrUtil.clone(this.option));
  134. },
  135. setReadOnly: function (properties) {
  136. // clazzUtil.setReadOnly(this, properties);
  137. },
  138. // If path is null/undefined, return null/undefined.
  139. parsePath: function (path) {
  140. if (typeof path === 'string') {
  141. path = path.split('.');
  142. }
  143. return path;
  144. },
  145. /**
  146. * @param {Function} getParentMethod
  147. * param {Array.<string>|string} path
  148. * return {module:echarts/model/Model}
  149. */
  150. customizeGetParent: function (getParentMethod) {
  151. inner(this).getParent = getParentMethod;
  152. },
  153. isAnimationEnabled: function () {
  154. if (!env.node) {
  155. if (this.option.animation != null) {
  156. return !!this.option.animation;
  157. }
  158. else if (this.parentModel) {
  159. return this.parentModel.isAnimationEnabled();
  160. }
  161. }
  162. }
  163. };
  164. function doGet(obj, pathArr, parentModel) {
  165. for (var i = 0; i < pathArr.length; i++) {
  166. // Ignore empty
  167. if (!pathArr[i]) {
  168. continue;
  169. }
  170. // obj could be number/string/... (like 0)
  171. obj = (obj && typeof obj === 'object') ? obj[pathArr[i]] : null;
  172. if (obj == null) {
  173. break;
  174. }
  175. }
  176. if (obj == null && parentModel) {
  177. obj = parentModel.get(pathArr);
  178. }
  179. return obj;
  180. }
  181. // `path` can be null/undefined
  182. function getParent(model, path) {
  183. var getParentMethod = inner(model).getParent;
  184. return getParentMethod ? getParentMethod.call(model, path) : model.parentModel;
  185. }
  186. // Enable Model.extend.
  187. enableClassExtend(Model);
  188. enableClassCheck(Model);
  189. mixin(Model, lineStyleMixin);
  190. mixin(Model, areaStyleMixin);
  191. mixin(Model, textStyleMixin);
  192. mixin(Model, itemStyleMixin);
  193. export default Model;