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
23 * @cfg {Boolean} editable_title can the title be edited
28 * Create a new Modal Dialog
29 * @param {Object} config The config object
32 Roo.bootstrap.Modal = function(config){
33 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
38 * The raw btnclick event for the button
39 * @param {Roo.EventObject} e
44 * Fire when dialog resize
45 * @param {Roo.bootstrap.Modal} this
46 * @param {Roo.EventObject} e
51 * Fire when the editable title has been changed
52 * @param {Roo.bootstrap.Modal} this
53 * @param {Roo.EventObject} e
54 * @param {Roo.EventObject} value
56 "titlechanged" : true,
59 this.buttons = this.buttons || [];
62 this.tmpl = Roo.factory(this.tmpl);
67 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
69 title : 'test dialog',
81 buttonPosition: 'right',
103 editable_title : false,
105 onRender : function(ct, position)
107 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
110 var cfg = Roo.apply({}, this.getAutoCreate());
113 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
115 //if (!cfg.name.length) {
119 cfg.cls += ' ' + this.cls;
122 cfg.style = this.style;
124 this.el = Roo.get(document.body).createChild(cfg, position);
126 //var type = this.el.dom.type;
129 if(this.tabIndex !== undefined){
130 this.el.dom.setAttribute('tabIndex', this.tabIndex);
133 this.dialogEl = this.el.select('.modal-dialog',true).first();
134 this.bodyEl = this.el.select('.modal-body',true).first();
135 this.closeEl = this.el.select('.modal-header .close', true).first();
136 this.headerEl = this.el.select('.modal-header',true).first();
137 this.titleEl = this.el.select('.modal-title',true).first();
138 this.footerEl = this.el.select('.modal-footer',true).first();
140 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
142 //this.el.addClass("x-dlg-modal");
144 if (this.buttons.length) {
145 Roo.each(this.buttons, function(bb) {
146 var b = Roo.apply({}, bb);
147 b.xns = b.xns || Roo.bootstrap;
148 b.xtype = b.xtype || 'Button';
149 if (typeof(b.listeners) == 'undefined') {
150 b.listeners = { click : this.onButtonClick.createDelegate(this) };
153 var btn = Roo.factory(b);
155 btn.render(this.getButtonContainer());
159 // render the children.
162 if(typeof(this.items) != 'undefined'){
163 var items = this.items;
166 for(var i =0;i < items.length;i++) {
167 nitems.push(this.addxtype(Roo.apply({}, items[i])));
173 // where are these used - they used to be body/close/footer
177 //this.el.addClass([this.fieldClass, this.cls]);
181 getAutoCreate : function()
183 // we will default to modal-body-overflow - might need to remove or make optional later.
185 cls : 'modal-body enable-modal-body-overflow ',
186 html : this.html || ''
195 if(this.specificTitle){
201 if (this.allow_close && Roo.bootstrap.version == 3) {
211 if (this.allow_close && Roo.bootstrap.version == 4) {
221 if(this.size.length){
222 size = 'modal-' + this.size;
225 var footer = Roo.bootstrap.version == 3 ?
227 cls : 'modal-footer',
231 cls: 'btn-' + this.buttonPosition
236 { // BS4 uses mr-auto on left buttons....
248 cls: "modal-dialog " + size,
251 cls : "modal-content",
254 cls : 'modal-header',
269 modal.cls += ' fade';
275 getChildContainer : function() {
280 getButtonContainer : function() {
282 return Roo.bootstrap.version == 4 ?
283 this.el.select('.modal-footer',true).first()
284 : this.el.select('.modal-footer div',true).first();
287 initEvents : function()
289 if (this.allow_close) {
290 this.closeEl.on('click', this.hide, this);
292 Roo.EventManager.onWindowResize(this.resize, this, true);
301 Roo.lib.Dom.getViewWidth(true),
302 Roo.lib.Dom.getViewHeight(true)
305 if (this.fitwindow) {
309 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
310 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
315 if(this.max_width !== 0) {
317 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
320 this.setSize(w, this.height);
324 if(this.max_height) {
325 this.setSize(w,Math.min(
327 Roo.lib.Dom.getViewportHeight(true) - 60
333 if(!this.fit_content) {
334 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
338 this.setSize(w, Math.min(
340 this.headerEl.getHeight() +
341 this.footerEl.getHeight() +
342 this.getChildHeight(this.bodyEl.dom.childNodes),
343 Roo.lib.Dom.getViewportHeight(true) - 60)
349 setSize : function(w,h)
360 if (!this.rendered) {
364 //this.el.setStyle('display', 'block');
365 this.el.removeClass('hideing');
366 this.el.dom.style.display='block';
368 Roo.get(document.body).addClass('modal-open');
370 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
373 this.el.addClass('show');
374 this.el.addClass('in');
377 this.el.addClass('show');
378 this.el.addClass('in');
381 // not sure how we can show data in here..
383 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
386 Roo.get(document.body).addClass("x-body-masked");
388 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
389 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
390 this.maskEl.dom.style.display = 'block';
391 this.maskEl.addClass('show');
396 this.fireEvent('show', this);
398 // set zindex here - otherwise it appears to be ignored...
399 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
402 this.items.forEach( function(e) {
403 e.layout ? e.layout() : false;
411 if(this.fireEvent("beforehide", this) !== false){
413 this.maskEl.removeClass('show');
415 this.maskEl.dom.style.display = '';
416 Roo.get(document.body).removeClass("x-body-masked");
417 this.el.removeClass('in');
418 this.el.select('.modal-dialog', true).first().setStyle('transform','');
420 if(this.animate){ // why
421 this.el.addClass('hideing');
422 this.el.removeClass('show');
424 if (!this.el.hasClass('hideing')) {
425 return; // it's been shown again...
428 this.el.dom.style.display='';
430 Roo.get(document.body).removeClass('modal-open');
431 this.el.removeClass('hideing');
435 this.el.removeClass('show');
436 this.el.dom.style.display='';
437 Roo.get(document.body).removeClass('modal-open');
440 this.fireEvent('hide', this);
443 isVisible : function()
446 return this.el.hasClass('show') && !this.el.hasClass('hideing');
450 addButton : function(str, cb)
454 var b = Roo.apply({}, { html : str } );
455 b.xns = b.xns || Roo.bootstrap;
456 b.xtype = b.xtype || 'Button';
457 if (typeof(b.listeners) == 'undefined') {
458 b.listeners = { click : cb.createDelegate(this) };
461 var btn = Roo.factory(b);
463 btn.render(this.getButtonContainer());
469 setDefaultButton : function(btn)
471 //this.el.select('.modal-footer').()
474 resizeTo: function(w,h)
476 this.dialogEl.setWidth(w);
478 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
480 this.bodyEl.setHeight(h - diff);
482 this.fireEvent('resize', this);
485 setContentSize : function(w, h)
489 onButtonClick: function(btn,e)
492 this.fireEvent('btnclick', btn.name, e);
495 * Set the title of the Dialog
496 * @param {String} str new Title
498 setTitle: function(str) {
499 this.titleEl.dom.innerHTML = str;
502 * Set the body of the Dialog
503 * @param {String} str new Title
505 setBody: function(str) {
506 this.bodyEl.dom.innerHTML = str;
509 * Set the body of the Dialog using the template
510 * @param {Obj} data - apply this data to the template and replace the body contents.
512 applyBody: function(obj)
515 Roo.log("Error - using apply Body without a template");
518 this.tmpl.overwrite(this.bodyEl, obj);
521 getChildHeight : function(child_nodes)
525 child_nodes.length == 0
530 var child_height = 0;
532 for(var i = 0; i < child_nodes.length; i++) {
535 * for modal with tabs...
536 if(child_nodes[i].classList.contains('roo-layout-panel')) {
538 var layout_childs = child_nodes[i].childNodes;
540 for(var j = 0; j < layout_childs.length; j++) {
542 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
544 var layout_body_childs = layout_childs[j].childNodes;
546 for(var k = 0; k < layout_body_childs.length; k++) {
548 if(layout_body_childs[k].classList.contains('navbar')) {
549 child_height += layout_body_childs[k].offsetHeight;
553 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
555 var layout_body_tab_childs = layout_body_childs[k].childNodes;
557 for(var m = 0; m < layout_body_tab_childs.length; m++) {
559 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
560 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
575 child_height += child_nodes[i].offsetHeight;
576 // Roo.log(child_nodes[i].offsetHeight);
585 Roo.apply(Roo.bootstrap.Modal, {
587 * Button config that displays a single OK button
596 * Button config that displays Yes and No buttons
612 * Button config that displays OK and Cancel buttons
627 * Button config that displays Yes, No and Cancel buttons