summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/components/new_list_dropdown.js
blob: c19c989680dd4bede526fe7a72159c8f7393356a (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
/* eslint-disable func-names, no-new, space-before-function-paren, one-var,
   promise/catch-or-return */
import _ from 'underscore';
import CreateLabelDropdown from '../../create_label';

window.gl = window.gl || {};
window.gl.issueBoards = window.gl.issueBoards || {};

const Store = gl.issueBoards.BoardsStore;

$(document).off('created.label').on('created.label', (e, label) => {
  Store.new({
    title: label.title,
    position: Store.state.lists.length - 2,
    list_type: 'label',
    label: {
      id: label.id,
      title: label.title,
      color: label.color,
    },
  });
});

gl.issueBoards.newListDropdownInit = () => {
  $('.js-new-board-list').each(function () {
    const $this = $(this);
    new CreateLabelDropdown($this.closest('.dropdown').find('.dropdown-new-label'), $this.data('namespace-path'), $this.data('project-path'));

    $this.glDropdown({
      data(term, callback) {
        $.get($this.attr('data-list-labels-path'))
          .then((resp) => {
            callback(resp);
          });
      },
      renderRow (label) {
        const active = Store.findList('title', label.title);
        const $li = $('<li />');
        const $a = $('<a />', {
          class: (active ? `is-active js-board-list-${active.id}` : ''),
          text: label.title,
          href: '#',
        });
        const $labelColor = $('<span />', {
          class: 'dropdown-label-box',
          style: `background-color: ${label.color}`,
        });

        return $li.append($a.prepend($labelColor));
      },
      search: {
        fields: ['title'],
      },
      filterable: true,
      selectable: true,
      multiSelect: true,
      clicked (options) {
        const { e } = options;
        const label = options.selectedObj;
        e.preventDefault();

        if (!Store.findList('title', label.title)) {
          Store.new({
            title: label.title,
            position: Store.state.lists.length - 2,
            list_type: 'label',
            label: {
              id: label.id,
              title: label.title,
              color: label.color,
            },
          });

          Store.state.lists = _.sortBy(Store.state.lists, 'position');
        }
      },
    });
  });
};