linkSeriesData.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  41. * Link lists and struct (graph or tree)
  42. */
  43. import { curry, each, assert, extend, map, keys } from 'zrender/lib/core/util.js';
  44. import { makeInner } from '../../util/model.js';
  45. var inner = makeInner();
  46. function linkSeriesData(opt) {
  47. var mainData = opt.mainData;
  48. var datas = opt.datas;
  49. if (!datas) {
  50. datas = {
  51. main: mainData
  52. };
  53. opt.datasAttr = {
  54. main: 'data'
  55. };
  56. }
  57. opt.datas = opt.mainData = null;
  58. linkAll(mainData, datas, opt);
  59. // Porxy data original methods.
  60. each(datas, function (data) {
  61. each(mainData.TRANSFERABLE_METHODS, function (methodName) {
  62. data.wrapMethod(methodName, curry(transferInjection, opt));
  63. });
  64. });
  65. // Beyond transfer, additional features should be added to `cloneShallow`.
  66. mainData.wrapMethod('cloneShallow', curry(cloneShallowInjection, opt));
  67. // Only mainData trigger change, because struct.update may trigger
  68. // another changable methods, which may bring about dead lock.
  69. each(mainData.CHANGABLE_METHODS, function (methodName) {
  70. mainData.wrapMethod(methodName, curry(changeInjection, opt));
  71. });
  72. // Make sure datas contains mainData.
  73. assert(datas[mainData.dataType] === mainData);
  74. }
  75. function transferInjection(opt, res) {
  76. if (isMainData(this)) {
  77. // Transfer datas to new main data.
  78. var datas = extend({}, inner(this).datas);
  79. datas[this.dataType] = res;
  80. linkAll(res, datas, opt);
  81. } else {
  82. // Modify the reference in main data to point newData.
  83. linkSingle(res, this.dataType, inner(this).mainData, opt);
  84. }
  85. return res;
  86. }
  87. function changeInjection(opt, res) {
  88. opt.struct && opt.struct.update();
  89. return res;
  90. }
  91. function cloneShallowInjection(opt, res) {
  92. // cloneShallow, which brings about some fragilities, may be inappropriate
  93. // to be exposed as an API. So for implementation simplicity we can make
  94. // the restriction that cloneShallow of not-mainData should not be invoked
  95. // outside, but only be invoked here.
  96. each(inner(res).datas, function (data, dataType) {
  97. data !== res && linkSingle(data.cloneShallow(), dataType, res, opt);
  98. });
  99. return res;
  100. }
  101. /**
  102. * Supplement method to List.
  103. *
  104. * @public
  105. * @param [dataType] If not specified, return mainData.
  106. */
  107. function getLinkedData(dataType) {
  108. var mainData = inner(this).mainData;
  109. return dataType == null || mainData == null ? mainData : inner(mainData).datas[dataType];
  110. }
  111. /**
  112. * Get list of all linked data
  113. */
  114. function getLinkedDataAll() {
  115. var mainData = inner(this).mainData;
  116. return mainData == null ? [{
  117. data: mainData
  118. }] : map(keys(inner(mainData).datas), function (type) {
  119. return {
  120. type: type,
  121. data: inner(mainData).datas[type]
  122. };
  123. });
  124. }
  125. function isMainData(data) {
  126. return inner(data).mainData === data;
  127. }
  128. function linkAll(mainData, datas, opt) {
  129. inner(mainData).datas = {};
  130. each(datas, function (data, dataType) {
  131. linkSingle(data, dataType, mainData, opt);
  132. });
  133. }
  134. function linkSingle(data, dataType, mainData, opt) {
  135. inner(mainData).datas[dataType] = data;
  136. inner(data).mainData = mainData;
  137. data.dataType = dataType;
  138. if (opt.struct) {
  139. data[opt.structAttr] = opt.struct;
  140. opt.struct[opt.datasAttr[dataType]] = data;
  141. }
  142. // Supplement method.
  143. data.getLinkedData = getLinkedData;
  144. data.getLinkedDataAll = getLinkedDataAll;
  145. }
  146. export default linkSeriesData;