Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render world locations #3337

Merged
merged 8 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ At time of writing, it also serves the priority campaign pages. See the [Campaig
- Organisation page: [gov.uk/government/organisations/cabinet-office](https://www.gov.uk/government/organisations/cabinet-office)
- Step by step page: [gov.uk/learn-to-drive-a-car](https://www.gov.uk/learn-to-drive-a-car)
- Past Prime Minister: [gov.uk/government/history/past-prime-ministers/tony-blair](https://www.gov.uk/government/history/past-prime-ministers/tony-blair)
- World index page: [gov.uk/world](https://www.gov.uk/world)

## Nomenclature

Expand Down
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
//= link views/_taxons.css
//= link views/_topical_events.css
//= link views/_topics.css
//= link views/_world_index.css
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
//= require govuk_publishing_components/components/step-by-step-nav

//= require support
//= require modules/organisation-list-filter
jkempster34 marked this conversation as resolved.
Show resolved Hide resolved
//= require modules/list-filter
//= require modules/toggle-attribute
//= require modules/coronavirus-landing-page
121 changes: 121 additions & 0 deletions app/assets/javascripts/modules/list-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* eslint-disable no-var */
//= require govuk_publishing_components/vendor/polyfills/closest

window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
function ListFilter ($module) {
this.$module = $module
this.filterTimeout = null
this.form = this.$module.querySelector('[data-filter="form"]')
this.searchResults = this.$module.querySelector('#search_results')
}

ListFilter.prototype.init = function () {
this.$module.filterList = this.filterList.bind(this)
// Form should only appear if the JS is working
this.form.classList.add('filter-list__form--active')
this.results = document.createElement('div')
this.results.classList.add('filter-list__results', 'govuk-heading-m', 'js-search-results')
this.results.setAttribute('aria-live', 'polite')
this.results.innerHTML = this.countInitialItems() + ' results found'
this.searchResults.insertBefore(this.results, this.searchResults.firstChild)

// We don't want the form to submit/refresh the page on enter key
this.form.onsubmit = function () { return false }

this.form.addEventListener('keyup', function (e) {
var searchTerm = e.target.value
clearTimeout(this.filterTimeout)
this.filterTimeout = setTimeout(function () {
this.$module.filterList(searchTerm)
}.bind(this), 200)
}.bind(this))
}

ListFilter.prototype.filterList = function (searchTerm) {
var itemsToFilter = this.$module.querySelectorAll('[data-filter="item"]')
var blocksToFilter = this.$module.querySelectorAll('[data-filter="block"]')
for (var i = 0; i <= itemsToFilter.length - 1; i++) {
var currentItem = itemsToFilter[i]
if (!this.matchSearchTerm(currentItem, searchTerm)) {
currentItem.classList.add('js-hidden')
}
}
this.updateItemCount(blocksToFilter)
}

ListFilter.prototype.matchSearchTerm = function (item, term) {
var normaliseWhitespace = function (string) {
return string
.trim() // Removes spaces at beginning and end of string.
.replace(/\r?\n|\r/g, ' ') // Replaces line breaks with one space.
.replace(/\s+/g, ' ') // Squashes multiple spaces to one space.
}

var searchTerms = item.getAttribute('data-filter-terms') || ''
var normalisedTerms = normaliseWhitespace(searchTerms)

item.classList.remove('js-hidden')

var searchTermRegexp = new RegExp(term.trim().replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i')
return searchTermRegexp.exec(normalisedTerms) !== null
}

ListFilter.prototype.countInitialItems = function () {
return this.$module.querySelectorAll('[data-filter="item"]').length
}

ListFilter.prototype.updateItemCount = function (blocksToFilter) {
var totalMatchingItems = 0

for (var i = 0; i < blocksToFilter.length; i++) {
var block = blocksToFilter[i].closest('[data-filter="block"]')
block.classList.remove('js-hidden')

var matchingItems = block.querySelectorAll('[data-filter="item"]')
var matchingItemCount = 0

var innerBlocks = block.querySelectorAll('[data-filter="inner-block"]')
for (var r = 0; r < innerBlocks.length; r++) {
innerBlocks[r].classList.add('js-hidden')
}

for (var j = 0; j < matchingItems.length; j++) {
if (!matchingItems[j].classList.contains('js-hidden')) {
matchingItemCount++

if (matchingItems[j].closest('[data-filter="inner-block"]') !== null) { matchingItems[j].closest('[data-filter="inner-block"]').classList.remove('js-hidden') }
}
}

var itemCount = block.querySelectorAll('[data-item-count="true"]')
var accessibleItemCount = block.querySelectorAll('.js-accessible-item-count')

if (matchingItemCount === 0) {
block.classList.toggle('js-hidden')
}

if (matchingItemCount > 0) {
for (var l = 0; l < itemCount.length; l++) {
itemCount[l].textContent = matchingItemCount
}

for (var k = 0; k < accessibleItemCount.length; k++) {
accessibleItemCount[k].textContent = matchingItemCount
}
}

totalMatchingItems += matchingItemCount
}

var text = ' results found'
if (totalMatchingItems === 1) {
text = ' result found'
}
this.results.innerHTML = totalMatchingItems + text
}

Modules.ListFilter = ListFilter
})(window.GOVUK.Modules)
129 changes: 0 additions & 129 deletions app/assets/javascripts/modules/organisation-list-filter.js

This file was deleted.

4 changes: 2 additions & 2 deletions app/assets/stylesheets/views/_organisations.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
}
}

.filter-organisations-list__form {
.filter-list__form {
display: none;
}

.filter-organisations-list__form--active {
.filter-list__form--active {
display: block;
}
49 changes: 49 additions & 0 deletions app/assets/stylesheets/views/_world_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@import "govuk_publishing_components/individual_component_support";

.world-locations {
.world-locations-heading {
@include govuk-font(19);
}

.world-locations-groups {
padding: govuk-spacing(5) 0 0 0;
}

.world-locations-group__list {
padding: 0;
}

.world-locations-group {
margin-bottom: govuk-spacing(6);
}

.world-locations-group__letter {
@include govuk-font(80);
font-weight: bold;
text-indent: -4px;
margin: 0;
}

.world_locations-group__item {
list-style-type: none;
border-bottom: 1px solid govuk-colour("mid-grey");
padding: govuk-spacing(1) 0;
@include govuk-font(19);
}

.filter-list__form {
display: none;
}

.filter-list__form--active {
display: block;
}

.js-hidden {
display: none;
}

.filter-list__results {
padding-left: $govuk-gutter-half;
}
}
7 changes: 7 additions & 0 deletions app/controllers/world_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class WorldController < ApplicationController
def index
index = WorldIndex.find!("/world")
@presented_index = WorldIndexPresenter.new(index)
hannako marked this conversation as resolved.
Show resolved Hide resolved
setup_content_item_and_navigation_helpers(index)
end
end
12 changes: 12 additions & 0 deletions app/models/world_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class WorldIndex
attr_reader :content_item

def initialize(content_item)
@content_item = content_item
end

def self.find!(base_path)
content_item = ContentItem.find!(base_path)
new(content_item)
end
end
7 changes: 7 additions & 0 deletions app/presenters/organisations/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def title
@organisations.content_item.title
end

def filter_terms(organisation_type, organisation)
acronym = organisation["acronym"]
title = ministerial_organisation?(organisation_type) ? organisation["logo"]["formatted_title"].squish : organisation["title"]

[title, acronym].compact.join(" ")
end

def all_organisations
{
number_10: @organisations.number_10,
Expand Down
Loading