addon/components/ui-date-time-input.js

  1. import moment from 'moment';
  2. import layout from '../templates/components/ui-date-time-input';
  3. import Component from '@ember/component';
  4. import { observer } from '@ember/object'
  5.  
  6. const minutes=[];
  7.  
  8. for (let i = 0; i < 60; i++) {
  9. minutes[i] = {'value':i, 'name':i};
  10. }
  11.  
  12. const hours = [];
  13.  
  14. for (let i = 0; i < 24; i++) {
  15. hours[i] = {'value':i, 'name':i};
  16. }
  17.  
  18. /**
  19. ui-date-time-input component
  20.  
  21. @module components
  22. @namespace components
  23. @class UiDateTimeInput
  24. @constructor
  25. */
  26. export default Component.extend({
  27. layout,
  28. minutes:minutes,
  29. hours:hours,
  30. /**
  31. datetime value
  32. @property {String} value
  33. @default null
  34. */
  35. value: null,
  36. classNameBindings: ['class'],
  37. /**
  38. class apply to this component
  39. @property {String} class
  40. @default ''
  41. */
  42. class: '',
  43. rtime: observer('d,h,m', function(){
  44. let hour = this.h;
  45. let minute = this.m;
  46. let date = moment(this.d);
  47. let options = {
  48. year: date.year(),
  49. month: date.month(),
  50. day: date.day(),
  51. hour: hour,
  52. minute: minute,
  53. };
  54. let time = moment(options).format('YYYY-MM-DD HH:mm');
  55. if(typeof this.attrs.update === 'function'){
  56. this.attrs.update(time);
  57. }
  58. }),
  59. init(){
  60. this._super(...arguments);
  61. if(this.value && moment(this.value).isValid()){
  62. let time = moment(this.value);
  63. this.set('d', time.format('YYYY-MM-DD'));
  64. this.set('h', time.hour());
  65. this.set('m', time.minute());
  66. }
  67. }
  68. });
  69.