index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 = utils_1.getSystemInfoSync().windowHeight;
  58. this.windowHeight = windowHeight;
  59. ARRAY.push(this);
  60. },
  61. destroyed: function () {
  62. var _this = this;
  63. ARRAY = ARRAY.filter(function (item) {
  64. return item !== _this;
  65. });
  66. },
  67. methods: {
  68. updateItemListData: function () {
  69. this.setData({
  70. itemListData: this.children.map(function (child) {
  71. return child.data;
  72. }),
  73. });
  74. },
  75. updateChildrenData: function () {
  76. this.children.forEach(function (child) {
  77. child.updateDataFromParent();
  78. });
  79. },
  80. toggleItem: function (active) {
  81. this.children.forEach(function (item, index) {
  82. var showPopup = item.data.showPopup;
  83. if (index === active) {
  84. item.toggle();
  85. } else if (showPopup) {
  86. item.toggle(false, { immediate: true });
  87. }
  88. });
  89. },
  90. close: function () {
  91. this.children.forEach(function (child) {
  92. child.toggle(false, { immediate: true });
  93. });
  94. },
  95. getChildWrapperStyle: function () {
  96. var _this = this;
  97. var _a = this.data,
  98. zIndex = _a.zIndex,
  99. direction = _a.direction;
  100. return utils_1.getRect(this, '.van-dropdown-menu').then(function (rect) {
  101. var _a = rect.top,
  102. top = _a === void 0 ? 0 : _a,
  103. _b = rect.bottom,
  104. bottom = _b === void 0 ? 0 : _b;
  105. var offset = direction === 'down' ? bottom : _this.windowHeight - top;
  106. var wrapperStyle = 'z-index: ' + zIndex + ';';
  107. if (direction === 'down') {
  108. wrapperStyle += 'top: ' + utils_1.addUnit(offset) + ';';
  109. } else {
  110. wrapperStyle += 'bottom: ' + utils_1.addUnit(offset) + ';';
  111. }
  112. return wrapperStyle;
  113. });
  114. },
  115. onTitleTap: function (event) {
  116. var _this = this;
  117. var index = event.currentTarget.dataset.index;
  118. var child = this.children[index];
  119. if (!child.data.disabled) {
  120. ARRAY.forEach(function (menuItem) {
  121. if (
  122. menuItem &&
  123. menuItem.data.closeOnClickOutside &&
  124. menuItem !== _this
  125. ) {
  126. menuItem.close();
  127. }
  128. });
  129. this.toggleItem(index);
  130. }
  131. },
  132. },
  133. });