_relationNode.js 2.3 KB

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