relation.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.ChildrenMixin = ChildrenMixin;
  4. exports.ParentMixin = ParentMixin;
  5. var _vnodes = require("../utils/vnodes");
  6. function ChildrenMixin(_parent, options) {
  7. var _inject, _computed;
  8. if (options === void 0) {
  9. options = {};
  10. }
  11. var indexKey = options.indexKey || 'index';
  12. return {
  13. inject: (_inject = {}, _inject[_parent] = {
  14. default: null
  15. }, _inject),
  16. computed: (_computed = {
  17. parent: function parent() {
  18. if (this.disableBindRelation) {
  19. return null;
  20. }
  21. return this[_parent];
  22. }
  23. }, _computed[indexKey] = function () {
  24. this.bindRelation();
  25. if (this.parent) {
  26. return this.parent.children.indexOf(this);
  27. }
  28. return null;
  29. }, _computed),
  30. watch: {
  31. disableBindRelation: function disableBindRelation(val) {
  32. if (!val) {
  33. this.bindRelation();
  34. }
  35. }
  36. },
  37. mounted: function mounted() {
  38. this.bindRelation();
  39. },
  40. beforeDestroy: function beforeDestroy() {
  41. var _this = this;
  42. if (this.parent) {
  43. this.parent.children = this.parent.children.filter(function (item) {
  44. return item !== _this;
  45. });
  46. }
  47. },
  48. methods: {
  49. bindRelation: function bindRelation() {
  50. if (!this.parent || this.parent.children.indexOf(this) !== -1) {
  51. return;
  52. }
  53. var children = [].concat(this.parent.children, [this]);
  54. (0, _vnodes.sortChildren)(children, this.parent);
  55. this.parent.children = children;
  56. }
  57. }
  58. };
  59. }
  60. function ParentMixin(parent) {
  61. return {
  62. provide: function provide() {
  63. var _ref;
  64. return _ref = {}, _ref[parent] = this, _ref;
  65. },
  66. data: function data() {
  67. return {
  68. children: []
  69. };
  70. }
  71. };
  72. }