govuk_tech_docs 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +29 -0
  3. data/.gitignore +34 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +5 -0
  6. data/CHANGELOG.md +36 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +21 -0
  9. data/README.md +47 -0
  10. data/Rakefile +9 -0
  11. data/example/.ruby-version +1 -0
  12. data/example/Gemfile +3 -0
  13. data/example/config.rb +3 -0
  14. data/example/config/tech-docs.yml +23 -0
  15. data/example/source/index.html.md.erb +17 -0
  16. data/example/source/javascripts/application.js +1 -0
  17. data/example/source/stylesheets/print.css.scss +3 -0
  18. data/example/source/stylesheets/screen-old-ie.css.scss +4 -0
  19. data/example/source/stylesheets/screen.css.scss +1 -0
  20. data/govuk_tech_docs.gemspec +38 -0
  21. data/lib/assets/javascripts/_analytics.js +31 -0
  22. data/lib/assets/javascripts/_govuk/modules.js +57 -0
  23. data/lib/assets/javascripts/_modules/anchored-headings.js +18 -0
  24. data/lib/assets/javascripts/_modules/in-page-navigation.js +124 -0
  25. data/lib/assets/javascripts/_modules/navigation.js +34 -0
  26. data/lib/assets/javascripts/_modules/table-of-contents.js +103 -0
  27. data/lib/assets/javascripts/_start-modules.js +9 -0
  28. data/lib/assets/javascripts/_vendor/fixedsticky.js +194 -0
  29. data/lib/assets/javascripts/_vendor/jquery.js +5 -0
  30. data/lib/assets/javascripts/_vendor/lodash.js +613 -0
  31. data/lib/assets/javascripts/_vendor/modernizr.js +3 -0
  32. data/lib/assets/javascripts/govuk_tech_docs.js +10 -0
  33. data/lib/assets/stylesheets/_accessibility.scss +9 -0
  34. data/lib/assets/stylesheets/_core.scss +62 -0
  35. data/lib/assets/stylesheets/_fonts.scss +29 -0
  36. data/lib/assets/stylesheets/_govuk_tech_docs.scss +2 -0
  37. data/lib/assets/stylesheets/_syntax-highlighting.scss +196 -0
  38. data/lib/assets/stylesheets/govuk_frontend_toolkit/_colours.scss +2 -0
  39. data/lib/assets/stylesheets/govuk_frontend_toolkit/_conditionals.scss +81 -0
  40. data/lib/assets/stylesheets/govuk_frontend_toolkit/_css3.scss +90 -0
  41. data/lib/assets/stylesheets/govuk_frontend_toolkit/_device-pixels.scss +10 -0
  42. data/lib/assets/stylesheets/govuk_frontend_toolkit/_font_stack.scss +19 -0
  43. data/lib/assets/stylesheets/govuk_frontend_toolkit/_grid_layout.scss +136 -0
  44. data/lib/assets/stylesheets/govuk_frontend_toolkit/_helpers.scss +16 -0
  45. data/lib/assets/stylesheets/govuk_frontend_toolkit/_measurements.scss +14 -0
  46. data/lib/assets/stylesheets/govuk_frontend_toolkit/_shims.scss +55 -0
  47. data/lib/assets/stylesheets/govuk_frontend_toolkit/_typography.scss +249 -0
  48. data/lib/assets/stylesheets/govuk_frontend_toolkit/_url-helpers.scss +16 -0
  49. data/lib/assets/stylesheets/govuk_frontend_toolkit/colours/_organisation.scss +103 -0
  50. data/lib/assets/stylesheets/govuk_frontend_toolkit/colours/_palette.scss +77 -0
  51. data/lib/assets/stylesheets/govuk_frontend_toolkit/design-patterns/_alpha-beta.scss +66 -0
  52. data/lib/assets/stylesheets/govuk_frontend_toolkit/design-patterns/_breadcrumbs.scss +53 -0
  53. data/lib/assets/stylesheets/govuk_frontend_toolkit/design-patterns/_buttons.scss +141 -0
  54. data/lib/assets/stylesheets/govuk_frontend_toolkit/design-patterns/_media-player.scss +242 -0
  55. data/lib/assets/stylesheets/modules/_anchored-heading.scss +54 -0
  56. data/lib/assets/stylesheets/modules/_app-pane.scss +64 -0
  57. data/lib/assets/stylesheets/modules/_footer.scss +130 -0
  58. data/lib/assets/stylesheets/modules/_govuk-logo.scss +47 -0
  59. data/lib/assets/stylesheets/modules/_header.scss +289 -0
  60. data/lib/assets/stylesheets/modules/_phase-banner.scss +22 -0
  61. data/lib/assets/stylesheets/modules/_skip-link.scss +31 -0
  62. data/lib/assets/stylesheets/modules/_technical-documentation.scss +236 -0
  63. data/lib/assets/stylesheets/modules/_toc.scss +221 -0
  64. data/lib/assets/stylesheets/palette/_syntax-highlighting.scss +23 -0
  65. data/lib/assets/stylesheets/utilities/_fonts.scss +29 -0
  66. data/lib/assets/stylesheets/utilities/_printable.scss +13 -0
  67. data/lib/assets/stylesheets/vendor/_fixedsticky.scss +22 -0
  68. data/lib/govuk_tech_docs.rb +54 -0
  69. data/lib/govuk_tech_docs/table_of_contents/heading.rb +29 -0
  70. data/lib/govuk_tech_docs/table_of_contents/heading_tree.rb +27 -0
  71. data/lib/govuk_tech_docs/table_of_contents/heading_tree_builder.rb +41 -0
  72. data/lib/govuk_tech_docs/table_of_contents/heading_tree_renderer.rb +46 -0
  73. data/lib/govuk_tech_docs/table_of_contents/headings_builder.rb +37 -0
  74. data/lib/govuk_tech_docs/table_of_contents/helpers.rb +20 -0
  75. data/lib/govuk_tech_docs/tech_docs_html_renderer.rb +24 -0
  76. data/lib/govuk_tech_docs/unique_identifier_extension.rb +13 -0
  77. data/lib/govuk_tech_docs/unique_identifier_generator.rb +72 -0
  78. data/lib/govuk_tech_docs/version.rb +3 -0
  79. data/lib/source/favicon.ico +0 -0
  80. data/lib/source/images/anchored-heading-icon-2x.png +0 -0
  81. data/lib/source/images/anchored-heading-icon.png +0 -0
  82. data/lib/source/images/gov.uk_logotype_crown-2x.png +0 -0
  83. data/lib/source/images/gov.uk_logotype_crown.png +0 -0
  84. data/lib/source/images/gov.uk_logotype_crown_invert_trans.png +0 -0
  85. data/lib/source/images/govuk-crest-2x.png +0 -0
  86. data/lib/source/images/govuk-crest.png +0 -0
  87. data/lib/source/images/govuk-icn-close.png +0 -0
  88. data/lib/source/images/govuk-icn-close@2x.png +0 -0
  89. data/lib/source/images/govuk-icn-numbered-list.png +0 -0
  90. data/lib/source/images/govuk-icn-numbered-list@2x.png +0 -0
  91. data/lib/source/images/open-government-licence.png +0 -0
  92. data/lib/source/images/open-government-licence_2x.png +0 -0
  93. data/lib/source/layouts/_analytics.erb +14 -0
  94. data/lib/source/layouts/_footer.erb +10 -0
  95. data/lib/source/layouts/_header.erb +46 -0
  96. data/lib/source/layouts/core.erb +59 -0
  97. data/lib/source/layouts/layout.erb +16 -0
  98. metadata +323 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02e41ba09a532d75fa642d0eb9ab0b1960fc83d6
