index.js 5.9 KB

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