From c430eef822c683cb78fc888bf69ed1372b487781 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Fri, 9 Oct 2020 21:36:45 +0200 Subject: [PATCH] Add `attributes` option to the Modal.open method. Closes #2592 --- src/modal_dialog/index.js | 3 ++- src/modal_dialog/view/ModalView.js | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/modal_dialog/index.js b/src/modal_dialog/index.js index 8201917f3..e9d6b8503 100644 --- a/src/modal_dialog/index.js +++ b/src/modal_dialog/index.js @@ -87,12 +87,13 @@ export default () => { * @param {Object} [opts={}] Options * @param {String|HTMLElement} [opts.title] Title to set for the modal * @param {String|HTMLElement} [opts.content] Content to set for the modal + * @param {Object} [opts.attributes] Updates the modal wrapper with custom attributes * @return {this} */ open(opts = {}) { opts.title && this.setTitle(opts.title); opts.content && this.setContent(opts.content); - modal.show(); + modal.show(opts); return this; }, diff --git a/src/modal_dialog/view/ModalView.js b/src/modal_dialog/view/ModalView.js index 797e4f3d9..414c92553 100644 --- a/src/modal_dialog/view/ModalView.js +++ b/src/modal_dialog/view/ModalView.js @@ -115,19 +115,28 @@ export default Backbone.View.extend({ * Show modal * @private * */ - show() { + show(opts = {}) { this.model.set('open', 1); + this.updateAttr(opts.attributes); + }, + + updateAttr(attr) { + const { pfx, $el, el } = this; + const currAttr = [].slice.call(el.attributes).map(i => i.name); + $el.removeAttr(currAttr.join(' ')); + $el.attr({ + ...(attr || {}), + class: `${pfx}container ${(attr && attr.class) || ''}`.trim() + }); }, render() { const el = this.$el; - const pfx = this.pfx; - const ppfx = this.ppfx; const obj = this.model.toJSON(); obj.pfx = this.pfx; obj.ppfx = this.ppfx; el.html(this.template(obj)); - el.attr('class', `${pfx}container`); + this.updateAttr(); this.updateOpen(); return this; }