_relationNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. var utils = require('../../api/utils');
  4. var browserPath = utils.browserPath;
  5. var posix = browserPath();
  6. function processRelationPath(self, relation) {
  7. var from = self.is;
  8. var to = relation;
  9. if (to[0] === '.') {
  10. to = "../".concat(to);
  11. }
  12. var _p = posix.join(from, to);
  13. return _p;
  14. }
  15. function _relationNode(node, info) {
  16. var relationInfo = info.relationInfo,
  17. relation = info.relation,
  18. _p = info._p; // 触发父级组件的 relations
  19. var type = relationInfo.type;
  20. var parentType = '';
  21. if (type === 'parent') {
  22. parentType = 'child';
  23. } else if (type === 'ancestor') {
  24. parentType = 'descendant';
  25. }
  26. var parentCtx = node.$self;
  27. var childCtx = this;
  28. if (_typeof(parentCtx.props.theRelations) === 'object') {
  29. Object.keys(parentCtx.props.theRelations).forEach(function (_relation) {
  30. var _relationInfo = parentCtx.props.theRelations[_relation];
  31. if (_relationInfo.type === parentType) {
  32. _relationNode.call(parentCtx, childCtx.$node, {
  33. relationInfo: _relationInfo,
  34. relation: _relation,
  35. _p: processRelationPath(parentCtx, _relation)
  36. });
  37. return true;
  38. }
  39. });
  40. }
  41. node = node.$self;
  42. this._storeRelationNodes = this._storeRelationNodes || {};
  43. if (this._storeRelationNodes[_p]) {
  44. this._storeRelationNodes[_p].push(node);
  45. } else {
  46. this._storeRelationNodes[_p] = [node];
  47. }
  48. if (this._storeRelationNodes[relation]) {
  49. this._storeRelationNodes[relation].push(node);
  50. } else {
  51. this._storeRelationNodes[relation] = [node];
  52. }
  53. var ctx = this || {};
  54. this.getRelationNodes = function (__p) {
  55. this._storeRelationNodes = this._storeRelationNodes || {};
  56. return this._storeRelationNodes[__p] || [];
  57. };
  58. if (typeof relationInfo.linked === 'function') {
  59. relationInfo.linked.call(ctx, node);
  60. }
  61. if (typeof relationInfo.linkChanged === 'function') {
  62. var self = this;
  63. ctx._lifes = ctx._lifes || {};
  64. ctx._lifes.didUpdate = ctx._lifes.didUpdate || [];
  65. ctx._lifes.didUpdate.push(function () {
  66. relationInfo.linkChanged.call(self, node);
  67. });
  68. }
  69. if (typeof relationInfo.unlinked === 'function') {
  70. var _self = this;
  71. ctx._lifes = ctx._lifes || {};
  72. ctx._lifes.didUnmount = ctx._lifes.didUnmount || [];
  73. ctx._lifes.didUnmount.push(function () {
  74. relationInfo.unlinked.call(_self, node);
  75. });
  76. }
  77. }
  78. module.exports = _relationNode;