BigW Consortium Gitlab

milestone.js 5.52 KB
Newer Older
1
/* 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, max-len */
2
/* global Flash */
3
/* global Sortable */
4

Fatih Acet committed
5 6 7 8 9 10 11
(function() {
  this.Milestone = (function() {
    Milestone.updateIssue = function(li, issue_url, data) {
      return $.ajax({
        type: "PUT",
        url: issue_url,
        data: data,
12 13 14
        success: function(_data) {
          return Milestone.successCallback(_data, li);
        },
Fatih Acet committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
        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,
29 30 31
        success: function(_data) {
          return Milestone.successCallback(_data);
        },
Fatih Acet committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
        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,
46 47 48
        success: function(_data) {
          return Milestone.successCallback(_data);
        },
Fatih Acet committed
49 50 51 52 53 54 55 56 57 58 59 60
        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,
61 62 63
        success: function(_data) {
          return Milestone.successCallback(_data, li);
        },
Fatih Acet committed
64 65 66 67 68 69 70 71 72 73 74 75 76
        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');
77
        $(element).find('.assignee-icon img').replaceWith(img_tag);
Fatih Acet committed
78
      } else {
79
        $(element).find('.assignee-icon').empty();
Fatih Acet committed
80 81 82 83 84 85 86 87 88 89 90
      }
    };

    function Milestone() {
      var oldMouseStart;
      this.bindIssuesSorting();
      this.bindMergeRequestSorting();
      this.bindTabsSwitching();
    }

    Milestone.prototype.bindIssuesSorting = function() {
91 92 93 94 95 96 97 98 99
      $('#issues-list-unassigned, #issues-list-ongoing, #issues-list-closed').each(function (i, el) {
        this.createSortable(el, {
          group: 'issue-list',
          listEls: $('.issues-sortable-list'),
          fieldName: 'issue',
          sortCallback: Milestone.sortIssues,
          updateCallback: Milestone.updateIssue,
        });
      }.bind(this));
Fatih Acet committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    };

    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() {
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
      $("#merge_requests-list-unassigned, #merge_requests-list-ongoing, #merge_requests-list-closed").each(function (i, el) {
        this.createSortable(el, {
          group: 'merge-request-list',
          listEls: $(".merge_requests-sortable-list:not(#merge_requests-list-merged)"),
          fieldName: 'merge_request',
          sortCallback: Milestone.sortMergeRequests,
          updateCallback: Milestone.updateMergeRequest,
        });
      }.bind(this));
    };

    Milestone.prototype.createSortable = function(el, opts) {
      return Sortable.create(el, {
        group: opts.group,
        filter: '.is-disabled',
        forceFallback: true,
        onStart: function(e) {
          opts.listEls.css('min-height', e.item.offsetHeight);
Fatih Acet committed
132
        },
133 134
        onEnd: function () {
          opts.listEls.css("min-height", "0px");
Fatih Acet committed
135
        },
136 137 138 139 140 141 142 143 144 145 146
        onUpdate: function(e) {
          var ids = this.toArray(),
            data;

          if (ids.length) {
            data = ids.map(function(id) {
              return 'sortable_' + opts.fieldName + '[]=' + id;
            }).join('&');

            opts.sortCallback(data);
          }
Fatih Acet committed
147
        },
148 149 150 151
        onAdd: function (e) {
          var data, issuableId, issuableUrl, newState;
          newState = e.to.dataset.state;
          issuableUrl = e.item.dataset.url;
Fatih Acet committed
152
          data = (function() {
153
            switch (newState) {
Fatih Acet committed
154
              case 'ongoing':
155
                return opts.fieldName + '[assignee_id]=' + gon.current_user_id;
Fatih Acet committed
156
              case 'unassigned':
157
                return opts.fieldName + '[assignee_id]=';
Fatih Acet committed
158
              case 'closed':
159
                return opts.fieldName + '[state_event]=close';
Fatih Acet committed
160 161
            }
          })();
162 163
          if (e.from.dataset.state === 'closed') {
            data += '&' + opts.fieldName + '[state_event]=reopen';
Fatih Acet committed
164
          }
165 166 167

          opts.updateCallback(e.item, issuableUrl, data);
          this.options.onUpdate.call(this, e);
Fatih Acet committed
168
        }
169
      });
Fatih Acet committed
170 171 172 173
    };

    return Milestone;
  })();
174
}).call(window);