Model.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. var zrUtil = require("zrender/lib/core/util");
  2. var env = require("zrender/lib/core/env");
  3. var clazzUtil = require("../util/clazz");
  4. var lineStyleMixin = require("./mixin/lineStyle");
  5. var areaStyleMixin = require("./mixin/areaStyle");
  6. var textStyleMixin = require("./mixin/textStyle");
  7. var itemStyleMixin = require("./mixin/itemStyle");
  8. /**
  9. * @module echarts/model/Model
  10. */
  11. var mixin = zrUtil.mixin;
  12. /**
  13. * @alias module:echarts/model/Model
  14. * @constructor
  15. * @param {Object} option
  16. * @param {module:echarts/model/Model} [parentModel]
  17. * @param {module:echarts/model/Global} [ecModel]
  18. */
  19. function Model(option, parentModel, ecModel) {
  20. /**
  21. * @type {module:echarts/model/Model}
  22. * @readOnly
  23. */
  24. this.parentModel = parentModel;
  25. /**
  26. * @type {module:echarts/model/Global}
  27. * @readOnly
  28. */
  29. this.ecModel = ecModel;
  30. /**
  31. * @type {Object}
  32. * @protected
  33. */
  34. this.option = option; // Simple optimization
  35. // if (this.init) {
  36. // if (arguments.length <= 4) {
  37. // this.init(option, parentModel, ecModel, extraOpt);
  38. // }
  39. // else {
  40. // this.init.apply(this, arguments);
  41. // }
  42. // }
  43. }
  44. Model.prototype = {
  45. constructor: Model,
  46. /**
  47. * Model 的初始化函数
  48. * @param {Object} option
  49. */
  50. init: null,
  51. /**
  52. * 从新的 Option merge
  53. */
  54. mergeOption: function (option) {
  55. zrUtil.merge(this.option, option, true);
  56. },
  57. /**
  58. * @param {string|Array.<string>} path
  59. * @param {boolean} [ignoreParent=false]
  60. * @return {*}
  61. */
  62. get: function (path, ignoreParent) {
  63. if (path == null) {
  64. return this.option;
  65. }
  66. return doGet(this.option, this.parsePath(path), !ignoreParent && getParent(this, path));
  67. },
  68. /**
  69. * @param {string} key
  70. * @param {boolean} [ignoreParent=false]
  71. * @return {*}
  72. */
  73. getShallow: function (key, ignoreParent) {
  74. var option = this.option;
  75. var val = option == null ? option : option[key];
  76. var parentModel = !ignoreParent && getParent(this, key);
  77. if (val == null && parentModel) {
  78. val = parentModel.getShallow(key);
  79. }
  80. return val;
  81. },
  82. /**
  83. * @param {string|Array.<string>} [path]
  84. * @param {module:echarts/model/Model} [parentModel]
  85. * @return {module:echarts/model/Model}
  86. */
  87. getModel: function (path, parentModel) {
  88. var obj = path == null ? this.option : doGet(this.option, path = this.parsePath(path));
  89. var thisParentModel;
  90. parentModel = parentModel || (thisParentModel = getParent(this, path)) && thisParentModel.getModel(path);
  91. return new Model(obj, parentModel, this.ecModel);
  92. },
  93. /**
  94. * If model has option
  95. */
  96. isEmpty: function () {
  97. return this.option == null;
  98. },
  99. restoreData: function () {},
  100. // Pending
  101. clone: function () {
  102. var Ctor = this.constructor;
  103. return new Ctor(zrUtil.clone(this.option));
  104. },
  105. setReadOnly: function (properties) {
  106. clazzUtil.setReadOnly(this, properties);
  107. },
  108. // If path is null/undefined, return null/undefined.
  109. parsePath: function (path) {
  110. if (typeof path === 'string') {
  111. path = path.split('.');
  112. }
  113. return path;
  114. },
  115. /**
  116. * @param {Function} getParentMethod
  117. * param {Array.<string>|string} path
  118. * return {module:echarts/model/Model}
  119. */
  120. customizeGetParent: function (getParentMethod) {
  121. clazzUtil.set(this, 'getParent', getParentMethod);
  122. },
  123. isAnimationEnabled: function () {
  124. if (!env.node) {
  125. if (this.option.animation != null) {
  126. return !!this.option.animation;
  127. } else if (this.parentModel) {
  128. return this.parentModel.isAnimationEnabled();
  129. }
  130. }
  131. }
  132. };
  133. function doGet(obj, pathArr, parentModel) {
  134. for (var i = 0; i < pathArr.length; i++) {
  135. // Ignore empty
  136. if (!pathArr[i]) {
  137. continue;
  138. } // obj could be number/string/... (like 0)
  139. obj = obj && typeof obj === 'object' ? obj[pathArr[i]] : null;
  140. if (obj == null) {
  141. break;
  142. }
  143. }
  144. if (obj == null && parentModel) {
  145. obj = parentModel.get(pathArr);
  146. }
  147. return obj;
  148. } // `path` can be null/undefined
  149. function getParent(model, path) {
  150. var getParentMethod = clazzUtil.get(model, 'getParent');
  151. return getParentMethod ? getParentMethod.call(model, path) : model.parentModel;
  152. } // Enable Model.extend.
  153. clazzUtil.enableClassExtend(Model);
  154. mixin(Model, lineStyleMixin);
  155. mixin(Model, areaStyleMixin);
  156. mixin(Model, textStyleMixin);
  157. mixin(Model, itemStyleMixin);
  158. var _default = Model;
  159. module.exports = _default;