7 * @class Roo.bootstrap.Modal
8 * @extends Roo.bootstrap.Component
9 * Bootstrap Modal class
10 * @cfg {String} title Title of dialog
11 * @cfg {String} html - the body of the dialog (for simple ones) - you can also use template..
12 * @cfg {Roo.Template} tmpl - a template with variables. to use it, add a handler in show:method adn
13 * @cfg {Boolean} specificTitle default false
14 * @cfg {Array} buttons Array of buttons or standard button set..
15 * @cfg {String} buttonPosition (left|right|center) default right (DEPRICATED) - use mr-auto on buttons to put them on the left
16 * @cfg {Boolean} animate default true
17 * @cfg {Boolean} allow_close default true
18 * @cfg {Boolean} fitwindow default false
19 * @cfg {Number} width fixed width - usefull for chrome extension only really.
20 * @cfg {Number} height fixed height - usefull for chrome extension only really.
21 * @cfg {String} size (sm|lg) default empty
22 * @cfg {Number} max_width set the max width of modal
26 * Create a new Modal Dialog
27 * @param {Object} config The config object
30 Roo.bootstrap.Modal = function(config){
31 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
36 * The raw btnclick event for the button
37 * @param {Roo.EventObject} e
42 * Fire when dialog resize
43 * @param {Roo.bootstrap.Modal} this
44 * @param {Roo.EventObject} e
48 this.buttons = this.buttons || [];
51 this.tmpl = Roo.factory(this.tmpl);
56 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
58 title : 'test dialog',
70 buttonPosition: 'right',
93 onRender : function(ct, position)
95 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
98 var cfg = Roo.apply({}, this.getAutoCreate());
101 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
103 //if (!cfg.name.length) {
107 cfg.cls += ' ' + this.cls;
110 cfg.style = this.style;
112 this.el = Roo.get(document.body).createChild(cfg, position);
114 //var type = this.el.dom.type;
117 if(this.tabIndex !== undefined){
118 this.el.dom.setAttribute('tabIndex', this.tabIndex);
121 this.dialogEl = this.el.select('.modal-dialog',true).first();
122 this.bodyEl = this.el.select('.modal-body',true).first();
123 this.closeEl = this.el.select('.modal-header .close', true).first();
124 this.headerEl = this.el.select('.modal-header',true).first();
125 this.titleEl = this.el.select('.modal-title',true).first();
126 this.footerEl = this.el.select('.modal-footer',true).first();
128 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
130 //this.el.addClass("x-dlg-modal");
132 if (this.buttons.length) {
133 Roo.each(this.buttons, function(bb) {
134 var b = Roo.apply({}, bb);
135 b.xns = b.xns || Roo.bootstrap;
136 b.xtype = b.xtype || 'Button';
137 if (typeof(b.listeners) == 'undefined') {
138 b.listeners = { click : this.onButtonClick.createDelegate(this) };
141 var btn = Roo.factory(b);
143 btn.render(this.getButtonContainer());
147 // render the children.
150 if(typeof(this.items) != 'undefined'){
151 var items = this.items;
154 for(var i =0;i < items.length;i++) {
155 nitems.push(this.addxtype(Roo.apply({}, items[i])));
161 // where are these used - they used to be body/close/footer
165 //this.el.addClass([this.fieldClass, this.cls]);
169 getAutoCreate : function()
171 // we will default to modal-body-overflow - might need to remove or make optional later.
173 cls : 'modal-body enable-modal-body-overflow ',
174 html : this.html || ''
183 if(this.specificTitle){
189 if (this.allow_close && Roo.bootstrap.version == 3) {
199 if (this.allow_close && Roo.bootstrap.version == 4) {
209 if(this.size.length){
210 size = 'modal-' + this.size;
213 var footer = Roo.bootstrap.version == 3 ?
215 cls : 'modal-footer',
219 cls: 'btn-' + this.buttonPosition
224 { // BS4 uses mr-auto on left buttons....
236 cls: "modal-dialog " + size,
239 cls : "modal-content",
242 cls : 'modal-header',
257 modal.cls += ' fade';
263 getChildContainer : function() {
268 getButtonContainer : function() {
270 return Roo.bootstrap.version == 4 ?
271 this.el.select('.modal-footer',true).first()
272 : this.el.select('.modal-footer div',true).first();
275 initEvents : function()
277 if (this.allow_close) {
278 this.closeEl.on('click', this.hide, this);
280 Roo.EventManager.onWindowResize(this.resize, this, true);
289 Roo.lib.Dom.getViewWidth(true),
290 Roo.lib.Dom.getViewHeight(true)
293 if (this.fitwindow) {
297 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
298 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
303 if(this.max_width !== 0) {
305 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
308 this.setSize(w, this.height);
312 if(this.max_height) {
313 this.setSize(w,Math.min(
315 Roo.lib.Dom.getViewportHeight(true) - 60
321 if(!this.fit_content) {
322 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
326 this.setSize(w, Math.min(
328 this.headerEl.getHeight() +
329 this.footerEl.getHeight() +
330 this.getChildHeight(this.bodyEl.dom.childNodes),
331 Roo.lib.Dom.getViewportHeight(true) - 60)
337 setSize : function(w,h)
348 if (!this.rendered) {
352 //this.el.setStyle('display', 'block');
353 this.el.removeClass('hideing');
354 this.el.dom.style.display='block';
356 Roo.get(document.body).addClass('modal-open');
358 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
361 this.el.addClass('show');
362 this.el.addClass('in');
365 this.el.addClass('show');
366 this.el.addClass('in');
369 // not sure how we can show data in here..
371 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
374 Roo.get(document.body).addClass("x-body-masked");
376 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
377 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
378 this.maskEl.dom.style.display = 'block';
379 this.maskEl.addClass('show');
384 this.fireEvent('show', this);
386 // set zindex here - otherwise it appears to be ignored...
387 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
390 this.items.forEach( function(e) {
391 e.layout ? e.layout() : false;
399 if(this.fireEvent("beforehide", this) !== false){
401 this.maskEl.removeClass('show');
403 this.maskEl.dom.style.display = '';
404 Roo.get(document.body).removeClass("x-body-masked");
405 this.el.removeClass('in');
406 this.el.select('.modal-dialog', true).first().setStyle('transform','');
408 if(this.animate){ // why
409 this.el.addClass('hideing');
410 this.el.removeClass('show');
412 if (!this.el.hasClass('hideing')) {
413 return; // it's been shown again...
416 this.el.dom.style.display='';
418 Roo.get(document.body).removeClass('modal-open');
419 this.el.removeClass('hideing');
423 this.el.removeClass('show');
424 this.el.dom.style.display='';
425 Roo.get(document.body).removeClass('modal-open');
428 this.fireEvent('hide', this);
431 isVisible : function()
434 return this.el.hasClass('show') && !this.el.hasClass('hideing');
438 addButton : function(str, cb)
442 var b = Roo.apply({}, { html : str } );
443 b.xns = b.xns || Roo.bootstrap;
444 b.xtype = b.xtype || 'Button';
445 if (typeof(b.listeners) == 'undefined') {
446 b.listeners = { click : cb.createDelegate(this) };
449 var btn = Roo.factory(b);
451 btn.render(this.getButtonContainer());
457 setDefaultButton : function(btn)
459 //this.el.select('.modal-footer').()
462 resizeTo: function(w,h)
464 this.dialogEl.setWidth(w);
466 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
468 this.bodyEl.setHeight(h - diff);
470 this.fireEvent('resize', this);
473 setContentSize : function(w, h)
477 onButtonClick: function(btn,e)
480 this.fireEvent('btnclick', btn.name, e);
483 * Set the title of the Dialog
484 * @param {String} str new Title
486 setTitle: function(str) {
487 this.titleEl.dom.innerHTML = str;
490 * Set the body of the Dialog
491 * @param {String} str new Title
493 setBody: function(str) {
494 this.bodyEl.dom.innerHTML = str;
497 * Set the body of the Dialog using the template
498 * @param {Obj} data - apply this data to the template and replace the body contents.
500 applyBody: function(obj)
503 Roo.log("Error - using apply Body without a template");
506 this.tmpl.overwrite(this.bodyEl, obj);
509 getChildHeight : function(child_nodes)
513 child_nodes.length == 0
518 var child_height = 0;
520 for(var i = 0; i < child_nodes.length; i++) {
523 * for modal with tabs...
524 if(child_nodes[i].classList.contains('roo-layout-panel')) {
526 var layout_childs = child_nodes[i].childNodes;
528 for(var j = 0; j < layout_childs.length; j++) {
530 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
532 var layout_body_childs = layout_childs[j].childNodes;
534 for(var k = 0; k < layout_body_childs.length; k++) {
536 if(layout_body_childs[k].classList.contains('navbar')) {
537 child_height += layout_body_childs[k].offsetHeight;
541 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
543 var layout_body_tab_childs = layout_body_childs[k].childNodes;
545 for(var m = 0; m < layout_body_tab_childs.length; m++) {
547 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
548 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
563 child_height += child_nodes[i].offsetHeight;
564 // Roo.log(child_nodes[i].offsetHeight);
573 Roo.apply(Roo.bootstrap.Modal, {
575 * Button config that displays a single OK button
584 * Button config that displays Yes and No buttons
600 * Button config that displays OK and Cancel buttons
615 * Button config that displays Yes, No and Cancel buttons