index.js 5.2 KB

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