hino / app / components / app-input-date-time.js
app-input-date-time.js
Raw
import Component from '@ember/component';
import {
    not,
    notEmpty,
    and,
    or,
    readOnly,
    alias
  } from '@ember/object/computed';
import { defineProperty } from '@ember/object';

export default Component.extend({
    id : '',
    text : '',
    readonly : true,
    isEditable : true,
    step : 5,
    positionalParams: ['model'],
    valuePath: '',

    validation: null,
    showValidations: false,
    didValidate: false,
  
    notValidating: not('validation.isValidating').readOnly(),
    hasContent: notEmpty('value').readOnly(),
    hasWarnings: notEmpty('validation.warnings').readOnly(),
    isValid: and('hasContent', 'validation.isTruelyValid').readOnly(),
    shouldDisplayValidations: or(
      'showValidations',
      'didValidate',
      'hasContent'
    ).readOnly(),
    showErrorClass: and(
      'notValidating',
      'showErrorMessage',
      'hasContent',
      'validation'
    ).readOnly(),
    showErrorMessage: and(
      'shouldDisplayValidations',
      'validation.isInvalid'
    ).readOnly(),
    showWarningMessage: and(
      'shouldDisplayValidations',
      'hasWarnings',
      'isValid'
    ).readOnly(),
    init() {
        this._super(...arguments);
        let valuePath = this.get('valuePath');
        if(valuePath != undefined && valuePath != null && valuePath != "" ){
          this.set('valuePath',valuePath);
        }else{
          this.set('valuePath',null);
        }
       
        defineProperty(
            this,
            'validation',
            readOnly(`model.validations.attrs.${valuePath}`)
        );
        defineProperty(this, 'value', alias(`model.${valuePath}`));
    },
    actions: {
        setCalendarValue(value) {
            let valuePath = this.get('valuePath');
            this.set(`model.${valuePath}`, value);
        },
        onBackSpace(event) {
            if (event != null) {
                if (event.keyCode == 8) {
                    let valuePath = this.get('valuePath');
                    this.set(`model.${valuePath}`, "");
                }
            }
        }
    }
});