4
+ data.tar.gz: 676133bd3cf4b436638be2699b60a62530101ba0
5
+ SHA512:
6
+ metadata.gz: 820f02328cc40233559cf3f4cfc1757bc194706e123729cb927856da064a700c5d8bf00c1dc030d444560cc7aec794fc7fc030cd8e342c857759a46aa274f471
7
+ data.tar.gz: c7ea93749ef1bf236eac5e58865406278313a7ce068b2aed51236997803f3c30bfab5daf7c0f810e7b82a69b9587e1ae0a876c6a3d5ac8ec486f7d74ea586a5f
@@ -0,0 +1,29 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+ # https://gds-way.cloudapps.digital/manuals/programming-languages/editorconfig
3
+
4
+ # top-most EditorConfig file
5
+ root = true
6
+
7
+ [*.java]
8
+ indent_style: space
9
+ indent_size: 4
10
+ end_of_line: lf
11
+ charset: utf-8
12
+ insert_final_newline: true
13
+ continuation_indent_size: 8
14
+
15
+ [*.{scss,erb}]
16
+ charset = utf-8
17
+ end_of_line = lf
18
+ indent_style = space
19
+ indent_size = 2
20
+ insert_final_newline = true
21
+ trim_trailing_whitespace = true
22
+
23
+ [*.{js,json}]
24
+ charset = utf-8
25
+ end_of_line = lf
26
+ indent_style = space
27
+ indent_size = 2
28
+ insert_final_newline = true
29
+ trim_trailing_whitespace = true
@@ -0,0 +1,34 @@
1
+ ### Ruby ###
2
+ *.gem
3
+ *.rbc
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalization:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
29
+ .rvmrc
30
+
31
+ /example/build
32
+ /example/.sass-cache
33
+
34
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.2
4
+ script:
5
+ - bundle exec rake
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+
5
+ The first release of the template as a gem. Most CSS, JS, images and layouts are
6
+ now imported from the gem and can be removed from your project.
7
+
8
+ The following script upgrades your project. This will work if you've not made
9
+ any modifications to the files distributed by the template. If you have, you'll
10
+ need to port your changes back in.
11
+
12
+ ```sh
13
+ rm -rf lib source/images source/layouts source/favicon.ico source/javascripts/* source/stylesheets/*
14
+
15
+ echo "//= require govuk_tech_docs" > source/javascripts/application.js
16
+ echo "@import 'govuk_tech_docs';" > source/stylesheets/screen.css.scss
17
+
18
+ echo '$is-print: true;
19
+
20
+ @import "govuk_tech_docs";' > source/stylesheets/print.css.scss
21
+
22
+ echo '$is-ie: true;
23
+ $ie-version: 8;
24
+
25
+ @import "govuk_tech_docs";' > source/stylesheets/screen-old-ie.css.scss
26
+
27
+ echo "require 'govuk_tech_docs'
28
+
29
+ TechDocs.configure(self)" > config.rb
30
+
31
+ echo "source 'https://rubygems.org'
32
+
33
+ gem 'govuk_tech_docs', path: '../govuk_tech_docs'" > Gemfile
34
+
35
+ bundle install
36
+ ```
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in govuk_tech_docs.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Crown Copyright (Government Digital Service)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Tech Docs Template - gem
2
+
3
+ This repo is part of the [tech-docs-template](https://github.com/alphagov/tech-docs-template).
4
+
5
+ ## Usage
6
+
7
+ To [generate a new site with the template](https://github.com/alphagov/tech-docs-template#creating-a-new-documentation-project), see the tech-docs-template repo.
8
+
9
+ ## Developing locally
10
+
11
+ There are 2 ways to develop with this gem.
12
+
13
+ The first is to point your site's Gemfile to your local checkout, and use it normally. This is good for if you want to see what effect your changes have to the actual site.
14
+
15
+ ```rb
16
+ gem 'govuk_tech_docs', path: '../tech-docs-gem'
17
+ ```
18
+
19
+ The second is to use the example app in this repo. You can start it by
20
+
21
+ ```
22
+ git clone git@github.com:alphagov/tech-docs-gem.git
23
+ cd tech-docs-gem/example
24
+ bundle install
25
+ bundle exec middleman serve
26
+ ```
27
+
28
+ Your site will appear on <http://localhost:4567>.
29
+
30
+ ## Tests
31
+
32
+ We have some automated JavaScript tests that use [Jasmine][jas] as a test
33
+ framework.
34
+
35
+ To run the tests on your machine:
36
+
37
+ - Run `bundle exec rake jasmine`
38
+ - Navigate to `http://localhost:8888` in a browser of your choosing
39
+ - Peruse the output of your tests
40
+
41
+ Or, on the command line, run `bundle exec rake jasmine:ci`.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](LICENSE.md).
46
+
47
+ [jas]: https://jasmine.github.io/
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'jasmine'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ load 'jasmine/tasks/jasmine.rake'
8
+
9
+ task default: ['spec', 'jasmine:ci']
@@ -0,0 +1 @@
1
+ 2.4.2
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'govuk_tech_docs', path: '..'
@@ -0,0 +1,3 @@
1
+ require 'govuk_tech_docs'
2
+
3
+ GovukTechDocs.configure(self)
@@ -0,0 +1,23 @@
1
+ # Host to use for canonical URL generation (without trailing slash)
2
+ host:
3
+
4
+ # Header-related options
5
+ show_govuk_logo: true
6
+ service_name: My First Service
7
+ service_link: /
8
+ phase: Beta
9
+
10
+ # Links to show on right-hand-side of header
11
+ header_links:
12
+ Documentation: /
13
+
14
+ # Tracking ID from Google Analytics (e.g. UA-XXXX-Y)
15
+ ga_tracking_id:
16
+
17
+ # Table of contents depth – how many levels to include in the table of contents.
18
+ # If your ToC is too long, reduce this number and we'll only show higher-level
19
+ # headings.
20
+ max_toc_heading_level: 6
21
+
22
+ # Prevent robots from indexing (e.g. whilst in development)
23
+ prevent_indexing: false
@@ -0,0 +1,17 @@
1
+ ---
2
+ title: GOV.UK Documentation Example
3
+ ---
4
+
5
+ # Hello, World!
6
+
7
+ ## Edit Me!
8
+
9
+ Open `source/documentation/index.md` in your favourite text editor and start editing!
10
+
11
+ You can write content in [Markdown](https://daringfireball.net/projects/markdown/) using **all** of the _usual_ syntax that you're used to!
12
+
13
+ To change the title of the page or include additional files you'll need to edit `source/index.html.md.erb`.
14
+
15
+ If you want slightly more control, you can always use <strong>HTML</strong>.
16
+
17
+ For more detail and troubleshooting, take a look at the `README.md` file in the root of this project.
@@ -0,0 +1 @@
1
+ //= require govuk_tech_docs
@@ -0,0 +1,3 @@
1
+ $is-print: true;
2
+
3
+ @import "govuk_tech_docs";
@@ -0,0 +1,4 @@
1
+ $is-ie: true;
2
+ $ie-version: 8;
3
+
4
+ @import "govuk_tech_docs";
@@ -0,0 +1 @@
1
+ @import "govuk_tech_docs";
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "govuk_tech_docs/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "govuk_tech_docs"
8
+ spec.version = GovukTechDocs::VERSION
9
+ spec.authors = ["Government Digital Service"]
10
+ spec.email = ["govuk-dev@digital.cabinet-office.gov.uk"]
11
+
12
+ spec.summary = %q{Gem to distribute the GOV.UK Tech Docs Template}
13
+ spec.description = %q{Gem to distribute the GOV.UK Tech Docs Template. See https://github.com/alphagov/govuk-tech-docs for the project.}
14
+ spec.homepage = "https://github.com/alphagov/govuk-tech-docs"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "middleman", "~> 4.0"
25
+ spec.add_dependency "middleman-autoprefixer", "~> 2.7.0"
26
+ spec.add_dependency "middleman-compass", ">= 4.0.0"
27
+ spec.add_dependency "middleman-livereload"
28
+ spec.add_dependency "middleman-sprockets", "~> 4.0.0"
29
+ spec.add_dependency "middleman-syntax", "~> 3.0.0"
30
+ spec.add_dependency "nokogiri"
31
+ spec.add_dependency "redcarpet", "~> 3.3.2"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "capybara", "~> 2.18.0"
36
+ spec.add_development_dependency "jasmine", "~> 3.1.0"
37
+ spec.add_development_dependency "rspec", "~> 3.7.0"
38
+ end
@@ -0,0 +1,31 @@
1
+ (function($) {
2
+ function trackLinkClick(action, $element) {
3
+ var linkText = $.trim($element.text());
4
+ var linkURL = $element.attr('href');
5
+ var label = linkText + '|' + linkURL;
6
+
7
+ ga(
8
+ 'send',
9
+ 'event',
10
+ 'SM Technical Documentation', // Event Category
11
+ action, // Event Action
12
+ label // Event Label
13
+ );
14
+ }
15
+
16
+ function linkTrackingEventHandler(action) {
17
+ return function() {
18
+ trackLinkClick(action, $(this));
19
+ };
20
+ };
21
+
22
+ $(document).on('ready', function() {
23
+ if (typeof ga === 'undefined') {
24
+ return;
25
+ }
26
+
27
+ $('.technical-documentation a').on('click', linkTrackingEventHandler('inTextClick'));
28
+ $('.header a').on('click', linkTrackingEventHandler('topNavigationClick'));
29
+ $('.toc a').on('click', linkTrackingEventHandler('tableOfContentsNavigationClick'));
30
+ });
31
+ })(jQuery);
@@ -0,0 +1,57 @@
1
+ (function($, root) {
2
+ "use strict";
3
+ root.GOVUK = root.GOVUK || {};
4
+ GOVUK.Modules = GOVUK.Modules || {};
5
+
6
+ GOVUK.modules = {
7
+ find: function(container) {
8
+ var modules,
9
+ moduleSelector = '[data-module]',
10
+ container = container || $('body');
11
+
12
+ modules = container.find(moduleSelector);
13
+
14
+ // Container could be a module too
15
+ if (container.is(moduleSelector)) {
16
+ modules = modules.add(container);
17
+ }
18
+
19
+ return modules;
20
+ },
21
+
22
+ start: function(container) {
23
+ var modules = this.find(container);
24
+
25
+ for (var i = 0, l = modules.length; i < l; i++) {
26
+ var module,
27
+ element = $(modules[i]),
28
+ type = camelCaseAndCapitalise(element.data('module')),
29
+ started = element.data('module-started');
30
+
31
+
32
+ if (typeof GOVUK.Modules[type] === "function" && !started) {
33
+ module = new GOVUK.Modules[type]();
34
+ module.start(element);
35
+ element.data('module-started', true);
36
+ }
37
+ }
38
+
39
+ // eg selectable-table to SelectableTable
40
+ function camelCaseAndCapitalise(string) {
41
+ return capitaliseFirstLetter(camelCase(string));
42
+ }
43
+
44
+ // http://stackoverflow.com/questions/6660977/convert-hyphens-to-camel-case-camelcase
45
+ function camelCase(string) {
46
+ return string.replace(/-([a-z])/g, function (g) {
47
+ return g[1].toUpperCase();
48
+ });
49
+ }
50
+
51
+ // http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript
52
+ function capitaliseFirstLetter(string) {
53
+ return string.charAt(0).toUpperCase() + string.slice(1);
54
+ }
55
+ }
56
+ }
57
+ })(jQuery, window);
@@ -0,0 +1,18 @@
1
+ (function($, Modules) {
2
+ 'use strict';
3
+
4
+ Modules.AnchoredHeadings = function() {
5
+ this.start = function($element) {
6
+ var headings = $element.find('h1, h2, h3, h4, h5, h6');
7
+ headings.each(injectAnchor);
8
+ };
9
+
10
+ function injectAnchor() {
11
+ var $this = $(this);
12
+ $this.addClass('anchored-heading');
13
+ $this.prepend(
14
+ '<a href="#' + $this.attr('id') + '" class="anchored-heading__icon" aria-hidden="true"></a>'
15
+ );
16
+ };
17
+ };
18
+ })(jQuery, window.GOVUK.Modules);
@@ -0,0 +1,124 @@
1
+ (function($, Modules) {
2
+ 'use strict';
3
+
4
+ Modules.InPageNavigation = function InPageNavigation() {
5
+ var $tocPane;
6
+ var $contentPane;
7
+ var $tocItems;
8
+ var $targets;
9
+
10
+ this.start = function start($element) {
11
+ $tocPane = $element.find('.app-pane__toc');
12
+ $contentPane = $element.find('.app-pane__content');
13
+ $tocItems = $('.js-toc-list').find('a');
14
+ $targets = $contentPane.find('[id]');
15
+
16
+ $contentPane.on('scroll', _.debounce(handleScrollEvent, 100, { maxWait: 100 }));
17
+
18
+ if (Modernizr.history) {
19
+ // Popstate is triggered when using the back button to navigate 'within'
20
+ // the page, i.e. changing the anchor part of the URL.
21
+ $(window).on('popstate', function (event) {
22
+ restoreScrollPosition(event.originalEvent.state);
23
+ });
24
+
25
+ if (history.state && history.state.scrollTop) {
26
+ // Restore existing state when e.g. using the back button to return to
27
+ // this page
28
+ restoreScrollPosition(history.state);
29
+ } else {
30
+ // Store the initial position so that we can restore it even if we
31
+ // never scroll.
32
+ handleInitialLoadEvent();
33
+ }
34
+ }
35
+ };
36
+
37
+ function restoreScrollPosition(state) {
38
+ if (state && typeof state.scrollTop !== 'undefined') {
39
+ $contentPane.scrollTop(state.scrollTop);
40
+ }
41
+ }
42
+
43
+ function handleInitialLoadEvent() {
44
+ var fragment = fragmentForTargetElement();
45
+
46
+ if (!fragment) {
47
+ fragment = fragmentForFirstElementInView();
48
+ }
49
+
50
+ handleChangeInActiveItem(fragment);
51
+ }
52
+
53
+ function handleScrollEvent() {
54
+ handleChangeInActiveItem(fragmentForFirstElementInView());
55
+ }
56
+
57
+ function handleChangeInActiveItem(fragment) {
58
+ storeCurrentPositionInHistoryApi(fragment);
59
+ highlightActiveItemInToc(fragment);
60
+ }
61
+
62
+ function storeCurrentPositionInHistoryApi(fragment) {
63
+ if (Modernizr.history && fragment) {
64
+ history.replaceState(
65
+ { scrollTop: $contentPane.scrollTop() },
66
+ "",
67
+ fragment
68
+ );
69
+ }
70
+ }
71
+
72
+ function highlightActiveItemInToc(fragment) {
73
+ var $activeTocItem = $tocItems.filter('[href="' + fragment + '"]');
74
+
75
+ if ($activeTocItem.get(0)) {
76
+ $tocItems.removeClass('toc-link--in-view');
77
+ $activeTocItem.addClass('toc-link--in-view');
78
+ scrollTocToActiveItem($activeTocItem);
79
+ }
80
+ }
81
+
82
+ function scrollTocToActiveItem($activeTocItem) {
83
+ var paneHeight = $tocPane.height();
84
+ var linkTop = $activeTocItem.position().top;
85
+ var linkBottom = linkTop + $activeTocItem.outerHeight();
86
+
87
+ var offset = null;
88
+
89
+ if (linkTop < 0) {
90
+ offset = linkTop;
91
+ } else if (linkBottom >= paneHeight) {
92
+ offset = -(paneHeight - linkBottom);
93
+ } else {
94
+ return;
95
+ }
96
+
97
+ var newScrollTop = $tocPane.scrollTop() + offset;
98
+
99
+ $tocPane.scrollTop(newScrollTop);
100
+ }
101
+
102
+ function fragmentForTargetElement() {
103
+ return window.location.hash;
104
+ }
105
+
106
+ function fragmentForFirstElementInView() {
107
+ var result = null;
108
+
109
+ $($targets.get().reverse()).each(function checkIfInView(index) {
110
+ if (result) {
111
+ return;
112
+ }
113
+
114
+ var $this = $(this);
115
+
116
+ if (Math.floor($this.position().top) <= 0) {
117
+ result = $this;
118
+ }
119
+ });
120
+
121
+ return result ? '#' + result.attr('id') : false;
122
+ }
123
+ };
124
+ })(jQuery, window.GOVUK.Modules);