Browse Source

Refactor Modal module

pull/3671/head
Artur Arseniev 5 years ago
parent
commit
f9a7a0d66d
  1. 13
      src/modal_dialog/config/config.js
  2. 8
      src/modal_dialog/index.js
  3. 24
      src/modal_dialog/model/Modal.js
  4. 47
      src/modal_dialog/view/ModalView.js
  5. 1
      test/specs/modal/index.js

13
src/modal_dialog/config/config.js

@ -5,5 +5,16 @@ export default {
content: '', content: '',
backdrop: true backdrop: true,
/**
* Extend ModalView object (view/ModalView.js)
* @example
* extend: {
* template() {
* return '<div>...New modal template...</div>';
* },
* },
*/
extend: {}
}; };

8
src/modal_dialog/index.js

@ -89,7 +89,8 @@ export default () => {
open(opts = {}) { open(opts = {}) {
opts.title && this.setTitle(opts.title); opts.title && this.setTitle(opts.title);
opts.content && this.setContent(opts.content); opts.content && this.setContent(opts.content);
modal.show(opts); model.open();
modal && modal.updateAttr(opts.attributes);
return this; return this;
}, },
@ -98,7 +99,7 @@ export default () => {
* @return {this} * @return {this}
*/ */
close() { close() {
modal.hide(); model.close();
return this; return this;
}, },
@ -197,8 +198,9 @@ export default () => {
* @private * @private
*/ */
render() { render() {
const View = ModalView.extend(c.extend);
modal && modal.remove(); modal && modal.remove();
modal = new ModalView({ modal = new View({
model, model,
config: c config: c
}); });

24
src/modal_dialog/model/Modal.js

@ -1,9 +1,19 @@
import Backbone from 'backbone'; import { Model } from 'backbone';
export default Backbone.Model.extend({ export default class Modal extends Model {
defaults: { defaults() {
title: '', return {
content: '', title: '',
open: false content: '',
open: false
};
} }
});
open() {
this.set('open', true);
}
close() {
this.set('open', false);
}
}

47
src/modal_dialog/view/ModalView.js

@ -1,6 +1,6 @@
import Backbone from 'backbone'; import { View } from 'backbone';
export default Backbone.View.extend({ export default class ModalView extends View {
template({ pfx, ppfx, content, title }) { template({ pfx, ppfx, content, title }) {
return `<div class="${pfx}dialog ${ppfx}one-bg ${ppfx}two-color"> return `<div class="${pfx}dialog ${ppfx}one-bg ${ppfx}two-color">
<div class="${pfx}header"> <div class="${pfx}header">
@ -13,12 +13,14 @@ export default Backbone.View.extend({
</div> </div>
</div> </div>
<div class="${pfx}collector" style="display: none"></div>`; <div class="${pfx}collector" style="display: none"></div>`;
}, }
events: { events() {
click: 'onClick', return {
'click [data-close-modal]': 'hide' click: 'onClick',
}, 'click [data-close-modal]': 'hide'
};
}
initialize(o) { initialize(o) {
const model = this.model; const model = this.model;
@ -30,12 +32,12 @@ export default Backbone.View.extend({
this.listenTo(model, 'change:open', this.updateOpen); this.listenTo(model, 'change:open', this.updateOpen);
this.listenTo(model, 'change:title', this.updateTitle); this.listenTo(model, 'change:title', this.updateTitle);
this.listenTo(model, 'change:content', this.updateContent); this.listenTo(model, 'change:content', this.updateContent);
}, }
onClick(e) { onClick(e) {
const bkd = this.config.backdrop; const bkd = this.config.backdrop;
bkd && e.target === this.el && this.hide(); bkd && e.target === this.el && this.hide();
}, }
/** /**
* Returns collector element * Returns collector element
@ -46,7 +48,7 @@ export default Backbone.View.extend({
if (!this.$collector) if (!this.$collector)
this.$collector = this.$el.find('.' + this.pfx + 'collector'); this.$collector = this.$el.find('.' + this.pfx + 'collector');
return this.$collector; return this.$collector;
}, }
/** /**
* Returns content element * Returns content element
@ -61,7 +63,7 @@ export default Backbone.View.extend({
} }
return this.$content; return this.$content;
}, }
/** /**
* Returns title element * Returns title element
@ -71,7 +73,7 @@ export default Backbone.View.extend({
getTitle() { getTitle() {
if (!this.$title) this.$title = this.$el.find('.' + this.pfx + 'title'); if (!this.$title) this.$title = this.$el.find('.' + this.pfx + 'title');
return this.$title.get(0); return this.$title.get(0);
}, }
/** /**
* Update content * Update content
@ -84,7 +86,7 @@ export default Backbone.View.extend({
const body = this.model.get('content'); const body = this.model.get('content');
children.length && coll.append(children); children.length && coll.append(children);
content.empty().append(body); content.empty().append(body);
}, }
/** /**
* Update title * Update title
@ -93,7 +95,7 @@ export default Backbone.View.extend({
updateTitle() { updateTitle() {
var title = this.getTitle(); var title = this.getTitle();
if (title) title.innerHTML = this.model.get('title'); if (title) title.innerHTML = this.model.get('title');
}, }
/** /**
* Update open * Update open
@ -101,24 +103,23 @@ export default Backbone.View.extend({
* */ * */
updateOpen() { updateOpen() {
this.el.style.display = this.model.get('open') ? '' : 'none'; this.el.style.display = this.model.get('open') ? '' : 'none';
}, }
/** /**
* Hide modal * Hide modal
* @private * @private
* */ * */
hide() { hide() {
this.model.set('open', 0); this.model.close();
}, }
/** /**
* Show modal * Show modal
* @private * @private
* */ * */
show(opts = {}) { show() {
this.model.set('open', 1); this.model.open();
this.updateAttr(opts.attributes); }
},
updateAttr(attr) { updateAttr(attr) {
const { pfx, $el, el } = this; const { pfx, $el, el } = this;
@ -128,7 +129,7 @@ export default Backbone.View.extend({
...(attr || {}), ...(attr || {}),
class: `${pfx}container ${(attr && attr.class) || ''}`.trim() class: `${pfx}container ${(attr && attr.class) || ''}`.trim()
}); });
}, }
render() { render() {
const el = this.$el; const el = this.$el;
@ -140,4 +141,4 @@ export default Backbone.View.extend({
this.updateOpen(); this.updateOpen();
return this; return this;
} }
}); }

1
test/specs/modal/index.js

@ -1,5 +1,4 @@
import Modal from 'modal_dialog'; import Modal from 'modal_dialog';
import ModalView from './view/ModalView';
describe('Modal dialog', () => { describe('Modal dialog', () => {
describe('Main', () => { describe('Main', () => {

Loading…
Cancel
Save