Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reveal sensitive field if part of an AJAX request #5209

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions modules/backend/formwidgets/sensitive/assets/js/sensitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
this.options = options
this.clean = Boolean(this.$el.data('clean'))
this.hidden = true
this.triggerElement = null

this.$input = this.$el.find('[data-input]').first()
this.$toggle = this.$el.find('[data-toggle]').first()
Expand Down Expand Up @@ -50,6 +51,9 @@
if (this.$copy.length) {
this.$copy.on('click', this.proxy(this.onCopy))
}

$(window).on('ajaxSetup', this.proxy(this.onAjaxSetup))
$(window).on('oc.beforeRequest', this.proxy(this.onBeforeRequest))
}

Sensitive.prototype.dispose = function () {
Expand All @@ -64,6 +68,9 @@
this.$copy.off('click', this.proxy(this.onCopy))
}

$(window).off('ajaxSetup', this.proxy(this.onAjaxSetup))
$(window).off('oc.beforeRequest', this.proxy(this.onBeforeRequest))

this.$input = this.$toggle = this.$icon = this.$loader = null
this.$el = null

Expand Down Expand Up @@ -126,6 +133,54 @@
}
}

Sensitive.prototype.onAjaxSetup = function (event, context) {
// Don't track AJAX request if it is for revealing the value
if (context.handler === this.options.eventHandler) {
return
}
// Don't track AJAX requests if this field has been revealed
if (!this.clean) {
return
}
// Only track trigger elements if the AJAX request is for an element in the same form as this input
if (!this.$el.closest('form').is($(event.target).closest('form'))) {
return
}

this.triggerElement = event.target
}

Sensitive.prototype.onBeforeRequest = function (event, context) {
var that = this,
deferred = $.Deferred()

// Don't track AJAX request if it is for revealing the value
if (context.handler === this.options.eventHandler) {
return
}
// Don't track AJAX requests if this field has been revealed
if (!this.clean) {
return
}
// Only defer AJAX requests for elements in the same form as this input
if (!this.triggerElement || !this.$el.closest('form').is($(this.triggerElement).closest('form'))) {
return
}

// Prevent the initial AJAX request and re-call it after revealing the value
event.preventDefault()

deferred.then(function () {
if (!that.hidden) {
that.toggleVisibility()
}

$(that.triggerElement).request()
})

this.reveal(deferred)
}

Sensitive.prototype.toggleVisibility = function() {
if (this.hidden) {
this.$input.attr('type', 'text')
Expand Down