All files / addon/components/bootstrap edit-form.js

10.53% Statements 2/19
0% Branches 0/16
10% Functions 1/10
10.53% Lines 2/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82              1x                                                       1x                                                                                            
import Component from '@ember/component';
import { computed } from '@ember/object';
import layout from '../../templates/components/bootstrap/edit-form';
import { PropTypes } from 'ember-prop-types';
import { task, didCancel } from 'ember-concurrency';
import { BuilderForPropTypes, BuilderForPropDefaults } from 'ember-bootstrap-controls/utils/prop-definition-tools';
 
export const propDefinitions = {
  isEditing: {
    default: false,
    description: 'Indicates whether the form is in an editing state.',
    type: PropTypes.bool,
  },
  save: {
    description: 'Function to be called when the form is submited/saved.',
    type: PropTypes.func.isRequired,
  },
  cancel: {
    description: 'Function to be called when the form is canceled.',
    type: PropTypes.func,
  },
};
 
export default Component.extend({
  layout,
  tagName: 'form',
  propTypes: BuilderForPropTypes(propDefinitions),
  isRunning: computed('asyncSaveTask.isRunning', 'asyncCancelTask.isRunning', function() {
    return this.get('asyncSaveTask.isRunning') || this.get('asyncCancelTask.isRunning');
  }),
  isDisabled: computed('isRunning', 'isEditing', function() {
    return this.get('isRunning') || !this.get('isEditing');
  }),
 
  getDefaultProps() {
    return BuilderForPropDefaults(propDefinitions)
  },
 
  asyncSaveTask: task(function * (asyncTask) {
      if (asyncTask) {
        if (yield asyncTask()) {
          this.set('isEditing', false);
        };
      } else {
        this.set('isEditing', false);
      }
  }).drop(),
 
  asyncCancelTask: task(function * (asyncTask) {
    if (asyncTask) {
      if (yield asyncTask()) {
        this.set('isEditing', false);
      };
    } else {
      this.set('isEditing', false);
    }
  }).drop(),
 
 
  actions: {
    edit() {
      this.set('isEditing', true);
    },
 
    save() {
      return this.get('asyncSaveTask').perform(this.get('save')).catch(e => {
        if (!didCancel(e)) {
          throw e;
        }
      });
    },
 
    cancel() {
      return this.get('asyncCancelTask').perform(this.get('cancel')).catch(e => {
        if (!didCancel(e)) {
          throw e;
        }
      });
    }
  }
});