3 var $ = require('jquery');
4 var _ = require('lodash');
5 var Backbone = require('backbone');
6 var Marionette = require('backbone.marionette');
7 var modalTemplate = require('./tmpl/modal.hbs');
8 var ModalFooterButtonListView = require('./modal-footer-button-list-view');
9 var isCompact = require('../../utils/detect-compact');
11 require('../../template/helpers/all');
12 require('../behaviors/isomorphic');
13 require('@gitterhq/styleguide/css/components/modals.css');
15 var ModalView = Marionette.LayoutView.extend({
16 template: modalTemplate,
20 title: '.js-modal-title-text'
24 'click .close': 'hide',
25 'click [data-action=close]': 'hide'
29 modalBody: '#modal-body-region',
30 modalFooter: '#modal-footer-region'
33 initialize: function(options) {
35 // Backbone Collection or array of items
38 modalClassVariation: null,
41 _.extend(this.options, options);
43 this.view = this.options.view || this.view;
45 if (this.options.menuItems && this.options.menuItems.models) {
46 this.menuItemCollection = this.options.menuItems;
48 var menuItems = this.menuItems || this.options.menuItems || [];
49 if (typeof menuItems === 'function') {
50 menuItems = menuItems.call(this);
53 this.menuItemCollection = new Backbone.Collection(menuItems);
56 this.footerView = new ModalFooterButtonListView({
57 collection: this.menuItemCollection
59 this.listenTo(this.footerView, 'item:activate', this.onMenuItemActivated, this);
62 serializeData: function() {
64 title: this.options.title,
65 modalClassVariation: this.options.modalClassVariation
69 onMenuItemActivated: function(itemModel) {
70 var action = itemModel.get('action');
71 this.view.trigger('menuItemClicked', action);
72 this.trigger('menuItemClicked', action);
75 onRender: function() {
79 this.modalBody.show(this.view);
80 this.modalFooter.show(this.footerView);
82 if (!isCompact() && !this.disableAutoFocus) {
83 window.setTimeout(function() {
85 var v = self.$el.find(
86 'input[type=text], input[type=url], input[type=tel], input[type=number], input[type=color], input[type=email]'
96 } else if (!isCompact()) {
97 // Focus the modal overall so you can start navigating to the controls
102 setButtonState: function(name, state) {
103 var $s = this.$el.find('button[data-action=' + name + ']');
105 $s.removeAttr('disabled');
107 $s.attr('disabled', true);
111 toggleButtonClass: function(name, className, enabled) {
112 var $s = this.$el.find('button[data-action=' + name + ']');
113 $s.toggleClass(className, enabled);
116 hideActions: function() {
117 var $s = this.$el.find('.modal-footer .action');
121 showActions: function() {
122 var $s = this.$el.find('.modal-footer .action');
126 showPremium: function() {
127 var $s = this.$el.find('.premium');
128 $s.removeClass('hidden');
132 hidePremium: function() {
133 var $s = this.$el.find('.modal-footer .premium');
134 $s.addClass('hidden');
138 onDestroy: function() {
140 this.view.dialog = null;
143 prepare: function() {
144 if (!this.rendered) {
146 this.rendered = true;
151 this.view.dialog = this;
154 if (this.isShown) return;
158 $('body').addClass('modal-open');
161 this.$el.trigger('show');
162 this.trigger('show');
165 this.backdrop(function() {
166 if (!that.$el.parent().length) {
167 that.$el.appendTo(that.$backdrop); //don't move modals dom position
171 that.$el.addClass('in');
172 that.$el.trigger('shown');
173 that.trigger('shown');
178 if (e) e.preventDefault();
179 if (this.navigable) {
180 window.location = '#';
186 /* Called after navigation to destroy an navigable dialog box */
187 navigationalHide: function() {
191 hideInternal: function() {
192 if (!this.isShown) return;
194 this.isShown = false;
196 $('body').removeClass('modal-open');
200 this.$el.trigger('hide').removeClass('in');
202 this.trigger('hide');
207 hideModal: function() {
208 this.$el.hide().trigger('hidden');
210 this.trigger('hidden');
216 backdrop: function(callback) {
217 if (this.isShown && this.options.backdrop !== false) {
218 this.$backdrop = $('<div class="modal-backdrop" />').appendTo(document.body);
220 var bd = this.$backdrop;
221 this.$backdrop.click(function(e) {
222 if (e.target !== this) return;
225 this.$backdrop.modal = this;
226 this.$backdrop.addClass('in');
229 } else if (!this.isShown && this.$backdrop) {
230 this.$backdrop.removeClass('in');
232 this.removeBackdrop();
233 } else if (callback) {
238 removeBackdrop: function() {
239 this.$backdrop.remove();
240 this.$backdrop = null;
246 $(document).on('keydown', keydown);
247 } else if (!this.isShown) {
248 $(document).off('keydown', keydown);
251 function keydown(e) {
252 if (e.which === 27) that.hide();
257 module.exports = ModalView;