aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/issues/workspace-header-view.js
blob: cc1f43e5ca00df6ff825d21b00d8b9c9a8d70b36 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import $ from 'jquery';
import _ from 'underscore';
import WorkspaceHeaderView from '../../components/navigator/workspace-header-view';
import Template from './templates/issues-workspace-header.hbs';

export default WorkspaceHeaderView.extend({
  template: Template,

  events: function () {
    return _.extend(WorkspaceHeaderView.prototype.events.apply(this, arguments), {
      'click .js-selection': 'onSelectionClick',
      'click .js-back': 'returnToList',
      'click .js-new-search': 'newSearch',
      'click .js-bulk-change-selected': 'onBulkChangeSelectedClick'
    });
  },

  initialize: function () {
    WorkspaceHeaderView.prototype.initialize.apply(this, arguments);
    this._onBulkIssues = window.onBulkIssues;
    window.onBulkIssues = _.bind(this.afterBulkChange, this);
  },

  onDestroy: function () {
    WorkspaceHeaderView.prototype.onDestroy.apply(this, arguments);
    window.onBulkIssues = this._onBulkIssues;
  },

  onSelectionClick: function (e) {
    e.preventDefault();
    this.toggleSelection();
  },

  onBulkChangeSelectedClick: function (e) {
    e.preventDefault();
    this.bulkChangeSelected();
  },

  afterBulkChange: function () {
    var that = this;
    $('#modal').dialog('close');
    var selectedIndex = this.options.app.state.get('selectedIndex');
    var selectedKeys = _.pluck(this.options.app.list.where({ selected: true }), 'id');
    this.options.app.controller.fetchList().done(function () {
      that.options.app.state.set({ selectedIndex: selectedIndex });
      that.options.app.list.selectByKeys(selectedKeys);
    });
  },

  render: function () {
    if (!this._suppressUpdate) {
      WorkspaceHeaderView.prototype.render.apply(this, arguments);
    }
  },

  toggleSelection: function () {
    this._suppressUpdate = true;
    var selectedCount = this.options.app.list.where({ selected: true }).length,
        someSelected = selectedCount > 0;
    return someSelected ? this.selectNone() : this.selectAll();
  },

  selectNone: function () {
    this.options.app.list.where({ selected: true }).forEach(function (issue) {
      issue.set({ selected: false });
    });
    this._suppressUpdate = false;
    this.render();
  },

  selectAll: function () {
    this.options.app.list.forEach(function (issue) {
      issue.set({ selected: true });
    });
    this._suppressUpdate = false;
    this.render();
  },

  returnToList: function () {
    this.options.app.controller.closeComponentViewer();
  },

  newSearch: function () {
    this.options.app.controller.newSearch();
  },

  bulkChange: function () {
    var query = this.options.app.controller.getQuery('&', true),
        url = baseUrl + '/issues/bulk_change_form?' + query;
    window.openModalWindow(url, {});
  },

  bulkChangeSelected: function () {
    var selected = this.options.app.list.where({ selected: true }),
        selectedKeys = _.first(_.pluck(selected, 'id'), 200),
        query = 'issues=' + selectedKeys.join(),
        url = baseUrl + '/issues/bulk_change_form?' + query;
    window.openModalWindow(url, {});
  },

  serializeData: function () {
    var issuesCount = this.options.app.list.length,
        selectedCount = this.options.app.list.where({ selected: true }).length,
        allSelected = issuesCount > 0 && issuesCount === selectedCount,
        someSelected = !allSelected && selectedCount > 0;
    return _.extend(WorkspaceHeaderView.prototype.serializeData.apply(this, arguments), {
      selectedCount: selectedCount,
      allSelected: allSelected,
      someSelected: someSelected
    });
  }
});