index.js 5.7 KB

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