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

95 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-10-14 08:14:18 -04:00
//= require vue
//= require vue-resource
//= require_tree ./stores
//= require_tree ./services
2016-10-14 13:38:13 -04:00
//= require ./components/environment_item
2016-10-25 08:50:27 -04:00
//= require ../boards/vue_resource_interceptor
2016-11-07 15:52:04 -05:00
/* globals Vue, EnvironmentsService */
/* eslint-disable no-param-reassign */
2016-10-14 08:14:18 -04:00
$(() => {
2016-10-17 13:03:19 -04:00
const environmentsListApp = document.getElementById('environments-list-view');
2016-10-14 08:14:18 -04:00
const Store = gl.environmentsList.EnvironmentsStore;
2016-10-26 11:00:16 -04:00
2016-10-14 08:14:18 -04:00
window.gl = window.gl || {};
2016-10-26 11:00:16 -04:00
2016-10-14 08:14:18 -04:00
if (gl.EnvironmentsListApp) {
gl.EnvironmentsListApp.$destroy(true);
}
2016-10-26 11:00:16 -04:00
const filterState = state => environment => environment.state === state && environment;
2016-10-26 11:00:16 -04:00
// recursiveMap :: (Function, Array) -> Array
2016-11-07 15:52:04 -05:00
const recursiveMap = (fn, arr) => arr.map((item) => {
if (item.children) {
2016-10-25 16:00:56 -04:00
const filteredChildren = recursiveMap(fn, item.children).filter(Boolean);
if (filteredChildren.length) {
item.children = filteredChildren;
return item;
}
2016-11-07 15:52:04 -05:00
}
return fn(item);
}).filter(Boolean);
2016-10-26 11:00:16 -04:00
2016-10-14 08:14:18 -04:00
gl.EnvironmentsListApp = new Vue({
2016-10-17 13:03:19 -04:00
el: '#environments-list-view',
2016-10-24 11:56:13 -04:00
2016-10-14 08:14:18 -04:00
components: {
2016-11-07 15:52:04 -05:00
item: gl.environmentsList.EnvironmentItem,
2016-10-14 08:14:18 -04:00
},
data: {
2016-10-14 13:38:13 -04:00
state: Store.state,
2016-11-09 10:22:11 -05:00
endpoint: environmentsListApp.dataset.environmentsDataEndpoint,
canCreateDeployment: environmentsListApp.dataset.canCreateDeployment,
canReadEnvironment: environmentsListApp.dataset.canReadEnvironment,
2016-10-24 11:10:58 -04:00
loading: true,
visibility: 'available',
2016-10-24 11:10:58 -04:00
},
2016-10-26 11:00:16 -04:00
2016-10-24 11:10:58 -04:00
computed: {
2016-11-07 15:52:04 -05:00
filteredEnvironments() {
2016-10-25 16:00:56 -04:00
return recursiveMap(filterState(this.visibility), this.state.environments);
},
2016-10-14 08:14:18 -04:00
},
2016-10-26 11:00:16 -04:00
2016-10-14 08:14:18 -04:00
init: Store.create.bind(Store),
2016-10-26 11:00:16 -04:00
2016-10-14 08:14:18 -04:00
created() {
gl.environmentsService = new EnvironmentsService(this.endpoint);
2016-10-26 11:00:16 -04:00
2016-10-24 11:10:58 -04:00
const scope = this.$options.getQueryParameter('scope');
if (scope) {
this.visibility = scope;
}
2016-10-14 08:14:18 -04:00
},
/**
* Fetches all the environmnets and stores them.
2016-10-26 11:00:16 -04:00
* Toggles loading property.
2016-10-14 08:14:18 -04:00
*/
ready() {
gl.environmentsService.all().then(resp => resp.json()).then((json) => {
Store.storeEnvironments(json);
2016-10-14 08:14:18 -04:00
this.loading = false;
});
2016-10-24 11:10:58 -04:00
},
2016-10-26 11:00:16 -04:00
2016-10-24 11:10:58 -04:00
/**
* Transforms the url parameter into an object and
* returns the one requested.
2016-10-26 11:00:16 -04:00
*
2016-10-24 11:10:58 -04:00
* @param {String} param
* @returns {String} The value of the requested parameter.
*/
2016-11-07 15:52:04 -05:00
getQueryParameter(parameter) {
2016-10-24 11:10:58 -04:00
return window.location.search.substring(1).split('&').reduce((acc, param) => {
2016-10-25 05:08:01 -04:00
const paramSplited = param.split('=');
acc[paramSplited[0]] = paramSplited[1];
2016-10-24 11:10:58 -04:00
return acc;
2016-11-07 15:52:04 -05:00
}, {})[parameter];
},
2016-10-14 08:14:18 -04:00
});
2016-10-20 07:09:35 -04:00
});