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 {String} size (sm|lg) default empty
20 * @cfg {Number} max_width set the max width of modal
24 * Create a new Modal Dialog
25 * @param {Object} config The config object
28 Roo.bootstrap.Modal = function(config){
29 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
34 * The raw btnclick event for the button
35 * @param {Roo.EventObject} e
40 * Fire when dialog resize
41 * @param {Roo.bootstrap.Modal} this
42 * @param {Roo.EventObject} e
46 this.buttons = this.buttons || [];
49 this.tmpl = Roo.factory(this.tmpl);
54 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
56 title : 'test dialog',
68 buttonPosition: 'right',
91 onRender : function(ct, position)
93 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
96 var cfg = Roo.apply({}, this.getAutoCreate());
99 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
101 //if (!cfg.name.length) {
105 cfg.cls += ' ' + this.cls;
108 cfg.style = this.style;
110 this.el = Roo.get(document.body).createChild(cfg, position);
112 //var type = this.el.dom.type;
115 if(this.tabIndex !== undefined){
116 this.el.dom.setAttribute('tabIndex', this.tabIndex);
119 this.dialogEl = this.el.select('.modal-dialog',true).first();
120 this.bodyEl = this.el.select('.modal-body',true).first();
121 this.closeEl = this.el.select('.modal-header .close', true).first();
122 this.headerEl = this.el.select('.modal-header',true).first();
123 this.titleEl = this.el.select('.modal-title',true).first();
124 this.footerEl = this.el.select('.modal-footer',true).first();
126 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
128 //this.el.addClass("x-dlg-modal");
130 if (this.buttons.length) {
131 Roo.each(this.buttons, function(bb) {
132 var b = Roo.apply({}, bb);
133 b.xns = b.xns || Roo.bootstrap;
134 b.xtype = b.xtype || 'Button';
135 if (typeof(b.listeners) == 'undefined') {
136 b.listeners = { click : this.onButtonClick.createDelegate(this) };
139 var btn = Roo.factory(b);
141 btn.render(this.el.select('.modal-footer div').first());
145 // render the children.
148 if(typeof(this.items) != 'undefined'){
149 var items = this.items;
152 for(var i =0;i < items.length;i++) {
153 nitems.push(this.addxtype(Roo.apply({}, items[i])));
159 // where are these used - they used to be body/close/footer
163 //this.el.addClass([this.fieldClass, this.cls]);
167 getAutoCreate : function()
171 html : this.html || ''
180 if(this.specificTitle){
186 if (this.allow_close && Roo.bootstrap.version == 3) {
196 if (this.allow_close && Roo.bootstrap.version == 4) {
206 if(this.size.length){
207 size = 'modal-' + this.size;
210 var footer = Roo.bootstrap.version == 3 ?
212 cls : 'modal-footer',
216 cls: 'btn-' + this.buttonPosition
221 { // BS4 uses mr-auto on left buttons....
233 cls: "modal-dialog " + size,
236 cls : "modal-content",
239 cls : 'modal-header',
254 modal.cls += ' fade';
260 getChildContainer : function() {
265 getButtonContainer : function() {
267 return Roo.bootstrap.version == 4 ?
268 this.el.select('.modal-footer',true).first()
269 : this.el.select('.modal-footer div',true).first();
272 initEvents : function()
274 if (this.allow_close) {
275 this.closeEl.on('click', this.hide, this);
277 Roo.EventManager.onWindowResize(this.resize, this, true);
285 Roo.lib.Dom.getViewWidth(true),
286 Roo.lib.Dom.getViewHeight(true)
289 if (this.fitwindow) {
291 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
292 this.height || Roo.lib.Dom.getViewportHeight(true) - 60
297 if(this.max_width !== 0) {
299 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
302 this.setSize(w, this.height);
306 if(this.max_height) {
307 this.setSize(w,Math.min(
309 Roo.lib.Dom.getViewportHeight(true) - 60
315 if(!this.fit_content) {
316 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
320 this.setSize(w, Math.min(
322 this.headerEl.getHeight() +
323 this.footerEl.getHeight() +
324 this.getChildHeight(this.bodyEl.dom.childNodes),
325 Roo.lib.Dom.getViewportHeight(true) - 60)
331 setSize : function(w,h)
342 if (!this.rendered) {
346 //this.el.setStyle('display', 'block');
347 this.el.removeClass('hideing');
348 this.el.dom.style.display='block';
350 Roo.get(document.body).addClass('modal-open');
352 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
355 this.el.addClass('show');
356 this.el.addClass('in');
359 this.el.addClass('show');
360 this.el.addClass('in');
363 // not sure how we can show data in here..
365 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
368 Roo.get(document.body).addClass("x-body-masked");
370 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
371 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
372 this.maskEl.dom.style.display = 'block';
373 this.maskEl.addClass('show');
378 this.fireEvent('show', this);
380 // set zindex here - otherwise it appears to be ignored...
381 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
384 this.items.forEach( function(e) {
385 e.layout ? e.layout() : false;
393 if(this.fireEvent("beforehide", this) !== false){
395 this.maskEl.removeClass('show');
397 this.maskEl.dom.style.display = '';
398 Roo.get(document.body).removeClass("x-body-masked");
399 this.el.removeClass('in');
400 this.el.select('.modal-dialog', true).first().setStyle('transform','');
402 if(this.animate){ // why
403 this.el.addClass('hideing');
404 this.el.removeClass('show');
406 if (!this.el.hasClass('hideing')) {
407 return; // it's been shown again...
410 this.el.dom.style.display='';
412 Roo.get(document.body).removeClass('modal-open');
413 this.el.removeClass('hideing');
417 this.el.removeClass('show');
418 this.el.dom.style.display='';
419 Roo.get(document.body).removeClass('modal-open');
422 this.fireEvent('hide', this);
425 isVisible : function()
428 return this.el.hasClass('show') && !this.el.hasClass('hideing');
432 addButton : function(str, cb)
436 var b = Roo.apply({}, { html : str } );
437 b.xns = b.xns || Roo.bootstrap;
438 b.xtype = b.xtype || 'Button';
439 if (typeof(b.listeners) == 'undefined') {
440 b.listeners = { click : cb.createDelegate(this) };
443 var btn = Roo.factory(b);
445 btn.render(this.getButtonContainer());
451 setDefaultButton : function(btn)
453 //this.el.select('.modal-footer').()
457 resizeTo: function(w,h)
461 this.dialogEl.setWidth(w);
462 if (this.diff === false) {
463 this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
466 this.bodyEl.setHeight(h - this.diff);
468 this.fireEvent('resize', this);
471 setContentSize : function(w, h)
475 onButtonClick: function(btn,e)
478 this.fireEvent('btnclick', btn.name, e);
481 * Set the title of the Dialog
482 * @param {String} str new Title
484 setTitle: function(str) {
485 this.titleEl.dom.innerHTML = str;
488 * Set the body of the Dialog
489 * @param {String} str new Title
491 setBody: function(str) {
492 this.bodyEl.dom.innerHTML = str;
495 * Set the body of the Dialog using the template
496 * @param {Obj} data - apply this data to the template and replace the body contents.
498 applyBody: function(obj)
501 Roo.log("Error - using apply Body without a template");
504 this.tmpl.overwrite(this.bodyEl, obj);
507 getChildHeight : function(child_nodes)
511 child_nodes.length == 0
516 var child_height = 0;
518 for(var i = 0; i < child_nodes.length; i++) {
521 * for modal with tabs...
522 if(child_nodes[i].classList.contains('roo-layout-panel')) {
524 var layout_childs = child_nodes[i].childNodes;
526 for(var j = 0; j < layout_childs.length; j++) {
528 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
530 var layout_body_childs = layout_childs[j].childNodes;
532 for(var k = 0; k < layout_body_childs.length; k++) {
534 if(layout_body_childs[k].classList.contains('navbar')) {
535 child_height += layout_body_childs[k].offsetHeight;
539 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
541 var layout_body_tab_childs = layout_body_childs[k].childNodes;
543 for(var m = 0; m < layout_body_tab_childs.length; m++) {
545 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
546 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
561 child_height += child_nodes[i].offsetHeight;
562 // Roo.log(child_nodes[i].offsetHeight);
571 Roo.apply(Roo.bootstrap.Modal, {
573 * Button config that displays a single OK button
582 * Button config that displays Yes and No buttons
598 * Button config that displays OK and Cancel buttons
613 * Button config that displays Yes, No and Cancel buttons