index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. var __assign =
  3. (this && this.__assign) ||
  4. function () {
  5. __assign =
  6. Object.assign ||
  7. function (t) {
  8. for (var s, i = 1, n = arguments.length; i < n; i++) {
  9. s = arguments[i];
  10. for (var p in s)
  11. if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  12. }
  13. return t;
  14. };
  15. return __assign.apply(this, arguments);
  16. };
  17. Object.defineProperty(exports, '__esModule', { value: true });
  18. var component_1 = require('../common/component');
  19. var shared_1 = require('./shared');
  20. component_1.VantComponent({
  21. classes: ['active-class', 'toolbar-class', 'column-class'],
  22. props: __assign(__assign({}, shared_1.pickerProps), {
  23. valueKey: {
  24. type: String,
  25. value: 'text',
  26. },
  27. toolbarPosition: {
  28. type: String,
  29. value: 'top',
  30. },
  31. defaultIndex: {
  32. type: Number,
  33. value: 0,
  34. },
  35. columns: {
  36. type: Array,
  37. value: [],
  38. observer: function (columns) {
  39. if (columns === void 0) {
  40. columns = [];
  41. }
  42. this.simple = columns.length && !columns[0].values;
  43. if (Array.isArray(this.children) && this.children.length) {
  44. this.setColumns().catch(function () {});
  45. }
  46. },
  47. },
  48. }),
  49. beforeCreate: function () {
  50. var _this = this;
  51. Object.defineProperty(this, 'children', {
  52. get: function () {
  53. return _this.selectAllComponents('.van-picker__column') || [];
  54. },
  55. });
  56. },
  57. methods: {
  58. noop: function () {},
  59. setColumns: function () {
  60. var _this = this;
  61. var data = this.data;
  62. var columns = this.simple ? [{ values: data.columns }] : data.columns;
  63. var stack = columns.map(function (column, index) {
  64. return _this.setColumnValues(index, column.values);
  65. });
  66. return Promise.all(stack);
  67. },
  68. emit: function (event) {
  69. var type = event.currentTarget.dataset.type;
  70. if (this.simple) {
  71. this.$emit(type, {
  72. value: this.getColumnValue(0),
  73. index: this.getColumnIndex(0),
  74. });
  75. } else {
  76. this.$emit(type, {
  77. value: this.getValues(),
  78. index: this.getIndexes(),
  79. });
  80. }
  81. },
  82. onChange: function (event) {
  83. if (this.simple) {
  84. this.$emit('change', {
  85. picker: this,
  86. value: this.getColumnValue(0),
  87. index: this.getColumnIndex(0),
  88. });
  89. } else {
  90. this.$emit('change', {
  91. picker: this,
  92. value: this.getValues(),
  93. index: event.currentTarget.dataset.index,
  94. });
  95. }
  96. },
  97. // get column instance by index
  98. getColumn: function (index) {
  99. return this.children[index];
  100. },
  101. // get column value by index
  102. getColumnValue: function (index) {
  103. var column = this.getColumn(index);
  104. return column && column.getValue();
  105. },
  106. // set column value by index
  107. setColumnValue: function (index, value) {
  108. var column = this.getColumn(index);
  109. if (column == null) {
  110. return Promise.reject(new Error('setColumnValue: 对应列不存在'));
  111. }
  112. return column.setValue(value);
  113. },
  114. // get column option index by column index
  115. getColumnIndex: function (columnIndex) {
  116. return (this.getColumn(columnIndex) || {}).data.currentIndex;
  117. },
  118. // set column option index by column index
  119. setColumnIndex: function (columnIndex, optionIndex) {
  120. var column = this.getColumn(columnIndex);
  121. if (column == null) {
  122. return Promise.reject(new Error('setColumnIndex: 对应列不存在'));
  123. }
  124. return column.setIndex(optionIndex);
  125. },
  126. // get options of column by index
  127. getColumnValues: function (index) {
  128. return (this.children[index] || {}).data.options;
  129. },
  130. // set options of column by index
  131. setColumnValues: function (index, options, needReset) {
  132. if (needReset === void 0) {
  133. needReset = true;
  134. }
  135. var column = this.children[index];
  136. if (column == null) {
  137. return Promise.reject(new Error('setColumnValues: 对应列不存在'));
  138. }
  139. var isSame =
  140. JSON.stringify(column.data.options) === JSON.stringify(options);
  141. if (isSame) {
  142. return Promise.resolve();
  143. }
  144. return column.set({ options: options }).then(function () {
  145. if (needReset) {
  146. column.setIndex(0);
  147. }
  148. });
  149. },
  150. // get values of all columns
  151. getValues: function () {
  152. return this.children.map(function (child) {
  153. return child.getValue();
  154. });
  155. },
  156. // set values of all columns
  157. setValues: function (values) {
  158. var _this = this;
  159. var stack = values.map(function (value, index) {
  160. return _this.setColumnValue(index, value);
  161. });
  162. return Promise.all(stack);
  163. },
  164. // get indexes of all columns
  165. getIndexes: function () {
  166. return this.children.map(function (child) {
  167. return child.data.currentIndex;
  168. });
  169. },
  170. // set indexes of all columns
  171. setIndexes: function (indexes) {
  172. var _this = this;
  173. var stack = indexes.map(function (optionIndex, columnIndex) {
  174. return _this.setColumnIndex(columnIndex, optionIndex);
  175. });
  176. return Promise.all(stack);
  177. },
  178. },
  179. });