index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. component_1.VantComponent({
  5. relation: {
  6. name: 'badge',
  7. type: 'descendant',
  8. linked: function (target) {
  9. this.badges.push(target);
  10. this.setActive(this.data.active);
  11. },
  12. unlinked: function (target) {
  13. this.badges = this.badges.filter(function (item) { return item !== target; });
  14. this.setActive(this.data.active);
  15. }
  16. },
  17. props: {
  18. active: {
  19. type: Number,
  20. value: 0,
  21. observer: 'setActive'
  22. }
  23. },
  24. beforeCreate: function () {
  25. this.badges = [];
  26. this.currentActive = -1;
  27. },
  28. methods: {
  29. setActive: function (active) {
  30. var _a = this, badges = _a.badges, currentActive = _a.currentActive;
  31. if (!badges.length) {
  32. return Promise.resolve();
  33. }
  34. this.currentActive = active;
  35. var stack = [];
  36. if (currentActive !== active && badges[currentActive]) {
  37. stack.push(badges[currentActive].setActive(false));
  38. }
  39. if (badges[active]) {
  40. stack.push(badges[active].setActive(true));
  41. }
  42. return Promise.all(stack);
  43. }
  44. }
  45. });