debian-mirror-gitlab/app/assets/javascripts/search.js

119 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* 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 */
2017-09-10 17:25:29 +05:30
/* global Flash */
import Api from './api';
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
(function() {
this.Search = (function() {
function Search() {
var $groupDropdown, $projectDropdown;
$groupDropdown = $('.js-search-group-dropdown');
$projectDropdown = $('.js-search-project-dropdown');
2017-09-10 17:25:29 +05:30
this.groupId = $groupDropdown.data('group-id');
2016-09-13 17:45:13 +05:30
this.eventListeners();
$groupDropdown.glDropdown({
selectable: true,
filterable: true,
fieldName: 'group_id',
2017-08-17 22:00:37 +05:30
search: {
fields: ['full_name']
},
2016-09-13 17:45:13 +05:30
data: function(term, callback) {
2017-08-17 22:00:37 +05:30
return Api.groups(term, {}, function(data) {
2016-09-13 17:45:13 +05:30
data.unshift({
2017-08-17 22:00:37 +05:30
full_name: 'Any'
2016-09-13 17:45:13 +05:30
});
data.splice(1, 0, 'divider');
return callback(data);
});
},
id: function(obj) {
return obj.id;
},
text: function(obj) {
2017-08-17 22:00:37 +05:30
return obj.full_name;
2016-09-13 17:45:13 +05:30
},
toggleLabel: function(obj) {
2017-08-17 22:00:37 +05:30
return ($groupDropdown.data('default-label')) + " " + obj.full_name;
2016-09-13 17:45:13 +05:30
},
clicked: (function(_this) {
return function() {
return _this.submitSearch();
};
})(this)
});
$projectDropdown.glDropdown({
selectable: true,
filterable: true,
fieldName: 'project_id',
2017-08-17 22:00:37 +05:30
search: {
fields: ['name']
},
2017-09-10 17:25:29 +05:30
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'));
2016-09-13 17:45:13 +05:30
},
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();
};
2017-09-10 17:25:29 +05:30
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);
}
});
};
2016-09-13 17:45:13 +05:30
return Search;
})();
2017-08-17 22:00:37 +05:30
}).call(window);