Guest User

Untitled

a guest
Oct 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import Ember from 'ember';
  2.  
  3. const {
  4. get,
  5. assert,
  6. isPresent,
  7. computed,
  8. Component
  9. } = Ember;
  10.  
  11. const STATE_MSG = 'You must include a "state" attribute for every use of {{my-component}}';
  12.  
  13. const CHECKED = 'CHECKED';
  14. const CHECKED_DISABLED = 'CHECKED_DISABLED';
  15. const NOT_CHECKED = 'NOT_CHECKED';
  16. const NOT_CHECKED_DISABLED = 'NOT_CHECKED_DISABLED';
  17.  
  18. const STATES = [CHECKED, CHECKED_DISABLED, NOT_CHECKED, NOT_CHECKED_DISABLED];
  19. const VALID_STATES = new Set(STATES);
  20.  
  21. const isCheckbox = state => !(state === NOT_CHECKED_DISABLED);
  22. const isChecked = state => [CHECKED, CHECKED_DISABLED].contains(state);
  23. const isDisabled = state => [CHECKED_DISABLED, NOT_CHECKED_DISABLED].contains(state);
  24.  
  25. const stateToTitle = state => isCheckbox(state) ?
  26. isDisabled(state) ? '_t.payment.template-user.tooltip.role.canManage' : '_t.payment.template-user.tooltip.role.canAccess' :
  27. '_t.payment.template-user.tooltip.role.cannotAccess';
  28.  
  29. const stateToAriaLabel = state => isDisabled(state) ?
  30. '_t.payment.template-user.tooltip.role.cannotAccess' :
  31. '_t.payment.template-info.selectUserRole';
  32.  
  33. export default Component.extend({
  34. validateState: value => assert(STATE_MSG, (isPresent(value) && VALID_STATES.has(value))),
  35.  
  36. didInitAttrs() {
  37. this._super(...arguments);
  38. let state = get(this, 'state');
  39. this.validateState(state);
  40. },
  41.  
  42. state: computed({
  43. get() {
  44. return null;
  45. },
  46. set(key, state) {
  47. this.validateState(state, STATE_MSG);
  48. return state;
  49. }
  50. }),
  51.  
  52. ariaLabel: computed('state', function() {
  53. let state = get(this, 'state');
  54. return stateToAriaLabel(state);
  55. }),
  56.  
  57. label: computed('state', function() {
  58. let state = get(this, 'state');
  59. return stateToTitle(state);
  60. }),
  61.  
  62. isCheckbox: computed('state', function() {
  63. let state = get(this, 'state');
  64. return isCheckbox(state);
  65. }),
  66.  
  67. isChecked: computed('state', function() {
  68. let state = get(this, 'state');
  69. return isChecked(state);
  70. }),
  71.  
  72. isDisabled: computed('state', function() {
  73. let state = get(this, 'state');
  74. return isDisabled(state);
  75. }),
  76. });
Add Comment
Please, Sign In to add comment