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()
173 html : this.html || ''
182 if(this.specificTitle){
188 if (this.allow_close && Roo.bootstrap.version == 3) {
198 if (this.allow_close && Roo.bootstrap.version == 4) {
208 if(this.size.length){
209 size = 'modal-' + this.size;
212 var footer = Roo.bootstrap.version == 3 ?
214 cls : 'modal-footer',
218 cls: 'btn-' + this.buttonPosition
223 { // BS4 uses mr-auto on left buttons....
235 cls: "modal-dialog " + size,
238 cls : "modal-content",
241 cls : 'modal-header',
256 modal.cls += ' fade';
262 getChildContainer : function() {
267 getButtonContainer : function() {
269 return Roo.bootstrap.version == 4 ?
270 this.el.select('.modal-footer',true).first()
271 : this.el.select('.modal-footer div',true).first();
274 initEvents : function()
276 if (this.allow_close) {
277 this.closeEl.on('click', this.hide, this);
279 Roo.EventManager.onWindowResize(this.resize, this, true);
288 Roo.lib.Dom.getViewWidth(true),
289 Roo.lib.Dom.getViewHeight(true)
292 if (this.fitwindow) {
296 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
297 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
302 if(this.max_width !== 0) {
304 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
307 this.setSize(w, this.height);
311 if(this.max_height) {
312 this.setSize(w,Math.min(
314 Roo.lib.Dom.getViewportHeight(true) - 60
320 if(!this.fit_content) {
321 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
325 this.setSize(w, Math.min(
327 this.headerEl.getHeight() +
328 this.footerEl.getHeight() +
329 this.getChildHeight(this.bodyEl.dom.childNodes),
330 Roo.lib.Dom.getViewportHeight(true) - 60)
336 setSize : function(w,h)
347 if (!this.rendered) {
351 //this.el.setStyle('display', 'block');
352 this.el.removeClass('hideing');
353 this.el.dom.style.display='block';
355 Roo.get(document.body).addClass('modal-open');
357 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
360 this.el.addClass('show');
361 this.el.addClass('in');
364 this.el.addClass('show');
365 this.el.addClass('in');
368 // not sure how we can show data in here..
370 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
373 Roo.get(document.body).addClass("x-body-masked");
375 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
376 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
377 this.maskEl.dom.style.display = 'block';
378 this.maskEl.addClass('show');
383 this.fireEvent('show', this);
385 // set zindex here - otherwise it appears to be ignored...
386 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
389 this.items.forEach( function(e) {
390 e.layout ? e.layout() : false;
398 if(this.fireEvent("beforehide", this) !== false){
400 this.maskEl.removeClass('show');
402 this.maskEl.dom.style.display = '';
403 Roo.get(document.body).removeClass("x-body-masked");
404 this.el.removeClass('in');
405 this.el.select('.modal-dialog', true).first().setStyle('transform','');
407 if(this.animate){ // why
408 this.el.addClass('hideing');
409 this.el.removeClass('show');
411 if (!this.el.hasClass('hideing')) {
412 return; // it's been shown again...
415 this.el.dom.style.display='';
417 Roo.get(document.body).removeClass('modal-open');
418 this.el.removeClass('hideing');
422 this.el.removeClass('show');
423 this.el.dom.style.display='';
424 Roo.get(document.body).removeClass('modal-open');
427 this.fireEvent('hide', this);
430 isVisible : function()
433 return this.el.hasClass('show') && !this.el.hasClass('hideing');
437 addButton : function(str, cb)
441 var b = Roo.apply({}, { html : str } );
442 b.xns = b.xns || Roo.bootstrap;
443 b.xtype = b.xtype || 'Button';
444 if (typeof(b.listeners) == 'undefined') {
445 b.listeners = { click : cb.createDelegate(this) };
448 var btn = Roo.factory(b);
450 btn.render(this.getButtonContainer());
456 setDefaultButton : function(btn)
458 //this.el.select('.modal-footer').()
461 resizeTo: function(w,h)
463 this.dialogEl.setWidth(w);
465 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
467 this.bodyEl.setHeight(h - diff);
469 this.fireEvent('resize', this);
472 setContentSize : function(w, h)
476 onButtonClick: function(btn,e)
479 this.fireEvent('btnclick', btn.name, e);
482 * Set the title of the Dialog
483 * @param {String} str new Title
485 setTitle: function(str) {
486 this.titleEl.dom.innerHTML = str;
489 * Set the body of the Dialog
490 * @param {String} str new Title
492 setBody: function(str) {
493 this.bodyEl.dom.innerHTML = str;
496 * Set the body of the Dialog using the template
497 * @param {Obj} data - apply this data to the template and replace the body contents.
499 applyBody: function(obj)
502 Roo.log("Error - using apply Body without a template");
505 this.tmpl.overwrite(this.bodyEl, obj);
508 getChildHeight : function(child_nodes)
512 child_nodes.length == 0
517 var child_height = 0;
519 for(var i = 0; i < child_nodes.length; i++) {
522 * for modal with tabs...
523 if(child_nodes[i].classList.contains('roo-layout-panel')) {
525 var layout_childs = child_nodes[i].childNodes;
527 for(var j = 0; j < layout_childs.length; j++) {
529 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
531 var layout_body_childs = layout_childs[j].childNodes;
533 for(var k = 0; k < layout_body_childs.length; k++) {
535 if(layout_body_childs[k].classList.contains('navbar')) {
536 child_height += layout_body_childs[k].offsetHeight;
540 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
542 var layout_body_tab_childs = layout_body_childs[k].childNodes;
544 for(var m = 0; m < layout_body_tab_childs.length; m++) {
546 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
547 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
562 child_height += child_nodes[i].offsetHeight;
563 // Roo.log(child_nodes[i].offsetHeight);
572 Roo.apply(Roo.bootstrap.Modal, {
574 * Button config that displays a single OK button
583 * Button config that displays Yes and No buttons
599 * Button config that displays OK and Cancel buttons
614 * Button config that displays Yes, No and Cancel buttons