mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-20 14:29:51 -04:00
Refactor hex colour preview Javascript
This commit improves the code that previews a hex colour when setting up or changing an email branding. Specifically it: - refactors the Javascript to conform to our patterns (module pattern, preprocessed with Gulp) - makes the code work when there are multiple colour previews on one page It also does some visual prettifying, because I couldn’t help myself…
This commit is contained in:
26
app/assets/javascripts/colourPreview.js
Normal file
26
app/assets/javascripts/colourPreview.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
(function(Modules) {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
let isSixDigitHex = value => value.match(/^#[0-9A-F]{6}$/i);
|
||||||
|
let colourOrWhite = value => isSixDigitHex(value) ? value : '#FFFFFF';
|
||||||
|
|
||||||
|
Modules.ColourPreview = function() {
|
||||||
|
|
||||||
|
this.start = component => {
|
||||||
|
|
||||||
|
this.$input = $('input', component);
|
||||||
|
this.$preview = $('.textbox-colour-preview', component);
|
||||||
|
|
||||||
|
this.$input
|
||||||
|
.on('change keyup', this.update)
|
||||||
|
.trigger('change');
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
this.update = () => this.$preview.css(
|
||||||
|
'background', colourOrWhite(this.$input.val())
|
||||||
|
);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
})(window.GOVUK.Modules);
|
||||||
@@ -62,11 +62,13 @@
|
|||||||
|
|
||||||
.textbox-colour-preview {
|
.textbox-colour-preview {
|
||||||
@include media(desktop) {
|
@include media(desktop) {
|
||||||
width: 34px;
|
width: 38px;
|
||||||
height: 34px;
|
height: 38px;
|
||||||
margin-left: 5px;
|
margin-left: 5px;
|
||||||
border: 2px solid #0B0C0C;
|
border-radius: 50%;
|
||||||
|
box-shadow: inset 0 0 0 1px rgba($black, 0.2);
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
vertical-align: top;
|
vertical-align: top;
|
||||||
|
transition: background 0.3s ease-out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -734,21 +734,18 @@ class ServiceUpdateEmailBranding(StripWhitespaceForm):
|
|||||||
domain = StringField('Domain')
|
domain = StringField('Domain')
|
||||||
colour = StringField(
|
colour = StringField(
|
||||||
'Colour',
|
'Colour',
|
||||||
render_kw={'onchange': 'update_colour(this)'},
|
|
||||||
validators=[
|
validators=[
|
||||||
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code')
|
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code')
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
banner_colour = StringField(
|
banner_colour = StringField(
|
||||||
'Banner colour',
|
'Banner colour',
|
||||||
render_kw={'onchange': 'update_colour(this)'},
|
|
||||||
validators=[
|
validators=[
|
||||||
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code')
|
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code')
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
single_id_colour = StringField(
|
single_id_colour = StringField(
|
||||||
'Single identity colour',
|
'Single identity colour',
|
||||||
render_kw={'onchange': 'update_colour(this)'},
|
|
||||||
validators=[
|
validators=[
|
||||||
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code')
|
Regexp(regex="^$|^#(?:[0-9a-fA-F]{3}){1,2}$", message='Must be a valid color hex code')
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -60,10 +60,9 @@
|
|||||||
|
|
||||||
{% macro colour_textbox(
|
{% macro colour_textbox(
|
||||||
field,
|
field,
|
||||||
width='2-3',
|
width='2-3'
|
||||||
colour='#FFFFFF'
|
|
||||||
) %}
|
) %}
|
||||||
<div class="form-group{% if field.errors %} form-group-error{% endif %}">
|
<div class="form-group{% if field.errors %} form-group-error{% endif %}" data-module="colour-preview">
|
||||||
<label class="form-label" for="{{ field.name }}">
|
<label class="form-label" for="{{ field.name }}">
|
||||||
{{ field.label.text }}
|
{{ field.label.text }}
|
||||||
{% if field.errors %}
|
{% if field.errors %}
|
||||||
@@ -80,6 +79,6 @@
|
|||||||
rows='1',
|
rows='1',
|
||||||
**kwargs
|
**kwargs
|
||||||
) }}
|
) }}
|
||||||
<span class="textbox-colour-preview" style="background:{{ colour }}"></span>
|
<span class="textbox-colour-preview"></span>
|
||||||
</div>
|
</div>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|||||||
@@ -34,15 +34,5 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<script type="text/javascript">
|
|
||||||
function update_colour(element){
|
|
||||||
colour_preview = document.getElementsByClassName("textbox-colour-preview")[0];
|
|
||||||
if (element.value.match(/^#[0-9A-F]{6}$/i)) {
|
|
||||||
colour_preview.style.background = element.value;
|
|
||||||
} else {
|
|
||||||
colour_preview.style.background = '#FFFFFF';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ gulp.task('javascripts', () => gulp
|
|||||||
paths.src + 'javascripts/preventDuplicateFormSubmissions.js',
|
paths.src + 'javascripts/preventDuplicateFormSubmissions.js',
|
||||||
paths.src + 'javascripts/fullscreenTable.js',
|
paths.src + 'javascripts/fullscreenTable.js',
|
||||||
paths.src + 'javascripts/emailPreviewPane.js',
|
paths.src + 'javascripts/emailPreviewPane.js',
|
||||||
|
paths.src + 'javascripts/colourPreview.js',
|
||||||
paths.src + 'javascripts/main.js'
|
paths.src + 'javascripts/main.js'
|
||||||
])
|
])
|
||||||
.pipe(plugins.prettyerror())
|
.pipe(plugins.prettyerror())
|
||||||
|
|||||||
Reference in New Issue
Block a user