search.js 3.5 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, one-var, one-var-declaration-per-line, object-shorthand, prefer-arrow-callback, comma-dangle, prefer-template, quotes, no-else-return, max-len */
2
/* global Flash */
3
import Api from './api';
4

Fatih Acet's avatar
Fatih Acet committed
5 6 7 8 9 10
(function() {
  this.Search = (function() {
    function Search() {
      var $groupDropdown, $projectDropdown;
      $groupDropdown = $('.js-search-group-dropdown');
      $projectDropdown = $('.js-search-project-dropdown');
11
      this.groupId = $groupDropdown.data('group-id');
Fatih Acet's avatar
Fatih Acet committed
12 13 14 15 16
      this.eventListeners();
      $groupDropdown.glDropdown({
        selectable: true,
        filterable: true,
        fieldName: 'group_id',
17
        search: {
18
          fields: ['full_name']
19
        },
Fatih Acet's avatar
Fatih Acet committed
20
        data: function(term, callback) {
21
          return Api.groups(term, {}, function(data) {
Fatih Acet's avatar
Fatih Acet committed
22
            data.unshift({
23
              full_name: 'Any'
Fatih Acet's avatar
Fatih Acet committed
24 25 26 27 28 29 30 31 32
            });
            data.splice(1, 0, 'divider');
            return callback(data);
          });
        },
        id: function(obj) {
          return obj.id;
        },
        text: function(obj) {
33
          return obj.full_name;
Fatih Acet's avatar
Fatih Acet committed
34 35
        },
        toggleLabel: function(obj) {
36
          return ($groupDropdown.data('default-label')) + " " + obj.full_name;
Fatih Acet's avatar
Fatih Acet committed
37 38 39 40 41 42 43 44 45 46 47
        },
        clicked: (function(_this) {
          return function() {
            return _this.submitSearch();
          };
        })(this)
      });
      $projectDropdown.glDropdown({
        selectable: true,
        filterable: true,
        fieldName: 'project_id',
48 49 50
        search: {
          fields: ['name']
        },
51 52 53 54 55 56 57 58 59 60 61 62
        data: (term, callback) => {
          this.getProjectsData(term)
            .then((data) => {
              data.unshift({
                name_with_namespace: 'Any'
              });
              data.splice(1, 0, 'divider');

              return data;
            })
            .then(data => callback(data))
            .catch(() => new Flash('Error fetching projects'));
Fatih Acet's avatar
Fatih Acet committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        },
        id: function(obj) {
          return obj.id;
        },
        text: function(obj) {
          return obj.name_with_namespace;
        },
        toggleLabel: function(obj) {
          return ($projectDropdown.data('default-label')) + " " + obj.name_with_namespace;
        },
        clicked: (function(_this) {
          return function() {
            return _this.submitSearch();
          };
        })(this)
      });
    }

    Search.prototype.eventListeners = function() {
      $(document).off('keyup', '.js-search-input').on('keyup', '.js-search-input', this.searchKeyUp);
      return $(document).off('click', '.js-search-clear').on('click', '.js-search-clear', this.clearSearchField);
    };

    Search.prototype.submitSearch = function() {
      return $('.js-search-form').submit();
    };

    Search.prototype.searchKeyUp = function() {
      var $input;
      $input = $(this);
      if ($input.val() === '') {
        return $('.js-search-clear').addClass('hidden');
      } else {
        return $('.js-search-clear').removeClass('hidden');
      }
    };

    Search.prototype.clearSearchField = function() {
      return $('.js-search-input').val('').trigger('keyup').focus();
    };

104 105 106 107 108 109 110 111 112 113 114 115
    Search.prototype.getProjectsData = function(term) {
      return new Promise((resolve) => {
        if (this.groupId) {
          Api.groupProjects(this.groupId, term, resolve);
        } else {
          Api.projects(term, {
            order_by: 'id',
          }, resolve);
        }
      });
    };

Fatih Acet's avatar
Fatih Acet committed
116 117
    return Search;
  })();
118
}).call(window);