index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. var utils_1 = require("../common/utils");
  5. var ARRAY = [];
  6. component_1.VantComponent({
  7. field: true,
  8. relation: {
  9. name: 'dropdown-item',
  10. type: 'descendant',
  11. current: 'dropdown-menu',
  12. linked: function () {
  13. this.updateItemListData();
  14. },
  15. unlinked: function () {
  16. this.updateItemListData();
  17. }
  18. },
  19. props: {
  20. activeColor: {
  21. type: String,
  22. observer: 'updateChildrenData'
  23. },
  24. overlay: {
  25. type: Boolean,
  26. value: true,
  27. observer: 'updateChildrenData'
  28. },
  29. zIndex: {
  30. type: Number,
  31. value: 10
  32. },
  33. duration: {
  34. type: Number,
  35. value: 200,
  36. observer: 'updateChildrenData'
  37. },
  38. direction: {
  39. type: String,
  40. value: 'down',
  41. observer: 'updateChildrenData'
  42. },
  43. closeOnClickOverlay: {
  44. type: Boolean,
  45. value: true,
  46. observer: 'updateChildrenData'
  47. },
  48. closeOnClickOutside: {
  49. type: Boolean,
  50. value: true
  51. }
  52. },
  53. data: {
  54. itemListData: []
  55. },
  56. beforeCreate: function () {
  57. var windowHeight = wx.getSystemInfoSync().windowHeight;
  58. this.windowHeight = windowHeight;
  59. ARRAY.push(this);
  60. },
  61. destroyed: function () {
  62. var _this = this;
  63. ARRAY = ARRAY.filter(function (item) { return item !== _this; });
  64. },
  65. methods: {
  66. updateItemListData: function () {
  67. this.setData({
  68. itemListData: this.children.map(function (child) { return child.data; })
  69. });
  70. },
  71. updateChildrenData: function () {
  72. this.children.forEach(function (child) {
  73. child.updateDataFromParent();
  74. });
  75. },
  76. toggleItem: function (active) {
  77. this.children.forEach(function (item, index) {
  78. var showPopup = item.data.showPopup;
  79. if (index === active) {
  80. item.toggle();
  81. }
  82. else if (showPopup) {
  83. item.toggle(false, { immediate: true });
  84. }
  85. });
  86. },
  87. close: function () {
  88. this.children.forEach(function (child) {
  89. child.toggle(false, { immediate: true });
  90. });
  91. },
  92. getChildWrapperStyle: function () {
  93. var _this = this;
  94. var _a = this.data, zIndex = _a.zIndex, direction = _a.direction;
  95. return this.getRect('.van-dropdown-menu').then(function (rect) {
  96. var _a = rect.top, top = _a === void 0 ? 0 : _a, _b = rect.bottom, bottom = _b === void 0 ? 0 : _b;
  97. var offset = direction === 'down' ? bottom : _this.windowHeight - top;
  98. var wrapperStyle = "z-index: " + zIndex + ";";
  99. if (direction === 'down') {
  100. wrapperStyle += "top: " + utils_1.addUnit(offset) + ";";
  101. }
  102. else {
  103. wrapperStyle += "bottom: " + utils_1.addUnit(offset) + ";";
  104. }
  105. return wrapperStyle;
  106. });
  107. },
  108. onTitleTap: function (event) {
  109. var _this = this;
  110. var index = event.currentTarget.dataset.index;
  111. var child = this.children[index];
  112. if (!child.data.disabled) {
  113. ARRAY.forEach(function (menuItem) {
  114. if (menuItem &&
  115. menuItem.data.closeOnClickOutside &&
  116. menuItem !== _this) {
  117. menuItem.close();
  118. }
  119. });
  120. this.toggleItem(index);
  121. }
  122. }
  123. }
  124. });