Skip to content

Commit

Permalink
Merge pull request #224 from alphagov/linting
Browse files Browse the repository at this point in the history
[RFC] Lint codebase using standard
  • Loading branch information
joelanman authored Aug 18, 2016
2 parents 34c8d40 + 0d9835b commit ed48bc7
Show file tree
Hide file tree
Showing 17 changed files with 514 additions and 389 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ install:
script:
# Reference the locally-installed version of Grunt
- ./node_modules/grunt-cli/bin/grunt test
- npm test
after_success:
# Check to see if the version file has been updated
- ./create-release-tag.sh
Expand Down
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ Please check that any issues related to that code are raised with that project,

### Indentation and whitespace

2-space, soft-tabs only please. No trailing whitespace.
Your JavaScript code should pass [linting](docs/linting.md).

For anything else, maintain 2-space, soft-tabs only indentation. No trailing whitespace.

### Versioning

Expand Down
39 changes: 19 additions & 20 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = function(grunt){
module.exports = function (grunt) {
grunt.initConfig({

// Builds Sass
sass: {
dev: {
options: {
style: "expanded",
style: 'expanded',
sourcemap: true,
includePaths: [
'govuk_modules/govuk_template/assets/stylesheets',
Expand All @@ -16,10 +15,10 @@ module.exports = function(grunt){
},
files: [{
expand: true,
cwd: "app/assets/sass",
src: ["*.scss"],
dest: "public/stylesheets/",
ext: ".css"
cwd: 'app/assets/sass',
src: ['*.scss'],
dest: 'public/stylesheets/',
ext: '.css'
}]
}
},
Expand All @@ -33,7 +32,7 @@ module.exports = function(grunt){
src: ['**/*', '!sass/**'],
dest: 'public/'
}],
ignoreInDest: "**/stylesheets/**",
ignoreInDest: '**/stylesheets/**',
updateAndDelete: true
},
govuk: {
Expand Down Expand Up @@ -73,14 +72,14 @@ module.exports = function(grunt){
files: ['app/assets/sass/**/*.scss'],
tasks: ['sass'],
options: {
spawn: false,
spawn: false
}
},
assets:{
assets: {
files: ['app/assets/**/*', '!app/assets/sass/**'],
tasks: ['sync:assets'],
options: {
spawn: false,
spawn: false
}
}
},
Expand All @@ -105,33 +104,33 @@ module.exports = function(grunt){
}
}
}
});
})

[
;[
'grunt-sync',
'grunt-contrib-watch',
'grunt-sass',
'grunt-nodemon',
'grunt-concurrent'
].forEach(function (task) {
grunt.loadNpmTasks(task);
});
grunt.loadNpmTasks(task)
})

grunt.registerTask('generate-assets', [
'sync',
'sass'
]);
])

grunt.registerTask('default', [
'generate-assets',
'concurrent:target'
]);
])

grunt.registerTask(
'test',
'default',
function () {
grunt.log.writeln('Test that the app runs');
grunt.log.writeln('Test that the app runs')
}
);
};
)
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Read the [project principles](docs/principles.md).

If you publish your prototypes online, they **must** be protected by a [username and password](docs/guides/publishing-on-heroku.md). This is to prevent members of the public finding prototypes and thinking they are real services.

You must protect user privacy at all times, even when using prototypes. Prototypes made with the kit look like GOV.UK, but do not have the same security provisions. Always make sure you are handling user data appropriately.
You must protect user privacy at all times, even when using prototypes. Prototypes made with the kit look like GOV.UK, but do not have the same security provisions. Always make sure you are handling user data appropriately.

## Installation instructions

