Model.js 7.1 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 env from 'zrender/lib/core/env';
  41. import { enableClassExtend, enableClassCheck } from '../util/clazz';
  42. import { AreaStyleMixin } from './mixin/areaStyle';
  43. import TextStyleMixin from './mixin/textStyle';
  44. import { LineStyleMixin } from './mixin/lineStyle';
  45. import { ItemStyleMixin } from './mixin/itemStyle';
  46. import { mixin, clone, merge } from 'zrender/lib/core/util';
  47. var Model =
  48. /** @class */
  49. function () {
  50. function Model(option, parentModel, ecModel) {
  51. this.parentModel = parentModel;
  52. this.ecModel = ecModel;
  53. this.option = option; // Simple optimization
  54. // if (this.init) {
  55. // if (arguments.length <= 4) {
  56. // this.init(option, parentModel, ecModel, extraOpt);
  57. // }
  58. // else {
  59. // this.init.apply(this, arguments);
  60. // }
  61. // }
  62. }
  63. Model.prototype.init = function (option, parentModel, ecModel) {
  64. var rest = [];
  65. for (var _i = 3; _i < arguments.length; _i++) {
  66. rest[_i - 3] = arguments[_i];
  67. }
  68. };
  69. /**
  70. * Merge the input option to me.
  71. */
  72. Model.prototype.mergeOption = function (option, ecModel) {
  73. merge(this.option, option, true);
  74. }; // `path` can be 'xxx.yyy.zzz', so the return value type have to be `ModelOption`
  75. // TODO: TYPE strict key check?
  76. // get(path: string | string[], ignoreParent?: boolean): ModelOption;
  77. Model.prototype.get = function (path, ignoreParent) {
  78. if (path == null) {
  79. return this.option;
  80. }
  81. return this._doGet(this.parsePath(path), !ignoreParent && this.parentModel);
  82. };
  83. Model.prototype.getShallow = function (key, ignoreParent) {
  84. var option = this.option;
  85. var val = option == null ? option : option[key];
  86. if (val == null && !ignoreParent) {
  87. var parentModel = this.parentModel;
  88. if (parentModel) {
  89. // FIXME:TS do not know how to make it works
  90. val = parentModel.getShallow(key);
  91. }
  92. }
  93. return val;
  94. }; // `path` can be 'xxx.yyy.zzz', so the return value type have to be `Model<ModelOption>`
  95. // getModel(path: string | string[], parentModel?: Model): Model;
  96. // TODO 'xxx.yyy.zzz' is deprecated
  97. Model.prototype.getModel = function (path, parentModel) {
  98. var hasPath = path != null;
  99. var pathFinal = hasPath ? this.parsePath(path) : null;
  100. var obj = hasPath ? this._doGet(pathFinal) : this.option;
  101. parentModel = parentModel || this.parentModel && this.parentModel.getModel(this.resolveParentPath(pathFinal));
  102. return new Model(obj, parentModel, this.ecModel);
  103. };
  104. /**
  105. * Squash option stack into one.
  106. * parentModel will be removed after squashed.
  107. *
  108. * NOTE: resolveParentPath will not be applied here for simplicity. DON'T use this function
  109. * if resolveParentPath is modified.
  110. *
  111. * @param deepMerge If do deep merge. Default to be false.
  112. */
  113. // squash(
  114. // deepMerge?: boolean,
  115. // handleCallback?: (func: () => object) => object
  116. // ) {
  117. // const optionStack = [];
  118. // let model: Model = this;
  119. // while (model) {
  120. // if (model.option) {
  121. // optionStack.push(model.option);
  122. // }
  123. // model = model.parentModel;
  124. // }
  125. // const newOption = {} as Opt;
  126. // let option;
  127. // while (option = optionStack.pop()) { // Top down merge
  128. // if (isFunction(option) && handleCallback) {
  129. // option = handleCallback(option);
  130. // }
  131. // if (deepMerge) {
  132. // merge(newOption, option);
  133. // }
  134. // else {
  135. // extend(newOption, option);
  136. // }
  137. // }
  138. // // Remove parentModel
  139. // this.option = newOption;
  140. // this.parentModel = null;
  141. // }
  142. /**
  143. * If model has option
  144. */
  145. Model.prototype.isEmpty = function () {
  146. return this.option == null;
  147. };
  148. Model.prototype.restoreData = function () {}; // Pending
  149. Model.prototype.clone = function () {
  150. var Ctor = this.constructor;
  151. return new Ctor(clone(this.option));
  152. }; // setReadOnly(properties): void {
  153. // clazzUtil.setReadOnly(this, properties);
  154. // }
  155. // If path is null/undefined, return null/undefined.
  156. Model.prototype.parsePath = function (path) {
  157. if (typeof path === 'string') {
  158. return path.split('.');
  159. }
  160. return path;
  161. }; // Resolve path for parent. Perhaps useful when parent use a different property.
  162. // Default to be a identity resolver.
  163. // Can be modified to a different resolver.
  164. Model.prototype.resolveParentPath = function (path) {
  165. return path;
  166. }; // FIXME:TS check whether put this method here
  167. Model.prototype.isAnimationEnabled = function () {
  168. if (!env.node && this.option) {
  169. if (this.option.animation != null) {
  170. return !!this.option.animation;
  171. } else if (this.parentModel) {
  172. return this.parentModel.isAnimationEnabled();
  173. }
  174. }
  175. };
  176. Model.prototype._doGet = function (pathArr, parentModel) {
  177. var obj = this.option;
  178. if (!pathArr) {
  179. return obj;
  180. }
  181. for (var i = 0; i < pathArr.length; i++) {
  182. // Ignore empty
  183. if (!pathArr[i]) {
  184. continue;
  185. } // obj could be number/string/... (like 0)
  186. obj = obj && typeof obj === 'object' ? obj[pathArr[i]] : null;
  187. if (obj == null) {
  188. break;
  189. }
  190. }
  191. if (obj == null && parentModel) {
  192. obj = parentModel._doGet(this.resolveParentPath(pathArr), parentModel.parentModel);
  193. }
  194. return obj;
  195. };
  196. return Model;
  197. }();
  198. ; // Enable Model.extend.
  199. enableClassExtend(Model);
  200. enableClassCheck(Model);
  201. mixin(Model, LineStyleMixin);
  202. mixin(Model, ItemStyleMixin);
  203. mixin(Model, AreaStyleMixin);
  204. mixin(Model, TextStyleMixin);
  205. export default Model;