Model.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. var zrUtil = require("zrender/lib/core/util");
  20. var env = require("zrender/lib/core/env");
  21. var _model = require("../util/model");
  22. var makeInner = _model.makeInner;
  23. var _clazz = require("../util/clazz");
  24. var enableClassExtend = _clazz.enableClassExtend;
  25. var enableClassCheck = _clazz.enableClassCheck;
  26. var lineStyleMixin = require("./mixin/lineStyle");
  27. var areaStyleMixin = require("./mixin/areaStyle");
  28. var textStyleMixin = require("./mixin/textStyle");
  29. var itemStyleMixin = require("./mixin/itemStyle");
  30. /*
  31. * Licensed to the Apache Software Foundation (ASF) under one
  32. * or more contributor license agreements. See the NOTICE file
  33. * distributed with this work for additional information
  34. * regarding copyright ownership. The ASF licenses this file
  35. * to you under the Apache License, Version 2.0 (the
  36. * "License"); you may not use this file except in compliance
  37. * with the License. You may obtain a copy of the License at
  38. *
  39. * http://www.apache.org/licenses/LICENSE-2.0
  40. *
  41. * Unless required by applicable law or agreed to in writing,
  42. * software distributed under the License is distributed on an
  43. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  44. * KIND, either express or implied. See the License for the
  45. * specific language governing permissions and limitations
  46. * under the License.
  47. */
  48. /**
  49. * @module echarts/model/Model
  50. */
  51. var mixin = zrUtil.mixin;
  52. var inner = makeInner();
  53. /**
  54. * @alias module:echarts/model/Model
  55. * @constructor
  56. * @param {Object} [option]
  57. * @param {module:echarts/model/Model} [parentModel]
  58. * @param {module:echarts/model/Global} [ecModel]
  59. */
  60. function Model(option, parentModel, ecModel) {
  61. /**
  62. * @type {module:echarts/model/Model}
  63. * @readOnly
  64. */
  65. this.parentModel = parentModel;
  66. /**
  67. * @type {module:echarts/model/Global}
  68. * @readOnly
  69. */
  70. this.ecModel = ecModel;
  71. /**
  72. * @type {Object}
  73. * @protected
  74. */
  75. this.option = option; // Simple optimization
  76. // if (this.init) {
  77. // if (arguments.length <= 4) {
  78. // this.init(option, parentModel, ecModel, extraOpt);
  79. // }
  80. // else {
  81. // this.init.apply(this, arguments);
  82. // }
  83. // }
  84. }
  85. Model.prototype = {
  86. constructor: Model,
  87. /**
  88. * Model 的初始化函数
  89. * @param {Object} option
  90. */
  91. init: null,
  92. /**
  93. * 从新的 Option merge
  94. */
  95. mergeOption: function (option) {
  96. zrUtil.merge(this.option, option, true);
  97. },
  98. /**
  99. * @param {string|Array.<string>} path
  100. * @param {boolean} [ignoreParent=false]
  101. * @return {*}
  102. */
  103. get: function (path, ignoreParent) {
  104. if (path == null) {
  105. return this.option;
  106. }
  107. return doGet(this.option, this.parsePath(path), !ignoreParent && getParent(this, path));
  108. },
  109. /**
  110. * @param {string} key
  111. * @param {boolean} [ignoreParent=false]
  112. * @return {*}
  113. */
  114. getShallow: function (key, ignoreParent) {
  115. var option = this.option;
  116. var val = option == null ? option : option[key];
  117. var parentModel = !ignoreParent && getParent(this, key);
  118. if (val == null && parentModel) {
  119. val = parentModel.getShallow(key);
  120. }
  121. return val;
  122. },
  123. /**
  124. * @param {string|Array.<string>} [path]
  125. * @param {module:echarts/model/Model} [parentModel]
  126. * @return {module:echarts/model/Model}
  127. */
  128. getModel: function (path, parentModel) {
  129. var obj = path == null ? this.option : doGet(this.option, path = this.parsePath(path));
  130. var thisParentModel;
  131. parentModel = parentModel || (thisParentModel = getParent(this, path)) && thisParentModel.getModel(path);
  132. return new Model(obj, parentModel, this.ecModel);
  133. },
  134. /**
  135. * If model has option
  136. */
  137. isEmpty: function () {
  138. return this.option == null;
  139. },
  140. restoreData: function () {},
  141. // Pending
  142. clone: function () {
  143. var Ctor = this.constructor;
  144. return new Ctor(zrUtil.clone(this.option));
  145. },
  146. setReadOnly: function (properties) {// clazzUtil.setReadOnly(this, properties);
  147. },
  148. // If path is null/undefined, return null/undefined.
  149. parsePath: function (path) {
  150. if (typeof path === 'string') {
  151. path = path.split('.');
  152. }
  153. return path;
  154. },
  155. /**
  156. * @param {Function} getParentMethod
  157. * param {Array.<string>|string} path
  158. * return {module:echarts/model/Model}
  159. */
  160. customizeGetParent: function (getParentMethod) {
  161. inner(this).getParent = getParentMethod;
  162. },
  163. isAnimationEnabled: function () {
  164. if (!env.node) {
  165. if (this.option.animation != null) {
  166. return !!this.option.animation;
  167. } else if (this.parentModel) {
  168. return this.parentModel.isAnimationEnabled();
  169. }
  170. }
  171. }
  172. };
  173. function doGet(obj, pathArr, parentModel) {
  174. for (var i = 0; i < pathArr.length; i++) {
  175. // Ignore empty
  176. if (!pathArr[i]) {
  177. continue;
  178. } // obj could be number/string/... (like 0)
  179. obj = obj && typeof obj === 'object' ? obj[pathArr[i]] : null;
  180. if (obj == null) {
  181. break;
  182. }
  183. }
  184. if (obj == null && parentModel) {
  185. obj = parentModel.get(pathArr);
  186. }
  187. return obj;
  188. } // `path` can be null/undefined
  189. function getParent(model, path) {
  190. var getParentMethod = inner(model).getParent;
  191. return getParentMethod ? getParentMethod.call(model, path) : model.parentModel;
  192. } // Enable Model.extend.
  193. enableClassExtend(Model);
  194. enableClassCheck(Model);
  195. mixin(Model, lineStyleMixin);
  196. mixin(Model, areaStyleMixin);
  197. mixin(Model, textStyleMixin);
  198. mixin(Model, itemStyleMixin);
  199. var _default = Model;
  200. module.exports = _default;