import $ from 'jquery'; import React from 'react/addons'; import Router from 'react-router'; import shell from 'shell'; import RetinaImage from 'react-retina-image'; import metrics from '../utils/MetricsUtil'; import containerActions from '../actions/ContainerActions'; import imageActions from '../actions/ImageActions'; import containerStore from '../stores/ContainerStore'; import tagStore from '../stores/TagStore'; import tagActions from '../actions/TagActions'; import numeral from 'numeral'; var ImageCard = React.createClass({ mixins: [Router.Navigation], getInitialState: function () { return { tags: this.props.tags || [], chosenTag: this.props.chosenTag || 'latest' }; }, componentDidMount: function () { tagStore.listen(this.update); }, componentWillUnmount: function () { tagStore.unlisten(this.update); }, update: function () { let repo = this.props.image.namespace + '/' + this.props.image.name; let state = tagStore.getState(); if (this.state.tags.length && !state.tags[repo]) { $(this.getDOMNode()).find('.tag-overlay').fadeOut(300); } this.setState({ loading: tagStore.getState().loading[repo] || false, tags: tagStore.getState().tags[repo] || [] }); }, handleTagClick: function (tag) { this.setState({ chosenTag: tag }); var $tagOverlay = $(this.getDOMNode()).find('.tag-overlay'); $tagOverlay.fadeOut(300); metrics.track('Selected Image Tag'); }, handleClick: function () { metrics.track('Created Container', { from: 'search', private: this.props.image.is_private, official: this.props.image.namespace === 'library', userowned: this.props.image.is_user_repo, recommended: this.props.image.is_recommended, local: this.props.image.is_local || false }); let name = containerStore.generateName(this.props.image.name); let localImage = this.props.image.is_local || false; let repo = (this.props.image.namespace === 'library' || this.props.image.namespace === 'local') ? this.props.image.name : this.props.image.namespace + '/' + this.props.image.name; containerActions.run(name, repo, this.state.chosenTag, localImage); this.transitionTo('containerHome', {name}); }, handleMenuOverlayClick: function () { let $menuOverlay = $(this.getDOMNode()).find('.menu-overlay'); $menuOverlay.fadeIn(300); }, handleCloseMenuOverlay: function () { var $menuOverlay = $(this.getDOMNode()).find('.menu-overlay'); $menuOverlay.fadeOut(300); }, handleTagOverlayClick: function () { let $tagOverlay = $(this.getDOMNode()).find('.tag-overlay'); $tagOverlay.fadeIn(300); let localImage = this.props.image.is_local || false; if (localImage) { tagActions.localTags(this.props.image.namespace + '/' + this.props.image.name, this.props.tags); } else { tagActions.tags(this.props.image.namespace + '/' + this.props.image.name); } }, handleCloseTagOverlay: function () { let $menuOverlay = $(this.getDOMNode()).find('.menu-overlay'); $menuOverlay.hide(); var $tagOverlay = $(this.getDOMNode()).find('.tag-overlay'); $tagOverlay.fadeOut(300); }, handleDeleteImgClick: function (image) { if (this.state.chosenTag && !this.props.image.inUse) { imageActions.destroy(image.RepoTags[0].split(':')[0] + ':' + this.state.chosenTag); } }, handleRepoClick: function () { var repoUri = 'https://hub.docker.com/'; if (this.props.image.namespace === 'library') { repoUri = repoUri + '_/' + this.props.image.name; } else { repoUri = repoUri + 'r/' + this.props.image.namespace + '/' + this.props.image.name; } shell.openExternal(repoUri); }, render: function () { var self = this; var name; if (this.props.image.namespace === 'library') { name = (
official
{this.props.image.name}
); } else { name = (
{this.props.image.namespace}
{this.props.image.name}
); } var description; if (this.props.image.description) { description = this.props.image.description; } else if(this.props.image.short_description){ description = this.props.image.short_description; } else { description = 'No description.'; } var logoStyle = { backgroundColor: this.props.image.gradient_start }; var imgsrc; if (this.props.image.img) { imgsrc = `https://kitematic.com/recommended/${this.props.image.img}`; } else { imgsrc = 'https://kitematic.com/recommended/kitematic_html.png'; } var tags; if (self.state.loading) { tags = ; } else if (self.state.tags.length === 0) { tags =
No Tags
; } else { var tagDisplay = self.state.tags.map(function (tag) { let t = tag.name; if (t === self.state.chosenTag) { return
{t}
; } else { return
{t}
; } }); tags = (
{tagDisplay}
); } var badge = null; if (this.props.image.namespace === 'library') { badge = ( ); } else if (this.props.image.is_private) { badge = ( ); } let create, overlay; if (this.props.image.is_local) { create = (
{this.state.chosenTag}
CREATE
); overlay = (
SELECTED TAG: {this.state.chosenTag}
Delete Tag
{this.props.image.inUse ?

To delete, remove all containers
using the above image

: null }
); } else { let favCount = (this.props.image.star_count < 1000) ? numeral(this.props.image.star_count).value() : numeral(this.props.image.star_count).format('0.0a').toUpperCase(); let pullCount = (this.props.image.pull_count < 1000) ? numeral(this.props.image.pull_count).value() : numeral(this.props.image.pull_count).format('0a').toUpperCase(); create = (
{favCount} {pullCount}
CREATE
); overlay = (
SELECTED TAG: {this.state.chosenTag}
VIEW ON DOCKER HUB
); } return (
{overlay}

Please select an image tag.

{tags}
{badge}
{name}
{description}
{create}
); } }); module.exports = ImageCard;