|
| 1 | +define [ |
| 2 | + 'i18n!publish_icon_component' |
| 3 | + 'ember' |
| 4 | + '../register' |
| 5 | + '../templates/components/ic-publish-icon' |
| 6 | +], (I18n, Ember, register) -> |
| 7 | + |
| 8 | + |
| 9 | + # example usage: |
| 10 | + # |
| 11 | + # {{ic-publish-icon |
| 12 | + # disabled=disabled |
| 13 | + # disabled-message=disabledMessage |
| 14 | + # is-published=isPublished |
| 15 | + # on-publish="publish" |
| 16 | + # on-unpublish="unpublish" |
| 17 | + # }} |
| 18 | + |
| 19 | + |
| 20 | + register 'component', 'ic-publish-icon', Ember.Component.extend |
| 21 | + |
| 22 | + # public published property the controller binds to |
| 23 | + 'is-published': false |
| 24 | + |
| 25 | + tagName: 'span' |
| 26 | + |
| 27 | + role: 'button' |
| 28 | + |
| 29 | + attributeBindings: ['data-tooltip', 'aria-label', 'title', 'tabindex', 'aria-disabled'] |
| 30 | + |
| 31 | + classNames: ['publish-icon'] |
| 32 | + |
| 33 | + classNameBindings: ['wrapperClass', 'disabled'] |
| 34 | + |
| 35 | + 'data-tooltip': 'top' |
| 36 | + |
| 37 | + # internal state that determines what is rendered |
| 38 | + publishState: null |
| 39 | + |
| 40 | + mouseIsHovered: false |
| 41 | + |
| 42 | + iconClass: (-> |
| 43 | + switch @get('publishState') |
| 44 | + when 'published' then 'icon-publish' |
| 45 | + when 'unpublished' then 'icon-unpublished' |
| 46 | + when 'hoverPublished' then 'icon-unpublish' |
| 47 | + when 'hoverUnpublished' then 'icon-unpublished' |
| 48 | + when 'publishing' then 'icon-publish' |
| 49 | + when 'unpublishing' then 'icon-unpublished' |
| 50 | + when 'hoverJustUnpublished' then 'icon-unpublished' |
| 51 | + when 'hoverJustPublished' then 'icon-publish' |
| 52 | + ).property('publishState') |
| 53 | + |
| 54 | + wrapperClass: (-> |
| 55 | + switch @get('publishState') |
| 56 | + when 'published' then 'publish-icon-published' |
| 57 | + when 'unpublished' then 'publish-icon-unpublished' |
| 58 | + when 'hoverPublished' then 'publish-icon-unpublish' |
| 59 | + when 'hoverUnpublished' then 'publish-icon-publish' |
| 60 | + when 'publishing' then 'publish-icon-publish' |
| 61 | + when 'unpublishing' then 'publish-icon-unpublish' |
| 62 | + when 'hoverJustUnpublished' then 'publish-icon-publish' |
| 63 | + when 'hoverJustPublished' then 'publish-icon-published' |
| 64 | + ).property('publishState') |
| 65 | + |
| 66 | + mouseEnter: -> |
| 67 | + return if @get('disabled') |
| 68 | + @set('mouseIsHovered', true) |
| 69 | + return if @get('publishState') in ['publishing', 'unpublishing'] |
| 70 | + if @get('is-published') |
| 71 | + @set('publishState', 'hoverPublished') |
| 72 | + else |
| 73 | + @set('publishState', 'hoverUnpublished') |
| 74 | + |
| 75 | + mouseLeave: (-> |
| 76 | + return if @get('disabled') and @get('state') is 'inDOM' |
| 77 | + @set('mouseIsHovered', false) |
| 78 | + return if @get('publishState') in ['publishing', 'unpublishing'] |
| 79 | + if @get('is-published') |
| 80 | + @set('publishState', 'published') |
| 81 | + else |
| 82 | + @set('publishState', 'unpublished') |
| 83 | + ).on('init') |
| 84 | + |
| 85 | + setPublishStateOnIsPublished: (-> |
| 86 | + return if @get('is-published') is null |
| 87 | + if @get('is-published') and @get('mouseIsHovered') |
| 88 | + @set('publishState', 'hoverJustPublished') |
| 89 | + else if !@get('is-published') and @get('mouseIsHovered') |
| 90 | + @set('publishState', 'hoverJustUnpublished') |
| 91 | + else if @get('is-published') |
| 92 | + @set('publishState', 'published') |
| 93 | + else |
| 94 | + @set('publishState', 'unpublished') |
| 95 | + ).observes('is-published') |
| 96 | + |
| 97 | + title: (-> |
| 98 | + if @get('disabled') |
| 99 | + @get('disabled-message') |
| 100 | + else if @get('is-published') |
| 101 | + I18n.t('unpublish', 'unpublish') |
| 102 | + else |
| 103 | + I18n.t('publish', 'publish') |
| 104 | + ).property('is-published') |
| 105 | + |
| 106 | + 'aria-disabled': (-> |
| 107 | + @get('disabled')+'' |
| 108 | + ).property('disabled') |
| 109 | + |
| 110 | + 'aria-label': (-> |
| 111 | + if @get('is-published') |
| 112 | + I18n.t('unpublish_click', 'unpublished, click to publish') |
| 113 | + else |
| 114 | + I18n.t('publish_click', 'published, click to unpublish') |
| 115 | + ).property('is-published') |
| 116 | + |
| 117 | + click: -> |
| 118 | + return if @get('disabled') |
| 119 | + if @get('is-published') |
| 120 | + @set('publishState', 'unpublishing') |
| 121 | + @sendAction 'on-unpublish' |
| 122 | + else |
| 123 | + @set('publishState', 'publishing') |
| 124 | + @sendAction 'on-publish' |
| 125 | + @set('is-published', null) |
| 126 | + |
0 commit comments