relation.js 1.7 KB

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