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
47 * Fire when the top 'x' close button is pressed.
48 * @param {Roo.bootstrap.Modal} this
49 * @param {Roo.EventObject} e
53 this.buttons = this.buttons || [];
56 this.tmpl = Roo.factory(this.tmpl);
61 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
63 title : 'test dialog',
75 buttonPosition: 'right',
98 onRender : function(ct, position)
100 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
103 var cfg = Roo.apply({}, this.getAutoCreate());
106 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
108 //if (!cfg.name.length) {
112 cfg.cls += ' ' + this.cls;
115 cfg.style = this.style;
117 this.el = Roo.get(document.body).createChild(cfg, position);
119 //var type = this.el.dom.type;
122 if(this.tabIndex !== undefined){
123 this.el.dom.setAttribute('tabIndex', this.tabIndex);
126 this.dialogEl = this.el.select('.modal-dialog',true).first();
127 this.bodyEl = this.el.select('.modal-body',true).first();
128 this.closeEl = this.el.select('.modal-header .close', true).first();
129 this.headerEl = this.el.select('.modal-header',true).first();
130 this.titleEl = this.el.select('.modal-title',true).first();
131 this.footerEl = this.el.select('.modal-footer',true).first();
133 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
135 //this.el.addClass("x-dlg-modal");
137 if (this.buttons.length) {
138 Roo.each(this.buttons, function(bb) {
139 var b = Roo.apply({}, bb);
140 b.xns = b.xns || Roo.bootstrap;
141 b.xtype = b.xtype || 'Button';
142 if (typeof(b.listeners) == 'undefined') {
143 b.listeners = { click : this.onButtonClick.createDelegate(this) };
146 var btn = Roo.factory(b);
148 btn.render(this.getButtonContainer());
152 // render the children.
155 if(typeof(this.items) != 'undefined'){
156 var items = this.items;
159 for(var i =0;i < items.length;i++) {
160 nitems.push(this.addxtype(Roo.apply({}, items[i])));
166 // where are these used - they used to be body/close/footer
170 //this.el.addClass([this.fieldClass, this.cls]);
174 getAutoCreate : function()
178 html : this.html || ''
187 if(this.specificTitle){
193 if (this.allow_close && Roo.bootstrap.version == 3) {
203 if (this.allow_close && Roo.bootstrap.version == 4) {
213 if(this.size.length){
214 size = 'modal-' + this.size;
217 var footer = Roo.bootstrap.version == 3 ?
219 cls : 'modal-footer',
223 cls: 'btn-' + this.buttonPosition
228 { // BS4 uses mr-auto on left buttons....
240 cls: "modal-dialog " + size,
243 cls : "modal-content",
246 cls : 'modal-header',
261 modal.cls += ' fade';
267 getChildContainer : function() {
272 getButtonContainer : function() {
274 return Roo.bootstrap.version == 4 ?
275 this.el.select('.modal-footer',true).first()
276 : this.el.select('.modal-footer div',true).first();
279 initEvents : function()
281 if (this.allow_close) {
282 this.closeEl.on('click', this.hide, this);
284 Roo.EventManager.onWindowResize(this.resize, this, true);
293 Roo.lib.Dom.getViewWidth(true),
294 Roo.lib.Dom.getViewHeight(true)
297 if (this.fitwindow) {
301 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
302 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
307 if(this.max_width !== 0) {
309 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
312 this.setSize(w, this.height);
316 if(this.max_height) {
317 this.setSize(w,Math.min(
319 Roo.lib.Dom.getViewportHeight(true) - 60
325 if(!this.fit_content) {
326 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
330 this.setSize(w, Math.min(
332 this.headerEl.getHeight() +
333 this.footerEl.getHeight() +
334 this.getChildHeight(this.bodyEl.dom.childNodes),
335 Roo.lib.Dom.getViewportHeight(true) - 60)
341 setSize : function(w,h)
352 if (!this.rendered) {
356 //this.el.setStyle('display', 'block');
357 this.el.removeClass('hideing');
358 this.el.dom.style.display='block';
360 Roo.get(document.body).addClass('modal-open');
362 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
365 this.el.addClass('show');
366 this.el.addClass('in');
369 this.el.addClass('show');
370 this.el.addClass('in');
373 // not sure how we can show data in here..
375 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
378 Roo.get(document.body).addClass("x-body-masked");
380 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
381 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
382 this.maskEl.dom.style.display = 'block';
383 this.maskEl.addClass('show');
388 this.fireEvent('show', this);
390 // set zindex here - otherwise it appears to be ignored...
391 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
394 this.items.forEach( function(e) {
395 e.layout ? e.layout() : false;
403 if(this.fireEvent("beforehide", this) !== false){
405 this.maskEl.removeClass('show');
407 this.maskEl.dom.style.display = '';
408 Roo.get(document.body).removeClass("x-body-masked");
409 this.el.removeClass('in');
410 this.el.select('.modal-dialog', true).first().setStyle('transform','');
412 if(this.animate){ // why
413 this.el.addClass('hideing');
414 this.el.removeClass('show');
416 if (!this.el.hasClass('hideing')) {
417 return; // it's been shown again...
420 this.el.dom.style.display='';
422 Roo.get(document.body).removeClass('modal-open');
423 this.el.removeClass('hideing');
427 this.el.removeClass('show');
428 this.el.dom.style.display='';
429 Roo.get(document.body).removeClass('modal-open');
432 this.fireEvent('hide', this);
435 isVisible : function()
438 return this.el.hasClass('show') && !this.el.hasClass('hideing');
442 addButton : function(str, cb)
446 var b = Roo.apply({}, { html : str } );
447 b.xns = b.xns || Roo.bootstrap;
448 b.xtype = b.xtype || 'Button';
449 if (typeof(b.listeners) == 'undefined') {
450 b.listeners = { click : cb.createDelegate(this) };
453 var btn = Roo.factory(b);
455 btn.render(this.getButtonContainer());
461 setDefaultButton : function(btn)
463 //this.el.select('.modal-footer').()
466 resizeTo: function(w,h)
468 this.dialogEl.setWidth(w);
470 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
472 this.bodyEl.setHeight(h - diff);
474 this.fireEvent('resize', this);
477 setContentSize : function(w, h)
481 onButtonClick: function(btn,e)
484 this.fireEvent('btnclick', btn.name, e);
487 * Set the title of the Dialog
488 * @param {String} str new Title
490 setTitle: function(str) {
491 this.titleEl.dom.innerHTML = str;
494 * Set the body of the Dialog
495 * @param {String} str new Title
497 setBody: function(str) {
498 this.bodyEl.dom.innerHTML = str;
501 * Set the body of the Dialog using the template
502 * @param {Obj} data - apply this data to the template and replace the body contents.
504 applyBody: function(obj)
507 Roo.log("Error - using apply Body without a template");
510 this.tmpl.overwrite(this.bodyEl, obj);
513 getChildHeight : function(child_nodes)
517 child_nodes.length == 0
522 var child_height = 0;
524 for(var i = 0; i < child_nodes.length; i++) {
527 * for modal with tabs...
528 if(child_nodes[i].classList.contains('roo-layout-panel')) {
530 var layout_childs = child_nodes[i].childNodes;
532 for(var j = 0; j < layout_childs.length; j++) {
534 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
536 var layout_body_childs = layout_childs[j].childNodes;
538 for(var k = 0; k < layout_body_childs.length; k++) {
540 if(layout_body_childs[k].classList.contains('navbar')) {
541 child_height += layout_body_childs[k].offsetHeight;
545 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
547 var layout_body_tab_childs = layout_body_childs[k].childNodes;
549 for(var m = 0; m < layout_body_tab_childs.length; m++) {
551 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
552 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
567 child_height += child_nodes[i].offsetHeight;
568 // Roo.log(child_nodes[i].offsetHeight);
577 Roo.apply(Roo.bootstrap.Modal, {
579 * Button config that displays a single OK button
588 * Button config that displays Yes and No buttons
604 * Button config that displays OK and Cancel buttons
619 * Button config that displays Yes, No and Cancel buttons