BigW Consortium Gitlab

milestone_select.js 7.61 KB
Newer Older
1
/* eslint-disable */
Fatih Acet committed
2 3 4 5 6 7 8 9 10
(function() {
  this.MilestoneSelect = (function() {
    function MilestoneSelect(currentProject) {
      var _this;
      if (currentProject != null) {
        _this = this;
        this.currentProject = JSON.parse(currentProject);
      }
      $('.js-milestone-select').each(function(i, dropdown) {
11
        var $block, $dropdown, $loading, $selectbox, $sidebarCollapsedValue, $value, abilityName, collapsedSidebarLabelTemplate, defaultLabel, issuableId, issueUpdateURL, milestoneLinkNoneTemplate, milestoneLinkTemplate, milestonesUrl, projectId, selectedMilestone, showAny, showNo, showUpcoming, useId, showMenuAbove;
Fatih Acet committed
12 13 14 15 16 17 18
        $dropdown = $(dropdown);
        projectId = $dropdown.data('project-id');
        milestonesUrl = $dropdown.data('milestones');
        issueUpdateURL = $dropdown.data('issueUpdate');
        selectedMilestone = $dropdown.data('selected');
        showNo = $dropdown.data('show-no');
        showAny = $dropdown.data('show-any');
19
        showMenuAbove = $dropdown.data('showMenuAbove');
Fatih Acet committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        showUpcoming = $dropdown.data('show-upcoming');
        useId = $dropdown.data('use-id');
        defaultLabel = $dropdown.data('default-label');
        issuableId = $dropdown.data('issuable-id');
        abilityName = $dropdown.data('ability-name');
        $selectbox = $dropdown.closest('.selectbox');
        $block = $selectbox.closest('.block');
        $sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon');
        $value = $block.find('.value');
        $loading = $block.find('.block-loading').fadeOut();
        if (issueUpdateURL) {
          milestoneLinkTemplate = _.template('<a href="/<%- namespace %>/<%- path %>/milestones/<%- iid %>" class="bold has-tooltip" data-container="body" title="<%- remaining %>"><%- title %></a>');
          milestoneLinkNoneTemplate = '<span class="no-value">None</span>';
          collapsedSidebarLabelTemplate = _.template('<span class="has-tooltip" data-container="body" title="<%- remaining %>" data-placement="left"> <%- title %> </span>');
        }
        return $dropdown.glDropdown({
36
          showMenuAbove: showMenuAbove,
Fatih Acet committed
37 38 39 40
          data: function(term, callback) {
            return $.ajax({
              url: milestonesUrl
            }).done(function(data) {
41
              var extraOptions = [];
Fatih Acet committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
              if (showAny) {
                extraOptions.push({
                  id: 0,
                  name: '',
                  title: 'Any Milestone'
                });
              }
              if (showNo) {
                extraOptions.push({
                  id: -1,
                  name: 'No Milestone',
                  title: 'No Milestone'
                });
              }
              if (showUpcoming) {
                extraOptions.push({
                  id: -2,
                  name: '#upcoming',
                  title: 'Upcoming'
                });
              }
63
              if (extraOptions.length) {
Fatih Acet committed
64 65
                extraOptions.push('divider');
              }
66 67 68 69 70

              callback(extraOptions.concat(data));
              if (showMenuAbove) {
                $dropdown.data('glDropdown').positionMenuAbove();
              }
Fatih Acet committed
71 72 73 74 75 76 77
            });
          },
          filterable: true,
          search: {
            fields: ['title']
          },
          selectable: true,
78 79
          toggleLabel: function(selected, el, e) {
            if (selected && 'id' in selected && $(el).hasClass('is-active')) {
Fatih Acet committed
80 81 82 83 84
              return selected.title;
            } else {
              return defaultLabel;
            }
          },
85
          defaultLabel: defaultLabel,
Fatih Acet committed
86 87 88 89 90
          fieldName: $dropdown.data('field-name'),
          text: function(milestone) {
            return _.escape(milestone.title);
          },
          id: function(milestone) {
91
            if (!useId && !$dropdown.is('.js-issuable-form-dropdown')) {
Fatih Acet committed
92 93 94 95 96 97 98 99 100 101
              return milestone.name;
            } else {
              return milestone.id;
            }
          },
          isSelected: function(milestone) {
            return milestone.name === selectedMilestone;
          },
          hidden: function() {
            $selectbox.hide();
102
            // display:block overrides the hide-collapse rule
Fatih Acet committed
103 104
            return $value.css('display', '');
          },
105
          vue: $dropdown.hasClass('js-issue-board-sidebar'),
106
          clicked: function(selected, $el, e) {
Fatih Acet committed
107 108 109 110
            var data, isIssueIndex, isMRIndex, page;
            page = $('body').data('page');
            isIssueIndex = page === 'projects:issues:index';
            isMRIndex = (page === page && page === 'projects:merge_requests:index');
111 112
            if ($dropdown.hasClass('js-filter-bulk-update') || $dropdown.hasClass('js-issuable-form-dropdown')) {
              e.preventDefault();
Fatih Acet committed
113 114
              return;
            }
115
            if ($('html').hasClass('issue-boards-page') && !$dropdown.hasClass('js-issue-board-sidebar')) {
116 117
              gl.issueBoards.BoardsStore.state.filters[$dropdown.data('field-name')] = selected.name;
              gl.issueBoards.BoardsStore.updateFiltersUrl();
118
              e.preventDefault();
119
            } else if ($dropdown.hasClass('js-filter-submit') && (isIssueIndex || isMRIndex)) {
Fatih Acet committed
120 121 122 123 124 125 126 127
              if (selected.name != null) {
                selectedMilestone = selected.name;
              } else {
                selectedMilestone = '';
              }
              return Issuable.filterResults($dropdown.closest('form'));
            } else if ($dropdown.hasClass('js-filter-submit')) {
              return $dropdown.closest('form').submit();
128 129 130 131 132 133 134 135 136 137 138 139 140
            } else if ($dropdown.hasClass('js-issue-board-sidebar')) {
              if (selected.id !== -1) {
                Vue.set(gl.issueBoards.BoardsStore.detail.issue, 'milestone', new ListMilestone({
                  id: selected.id,
                  title: selected.name
                }));
              } else {
                Vue.delete(gl.issueBoards.BoardsStore.detail.issue, 'milestone');
              }

              $dropdown.trigger('loading.gl.dropdown');
              $loading.fadeIn();

141
              gl.issueBoards.BoardsStore.detail.issue.update($dropdown.attr('data-issue-update'))
142 143 144 145
                .then(function () {
                  $dropdown.trigger('loaded.gl.dropdown');
                  $loading.fadeOut();
                });
Fatih Acet committed
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
            } else {
              selected = $selectbox.find('input[type="hidden"]').val();
              data = {};
              data[abilityName] = {};
              data[abilityName].milestone_id = selected != null ? selected : null;
              $loading.fadeIn();
              $dropdown.trigger('loading.gl.dropdown');
              return $.ajax({
                type: 'PUT',
                url: issueUpdateURL,
                data: data
              }).done(function(data) {
                $dropdown.trigger('loaded.gl.dropdown');
                $loading.fadeOut();
                $selectbox.hide();
                $value.css('display', '');
                if (data.milestone != null) {
                  data.milestone.namespace = _this.currentProject.namespace;
                  data.milestone.path = _this.currentProject.path;
                  data.milestone.remaining = $.timefor(data.milestone.due_date);
                  $value.html(milestoneLinkTemplate(data.milestone));
                  return $sidebarCollapsedValue.find('span').html(collapsedSidebarLabelTemplate(data.milestone));
                } else {
                  $value.html(milestoneLinkNoneTemplate);
                  return $sidebarCollapsedValue.find('span').text('No');
                }
              });
            }
          }
        });
      });
    }

    return MilestoneSelect;

  })();

}).call(this);