linkSeriesData.js 4.9 KB

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