summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/environments_bundle.js.es6
blob: 286d81bab7f7255667a2490048966869a082075f (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
//= require vue
//= require vue-resource
//= require_tree ./stores
//= require_tree ./services
//= require ./components/environment_item
//= require ../boards/vue_resource_interceptor

$(() => {

  const environmentsListApp = document.getElementById('environments-list-view');
  const Store = gl.environmentsList.EnvironmentsStore;

  window.gl = window.gl || {};

  if (gl.EnvironmentsListApp) {
    gl.EnvironmentsListApp.$destroy(true);
  }

  const filterState = (state) => (environment) => environment.state === state && environment;

  // recursiveMap :: (Function, Array) -> Array
  const recursiveMap = (fn, arr) => {
    return arr.map((item) => {
      if (!item.children) { return fn(item); }

      const filteredChildren = recursiveMap(fn, item.children).filter(Boolean);
      if (filteredChildren.length) {
        item.children = filteredChildren;
        return item;
      }

    }).filter(Boolean);
  };

  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 recursiveMap(filterState(this.visibility), this.state.environments);
      }
    },

    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) => {
        const paramSplited = param.split('=');
        acc[paramSplited[0]] = paramSplited[1];
        return acc;
      }, {})[param];
    }
  });
});