Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Ember from 'ember';
- const {
- get,
- assert,
- isPresent,
- computed,
- Component
- } = Ember;
- const STATE_MSG = 'You must include a "state" attribute for every use of {{my-component}}';
- const CHECKED = 'CHECKED';
- const CHECKED_DISABLED = 'CHECKED_DISABLED';
- const NOT_CHECKED = 'NOT_CHECKED';
- const NOT_CHECKED_DISABLED = 'NOT_CHECKED_DISABLED';
- const STATES = [CHECKED, CHECKED_DISABLED, NOT_CHECKED, NOT_CHECKED_DISABLED];
- const VALID_STATES = new Set(STATES);
- const isCheckbox = state => !(state === NOT_CHECKED_DISABLED);
- const isChecked = state => [CHECKED, CHECKED_DISABLED].contains(state);
- const isDisabled = state => [CHECKED_DISABLED, NOT_CHECKED_DISABLED].contains(state);
- const stateToTitle = state => isCheckbox(state) ?
- isDisabled(state) ? '_t.payment.template-user.tooltip.role.canManage' : '_t.payment.template-user.tooltip.role.canAccess' :
- '_t.payment.template-user.tooltip.role.cannotAccess';
- const stateToAriaLabel = state => isDisabled(state) ?
- '_t.payment.template-user.tooltip.role.cannotAccess' :
- '_t.payment.template-info.selectUserRole';
- export default Component.extend({
- validateState: value => assert(STATE_MSG, (isPresent(value) && VALID_STATES.has(value))),
- didInitAttrs() {
- this._super(...arguments);
- let state = get(this, 'state');
- this.validateState(state);
- },
- state: computed({
- get() {
- return null;
- },
- set(key, state) {
- this.validateState(state, STATE_MSG);
- return state;
- }
- }),
- ariaLabel: computed('state', function() {
- let state = get(this, 'state');
- return stateToAriaLabel(state);
- }),
- label: computed('state', function() {
- let state = get(this, 'state');
- return stateToTitle(state);
- }),
- isCheckbox: computed('state', function() {
- let state = get(this, 'state');
- return isCheckbox(state);
- }),
- isChecked: computed('state', function() {
- let state = get(this, 'state');
- return isChecked(state);
- }),
- isDisabled: computed('state', function() {
- let state = get(this, 'state');
- return isDisabled(state);
- }),
- });
Add Comment
Please, Sign In to add comment