UNPKG

drapcode-builder

Version:

Drapcode Builder Library

104 lines (88 loc) 2.72 kB
import Backbone from 'backbone'; export default Backbone.View.extend({ template({ pfx, ppfx, content, title, top, bottom, left, right }) { return `<div class="${pfx}dialog ${ppfx}one-bg ${ppfx}two-color"> <div class="${pfx}header"> <div class="${pfx}title">${title}</div> <div class="${pfx}btn-close" data-close-popover>&Cross;</div> </div> <div class="${pfx}content"> <div id="${pfx}c">${content}</div> <div style="clear:both"></div> </div> </div> <div class="${pfx}collector" style="display: none"></div>`; }, events: { click: 'onClick', 'click [data-close-popover]': 'hide' }, initialize(o) { const model = this.model; const config = o.config || {}; const pfx = config.stylePrefix || ''; this.config = config; this.pfx = pfx; this.ppfx = config.pStylePrefix || ''; this.listenTo(model, 'change:open', this.updateOpen); this.listenTo(model, 'change:title', this.updateTitle); this.listenTo(model, 'change:content', this.updateContent); }, onClick(e) { const bkd = this.config.backdrop; bkd && e.target === this.el && this.hide(); }, getCollector() { if (!this.$collector) this.$collector = this.$el.find('.' + this.pfx + 'collector'); return this.$collector; }, getContent() { const pfx = this.pfx; if (!this.$content) { this.$content = this.$el.find(`.${pfx}content #${pfx}c`); } return this.$content; }, getTitle() { if (!this.$title) this.$title = this.$el.find('.' + this.pfx + 'title'); return this.$title.get(0); }, updateContent() { var content = this.getContent(); const children = content.children(); const coll = this.getCollector(); const body = this.model.get('content'); children.length && coll.append(children); content.empty().append(body); }, updateTitle() { var title = this.getTitle(); if (title) title.innerHTML = this.model.get('title'); }, updateOpen() { this.el.style.display = this.model.get('open') ? '' : 'none'; this.el.style.top = this.model.get('top') + 'px'; this.el.style.bottom = this.model.get('bottom') + 'px'; this.el.style.right = this.model.get('right') + 'px'; this.el.style.left = this.model.get('left') + 'px'; }, hide() { this.model.set('open', 0); }, show() { this.model.set('open', 1); }, 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.updateOpen(); return this; } });