popup.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. var domQuery = require('min-dom').query,
  3. domClasses = require('min-dom').classes,
  4. domify = require('min-dom').domify,
  5. bind = require('lodash/bind');
  6. /**
  7. * @class
  8. * @constructor
  9. */
  10. function Popup(options) {
  11. options = options || {};
  12. this.template = options.template || this.template;
  13. var el = this.el = domify(this.template);
  14. this.header = domQuery('.popup-header', el);
  15. this.body = domQuery('.popup-body', el);
  16. this.footer = domQuery('.popup-footer', el);
  17. document.body.appendChild(el);
  18. this._attachEvents();
  19. }
  20. Popup.prototype.template = '<div class="bpp-properties-panel-popup">' +
  21. '<div class="underlay"></div>' +
  22. '<div class="popup">' +
  23. '<button class="popup-close"><span>Close</span></button>' +
  24. '<div class="popup-header"></div>' +
  25. '<div class="popup-body"></div>' +
  26. '<div class="popup-footer"></div>' +
  27. '</div>' +
  28. '</div>';
  29. Popup.prototype._attachEvents = function() {
  30. var self = this;
  31. var events = this.events;
  32. var el = this.el;
  33. Object.keys(events).forEach(function(instruction) {
  34. var cb = bind(self[events[instruction]], self);
  35. var parts = instruction.split(' ');
  36. var evtName = parts.shift();
  37. var target = parts.length ? parts.shift() : false;
  38. target = target ? domQuery(target, el) : el;
  39. if (!target) { return; }
  40. target.addEventListener(evtName, cb);
  41. });
  42. };
  43. Popup.prototype._detachEvents = function() {
  44. var self = this;
  45. var events = this.events;
  46. var el = this.el;
  47. Object.keys(events).forEach(function(instruction) {
  48. var cb = bind(self[events[instruction]], self);
  49. var parts = instruction.split(' ');
  50. var evtName = parts.shift();
  51. var target = parts.length ? parts.shift() : false;
  52. target = target ? domQuery(target, el) : el;
  53. if (!target) { return; }
  54. target.removeEventListener(evtName, cb);
  55. });
  56. };
  57. Popup.prototype.events = {
  58. // 'keydown:esc': '_handleClose',
  59. 'click .underlay': '_handleClose',
  60. 'click .popup-close': '_handleClose'
  61. };
  62. Popup.prototype._handleClose = function(evt) {
  63. this.close();
  64. };
  65. Popup.prototype.open = function(content) {
  66. domClasses(this.el).add('open');
  67. };
  68. Popup.prototype.close = function() {
  69. domClasses(this.el).remove('open');
  70. };
  71. Popup.prototype.remove = function() {
  72. this._detachEvents();
  73. if (document.body.contains(this.el)) {
  74. document.body.removeChild(this.el);
  75. }
  76. };
  77. var popup;
  78. module.exports = function() {
  79. if (!popup) {
  80. popup = new Popup();
  81. }
  82. return popup;
  83. };