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.onClosePress, this);
284 Roo.EventManager.onWindowResize(this.resize, this, true);
289 onClosePress : function()
297 Roo.lib.Dom.getViewWidth(true),
298 Roo.lib.Dom.getViewHeight(true)
301 if (this.fitwindow) {
303 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
304 this.height || Roo.lib.Dom.getViewportHeight(true) - 60
309 if(this.max_width !== 0) {
311 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
314 this.setSize(w, this.height);
318 if(this.max_height) {
319 this.setSize(w,Math.min(
321 Roo.lib.Dom.getViewportHeight(true) - 60
327 if(!this.fit_content) {
328 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
332 this.setSize(w, Math.min(
334 this.headerEl.getHeight() +
335 this.footerEl.getHeight() +
336 this.getChildHeight(this.bodyEl.dom.childNodes),
337 Roo.lib.Dom.getViewportHeight(true) - 60)
343 setSize : function(w,h)
354 if (!this.rendered) {
358 //this.el.setStyle('display', 'block');
359 this.el.removeClass('hideing');
360 this.el.dom.style.display='block';
362 Roo.get(document.body).addClass('modal-open');
364 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
367 this.el.addClass('show');
368 this.el.addClass('in');
371 this.el.addClass('show');
372 this.el.addClass('in');
375 // not sure how we can show data in here..
377 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
380 Roo.get(document.body).addClass("x-body-masked");
382 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
383 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
384 this.maskEl.dom.style.display = 'block';
385 this.maskEl.addClass('show');
390 this.fireEvent('show', this);
392 // set zindex here - otherwise it appears to be ignored...
393 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
396 this.items.forEach( function(e) {
397 e.layout ? e.layout() : false;
405 if(this.fireEvent("beforehide", this) !== false){
407 this.maskEl.removeClass('show');
409 this.maskEl.dom.style.display = '';
410 Roo.get(document.body).removeClass("x-body-masked");
411 this.el.removeClass('in');
412 this.el.select('.modal-dialog', true).first().setStyle('transform','');
414 if(this.animate){ // why
415 this.el.addClass('hideing');
416 this.el.removeClass('show');
418 if (!this.el.hasClass('hideing')) {
419 return; // it's been shown again...
422 this.el.dom.style.display='';
424 Roo.get(document.body).removeClass('modal-open');
425 this.el.removeClass('hideing');
429 this.el.removeClass('show');
430 this.el.dom.style.display='';
431 Roo.get(document.body).removeClass('modal-open');
434 this.fireEvent('hide', this);
437 isVisible : function()
440 return this.el.hasClass('show') && !this.el.hasClass('hideing');
444 addButton : function(str, cb)
448 var b = Roo.apply({}, { html : str } );
449 b.xns = b.xns || Roo.bootstrap;
450 b.xtype = b.xtype || 'Button';
451 if (typeof(b.listeners) == 'undefined') {
452 b.listeners = { click : cb.createDelegate(this) };
455 var btn = Roo.factory(b);
457 btn.render(this.getButtonContainer());
463 setDefaultButton : function(btn)
465 //this.el.select('.modal-footer').()
469 resizeTo: function(w,h)
473 this.dialogEl.setWidth(w);
474 if (this.diff === false) {
475 this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
478 this.bodyEl.setHeight(h - this.diff);
480 this.fireEvent('resize', this);
483 setContentSize : function(w, h)
487 onButtonClick: function(btn,e)
490 this.fireEvent('btnclick', btn.name, e);
493 * Set the title of the Dialog
494 * @param {String} str new Title
496 setTitle: function(str) {
497 this.titleEl.dom.innerHTML = str;
500 * Set the body of the Dialog
501 * @param {String} str new Title
503 setBody: function(str) {
504 this.bodyEl.dom.innerHTML = str;
507 * Set the body of the Dialog using the template
508 * @param {Obj} data - apply this data to the template and replace the body contents.
510 applyBody: function(obj)
513 Roo.log("Error - using apply Body without a template");
516 this.tmpl.overwrite(this.bodyEl, obj);
519 getChildHeight : function(child_nodes)
523 child_nodes.length == 0
528 var child_height = 0;
530 for(var i = 0; i < child_nodes.length; i++) {
533 * for modal with tabs...
534 if(child_nodes[i].classList.contains('roo-layout-panel')) {
536 var layout_childs = child_nodes[i].childNodes;
538 for(var j = 0; j < layout_childs.length; j++) {
540 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
542 var layout_body_childs = layout_childs[j].childNodes;
544 for(var k = 0; k < layout_body_childs.length; k++) {
546 if(layout_body_childs[k].classList.contains('navbar')) {
547 child_height += layout_body_childs[k].offsetHeight;
551 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
553 var layout_body_tab_childs = layout_body_childs[k].childNodes;
555 for(var m = 0; m < layout_body_tab_childs.length; m++) {
557 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
558 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
573 child_height += child_nodes[i].offsetHeight;
574 // Roo.log(child_nodes[i].offsetHeight);
583 Roo.apply(Roo.bootstrap.Modal, {
585 * Button config that displays a single OK button
594 * Button config that displays Yes and No buttons
610 * Button config that displays OK and Cancel buttons
625 * Button config that displays Yes, No and Cancel buttons