diff --git a/app/assets/javascripts/enhancedTextbox.js b/app/assets/javascripts/enhancedTextbox.js new file mode 100644 index 000000000..8adb210f8 --- /dev/null +++ b/app/assets/javascripts/enhancedTextbox.js @@ -0,0 +1,85 @@ +(function(Modules) { + "use strict"; + + if ( + !('oninput' in document.createElement('input')) + ) return; + + const tagPattern = /\(\(([^\)\((\?)]+)(\?\?)?([^\)\(]*)\)\)/g; + + Modules.EnhancedTextbox = function() { + + this.start = function(textarea) { + + let visibleTextbox; + + this.highlightPlaceholders = ( + typeof textarea.data('highlightPlaceholders') === 'undefined' || + !!textarea.data('highlightPlaceholders') + ); + + this.$textbox = $(textarea) + .wrap(` +
+ `) + .after(this.$background = $(` + + `)) + .on("input", this.update); + + visibleTextbox = this.$textbox.clone().appendTo("body").css({ + position: 'absolute', + visibility: 'hidden', + display: 'block' + }); + this.initialHeight = visibleTextbox.height(); + + this.$background.css({ + 'border-width': this.$textbox.css('border-width') + }); + + visibleTextbox.remove(); + + this.$textbox + .trigger("input"); + + }; + + this.resize = () => { + + this.$background.width(this.$textbox.outerWidth()); + + this.$textbox.height( + Math.max( + this.initialHeight, + this.$background.outerHeight() + ) + ); + + if ('stickAtBottomWhenScrolling' in GOVUK) { + GOVUK.stickAtBottomWhenScrolling.recalculate(); + } + + }; + + this.contentEscaped = () => $('').text(this.$textbox.val()).html(); + + this.contentReplaced = () => this.contentEscaped().replace( + tagPattern, (match, name, separator, value) => value && separator ? + `((${name}??${value}))` : + `((${name}${value}))` + ); + + this.update = () => { + + this.$background.html( + this.highlightPlaceholders ? this.contentReplaced() : this.contentEscaped() + ); + + this.resize(); + + }; + + }; + +})(window.GOVUK.Modules); diff --git a/app/assets/javascripts/highlightTags.js b/app/assets/javascripts/highlightTags.js deleted file mode 100644 index bc533d4b7..000000000 --- a/app/assets/javascripts/highlightTags.js +++ /dev/null @@ -1,64 +0,0 @@ -(function(Modules) { - "use strict"; - - if ( - !('oninput' in document.createElement('input')) - ) return; - - const tagPattern = /\(\(([^\)\((\?)]+)(\?\?)?([^\)\(]*)\)\)/g; - - Modules.HighlightTags = function() { - - this.start = function(textarea) { - - this.$textbox = $(textarea) - .wrap(` - - `) - .after(this.$background = $(` - - `)) - .on("input", this.update); - - this.initialHeight = this.$textbox.height(); - - this.$background.css({ - 'width': this.$textbox.outerWidth(), - 'border-width': this.$textbox.css('border-width') - }); - - this.$textbox - .trigger("input"); - - }; - - this.resize = () => { - - this.$textbox.height( - Math.max( - this.initialHeight, - this.$background.outerHeight() - ) - ); - - if ('stickAtBottomWhenScrolling' in GOVUK) { - GOVUK.stickAtBottomWhenScrolling.recalculate(); - } - - }; - - this.escapedMessage = () => $('').text(this.$textbox.val()).html(); - - this.replacePlaceholders = () => this.$background.html( - this.escapedMessage().replace( - tagPattern, (match, name, separator, value) => value && separator ? - `((${name}??${value}))` : - `((${name}${value}))` - ) - ); - - this.update = () => this.replacePlaceholders() && this.resize(); - - }; - -})(window.GOVUK.Modules); diff --git a/app/templates/components/textbox.html b/app/templates/components/textbox.html index fc14d6dc4..6929de938 100644 --- a/app/templates/components/textbox.html +++ b/app/templates/components/textbox.html @@ -2,8 +2,9 @@ field, label=None, hint=False, - highlight_tags=False, + highlight_placeholders=False, autofocus=False, + autosize=False, colour_preview=False, help_link=None, help_link_text=None, @@ -34,7 +35,7 @@ {% endif %} {% - if highlight_tags + if highlight_placeholders or autosize %} {% set field_class = 'form-control-{} textbox-highlight-textbox'.format(width) %} {% else %} @@ -47,7 +48,8 @@ %} {{ field( class=field_class, - data_module='highlight-tags' if highlight_tags else '', + data_module='enhanced-textbox' if highlight_placeholders or autosize else '', + data_highlight_placeholders='true' if highlight_placeholders else 'false', rows=rows|string, **kwargs ) }} diff --git a/app/templates/views/edit-email-template.html b/app/templates/views/edit-email-template.html index d09a81d99..ebe027946 100644 --- a/app/templates/views/edit-email-template.html +++ b/app/templates/views/edit-email-template.html @@ -20,8 +20,8 @@Leave your details below if you’d like a response.
diff --git a/app/templates/views/support/report-problem.html b/app/templates/views/support/report-problem.html index 002811a30..12f40e2c1 100644 --- a/app/templates/views/support/report-problem.html +++ b/app/templates/views/support/report-problem.html @@ -24,7 +24,7 @@