2015-12-20 00:00:01 +00:00
|
|
|
(function(Modules) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2015-12-20 00:00:01 +00:00
|
|
|
if (
|
|
|
|
|
!('oninput' in document.createElement('input'))
|
|
|
|
|
) return;
|
|
|
|
|
|
2016-07-12 12:48:09 +01:00
|
|
|
const tagPattern = /\(\(([^\)\((\?)]+)(\?\?)?([^\)\(]*)\)\)/g;
|
2015-12-20 00:00:01 +00:00
|
|
|
|
2019-10-16 15:20:05 +01:00
|
|
|
Modules.EnhancedTextbox = function() {
|
2015-12-20 00:00:01 +00:00
|
|
|
|
|
|
|
|
this.start = function(textarea) {
|
|
|
|
|
|
2019-10-16 14:35:18 +01:00
|
|
|
let visibleTextbox;
|
|
|
|
|
|
|
|
|
|
this.highlightPlaceholders = (
|
|
|
|
|
typeof textarea.data('highlightPlaceholders') === 'undefined' ||
|
|
|
|
|
!!textarea.data('highlightPlaceholders')
|
|
|
|
|
);
|
|
|
|
|
|
2015-12-20 00:00:01 +00:00
|
|
|
this.$textbox = $(textarea)
|
2015-12-20 00:00:01 +00:00
|
|
|
.wrap(`
|
|
|
|
|
<div class='textbox-highlight-wrapper' />
|
|
|
|
|
`)
|
2016-06-02 10:40:17 +01:00
|
|
|
.after(this.$background = $(`
|
2015-12-20 00:00:01 +00:00
|
|
|
<div class="textbox-highlight-background" aria-hidden="true" />
|
|
|
|
|
`))
|
2016-03-18 09:16:59 +00:00
|
|
|
.on("input", this.update);
|
2015-12-20 00:00:01 +00:00
|
|
|
|
2020-03-05 15:44:31 +00:00
|
|
|
$(window).on("resize", this.resize);
|
|
|
|
|
|
2019-10-16 14:35:18 +01:00
|
|
|
visibleTextbox = this.$textbox.clone().appendTo("body").css({
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
visibility: 'hidden',
|
|
|
|
|
display: 'block'
|
|
|
|
|
});
|
|
|
|
|
this.initialHeight = visibleTextbox.height();
|
2016-03-18 08:52:29 +00:00
|
|
|
|
2016-06-02 10:40:17 +01:00
|
|
|
this.$background.css({
|
2016-04-07 16:08:40 +01:00
|
|
|
'border-width': this.$textbox.css('border-width')
|
|
|
|
|
});
|
2016-02-08 09:59:04 +00:00
|
|
|
|
2019-10-16 14:35:18 +01:00
|
|
|
visibleTextbox.remove();
|
|
|
|
|
|
2015-12-20 00:00:01 +00:00
|
|
|
this.$textbox
|
|
|
|
|
.trigger("input");
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-14 13:30:12 +00:00
|
|
|
this.resize = () => {
|
|
|
|
|
|
2020-03-05 15:44:31 +00:00
|
|
|
this.$background.width(this.$textbox.width());
|
2019-10-16 14:35:18 +01:00
|
|
|
|
2019-01-14 13:30:12 +00:00
|
|
|
this.$textbox.height(
|
|
|
|
|
Math.max(
|
|
|
|
|
this.initialHeight,
|
|
|
|
|
this.$background.outerHeight()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ('stickAtBottomWhenScrolling' in GOVUK) {
|
|
|
|
|
GOVUK.stickAtBottomWhenScrolling.recalculate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
2016-03-18 08:52:29 +00:00
|
|
|
|
2019-10-16 14:35:18 +01:00
|
|
|
this.contentEscaped = () => $('<div/>').text(this.$textbox.val()).html();
|
2016-03-30 11:39:16 +01:00
|
|
|
|
2019-10-16 14:35:18 +01:00
|
|
|
this.contentReplaced = () => this.contentEscaped().replace(
|
|
|
|
|
tagPattern, (match, name, separator, value) => value && separator ?
|
|
|
|
|
`<span class='placeholder-conditional'>((${name}??</span>${value}))` :
|
|
|
|
|
`<span class='placeholder'>((${name}${value}))</span>`
|
2016-03-18 09:16:59 +00:00
|
|
|
);
|
2016-03-18 08:52:29 +00:00
|
|
|
|
2019-10-16 14:35:18 +01:00
|
|
|
this.update = () => {
|
|
|
|
|
|
|
|
|
|
this.$background.html(
|
|
|
|
|
this.highlightPlaceholders ? this.contentReplaced() : this.contentEscaped()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.resize();
|
|
|
|
|
|
|
|
|
|
};
|
2015-12-20 00:00:01 +00:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window.GOVUK.Modules);
|