Model.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.js';
  41. import { enableClassExtend, enableClassCheck } from '../util/clazz.js';
  42. import { AreaStyleMixin } from './mixin/areaStyle.js';
  43. import TextStyleMixin from './mixin/textStyle.js';
  44. import { LineStyleMixin } from './mixin/lineStyle.js';
  45. import { ItemStyleMixin } from './mixin/itemStyle.js';
  46. import { mixin, clone, merge } from 'zrender/lib/core/util.js';
  47. var Model = /** @class */function () {
  48. function Model(option, parentModel, ecModel) {
  49. this.parentModel = parentModel;
  50. this.ecModel = ecModel;
  51. this.option = option;
  52. // Simple optimization
  53. // if (this.init) {
  54. // if (arguments.length <= 4) {
  55. // this.init(option, parentModel, ecModel, extraOpt);
  56. // }
  57. // else {
  58. // this.init.apply(this, arguments);
  59. // }
  60. // }
  61. }
  62. Model.prototype.init = function (option, parentModel, ecModel) {
  63. var rest = [];
  64. for (var _i = 3; _i < arguments.length; _i++) {
  65. rest[_i - 3] = arguments[_i];
  66. }
  67. };
  68. /**
  69. * Merge the input option to me.
  70. */
  71. Model.prototype.mergeOption = function (option, ecModel) {
  72. merge(this.option, option, true);
  73. };
  74. // `path` can be 'a.b.c', 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. };
  95. // `path` can be 'a.b.c', so the return value type have to be `Model<ModelOption>`
  96. // getModel(path: string | string[], parentModel?: Model): Model;
  97. // TODO 'a.b.c' is deprecated
  98. Model.prototype.getModel = function (path, parentModel) {
  99. var hasPath = path != null;
  100. var pathFinal = hasPath ? this.parsePath(path) : null;
  101. var obj = hasPath ? this._doGet(pathFinal) : this.option;
  102. parentModel = parentModel || this.parentModel && this.parentModel.getModel(this.resolveParentPath(pathFinal));
  103. return new Model(obj, parentModel, this.ecModel);
  104. };
  105. /**
  106. * If model has option
  107. */
  108. Model.prototype.isEmpty = function () {
  109. return this.option == null;
  110. };
  111. Model.prototype.restoreData = function () {};
  112. // Pending
  113. Model.prototype.clone = function () {
  114. var Ctor = this.constructor;
  115. return new Ctor(clone(this.option));
  116. };
  117. // setReadOnly(properties): void {
  118. // clazzUtil.setReadOnly(this, properties);
  119. // }
  120. // If path is null/undefined, return null/undefined.
  121. Model.prototype.parsePath = function (path) {
  122. if (typeof path === 'string') {
  123. return path.split('.');
  124. }
  125. return path;
  126. };
  127. // Resolve path for parent. Perhaps useful when parent use a different property.
  128. // Default to be a identity resolver.
  129. // Can be modified to a different resolver.
  130. Model.prototype.resolveParentPath = function (path) {
  131. return path;
  132. };
  133. // FIXME:TS check whether put this method here
  134. Model.prototype.isAnimationEnabled = function () {
  135. if (!env.node && this.option) {
  136. if (this.option.animation != null) {
  137. return !!this.option.animation;
  138. } else if (this.parentModel) {
  139. return this.parentModel.isAnimationEnabled();
  140. }
  141. }
  142. };
  143. Model.prototype._doGet = function (pathArr, parentModel) {
  144. var obj = this.option;
  145. if (!pathArr) {
  146. return obj;
  147. }
  148. for (var i = 0; i < pathArr.length; i++) {
  149. // Ignore empty
  150. if (!pathArr[i]) {
  151. continue;
  152. }
  153. // obj could be number/string/... (like 0)
  154. obj = obj && typeof obj === 'object' ? obj[pathArr[i]] : null;
  155. if (obj == null) {
  156. break;
  157. }
  158. }
  159. if (obj == null && parentModel) {
  160. obj = parentModel._doGet(this.resolveParentPath(pathArr), parentModel.parentModel);
  161. }
  162. return obj;
  163. };
  164. return Model;
  165. }();
  166. ;
  167. // Enable Model.extend.
  168. enableClassExtend(Model);
  169. enableClassCheck(Model);
  170. mixin(Model, LineStyleMixin);
  171. mixin(Model, ItemStyleMixin);
  172. mixin(Model, AreaStyleMixin);
  173. mixin(Model, TextStyleMixin);
  174. export default Model;