|
| 1 | +class @Article extends React.Component |
| 2 | + @propTypes = |
| 3 | + article: React.PropTypes.object |
| 4 | + authors: React.PropTypes.array |
| 5 | + articles: React.PropTypes.array |
| 6 | + rankSelect: React.PropTypes.bool |
| 7 | + from_issues_page: React.PropTypes.integer |
| 8 | + |
| 9 | + constructor: (props) -> |
| 10 | + super(props) |
| 11 | + @state = {} |
| 12 | + @styles = |
| 13 | + statusCol: |
| 14 | + width: '120px' |
| 15 | + textAlign: 'center' |
| 16 | + padding: '8px 0' |
| 17 | + mainCol: |
| 18 | + padding: '8px 6px' |
| 19 | + actionsCol: |
| 20 | + width: '120px' |
| 21 | + textAlign: 'center' |
| 22 | + paddingBottom: '2px' |
| 23 | + label: |
| 24 | + marginRight: '4px' |
| 25 | + slug: |
| 26 | + fontWeight: 'bold' |
| 27 | + marginLeft: '3px' |
| 28 | + marginRight: '4px' |
| 29 | + headline: |
| 30 | + marginLeft: '4px' |
| 31 | + publishTime: |
| 32 | + marginRight: '4px' |
| 33 | + authors: |
| 34 | + marginLeft: '4px' |
| 35 | + secondaryLine: |
| 36 | + color: '#888' |
| 37 | + rankSelect: |
| 38 | + width: '60px' |
| 39 | + |
| 40 | + ################################################################################ |
| 41 | + # Handlers |
| 42 | + ################################################################################ |
| 43 | + |
| 44 | + handlePublish: (cb) => |
| 45 | + @props.onAction('post', Routes.publish_article_draft_path(@props.article, @props.article.pending_draft.id), {}, cb) |
| 46 | + |
| 47 | + handleUnpublish: (cb) => |
| 48 | + @props.onAction('post', Routes.unpublish_article_path(@props.article), {}, cb) |
| 49 | + |
| 50 | + handleDelete: (cb) => |
| 51 | + @props.onAction('delete', Routes.article_path(@props.article), {}, cb) |
| 52 | + |
| 53 | + handleRankSelect: (rank, cb) => |
| 54 | + @props.onAction('patch', Routes.update_rank_article_path(@props.article), {article: {rank: rank}}, cb) |
| 55 | + cb() |
| 56 | + |
| 57 | + ################################################################################ |
| 58 | + # Renderers |
| 59 | + ################################################################################ |
| 60 | + |
| 61 | + renderButton: (type, text, handler, cond = true, confirm = null) -> |
| 62 | + return null if !cond |
| 63 | + `<Button type={type} text={text} onClick={handler} confirm={confirm}/>` |
| 64 | + |
| 65 | + renderLink: (type, text, href, cond = true, confirm = null) -> |
| 66 | + return null if !cond |
| 67 | + gotoURL = (cb) -> |
| 68 | + window.location.href = href |
| 69 | + cb() |
| 70 | + `<Button type={type} text={text} onClick={gotoURL} confirm={confirm}/>` |
| 71 | + |
| 72 | + renderWebStatus: -> |
| 73 | + if @props.article.has_web_published_draft |
| 74 | + if @props.article.has_pending_draft |
| 75 | + `<StatusLabel type="info">Web: Has Update</StatusLabel>` |
| 76 | + else |
| 77 | + `<StatusLabel type="success">Web: Published</StatusLabel>` |
| 78 | + else if @props.article.has_web_ready_draft |
| 79 | + `<StatusLabel type="info">Web: Ready</StatusLabel>` |
| 80 | + else |
| 81 | + `<StatusLabel type="danger">Web: Draft</StatusLabel>` |
| 82 | + |
| 83 | + renderPrintStatus: -> |
| 84 | + if @props.article.has_print_ready_draft |
| 85 | + `<StatusLabel type="success">Print: Ready</StatusLabel>` |
| 86 | + else |
| 87 | + `<StatusLabel type="danger">Print: Draft</StatusLabel>` |
| 88 | + |
| 89 | + renderActions: -> |
| 90 | + `<td style={this.styles.actionsCol}> |
| 91 | + {this.renderButton('success', 'Publish', this.handlePublish, (this.props.article.has_pending_draft && |
| 92 | + !this.props.article.has_web_published_draft && |
| 93 | + this.props.article.can_publish))} |
| 94 | + {this.renderButton('success', 'Publish Update', this.handlePublish, (this.props.article.has_pending_draft && |
| 95 | + this.props.article.has_web_published_draft && |
| 96 | + this.props.article.can_publish))} |
| 97 | + {this.renderButton('danger', 'Delete', this.handleDelete, (!this.props.article.has_web_published_draft && |
| 98 | + this.props.article.can_destroy), |
| 99 | + 'Are you sure that you want to delete this article? This will delete all drafts. ')} |
| 100 | + {this.renderButton('danger', 'Unpublish', this.handleUnpublish, (this.props.article.has_web_published_draft && |
| 101 | + this.props.article.can_unpublish), |
| 102 | + 'Are you sure that you want to unpublish this article? The publish time would change if you ' + |
| 103 | + 'later publish it again. ')} |
| 104 | + {this.renderLink('default', 'View Drafts', Routes.article_drafts_path(this.props.article, {format: 'html'}))} |
| 105 | + </td>` |
| 106 | + |
| 107 | + render: -> |
| 108 | + `<tr> |
| 109 | + <td style={this.styles.statusCol}> |
| 110 | + {this.renderWebStatus()} |
| 111 | + {this.renderPrintStatus()} |
| 112 | + { this.props.rankSelect && this.props.article.can_update_rank && |
| 113 | + <AutoSelect style={this.styles.rankSelect} |
| 114 | + titles={[99].concat(range(1, 20))} |
| 115 | + ids={[99].concat(range(1, 20))} |
| 116 | + initial={this.props.article.rank} |
| 117 | + onChange={this.handleRankSelect}/> |
| 118 | + } |
| 119 | + </td> |
| 120 | + <td style={this.styles.mainCol}> |
| 121 | + <p> |
| 122 | + <span style={this.styles.label} className="label label-default">{this.props.article.issue.short_name}</span> |
| 123 | + <span style={this.styles.label} className="label label-default">{this.props.article.section.name.toUpperCase()}</span> |
| 124 | + <span style={this.styles.label} className="label label-default">{this.props.article.newest_draft.primary_tag.toUpperCase()}</span> |
| 125 | + { |
| 126 | + (this.props.from_issues_page) |
| 127 | + ? <a style={this.styles.slug} href={Routes.article_drafts_path(this.props.article, {format: 'html'})+"?issue="+String(this.props.from_issues_page)}>{this.props.article.slug}</a> |
| 128 | + : <a style={this.styles.slug} href={Routes.article_drafts_path(this.props.article, {format: 'html'})}>{this.props.article.slug}</a> |
| 129 | + } |
| 130 | + • |
| 131 | + <span style={this.styles.headline}>{this.props.article.newest_draft.headline}</span> |
| 132 | + </p> |
| 133 | + <p style={this.styles.secondaryLine}> |
| 134 | + { |
| 135 | + (this.props.article.oldest_web_published_draft != null && |
| 136 | + this.props.article.oldest_web_published_draft != undefined) |
| 137 | + ? <span style={this.styles.publishTime}>Published online {$.timeago(Date.parse(this.props.article.oldest_web_published_draft.published_at))}</span> |
| 138 | + : <span style={this.styles.publishTime}>Not published online yet</span> |
| 139 | + } |
| 140 | + • |
| 141 | + <span style={this.styles.authors}>Authored by {this.props.article.newest_draft.authors_string}</span> |
| 142 | + </p> |
| 143 | + </td> |
| 144 | + {this.renderActions()} |
| 145 | + </tr>` |
0 commit comments