aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/api-documentation/controller.js
blob: 4b75c7138df466b6a60607b5f4f53d8ad700e354 (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
import _ from 'underscore';
import Backbone from 'backbone';
import Marionette from 'backbone.marionette';
import ActionsView from './actions-view';
import HeaderView from './header-view';

export default Marionette.Controller.extend({
  initialize: function (options) {
    this.list = options.app.list;
    this.listenTo(this.list, 'select', this.onItemSelect);
  },

  show: function (path) {
    var that = this;
    this.fetchList().done(function () {
      if (path) {
        var item = that.list.findWhere({ path: path });
        if (item != null) {
          that.showWebService(path);
        } else {
          that.showAction(path);
        }
      }
    });
  },

  showWebService: function (path) {
    var item = this.list.findWhere({ path: path });
    if (item != null) {
      item.trigger('select', item);
    }
  },

  showAction: function (path) {
    var webService = this.list.find(function (item) {
      return path.indexOf(item.get('path')) === 0;
    });
    if (webService != null) {
      var action = path.substr(webService.get('path').length + 1);
      webService.trigger('select', webService, { trigger: false, action: action });
    }
  },

  onItemSelect: function (item, options) {
    var path = item.get('path'),
        opts = _.defaults(options || {}, { trigger: true });
    if (opts.trigger) {
      this.options.app.router.navigate(path);
    }
    this.options.app.listView.highlight(path);

    if (item.get('internal')) {
      this.options.state.set({ internal: true });
    }

    var actions = new Backbone.Collection(item.get('actions')),
        actionsView = new ActionsView({
          collection: actions,
          state: this.options.state
        });
    this.options.app.layout.detailsRegion.show(actionsView);
    this.options.app.layout.headerRegion.show(new HeaderView({ model: item }));

    if (opts.action != null) {
      var model = actions.findWhere({ key: opts.action });
      if (model) {
        if (model.get('internal')) {
          this.options.state.set({ internal: true });
        }
        actionsView.scrollToAction(opts.action);
      }
    } else {
      actionsView.scrollToTop();
    }
  },

  fetchList: function () {
    return this.list.fetch({ data: { 'include_internals': true } });
  }
});