BigW Consortium Gitlab

issuable.js.es6 4.23 KB
Newer Older
Fatih Acet committed
1 2 3 4 5 6 7
(function() {
  var issuable_created;

  issuable_created = false;

  this.Issuable = {
    init: function() {
Phil Hughes committed
8 9 10
      Issuable.initTemplates();
      Issuable.initSearch();
      Issuable.initChecks();
11
      Issuable.initResetFilters();
Phil Hughes committed
12
      return Issuable.initLabelFilterRemove();
Fatih Acet committed
13 14 15 16 17
    },
    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() {
18 19 20 21 22 23 24 25 26
      // `immediate` param set to false debounces on the `trailing` edge, lets user finish typing
      const debouncedExecSearch = _.debounce(Issuable.executeSearch, 500, false);

      $('#issuable_search').off('keyup').on('keyup', debouncedExecSearch);

      // ensures existing filters are preserved when manually submitted
      $('#issue_search_form').on('submit', (e) => {
        e.preventDefault();
        debouncedExecSearch(e);
Fatih Acet committed
27 28
      });
    },
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    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);

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

      Issuable.filterResults($filtersForm);
    },
Fatih Acet committed
44 45 46 47
    initLabelFilterRemove: function() {
      return $(document).off('click', '.js-label-filter-remove').on('click', '.js-label-filter-remove', function(e) {
        var $button;
        $button = $(this);
48
        // Remove the label input box
Fatih Acet committed
49 50 51
        $('input[name="label_name[]"]').filter(function() {
          return this.value === $button.data('label');
        }).remove();
52
        // Submit the form to get new data
Fatih Acet committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66
        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),
67 68 69 70 71 72 73 74 75 76 77
    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
78 79 80 81 82 83 84 85 86
    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() {
87
      const $checkedIssues = $('.selected_issue:checked');
88
      const $updateIssuesIds = $('#update_issuable_ids');
89 90 91 92 93
      const $issuesOtherFilters = $('.issues-other-filters');
      const $issuesBulkUpdate = $('.issues_bulk_update');

      if ($checkedIssues.length > 0) {
        let ids = $.map($checkedIssues, function(value) {
Fatih Acet committed
94 95
          return $(value).data('id');
        });
96 97 98
        $updateIssuesIds.val(ids);
        $issuesOtherFilters.hide();
        $issuesBulkUpdate.show();
Fatih Acet committed
99
      } else {
100 101 102
        $updateIssuesIds.val([]);
        $issuesBulkUpdate.hide();
        $issuesOtherFilters.show();
Fatih Acet committed
103 104 105 106 107 108 109
        this.issuableBulkActions.willUpdateLabels = false;
      }
      return true;
    }
  };

}).call(this);