summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/milestone.js
blob: 42152362e60ee63e58b6d81c3aa52947ce3d3fa3 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-use-before-define, camelcase, quotes, object-shorthand, no-shadow, no-unused-vars, comma-dangle, no-var, prefer-template, no-underscore-dangle, consistent-return, one-var, one-var-declaration-per-line, default-case, prefer-arrow-callback, padded-blocks, max-len */
/* global Flash */

(function() {
  this.Milestone = (function() {
    Milestone.updateIssue = function(li, issue_url, data) {
      return $.ajax({
        type: "PUT",
        url: issue_url,
        data: data,
        success: (function(_this) {
          return function(_data) {
            return _this.successCallback(_data, li);
          };
        })(this),
        error: function(data) {
          return new Flash("Issue update failed", 'alert');
        },
        dataType: "json"
      });
    };

    Milestone.sortIssues = function(data) {
      var sort_issues_url;
      sort_issues_url = location.href + "/sort_issues";
      return $.ajax({
        type: "PUT",
        url: sort_issues_url,
        data: data,
        success: (function(_this) {
          return function(_data) {
            return _this.successCallback(_data);
          };
        })(this),
        error: function() {
          return new Flash("Issues update failed", 'alert');
        },
        dataType: "json"
      });
    };

    Milestone.sortMergeRequests = function(data) {
      var sort_mr_url;
      sort_mr_url = location.href + "/sort_merge_requests";
      return $.ajax({
        type: "PUT",
        url: sort_mr_url,
        data: data,
        success: (function(_this) {
          return function(_data) {
            return _this.successCallback(_data);
          };
        })(this),
        error: function(data) {
          return new Flash("Issue update failed", 'alert');
        },
        dataType: "json"
      });
    };

    Milestone.updateMergeRequest = function(li, merge_request_url, data) {
      return $.ajax({
        type: "PUT",
        url: merge_request_url,
        data: data,
        success: (function(_this) {
          return function(_data) {
            return _this.successCallback(_data, li);
          };
        })(this),
        error: function(data) {
          return new Flash("Issue update failed", 'alert');
        },
        dataType: "json"
      });
    };

    Milestone.successCallback = function(data, element) {
      var img_tag;
      if (data.assignee) {
        img_tag = $('<img/>');
        img_tag.attr('src', data.assignee.avatar_url);
        img_tag.addClass('avatar s16');
        $(element).find('.assignee-icon').html(img_tag);
      } else {
        $(element).find('.assignee-icon').html('');
      }
      return $(element).effect('highlight');
    };

    function Milestone() {
      var oldMouseStart;
      oldMouseStart = $.ui.sortable.prototype._mouseStart;
      $.ui.sortable.prototype._mouseStart = function(event, overrideHandle, noActivation) {
        this._trigger("beforeStart", event, this._uiHash());
        return oldMouseStart.apply(this, [event, overrideHandle, noActivation]);
      };
      this.bindIssuesSorting();
      this.bindMergeRequestSorting();
      this.bindTabsSwitching();
    }

    Milestone.prototype.bindIssuesSorting = function() {
      return $("#issues-list-unassigned, #issues-list-ongoing, #issues-list-closed").sortable({
        connectWith: ".issues-sortable-list",
        dropOnEmpty: true,
        items: "li:not(.ui-sort-disabled)",
        beforeStart: function(event, ui) {
          return $(".issues-sortable-list").css("min-height", ui.item.outerHeight());
        },
        stop: function(event, ui) {
          return $(".issues-sortable-list").css("min-height", "0px");
        },
        update: function(event, ui) {
          var data;
          // Prevents sorting from container which element has been removed.
          if ($(this).find(ui.item).length > 0) {
            data = $(this).sortable("serialize");
            return Milestone.sortIssues(data);
          }
        },
        receive: function(event, ui) {
          var data, issue_id, issue_url, new_state;
          new_state = $(this).data('state');
          issue_id = ui.item.data('iid');
          issue_url = ui.item.data('url');
          data = (function() {
            switch (new_state) {
              case 'ongoing':
                return "issue[assignee_id]=" + gon.current_user_id;
              case 'unassigned':
                return "issue[assignee_id]=";
              case 'closed':
                return "issue[state_event]=close";
            }
          })();
          if ($(ui.sender).data('state') === "closed") {
            data += "&issue[state_event]=reopen";
          }
          return Milestone.updateIssue(ui.item, issue_url, data);
        }
      }).disableSelection();
    };

    Milestone.prototype.bindTabsSwitching = function() {
      return $('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
        var currentTabClass, previousTabClass;
        currentTabClass = $(e.target).data('show');
        previousTabClass = $(e.relatedTarget).data('show');
        $(previousTabClass).hide();
        $(currentTabClass).removeClass('hidden');
        return $(currentTabClass).show();
      });
    };

    Milestone.prototype.bindMergeRequestSorting = function() {
      return $("#merge_requests-list-unassigned, #merge_requests-list-ongoing, #merge_requests-list-closed").sortable({
        connectWith: ".merge_requests-sortable-list",
        dropOnEmpty: true,
        items: "li:not(.ui-sort-disabled)",
        beforeStart: function(event, ui) {
          return $(".merge_requests-sortable-list").css("min-height", ui.item.outerHeight());
        },
        stop: function(event, ui) {
          return $(".merge_requests-sortable-list").css("min-height", "0px");
        },
        update: function(event, ui) {
          var data;
          data = $(this).sortable("serialize");
          return Milestone.sortMergeRequests(data);
        },
        receive: function(event, ui) {
          var data, merge_request_id, merge_request_url, new_state;
          new_state = $(this).data('state');
          merge_request_id = ui.item.data('iid');
          merge_request_url = ui.item.data('url');
          data = (function() {
            switch (new_state) {
              case 'ongoing':
                return "merge_request[assignee_id]=" + gon.current_user_id;
              case 'unassigned':
                return "merge_request[assignee_id]=";
              case 'closed':
                return "merge_request[state_event]=close";
            }
          })();
          if ($(ui.sender).data('state') === "closed") {
            data += "&merge_request[state_event]=reopen";
          }
          return Milestone.updateMergeRequest(ui.item, merge_request_url, data);
        }
      }).disableSelection();
    };

    return Milestone;

  })();

}).call(this);