time-spinner.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <div class="el-time-spinner" :class="{ 'has-seconds': showSeconds }">
  3. <template v-if="!arrowControl">
  4. <el-scrollbar
  5. @mouseenter.native="emitSelectRange('hours')"
  6. @mousemove.native="adjustCurrentSpinner('hours')"
  7. class="el-time-spinner__wrapper"
  8. wrap-style="max-height: inherit;"
  9. view-class="el-time-spinner__list"
  10. noresize
  11. tag="ul"
  12. ref="hours">
  13. <li
  14. @click="handleClick('hours', { value: hour, disabled: disabled })"
  15. v-for="(disabled, hour) in hoursList"
  16. class="el-time-spinner__item"
  17. :key="hour"
  18. :class="{ 'active': hour === hours, 'disabled': disabled }">{{ ('0' + (amPmMode ? (hour % 12 || 12) : hour )).slice(-2) }}{{ amPm(hour) }}</li>
  19. </el-scrollbar>
  20. <el-scrollbar
  21. @mouseenter.native="emitSelectRange('minutes')"
  22. @mousemove.native="adjustCurrentSpinner('minutes')"
  23. class="el-time-spinner__wrapper"
  24. wrap-style="max-height: inherit;"
  25. view-class="el-time-spinner__list"
  26. noresize
  27. tag="ul"
  28. ref="minutes">
  29. <li
  30. @click="handleClick('minutes', { value: key, disabled: false })"
  31. v-for="(enabled, key) in minutesList"
  32. :key="key"
  33. class="el-time-spinner__item"
  34. :class="{ 'active': key === minutes, disabled: !enabled }">{{ ('0' + key).slice(-2) }}</li>
  35. </el-scrollbar>
  36. <el-scrollbar
  37. v-show="showSeconds"
  38. @mouseenter.native="emitSelectRange('seconds')"
  39. @mousemove.native="adjustCurrentSpinner('seconds')"
  40. class="el-time-spinner__wrapper"
  41. wrap-style="max-height: inherit;"
  42. view-class="el-time-spinner__list"
  43. noresize
  44. tag="ul"
  45. ref="seconds">
  46. <li
  47. @click="handleClick('seconds', { value: key, disabled: false })"
  48. v-for="(second, key) in 60"
  49. class="el-time-spinner__item"
  50. :class="{ 'active': key === seconds }"
  51. :key="key">{{ ('0' + key).slice(-2) }}</li>
  52. </el-scrollbar>
  53. </template>
  54. <template v-if="arrowControl">
  55. <div
  56. @mouseenter="emitSelectRange('hours')"
  57. class="el-time-spinner__wrapper is-arrow">
  58. <i v-repeat-click="decrease" class="el-time-spinner__arrow el-icon-arrow-up"></i>
  59. <i v-repeat-click="increase" class="el-time-spinner__arrow el-icon-arrow-down"></i>
  60. <ul class="el-time-spinner__list" ref="hours">
  61. <li
  62. class="el-time-spinner__item"
  63. :class="{ 'active': hour === hours, 'disabled': hoursList[hour] }"
  64. v-for="(hour, key) in arrowHourList"
  65. :key="key">{{ hour === undefined ? '' : ('0' + (amPmMode ? (hour % 12 || 12) : hour )).slice(-2) + amPm(hour) }}</li>
  66. </ul>
  67. </div>
  68. <div
  69. @mouseenter="emitSelectRange('minutes')"
  70. class="el-time-spinner__wrapper is-arrow">
  71. <i v-repeat-click="decrease" class="el-time-spinner__arrow el-icon-arrow-up"></i>
  72. <i v-repeat-click="increase" class="el-time-spinner__arrow el-icon-arrow-down"></i>
  73. <ul class="el-time-spinner__list" ref="minutes">
  74. <li
  75. class="el-time-spinner__item"
  76. :class="{ 'active': minute === minutes }"
  77. v-for="(minute, key) in arrowMinuteList"
  78. :key="key">
  79. {{ minute === undefined ? '' : ('0' + minute).slice(-2) }}
  80. </li>
  81. </ul>
  82. </div>
  83. <div
  84. @mouseenter="emitSelectRange('seconds')"
  85. class="el-time-spinner__wrapper is-arrow"
  86. v-if="showSeconds">
  87. <i v-repeat-click="decrease" class="el-time-spinner__arrow el-icon-arrow-up"></i>
  88. <i v-repeat-click="increase" class="el-time-spinner__arrow el-icon-arrow-down"></i>
  89. <ul class="el-time-spinner__list" ref="seconds">
  90. <li
  91. v-for="(second, key) in arrowSecondList"
  92. class="el-time-spinner__item"
  93. :class="{ 'active': second === seconds }"
  94. :key="key">
  95. {{ second === undefined ? '' : ('0' + second).slice(-2) }}
  96. </li>
  97. </ul>
  98. </div>
  99. </template>
  100. </div>
  101. </template>
  102. <script type="text/babel">
  103. import { getRangeHours, getRangeMinutes, modifyTime } from 'element-ui/src/utils/date-util';
  104. import ElScrollbar from 'element-ui/packages/scrollbar';
  105. import RepeatClick from 'element-ui/src/directives/repeat-click';
  106. export default {
  107. components: { ElScrollbar },
  108. directives: {
  109. repeatClick: RepeatClick
  110. },
  111. props: {
  112. date: {},
  113. defaultValue: {}, // reserved for future use
  114. showSeconds: {
  115. type: Boolean,
  116. default: true
  117. },
  118. arrowControl: Boolean,
  119. amPmMode: {
  120. type: String,
  121. default: '' // 'a': am/pm; 'A': AM/PM
  122. }
  123. },
  124. computed: {
  125. hours() {
  126. return this.date.getHours();
  127. },
  128. minutes() {
  129. return this.date.getMinutes();
  130. },
  131. seconds() {
  132. return this.date.getSeconds();
  133. },
  134. hoursList() {
  135. return getRangeHours(this.selectableRange);
  136. },
  137. minutesList() {
  138. return getRangeMinutes(this.selectableRange, this.hours);
  139. },
  140. arrowHourList() {
  141. const hours = this.hours;
  142. return [
  143. hours > 0 ? hours - 1 : undefined,
  144. hours,
  145. hours < 23 ? hours + 1 : undefined
  146. ];
  147. },
  148. arrowMinuteList() {
  149. const minutes = this.minutes;
  150. return [
  151. minutes > 0 ? minutes - 1 : undefined,
  152. minutes,
  153. minutes < 59 ? minutes + 1 : undefined
  154. ];
  155. },
  156. arrowSecondList() {
  157. const seconds = this.seconds;
  158. return [
  159. seconds > 0 ? seconds - 1 : undefined,
  160. seconds,
  161. seconds < 59 ? seconds + 1 : undefined
  162. ];
  163. }
  164. },
  165. data() {
  166. return {
  167. selectableRange: [],
  168. currentScrollbar: null
  169. };
  170. },
  171. mounted() {
  172. this.$nextTick(() => {
  173. !this.arrowControl && this.bindScrollEvent();
  174. });
  175. },
  176. methods: {
  177. increase() {
  178. this.scrollDown(1);
  179. },
  180. decrease() {
  181. this.scrollDown(-1);
  182. },
  183. modifyDateField(type, value) {
  184. switch (type) {
  185. case 'hours': this.$emit('change', modifyTime(this.date, value, this.minutes, this.seconds)); break;
  186. case 'minutes': this.$emit('change', modifyTime(this.date, this.hours, value, this.seconds)); break;
  187. case 'seconds': this.$emit('change', modifyTime(this.date, this.hours, this.minutes, value)); break;
  188. }
  189. },
  190. handleClick(type, {value, disabled}) {
  191. if (!disabled) {
  192. this.modifyDateField(type, value);
  193. this.emitSelectRange(type);
  194. this.adjustSpinner(type, value);
  195. }
  196. },
  197. emitSelectRange(type) {
  198. if (type === 'hours') {
  199. this.$emit('select-range', 0, 2);
  200. } else if (type === 'minutes') {
  201. this.$emit('select-range', 3, 5);
  202. } else if (type === 'seconds') {
  203. this.$emit('select-range', 6, 8);
  204. }
  205. this.currentScrollbar = type;
  206. },
  207. bindScrollEvent() {
  208. const bindFuntion = (type) => {
  209. this.$refs[type].wrap.onscroll = (e) => {
  210. // TODO: scroll is emitted when set scrollTop programatically
  211. // should find better solutions in the future!
  212. this.handleScroll(type, e);
  213. };
  214. };
  215. bindFuntion('hours');
  216. bindFuntion('minutes');
  217. bindFuntion('seconds');
  218. },
  219. handleScroll(type) {
  220. const value = Math.min(Math.round((this.$refs[type].wrap.scrollTop - (this.scrollBarHeight(type) * 0.5 - 10) / this.typeItemHeight(type) + 3) / this.typeItemHeight(type)), (type === 'hours' ? 23 : 59));
  221. this.modifyDateField(type, value);
  222. },
  223. // NOTE: used by datetime / date-range panel
  224. // renamed from adjustScrollTop
  225. // should try to refactory it
  226. adjustSpinners() {
  227. this.adjustSpinner('hours', this.hours);
  228. this.adjustSpinner('minutes', this.minutes);
  229. this.adjustSpinner('seconds', this.seconds);
  230. },
  231. adjustCurrentSpinner(type) {
  232. this.adjustSpinner(type, this[type]);
  233. },
  234. adjustSpinner(type, value) {
  235. if (this.arrowControl) return;
  236. const el = this.$refs[type].wrap;
  237. if (el) {
  238. el.scrollTop = Math.max(0, value * this.typeItemHeight(type));
  239. }
  240. },
  241. scrollDown(step) {
  242. if (!this.currentScrollbar) {
  243. this.emitSelectRange('hours');
  244. }
  245. const label = this.currentScrollbar;
  246. const hoursList = this.hoursList;
  247. let now = this[label];
  248. if (this.currentScrollbar === 'hours') {
  249. let total = Math.abs(step);
  250. step = step > 0 ? 1 : -1;
  251. let length = hoursList.length;
  252. while (length-- && total) {
  253. now = (now + step + hoursList.length) % hoursList.length;
  254. if (hoursList[now]) {
  255. continue;
  256. }
  257. total--;
  258. }
  259. if (hoursList[now]) return;
  260. } else {
  261. now = (now + step + 60) % 60;
  262. }
  263. this.modifyDateField(label, now);
  264. this.adjustSpinner(label, now);
  265. this.$nextTick(() => this.emitSelectRange(this.currentScrollbar));
  266. },
  267. amPm(hour) {
  268. let shouldShowAmPm = this.amPmMode.toLowerCase() === 'a';
  269. if (!shouldShowAmPm) return '';
  270. let isCapital = this.amPmMode === 'A';
  271. let content = (hour < 12) ? ' am' : ' pm';
  272. if (isCapital) content = content.toUpperCase();
  273. return content;
  274. },
  275. typeItemHeight(type) {
  276. return this.$refs[type].$el.querySelector('li').offsetHeight;
  277. },
  278. scrollBarHeight(type) {
  279. return this.$refs[type].$el.offsetHeight;
  280. }
  281. }
  282. };
  283. </script>