diff --git a/app/assets/javascripts/highlightTags.js b/app/assets/javascripts/highlightTags.js index 8cbd18bfe..1e2dae456 100644 --- a/app/assets/javascripts/highlightTags.js +++ b/app/assets/javascripts/highlightTags.js @@ -7,25 +7,6 @@ const tagPattern = /\(\([^\)\(]+\)\)/g; - const getPlaceholderHint = function(placeholders) { - if (0 === placeholders.length) { - return ` -

Add fields using ((double brackets))

- Show me how - `; - } - if (1 === placeholders.length) { - return ` -

Add fields using ((double brackets))

-

You’ll populate the ‘${placeholders[0]}’ field when you send messages using this template

- `; - } - return ` -

Add fields using ((double brackets))

-

You’ll populate your fields when you send some messages

- `; - }; - Modules.HighlightTags = function() { this.start = function(textarea) { @@ -41,9 +22,6 @@ `)) .on("input", this.update); - this.$placeHolderHint = $('#placeholder-hint') - .on("click", ".placeholder-hint-action", this.demo); - this.initialHeight = this.$textbox.height(); this.$backgroundMaskForeground.css({ @@ -65,29 +43,13 @@ this.escapedMessage = () => $('
').text(this.$textbox.val()).html(); - this.listPlaceholders = () => this.escapedMessage().match(tagPattern) || []; - - this.listPlaceholdersWithoutBrackets = () => this.listPlaceholders().map( - placeholder => placeholder.substring(2, placeholder.length - 2) - ); - this.replacePlaceholders = () => this.$backgroundMaskForeground.html( this.escapedMessage().replace( tagPattern, match => `${match}` ) ); - this.hint = () => this.$placeHolderHint.html( - getPlaceholderHint(this.listPlaceholdersWithoutBrackets()) - ); - - this.update = () => ( - this.replacePlaceholders() && this.resize() && this.hint() - ); - - this.demo = () => ( - this.$textbox.val((i, current) => `Dear ((name)), ${current}`) && this.update() - ); + this.update = () => this.replacePlaceholders() && this.resize(); }; diff --git a/app/assets/javascripts/placeholderHint.js b/app/assets/javascripts/placeholderHint.js new file mode 100644 index 000000000..bf73cd805 --- /dev/null +++ b/app/assets/javascripts/placeholderHint.js @@ -0,0 +1,75 @@ +(function(Modules) { + "use strict"; + + if ( + !('oninput' in document.createElement('input')) + ) return; + + const tagPattern = /\(\([^\)\(]+\)\)/g; + + Modules.PlaceholderHint = function() { + + this.start = function(component) { + + this.$component = $(component); + this.originalHTML = this.$component.html(); + this.$allTextboxes = $(this.$component.data('textboxes-selector')); + this.$targetTextbox = $(this.$component.data('target-textbox-selector')); + + this.$component + .on('click', '.placeholder-hint-action', this.demo); + + this.$allTextboxes + .on('input', this.hint) + .trigger('input'); + + }; + + this.getPlaceholderHint = function() { + + let placeholders = this.listPlaceholdersWithoutBrackets(); + + if (0 === placeholders.length) { + return ` + ${this.originalHTML} + Show me how + `; + } + if (1 === placeholders.length) { + return ` + ${this.originalHTML} +

You’ll populate the ‘${placeholders[0]}’ field when you send messages using this template

+ `; + } + return ` + ${this.originalHTML} +

You’ll populate your fields when you send some messages

+ `; + + }; + + this.escapedMessages = () => $('
').text( + this.$allTextboxes.map(function() { + return $(this).val(); + }).get() + ).html(); + + this.listPlaceholders = () => this.escapedMessages().match(tagPattern) || []; + + this.listPlaceholdersWithoutBrackets = () => this.listPlaceholders().map( + placeholder => placeholder.substring(2, placeholder.length - 2) + ); + + this.renderDemo = () => this.$targetTextbox.val((i, current) => `Dear ((name)), ${current}`); + + this.hint = () => this.$component.html( + this.getPlaceholderHint() + ); + + this.demo = () => ( + this.renderDemo() && this.$targetTextbox.trigger('input') && this.hint() + ); + + }; + +})(window.GOVUK.Modules); diff --git a/app/templates/views/edit-email-template.html b/app/templates/views/edit-email-template.html index bca5d1ab1..87d5f5d78 100644 --- a/app/templates/views/edit-email-template.html +++ b/app/templates/views/edit-email-template.html @@ -27,8 +27,8 @@ ) }}
-