index.js 3.3 KB

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