index.js 5.8 KB

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