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) {
299 var view_height = Roo.lib.Dom.getViewportHeight(true);
302 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
303 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
308 if(this.max_width !== 0) {
310 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
313 this.setSize(w, this.height);
317 if(this.max_height) {
318 this.setSize(w,Math.min(
320 Roo.lib.Dom.getViewportHeight(true) - 60
326 if(!this.fit_content) {
327 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
331 this.setSize(w, Math.min(
333 this.headerEl.getHeight() +
334 this.footerEl.getHeight() +
335 this.getChildHeight(this.bodyEl.dom.childNodes),
336 Roo.lib.Dom.getViewportHeight(true) - 60)
342 setSize : function(w,h)
353 if (!this.rendered) {
357 //this.el.setStyle('display', 'block');
358 this.el.removeClass('hideing');
359 this.el.dom.style.display='block';
361 Roo.get(document.body).addClass('modal-open');
363 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
366 this.el.addClass('show');
367 this.el.addClass('in');
370 this.el.addClass('show');
371 this.el.addClass('in');
374 // not sure how we can show data in here..
376 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
379 Roo.get(document.body).addClass("x-body-masked");
381 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
382 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
383 this.maskEl.dom.style.display = 'block';
384 this.maskEl.addClass('show');
389 this.fireEvent('show', this);
391 // set zindex here - otherwise it appears to be ignored...
392 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
395 this.items.forEach( function(e) {
396 e.layout ? e.layout() : false;
404 if(this.fireEvent("beforehide", this) !== false){
406 this.maskEl.removeClass('show');
408 this.maskEl.dom.style.display = '';
409 Roo.get(document.body).removeClass("x-body-masked");
410 this.el.removeClass('in');
411 this.el.select('.modal-dialog', true).first().setStyle('transform','');
413 if(this.animate){ // why
414 this.el.addClass('hideing');
415 this.el.removeClass('show');
417 if (!this.el.hasClass('hideing')) {
418 return; // it's been shown again...
421 this.el.dom.style.display='';
423 Roo.get(document.body).removeClass('modal-open');
424 this.el.removeClass('hideing');
428 this.el.removeClass('show');
429 this.el.dom.style.display='';
430 Roo.get(document.body).removeClass('modal-open');
433 this.fireEvent('hide', this);
436 isVisible : function()
439 return this.el.hasClass('show') && !this.el.hasClass('hideing');
443 addButton : function(str, cb)
447 var b = Roo.apply({}, { html : str } );
448 b.xns = b.xns || Roo.bootstrap;
449 b.xtype = b.xtype || 'Button';
450 if (typeof(b.listeners) == 'undefined') {
451 b.listeners = { click : cb.createDelegate(this) };
454 var btn = Roo.factory(b);
456 btn.render(this.getButtonContainer());
462 setDefaultButton : function(btn)
464 //this.el.select('.modal-footer').()
467 resizeTo: function(w,h)
469 this.dialogEl.setWidth(w);
471 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
473 this.bodyEl.setHeight(h - diff);
475 this.fireEvent('resize', this);
478 setContentSize : function(w, h)
482 onButtonClick: function(btn,e)
485 this.fireEvent('btnclick', btn.name, e);
488 * Set the title of the Dialog
489 * @param {String} str new Title
491 setTitle: function(str) {
492 this.titleEl.dom.innerHTML = str;
495 * Set the body of the Dialog
496 * @param {String} str new Title
498 setBody: function(str) {
499 this.bodyEl.dom.innerHTML = str;
502 * Set the body of the Dialog using the template
503 * @param {Obj} data - apply this data to the template and replace the body contents.
505 applyBody: function(obj)
508 Roo.log("Error - using apply Body without a template");
511 this.tmpl.overwrite(this.bodyEl, obj);
514 getChildHeight : function(child_nodes)
518 child_nodes.length == 0
523 var child_height = 0;
525 for(var i = 0; i < child_nodes.length; i++) {
528 * for modal with tabs...
529 if(child_nodes[i].classList.contains('roo-layout-panel')) {
531 var layout_childs = child_nodes[i].childNodes;
533 for(var j = 0; j < layout_childs.length; j++) {
535 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
537 var layout_body_childs = layout_childs[j].childNodes;
539 for(var k = 0; k < layout_body_childs.length; k++) {
541 if(layout_body_childs[k].classList.contains('navbar')) {
542 child_height += layout_body_childs[k].offsetHeight;
546 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
548 var layout_body_tab_childs = layout_body_childs[k].childNodes;
550 for(var m = 0; m < layout_body_tab_childs.length; m++) {
552 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
553 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
568 child_height += child_nodes[i].offsetHeight;
569 // Roo.log(child_nodes[i].offsetHeight);
578 Roo.apply(Roo.bootstrap.Modal, {
580 * Button config that displays a single OK button
589 * Button config that displays Yes and No buttons
605 * Button config that displays OK and Cancel buttons
620 * Button config that displays Yes, No and Cancel buttons