BigW Consortium Gitlab

issuable.js.es6 6.46 KB
Newer Older
1
/* eslint-disable */
Fatih Acet committed
2 3 4 5 6 7 8
(function() {
  var issuable_created;

  issuable_created = false;

  this.Issuable = {
    init: function() {
Phil Hughes committed
9 10 11
      Issuable.initTemplates();
      Issuable.initSearch();
      Issuable.initChecks();
12
      Issuable.initResetFilters();
13
      Issuable.resetIncomingEmailToken();
Phil Hughes committed
14
      return Issuable.initLabelFilterRemove();
Fatih Acet committed
15 16 17 18 19
    },
    initTemplates: function() {
      return Issuable.labelRow = _.template('<% _.each(labels, function(label){ %> <span class="label-row btn-group" role="group" aria-label="<%- label.title %>" style="color: <%- label.text_color %>;"> <a href="#" class="btn btn-transparent has-tooltip" style="background-color: <%- label.color %>;" title="<%- label.description %>" data-container="body"> <%- label.title %> </a> <button type="button" class="btn btn-transparent label-remove js-label-filter-remove" style="background-color: <%- label.color %>;" data-label="<%- label.title %>"> <i class="fa fa-times"></i> </button> </span> <% }); %>');
    },
    initSearch: function() {
20 21 22 23
      const $searchInput = $('#issuable_search');

      Issuable.initSearchState($searchInput);

24
      // `immediate` param set to false debounces on the `trailing` edge, lets user finish typing
25
      const debouncedExecSearch = _.debounce(Issuable.executeSearch, 1000, false);
26

27
      $searchInput.off('keyup').on('keyup', debouncedExecSearch);
28 29

      // ensures existing filters are preserved when manually submitted
30
      $('#issuable_search_form').on('submit', (e) => {
31 32
        e.preventDefault();
        debouncedExecSearch(e);
Fatih Acet committed
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

    },
    initSearchState: function($searchInput) {
      const currentSearchVal = $searchInput.val();

      Issuable.searchState = {
        elem: $searchInput,
        current: currentSearchVal
      };

      Issuable.maybeFocusOnSearch();
    },
    accessSearchPristine: function(set) {
      // store reference to previous value to prevent search on non-mutating keyup
      const state = Issuable.searchState;
      const currentSearchVal = state.elem.val();

      if (set) {
        state.current = currentSearchVal;
      } else {
        return state.current === currentSearchVal;
      }
    },
    maybeFocusOnSearch: function() {
58
      const currentSearchVal = Issuable.searchState.current;
59
      if (currentSearchVal && currentSearchVal !== '') {
60 61 62 63 64 65 66 67 68 69 70 71 72 73
        const queryLength = currentSearchVal.length;
        const $searchInput = Issuable.searchState.elem;

      /* The following ensures that the cursor is initially placed at
        * the end of search input when focus is applied. It accounts
        * for differences in browser implementations of `setSelectionRange`
        * and cursor placement for elements in focus.
      */
        $searchInput.focus();
        if ($searchInput.setSelectionRange) {
          $searchInput.setSelectionRange(queryLength, queryLength);
        } else {
          $searchInput.val(currentSearchVal);
        }
74
      }
Fatih Acet committed
75
    },
76 77 78 79 80 81
    executeSearch: function(e) {
      const $search = $('#issuable_search');
      const $searchName = $search.attr('name');
      const $searchValue = $search.val();
      const $filtersForm = $('.js-filter-form');
      const $input = $(`input[name='${$searchName}']`, $filtersForm);
82 83 84 85 86
      const isPristine = Issuable.accessSearchPristine();

      if (isPristine) {
        return;
      }
87 88 89 90 91 92 93 94 95

      if (!$input.length) {
        $filtersForm.append(`<input type='hidden' name='${$searchName}' value='${_.escape($searchValue)}'/>`);
      } else {
        $input.val($searchValue);
      }

      Issuable.filterResults($filtersForm);
    },
Fatih Acet committed
96 97 98 99
    initLabelFilterRemove: function() {
      return $(document).off('click', '.js-label-filter-remove').on('click', '.js-label-filter-remove', function(e) {
        var $button;
        $button = $(this);
100
        // Remove the label input box
Fatih Acet committed
101 102 103
        $('input[name="label_name[]"]').filter(function() {
          return this.value === $button.data('label');
        }).remove();
104
        // Submit the form to get new data
Fatih Acet committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
        Issuable.filterResults($('.filter-form'));
      });
    },
    filterResults: (function(_this) {
      return function(form) {
        var formAction, formData, issuesUrl;
        formData = form.serialize();
        formAction = form.attr('action');
        issuesUrl = formAction;
        issuesUrl += "" + (formAction.indexOf('?') < 0 ? '?' : '&');
        issuesUrl += formData;
        return Turbolinks.visit(issuesUrl);
      };
    })(this),
119 120 121 122 123 124 125 126 127 128 129
    initResetFilters: function() {
      $('.reset-filters').on('click', function(e) {
        e.preventDefault();
        const target = e.target;
        const $form = $(target).parents('.js-filter-form');
        const baseIssuesUrl = target.href;

        $form.attr('action', baseIssuesUrl);
        Turbolinks.visit(baseIssuesUrl);
      });
    },
Fatih Acet committed
130 131 132 133 134 135 136 137 138
    initChecks: function() {
      this.issuableBulkActions = $('.bulk-update').data('bulkActions');
      $('.check_all_issues').off('click').on('click', function() {
        $('.selected_issue').prop('checked', this.checked);
        return Issuable.checkChanged();
      });
      return $('.selected_issue').off('change').on('change', Issuable.checkChanged.bind(this));
    },
    checkChanged: function() {
139
      const $checkedIssues = $('.selected_issue:checked');
140
      const $updateIssuesIds = $('#update_issuable_ids');
141 142 143 144 145
      const $issuesOtherFilters = $('.issues-other-filters');
      const $issuesBulkUpdate = $('.issues_bulk_update');

      if ($checkedIssues.length > 0) {
        let ids = $.map($checkedIssues, function(value) {
Fatih Acet committed
146 147
          return $(value).data('id');
        });
148 149 150
        $updateIssuesIds.val(ids);
        $issuesOtherFilters.hide();
        $issuesBulkUpdate.show();
Fatih Acet committed
151
      } else {
152 153 154
        $updateIssuesIds.val([]);
        $issuesBulkUpdate.hide();
        $issuesOtherFilters.show();
Fatih Acet committed
155 156 157
        this.issuableBulkActions.willUpdateLabels = false;
      }
      return true;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    },

    resetIncomingEmailToken: function() {
      $('.incoming-email-token-reset').on('click', function(e) {
        e.preventDefault();

        $.ajax({
          type: 'PUT',
          url: $('.incoming-email-token-reset').attr('href'),
          dataType: 'json',
          success: function(response) {
            $('#issue_email').val(response.new_issue_address).focus();
          },
          beforeSend: function() {
            $('.incoming-email-token-reset').text('resetting...');
          },
          complete: function() {
            $('.incoming-email-token-reset').text('reset it');
          }
        });
      });
Fatih Acet committed
179 180 181 182
    }
  };

}).call(this);