gitlab-org--gitlab-foss/app/assets/javascripts/environments/components/environment_item.js.es6

358 lines
9.7 KiB
JavaScript
Raw Normal View History

/*= require vue_common_component/commit
2016-11-09 12:36:42 -05:00
/*= require ./environment_actions
2016-11-09 06:26:27 -05:00
/* globals Vue, timeago */
2016-10-14 13:38:13 -04:00
(() => {
/**
* Envrionment Item Component
2016-10-26 11:00:16 -04:00
*
* Used in a hierarchical structure to show folders with children
* in a table.
2016-10-26 11:00:16 -04:00
* Recursive component based on [Tree View](https://vuejs.org/examples/tree-view.html)
*
* See this [issue](https://gitlab.com/gitlab-org/gitlab-ce/issues/22539)
* for more information.
*/
2016-10-26 11:00:16 -04:00
2016-10-14 13:38:13 -04:00
window.gl = window.gl || {};
window.gl.environmentsList = window.gl.environmentsList || {};
2016-10-26 11:00:16 -04:00
gl.environmentsList.EnvironmentItem = Vue.component('environment-item', {
2016-10-26 11:00:16 -04:00
components: {
2016-11-09 12:36:42 -05:00
'commit-component': window.gl.CommitComponent,
'actions-component': window.gl.environmentsList.ActionsComponent,
},
2016-10-14 13:38:13 -04:00
props: {
model: Object,
2016-10-14 13:38:13 -04:00
},
data() {
2016-10-14 13:38:13 -04:00
return {
open: false,
rowClass: {
'children-row': this.model['vue-isChildren'],
},
2016-10-14 13:38:13 -04:00
};
},
2016-10-24 07:42:01 -04:00
2016-10-14 13:38:13 -04:00
computed: {
2016-10-26 11:00:16 -04:00
/**
* If an item has a `children` entry it means it is a folder.
* Folder items have different behaviours - it is possible to toggle
2016-10-26 11:00:16 -04:00
* them and show their children.
*
* @returns {Boolean}
*/
isFolder() {
return this.$options.hasKey(this.model, 'children') &&
this.model.children.length > 0;
},
2016-10-24 07:42:01 -04:00
/**
* If an item is inside a folder structure will return true.
* Used for css purposes.
*
* @returns {Boolean|undefined}
*/
isChildren() {
return this.model['vue-isChildren'];
},
2016-10-24 07:42:01 -04:00
/**
* Counts the number of environments in each folder.
* Used to show a badge with the counter.
*
* @returns {Boolean} The number of environments for the current folder
*/
childrenCounter() {
return this.$options.hasKey(this.model, 'children') &&
this.model.children.length;
},
/**
* Returns the value of the `last?` key sent in the API.
* Used to know wich title to render when the environment can be re-deployed
*
* @returns {Boolean}
*/
isLast() {
return this.$options.hasKey(this.model, 'last_deployment') &&
this.model.last_deployment['last?'];
},
/**
* Verifies if `last_deployment` key exists in the current Envrionment.
* This key is required to render most of the html - this method works has
* an helper.
*
* @returns {Boolean}
*/
hasLastDeploymentKey() {
return this.$options.hasKey(this.model, 'last_deployment');
},
/**
* Verifies is the given environment has manual actions.
* Used to verify if we should render them or nor.
*
2016-11-07 16:53:20 -05:00
* @returns {Boolean}
*/
hasManualActions() {
return this.$options.hasKey(this.model, 'manual_actions') &&
this.model.manual_actions.length;
},
/**
* Returns the value of the `stoppable?` key provided in the response.
*
* @returns {Boolean}
*/
isStoppable() {
return this.model['stoppable?'];
},
/**
* Verifies if the `deployable` key is present in `last_deployment` key.
* Used to verify whether we should or not render the rollback partial.
*
* @returns {Boolean}
*/
canRetry() {
return this.hasLastDeploymentKey &&
this.model.last_deployment &&
this.$options.hasKey(this.model.last_deployment, 'deployable');
},
2016-11-07 15:52:04 -05:00
/**
* Human readable date.
*
* @returns {String}
*/
createdDate() {
2016-11-09 06:23:04 -05:00
const timeagoInstance = new timeago();
return timeagoInstance.format(this.model.created_at);
},
2016-11-09 12:36:42 -05:00
/**
* Verifies if the environment has any manual actions to be rendered.
*
* @returns {Boolean}
*/
hasManualActions() {
return this.model.last_deployment &&
this.model.last_deployment.manual_actions &&
this.model.last_deployment.manual_actions.length > 0;
},
2016-11-07 15:52:04 -05:00
/**
* Returns the manual actions with the name parsed.
*
* @returns {Array.<Object>}
*/
manualActions() {
2016-11-09 12:36:42 -05:00
return this.model.last_deployment.manual_actions.map((action) => {
2016-11-07 15:52:04 -05:00
const parsedAction = {
name: gl.text.humanize(action.name),
play_url: action.play_url,
};
return parsedAction;
});
},
userImageAltDescription() {
return `${this.model.last_deployment.user.username}'s avatar'`;
},
2016-11-09 06:26:27 -05:00
/**
* If provided, returns the commit tag.
*
* @returns {String|Undefined}
*/
commitTag() {
if (this.model.last_deployment && this.model.last_deployment.tag) {
return this.model.last_deployment.tag;
}
2016-11-09 06:26:27 -05:00
return undefined;
},
/**
* If provided, returns the commit ref.
*
* @returns {Object|Undefined}
*/
commitRef() {
if (this.model.last_deployment && this.model.last_deployment.ref) {
2016-11-09 06:26:27 -05:00
return this.model.last_deployment.ref;
}
2016-11-09 06:26:27 -05:00
return undefined;
},
2016-11-09 06:26:27 -05:00
/**
* If provided, returns the commit url.
*
* @returns {String|Undefined}
*/
commitUrl() {
if (this.model.last_deployment &&
this.model.last_deployment.commit &&
this.model.last_deployment.commit.commit_url) {
return this.model.last_deployment.commit.commit_url;
}
2016-11-09 06:26:27 -05:00
return undefined;
},
/**
* If provided, returns the commit short sha.
*
* @returns {String|Undefined}
*/
commitShortSha() {
if (this.model.last_deployment &&
this.model.last_deployment.commit &&
this.model.last_deployment.commit.short_id) {
2016-11-09 06:26:27 -05:00
return this.model.last_deployment.commit.short_id;
}
2016-11-09 06:26:27 -05:00
return undefined;
},
/**
* If provided, returns the commit title.
*
* @returns {String|Undefined}
*/
2016-11-09 06:26:27 -05:00
commitTitle() {
if (this.model.last_deployment &&
this.model.last_deployment.commit &&
this.model.last_deployment.commit.title) {
2016-11-09 06:26:27 -05:00
return this.model.last_deployment.commit.title;
}
2016-11-09 06:26:27 -05:00
return undefined;
},
/**
* If provided, returns the commit tag.
*
* @returns {Object|Undefined}
*/
2016-11-09 06:26:27 -05:00
commitAuthor() {
if (this.model.last_deployment &&
this.model.last_deployment.commit &&
this.model.last_deployment.commit.author) {
return this.model.last_deployment.commit.author;
}
2016-11-09 06:26:27 -05:00
return undefined;
},
},
/**
* Helper to verify if key is present in an object.
* Can be removed once we start using lodash.
*
* @param {Object} obj
* @param {String} key
* @returns {Boolean}
*/
hasKey(obj, key) {
return {}.hasOwnProperty.call(obj, key);
2016-10-14 13:38:13 -04:00
},
methods: {
/**
* Toggles the visibility of a folders' children.
*/
toggle() {
2016-10-14 13:38:13 -04:00
if (this.isFolder) {
this.open = !this.open;
}
},
},
template: `
<tr>
<td v-bind:class="rowClass">
<a v-if="!isFolder" class="environment-name" :href="model.environment_url">
{{model.name}}
</a>
<span v-else v-on:click="toggle" class="folder-name">
<span class="folder-icon">
<i v-show="open" class="fa fa-caret-down"></i>
<i v-show="!open" class="fa fa-caret-right"></i>
</span>
{{model.name}}
<span class="badge">
{{childrenCounter}}
</span>
</span>
</td>
<td class="deployment-column">
<span v-if="!isFolder && model.last_deployment && model.last_deployment.iid">
#{{model.last_deployment.iid}}
<span v-if="model.last_deployment.user">
by
<a :href="model.last_deployment.user.web_url">
<img class="avatar has-tooltip s20"
:src="model.last_deployment.user.avatar_url"
:alt="userImageAltDescription"
:title="model.last_deployment.user.username" />
</a>
</span>
</span>
</td>
<td>
<a v-if="!isFolder && model.last_deployment && model.last_deployment.deployable"
class="build-link"
:href="model.last_deployment.deployable.build_url">
{{model.last_deployment.deployable.name}} #{{model.last_deployment.deployable.id}}
</a>
</td>
<td>
<div v-if="!isFolder && model.last_deployment">
<commit-component
:tag="commitTag"
:ref="commitRef"
:commit_url="commitUrl"
:short_sha="commitShortSha"
:title="commitTitle"
:author="commitAuthor">
</commit-component>
</div>
<p v-if="!isFolder && !model.last_deployment" class="commit-title">
No deployments yet
</p>
</td>
<td>
<span v-if="!isFolder && model.last_deployment" class="environment-created-date-timeago">
{{createdDate}}
</span>
</td>
<td class="hidden-xs">
<div v-if="!isFolder">
2016-11-09 12:36:42 -05:00
<div v-if="hasManualActions">
<actions-component :actions="manualActions"></actions-component>
</div>
</div>
</td>
</tr>
<tr v-if="open && isFolder"
is="environment-item"
v-for="model in model.children"
:model="model">
</tr>
`,
});
2016-10-20 07:09:35 -04:00
})();