diff options
author | Filipa Lacerda <filipa@gitlab.com> | 2017-03-02 11:05:48 +0000 |
---|---|---|
committer | Filipa Lacerda <filipa@gitlab.com> | 2017-03-02 11:05:48 +0000 |
commit | 0a10d85927a62bfbe953656d55d50b3a3e7ffa01 (patch) | |
tree | d61e571217c72a3d6c19af93e03709c13d39a799 | |
parent | b7c30cae4eedab5e8e41d9764ac08ca12361d054 (diff) | |
download | gitlab-ce-0a10d85927a62bfbe953656d55d50b3a3e7ffa01.tar.gz |
Update JS to use new standards
-rw-r--r-- | app/assets/javascripts/application.js | 1 | ||||
-rw-r--r-- | app/assets/javascripts/dispatcher.js.es6 | 6 | ||||
-rw-r--r-- | app/assets/javascripts/groups_list.js | 97 | ||||
-rw-r--r-- | app/views/dashboard/_groups_head.html.haml | 2 | ||||
-rw-r--r-- | app/views/dashboard/groups/_groups.html.haml | 2 | ||||
-rw-r--r-- | app/views/dashboard/groups/index.html.haml | 3 | ||||
-rw-r--r-- | app/views/explore/groups/_groups.html.haml | 2 | ||||
-rw-r--r-- | app/views/explore/groups/index.html.haml | 3 | ||||
-rw-r--r-- | config/webpack.config.js | 1 | ||||
-rw-r--r-- | spec/features/dashboard/groups_list_spec.rb | 13 | ||||
-rw-r--r-- | spec/features/explore/groups_list_spec.rb | 13 |
11 files changed, 83 insertions, 60 deletions
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 44e02a01a3a..c51860d1604 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -177,7 +177,6 @@ require('./project_select'); require('./project_show'); require('./project_variables'); require('./projects_list'); -require('./groups_list'); require('./render_gfm'); require('./render_math'); require('./right_sidebar'); diff --git a/app/assets/javascripts/dispatcher.js.es6 b/app/assets/javascripts/dispatcher.js.es6 index 0f678492d4c..fc25122aedc 100644 --- a/app/assets/javascripts/dispatcher.js.es6 +++ b/app/assets/javascripts/dispatcher.js.es6 @@ -35,6 +35,8 @@ /* global Labels */ /* global Shortcuts */ +import GroupsList from './groups_list'; + const ShortcutsBlob = require('./shortcuts_blob'); const UserCallout = require('./user_callout'); @@ -96,6 +98,10 @@ const UserCallout = require('./user_callout'); case 'dashboard:todos:index': new gl.Todos(); break; + case 'dashboard:groups:index': + case 'explore:groups:index': + new GroupsList(); + break; case 'projects:milestones:new': case 'projects:milestones:edit': case 'projects:milestones:update': diff --git a/app/assets/javascripts/groups_list.js b/app/assets/javascripts/groups_list.js index 5358121b063..0ef81e49444 100644 --- a/app/assets/javascripts/groups_list.js +++ b/app/assets/javascripts/groups_list.js @@ -1,50 +1,47 @@ -/* eslint-disable func-names, space-before-function-paren, object-shorthand, quotes, no-var, one-var, one-var-declaration-per-line, prefer-arrow-callback, consistent-return, no-unused-vars, camelcase, prefer-template, comma-dangle, max-len */ - -(function() { - window.GroupsList = { - init: function() { - $(".groups-list-filter").off('keyup'); - this.initSearch(); - return this.initPagination(); - }, - initSearch: function() { - var debounceFilter, groupsListFilter; - groupsListFilter = $('.groups-list-filter'); - debounceFilter = _.debounce(window.GroupsList.filterResults, 500); - return groupsListFilter.on('keyup', function(e) { - if (groupsListFilter.val() !== '') { - return debounceFilter(); - } - }); - }, - filterResults: function() { - var form, group_filter_url, search; - $('.groups-list-holder').fadeTo(250, 0.5); - form = null; - form = $("form#group-filter-form"); - search = $(".groups-list-filter").val(); - group_filter_url = form.attr('action') + '?' + form.serialize(); - return $.ajax({ - type: "GET", - url: form.attr('action'), - data: form.serialize(), - complete: function() { - return $('.groups-list-holder').fadeTo(250, 1); - }, - success: function(data) { - $('.groups-list-holder').replaceWith(data.html); - return history.replaceState({ - page: group_filter_url - // Change url so if user reload a page - search results are saved - }, document.title, group_filter_url); - }, - dataType: "json" - }); - }, - initPagination: function() { - return $('.groups-list-holder .pagination').on('ajax:success', function(e, data) { - return $('.groups-list-holder').replaceWith(data.html); - }); - } - }; -}).call(window); +/** + * Based on project list search. + * Makes search request for groups when user types a value in the search input. + * Updates the html content of the page with the received one. + */ +export default class GroupsList { + constructor() { + this.groupsListFilterElement = document.querySelector('.js-groups-list-filter'); + this.groupsListHolderElement = document.querySelector('.js-groups-list-holder'); + + this.initSearch(); + } + + initSearch() { + this.debounceFilter = _.debounce(this.filterResults.bind(this), 500); + + this.groupsListFilterElement.removeEventListener('input', this.debounceFilter); + this.groupsListFilterElement.addEventListener('input', this.debounceFilter); + } + + filterResults() { + const form = document.querySelector('form#group-filter-form'); + const groupFilterUrl = `${form.getAttribute('action')}?${$(form).serialize()}`; + + $(this.groupsListHolderElement).fadeTo(250, 0.5); + + return $.ajax({ + url: form.getAttribute('action'), + data: $(form).serialize(), + type: 'GET', + dataType: 'json', + context: this, + complete() { + $(this.groupsListHolderElement).fadeTo(250, 1); + }, + success(data) { + this.groupsListHolderElement.innerHTML = data.html; + + // Change url so if user reload a page - search results are saved + return window.history.replaceState({ + page: groupFilterUrl, + + }, document.title, groupFilterUrl); + }, + }); + } +} diff --git a/app/views/dashboard/_groups_head.html.haml b/app/views/dashboard/_groups_head.html.haml index d49913f39c5..c6d5937a3c3 100644 --- a/app/views/dashboard/_groups_head.html.haml +++ b/app/views/dashboard/_groups_head.html.haml @@ -8,7 +8,7 @@ Explore Groups .nav-controls = form_tag request.path, method: :get, class: 'group-filter-form', id: 'group-filter-form' do |f| - = search_field_tag :filter_groups, params[:filter_groups], placeholder: 'Filter by name...', class: 'group-filter-form-field form-control input-short groups-list-filter', spellcheck: false, id: 'group-filter-form-field', tabindex: "2" + = search_field_tag :filter_groups, params[:filter_groups], placeholder: 'Filter by name...', class: 'group-filter-form-field form-control input-short js-groups-list-filter', spellcheck: false, id: 'group-filter-form-field', tabindex: "2" = render 'shared/groups/dropdown' - if current_user.can_create_group? = link_to new_group_path, class: "btn btn-new" do diff --git a/app/views/dashboard/groups/_groups.html.haml b/app/views/dashboard/groups/_groups.html.haml index 750559d3d24..7385e1725f0 100644 --- a/app/views/dashboard/groups/_groups.html.haml +++ b/app/views/dashboard/groups/_groups.html.haml @@ -1,4 +1,4 @@ -.groups-list-holder +.js-groups-list-holder %ul.content-list - @group_members.each do |group_member| - group = group_member.group diff --git a/app/views/dashboard/groups/index.html.haml b/app/views/dashboard/groups/index.html.haml index 75af02b9edf..73ab2c95ff9 100644 --- a/app/views/dashboard/groups/index.html.haml +++ b/app/views/dashboard/groups/index.html.haml @@ -6,6 +6,3 @@ = render 'empty_state' - else = render 'groups' - -:javascript - GroupsList.init(); diff --git a/app/views/explore/groups/_groups.html.haml b/app/views/explore/groups/_groups.html.haml index 8a8c2a740f1..794c6d1d170 100644 --- a/app/views/explore/groups/_groups.html.haml +++ b/app/views/explore/groups/_groups.html.haml @@ -1,4 +1,4 @@ -.groups-list-holder +.js-groups-list-holder %ul.content-list - @groups.each do |group| = render 'shared/groups/group', group: group diff --git a/app/views/explore/groups/index.html.haml b/app/views/explore/groups/index.html.haml index 10383613c2b..7f1bacc91cb 100644 --- a/app/views/explore/groups/index.html.haml +++ b/app/views/explore/groups/index.html.haml @@ -13,6 +13,3 @@ .nothing-here-block No public groups = paginate @groups, theme: "gitlab" - -:javascript - GroupsList.init(); diff --git a/config/webpack.config.js b/config/webpack.config.js index a71ec0c5f52..e91794208e6 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -28,6 +28,7 @@ var config = { environments_folder: './environments/folder/environments_folder_bundle.js', filtered_search: './filtered_search/filtered_search_bundle.js', graphs: './graphs/graphs_bundle.js', + groups_list: './groups_list.js', issuable: './issuable/issuable_bundle.js', merge_conflicts: './merge_conflicts/merge_conflicts_bundle.js', merge_request_widget: './merge_request_widget/ci_bundle.js', diff --git a/spec/features/dashboard/groups_list_spec.rb b/spec/features/dashboard/groups_list_spec.rb index 8cdd14ba1bb..c1d5e4069c2 100644 --- a/spec/features/dashboard/groups_list_spec.rb +++ b/spec/features/dashboard/groups_list_spec.rb @@ -31,4 +31,17 @@ RSpec.describe 'Dashboard Groups page', js: true, feature: true do expect(page).not_to have_content(nested_group.full_name) expect(page).not_to have_content(another_group.full_name) end + + it 'resets search when user cleans the input' do + fill_in 'filter_groups', with: group.name + wait_for_ajax + + fill_in 'filter_groups', with: "" + wait_for_ajax + + expect(page).to have_content(group.full_name) + expect(page).to have_content(public_group.full_name) + expect(page).not_to have_content(private_group.full_name) + expect(page.all('.js-groups-list-holder .content-list li').length).to eq 2 + end end diff --git a/spec/features/explore/groups_list_spec.rb b/spec/features/explore/groups_list_spec.rb index 2ab97a8928b..7b23115c527 100644 --- a/spec/features/explore/groups_list_spec.rb +++ b/spec/features/explore/groups_list_spec.rb @@ -30,4 +30,17 @@ RSpec.describe 'Explore Groups page', js: true, feature: true do expect(page).not_to have_content(public_group.full_name) expect(page).not_to have_content(private_group.full_name) end + + it 'resets search when user cleans the input' do + fill_in 'filter_groups', with: group.name + wait_for_ajax + + fill_in 'filter_groups', with: "" + wait_for_ajax + + expect(page).to have_content(group.full_name) + expect(page).to have_content(public_group.full_name) + expect(page).not_to have_content(private_group.full_name) + expect(page.all('.js-groups-list-holder .content-list li').length).to eq 2 + end end |