Expand Down
128 changes: 57 additions & 71 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1,127 +1,113 @@
function ShowHideContent() {
var self = this;
/* global $ */
/* global GOVUK */

self.escapeElementName = function(str) {
result = str.replace('[', '\\[').replace(']', '\\]')
return(result);
};
function ShowHideContent () {
var self = this

self.escapeElementName = function (str) {
var result = str.replace('[', '\\[').replace(']', '\\]')
return (result)
}

self.showHideRadioToggledContent = function () {
$(".block-label input[type='radio']").each(function () {
var $radio = $(this)
var $radioGroupName = $radio.attr('name')
var $radioLabel = $radio.parent('label')

var $radio = $(this);
var $radioGroupName = $radio.attr('name');
var $radioLabel = $radio.parent('label');

var dataTarget = $radioLabel.attr('data-target');
var dataTarget = $radioLabel.attr('data-target')

// Add ARIA attributes

// If the data-target attribute is defined
if (dataTarget) {

// Set aria-controls
$radio.attr('aria-controls', dataTarget);
$radio.attr('aria-controls', dataTarget)

$radio.on('click', function () {

// Select radio buttons in the same group
$radio.closest('form').find(".block-label input[name=" + self.escapeElementName($radioGroupName) + "]").each(function () {
var $this = $(this);
$radio.closest('form').find('.block-label input[name=' + self.escapeElementName($radioGroupName) + ']').each(function () {
var $this = $(this)

var groupDataTarget = $this.parent('label').attr('data-target');
var $groupDataTarget = $('#' + groupDataTarget);
var groupDataTarget = $this.parent('label').attr('data-target')
var $groupDataTarget = $('#' + groupDataTarget)

// Hide toggled content
$groupDataTarget.addClass('js-hidden');
$groupDataTarget.addClass('js-hidden')
// Set aria-expanded and aria-hidden for hidden content
$this.attr('aria-expanded', 'false');
$groupDataTarget.attr('aria-hidden', 'true');
});
$this.attr('aria-expanded', 'false')
$groupDataTarget.attr('aria-hidden', 'true')
})

var $dataTarget = $('#' + dataTarget);
$dataTarget.removeClass('js-hidden');
var $dataTarget = $('#' + dataTarget)
$dataTarget.removeClass('js-hidden')
// Set aria-expanded and aria-hidden for clicked radio
$radio.attr('aria-expanded', 'true');
$dataTarget.attr('aria-hidden', 'false');

});

$radio.attr('aria-expanded', 'true')
$dataTarget.attr('aria-hidden', 'false')
})
} else {
// If the data-target attribute is undefined for a radio button,
// hide visible data-target content for radio buttons in the same group

$radio.on('click', function () {

// Select radio buttons in the same group
$(".block-label input[name=" + self.escapeElementName($radioGroupName) + "]").each(function () {

var groupDataTarget = $(this).parent('label').attr('data-target');
var $groupDataTarget = $('#' + groupDataTarget);
$('.block-label input[name=' + self.escapeElementName($radioGroupName) + ']').each(function () {
var groupDataTarget = $(this).parent('label').attr('data-target')
var $groupDataTarget = $('#' + groupDataTarget)

// Hide toggled content
$groupDataTarget.addClass('js-hidden');
$groupDataTarget.addClass('js-hidden')
// Set aria-expanded and aria-hidden for hidden content
$(this).attr('aria-expanded', 'false');
$groupDataTarget.attr('aria-hidden', 'true');
});

});
$(this).attr('aria-expanded', 'false')
$groupDataTarget.attr('aria-hidden', 'true')
})
})
}

});
})
}
self.showHideCheckboxToggledContent = function () {
$(".block-label input[type='checkbox']").each(function () {
var $checkbox = $(this)
var $checkboxLabel = $(this).parent()

$(".block-label input[type='checkbox']").each(function() {

var $checkbox = $(this);
var $checkboxLabel = $(this).parent();

var $dataTarget = $checkboxLabel.attr('data-target');
var $dataTarget = $checkboxLabel.attr('data-target')

// Add ARIA attributes

// If the data-target attribute is defined
if (typeof $dataTarget !== 'undefined' && $dataTarget !== false) {

// Set aria-controls
$checkbox.attr('aria-controls', $dataTarget);
$checkbox.attr('aria-controls', $dataTarget)

// Set aria-expanded and aria-hidden
$checkbox.attr('aria-expanded', 'false');
$('#'+$dataTarget).attr('aria-hidden', 'true');
$checkbox.attr('aria-expanded', 'false')
$('#' + $dataTarget).attr('aria-hidden', 'true')

// For checkboxes revealing hidden content
$checkbox.on('click', function() {

var state = $(this).attr('aria-expanded') === 'false' ? true : false;
$checkbox.on('click', function () {
var state = $(this).attr('aria-expanded') === 'false'

// Toggle hidden content
$('#'+$dataTarget).toggleClass('js-hidden');
$('#' + $dataTarget).toggleClass('js-hidden')

// Update aria-expanded and aria-hidden attributes
$(this).attr('aria-expanded', state);
$('#'+$dataTarget).attr('aria-hidden', !state);

});
$(this).attr('aria-expanded', state)
$('#' + $dataTarget).attr('aria-hidden', !state)
})
}

});
})
}
}

$(document).ready(function() {

$(document).ready(function () {
// Use GOV.UK selection-buttons.js to set selected
// and focused states for block labels
var $blockLabels = $(".block-label input[type='radio'], .block-label input[type='checkbox']");
new GOVUK.SelectionButtons($blockLabels);
var $blockLabels = $(".block-label input[type='radio'], .block-label input[type='checkbox']")
new GOVUK.SelectionButtons($blockLabels) // eslint-disable-line

// Show and hide toggled content
// Where .block-label uses the data-target attribute
var toggleContent = new ShowHideContent();
toggleContent.showHideRadioToggledContent();
toggleContent.showHideCheckboxToggledContent();

});
var toggleContent = new ShowHideContent()
toggleContent.showHideRadioToggledContent()
toggleContent.showHideCheckboxToggledContent()
})
Loading

0 comments on commit ed48bc7

Please sign in to comment.