summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/environments_bundle.js.es6
blob: f8cefabefa4eb79f209d275e35c5a248c0145a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//= require vue
//= require vue-resource
//= require_tree ./stores
//= require_tree ./services
//= require ./components/environment_item

$(() => {
  
  const environmentsListApp = document.getElementById('environments-list-view');
  const Store = gl.environmentsList.EnvironmentsStore;
  
  window.gl = window.gl || {};
  
  if (gl.EnvironmentsListApp) {
    gl.EnvironmentsListApp.$destroy(true);
  }
  
  const filterEnvironments = (environments = [], filter = "") => {
    return environments.filter((env) => {
      if (env.children) {
        return env.children.filter((child) =>  child.state === filter).length;
      } else {
        return env.state === filter;
      };
    });
  };
  
  gl.EnvironmentsListApp = new Vue({

    el: '#environments-list-view',

    components: {
      'item': gl.environmentsList.EnvironmentItem
    },

    data: {
      state: Store.state,
      endpoint: environmentsListApp.dataset.endpoint,
      loading: true,
      visibility: 'available'
    },
    
    computed: {
      filteredEnvironments () {
        return filterEnvironments(this.state.environments, this.visibility);
      },

      countStopped () {
        return filterEnvironments(this.state.environments, 'stopped').length;
      },

      counAvailable () {
        return filterEnvironments(this.state.environments, 'available').length;
      }
    },
    
    init: Store.create.bind(Store),
    
    created() {
      gl.environmentsService = new EnvironmentsService(this.endpoint);
      
      const scope = this.$options.getQueryParameter('scope');
      if (scope) {
        this.visibility = scope;
      }
    },

    /**
     * Fetches all the environmnets and stores them.
     * Toggles loading property. 
     */
    ready() {
      gl.environmentsService.all().then((resp) => {
        Store.storeEnvironments(resp.json());

        this.loading = false;
      });
    },
    
    /**
     * Transforms the url parameter into an object and
     * returns the one requested.
     * 
     * @param  {String} param
     * @returns {String}       The value of the requested parameter.
     */
    getQueryParameter(param) {
      return window.location.search.substring(1).split('&').reduce((acc, param) => {
        acc[param.split('=')[0]] = param.split('=')[1];
        return acc;
      }, {})[param];
    }

  });
});