time-spinner.vue 9.0 KB

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