index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. this.children = this.selectAllComponents('.van-picker__column');
  44. if (Array.isArray(this.children) && this.children.length) {
  45. this.setColumns().catch(function () {});
  46. }
  47. },
  48. },
  49. }),
  50. beforeCreate: function () {
  51. this.children = [];
  52. },
  53. methods: {
  54. noop: function () {},
  55. setColumns: function () {
  56. var _this = this;
  57. var data = this.data;
  58. var columns = this.simple ? [{ values: data.columns }] : data.columns;
  59. var stack = columns.map(function (column, index) {
  60. return _this.setColumnValues(index, column.values);
  61. });
  62. return Promise.all(stack);
  63. },
  64. emit: function (event) {
  65. var type = event.currentTarget.dataset.type;
  66. if (this.simple) {
  67. this.$emit(type, {
  68. value: this.getColumnValue(0),
  69. index: this.getColumnIndex(0),
  70. });
  71. } else {
  72. this.$emit(type, {
  73. value: this.getValues(),
  74. index: this.getIndexes(),
  75. });
  76. }
  77. },
  78. onChange: function (event) {
  79. if (this.simple) {
  80. this.$emit('change', {
  81. picker: this,
  82. value: this.getColumnValue(0),
  83. index: this.getColumnIndex(0),
  84. });
  85. } else {
  86. this.$emit('change', {
  87. picker: this,
  88. value: this.getValues(),
  89. index: event.currentTarget.dataset.index,
  90. });
  91. }
  92. },
  93. // get column instance by index
  94. getColumn: function (index) {
  95. return this.children[index];
  96. },
  97. // get column value by index
  98. getColumnValue: function (index) {
  99. var column = this.getColumn(index);
  100. return column && column.getValue();
  101. },
  102. // set column value by index
  103. setColumnValue: function (index, value) {
  104. var column = this.getColumn(index);
  105. if (column == null) {
  106. return Promise.reject(new Error('setColumnValue: 对应列不存在'));
  107. }
  108. return column.setValue(value);
  109. },
  110. // get column option index by column index
  111. getColumnIndex: function (columnIndex) {
  112. return (this.getColumn(columnIndex) || {}).data.currentIndex;
  113. },
  114. // set column option index by column index
  115. setColumnIndex: function (columnIndex, optionIndex) {
  116. var column = this.getColumn(columnIndex);
  117. if (column == null) {
  118. return Promise.reject(new Error('setColumnIndex: 对应列不存在'));
  119. }
  120. return column.setIndex(optionIndex);
  121. },
  122. // get options of column by index
  123. getColumnValues: function (index) {
  124. return (this.children[index] || {}).data.options;
  125. },
  126. // set options of column by index
  127. setColumnValues: function (index, options, needReset) {
  128. if (needReset === void 0) {
  129. needReset = true;
  130. }
  131. var column = this.children[index];
  132. if (column == null) {
  133. return Promise.reject(new Error('setColumnValues: 对应列不存在'));
  134. }
  135. var isSame =
  136. JSON.stringify(column.data.options) === JSON.stringify(options);
  137. if (isSame) {
  138. return Promise.resolve();
  139. }
  140. return column.set({ options: options }).then(function () {
  141. if (needReset) {
  142. column.setIndex(0);
  143. }
  144. });
  145. },
  146. // get values of all columns
  147. getValues: function () {
  148. return this.children.map(function (child) {
  149. return child.getValue();
  150. });
  151. },
  152. // set values of all columns
  153. setValues: function (values) {
  154. var _this = this;
  155. var stack = values.map(function (value, index) {
  156. return _this.setColumnValue(index, value);
  157. });
  158. return Promise.all(stack);
  159. },
  160. // get indexes of all columns
  161. getIndexes: function () {
  162. return this.children.map(function (child) {
  163. return child.data.currentIndex;
  164. });
  165. },
  166. // set indexes of all columns
  167. setIndexes: function (indexes) {
  168. var _this = this;
  169. var stack = indexes.map(function (optionIndex, columnIndex) {
  170. return _this.setColumnIndex(columnIndex, optionIndex);
  171. });
  172. return Promise.all(stack);
  173. },
  174. },
  175. });