7 * @class Roo.bootstrap.Modal
8 * @extends Roo.bootstrap.Component
10 * @children Roo.bootstrap.Component
11 * Bootstrap Modal class
12 * @cfg {String} title Title of dialog
13 * @cfg {String} html - the body of the dialog (for simple ones) - you can also use template..
14 * @cfg {Roo.Template} tmpl - a template with variables. to use it, add a handler in show:method adn
15 * @cfg {Boolean} specificTitle default false
16 * @cfg {Roo.bootstrap.Button} buttons[] Array of buttons or standard button set..
17 * @cfg {String} buttonPosition (left|right|center) default right (DEPRICATED) - use mr-auto on buttons to put them on the left
18 * @cfg {Boolean} animate default true
19 * @cfg {Boolean} allow_close default true
20 * @cfg {Boolean} fitwindow default false
21 * @cfg {Boolean} bodyOverflow should the body element have overflow auto added default false
22 * @cfg {Number} width fixed width - usefull for chrome extension only really.
23 * @cfg {Number} height fixed height - usefull for chrome extension only really.
24 * @cfg {String} size (sm|lg|xl) default empty
25 * @cfg {Number} max_width set the max width of modal
26 * @cfg {Boolean} editableTitle can the title be edited
31 * Create a new Modal Dialog
32 * @param {Object} config The config object
35 Roo.bootstrap.Modal = function(config){
36 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
41 * The raw btnclick event for the button
42 * @param {Roo.EventObject} e
47 * Fire when dialog resize
48 * @param {Roo.bootstrap.Modal} this
49 * @param {Roo.EventObject} e
54 * Fire when the editable title has been changed
55 * @param {Roo.bootstrap.Modal} this
56 * @param {Roo.EventObject} value
61 this.buttons = this.buttons || [];
64 this.tmpl = Roo.factory(this.tmpl);
69 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
71 title : 'test dialog',
83 buttonPosition: 'right',
105 editableTitle : false,
107 onRender : function(ct, position)
109 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
112 var cfg = Roo.apply({}, this.getAutoCreate());
115 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
117 //if (!cfg.name.length) {
121 cfg.cls += ' ' + this.cls;
124 cfg.style = this.style;
126 this.el = Roo.get(document.body).createChild(cfg, position);
128 //var type = this.el.dom.type;
131 if(this.tabIndex !== undefined){
132 this.el.dom.setAttribute('tabIndex', this.tabIndex);
135 this.dialogEl = this.el.select('.modal-dialog',true).first();
136 this.bodyEl = this.el.select('.modal-body',true).first();
137 this.closeEl = this.el.select('.modal-header .close', true).first();
138 this.headerEl = this.el.select('.modal-header',true).first();
139 this.titleEl = this.el.select('.modal-title',true).first();
140 this.footerEl = this.el.select('.modal-footer',true).first();
142 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
144 //this.el.addClass("x-dlg-modal");
146 if (this.buttons.length) {
147 Roo.each(this.buttons, function(bb) {
148 var b = Roo.apply({}, bb);
149 b.xns = b.xns || Roo.bootstrap;
150 b.xtype = b.xtype || 'Button';
151 if (typeof(b.listeners) == 'undefined') {
152 b.listeners = { click : this.onButtonClick.createDelegate(this) };
155 var btn = Roo.factory(b);
157 btn.render(this.getButtonContainer());
161 // render the children.
164 if(typeof(this.items) != 'undefined'){
165 var items = this.items;
168 for(var i =0;i < items.length;i++) {
169 // we force children not to montor widnow resize - as we do that for them.
170 items[i].monitorWindowResize = false;
171 nitems.push(this.addxtype(Roo.apply({}, items[i])));
177 // where are these used - they used to be body/close/footer
181 //this.el.addClass([this.fieldClass, this.cls]);
185 getAutoCreate : function()
187 // we will default to modal-body-overflow - might need to remove or make optional later.
189 cls : 'modal-body ' + (this.bodyOverflow ? 'overflow-auto' : ''),
190 html : this.html || ''
199 if(this.specificTitle){ // WTF is this?
204 if (this.allow_close && Roo.bootstrap.version == 3) {
214 if (this.editableTitle) {
216 cls: 'form-control roo-editable-title d-none',
222 if (this.allow_close && Roo.bootstrap.version == 4) {
232 if(this.size.length){
233 size = 'modal-' + this.size;
236 var footer = Roo.bootstrap.version == 3 ?
238 cls : 'modal-footer',
242 cls: 'btn-' + this.buttonPosition
247 { // BS4 uses mr-auto on left buttons....
259 cls: "modal-dialog " + size,
262 cls : "modal-content",
265 cls : 'modal-header',
280 modal.cls += ' fade';
286 getChildContainer : function() {
291 getButtonContainer : function() {
293 return Roo.bootstrap.version == 4 ?
294 this.el.select('.modal-footer',true).first()
295 : this.el.select('.modal-footer div',true).first();
298 initEvents : function()
300 if (this.allow_close) {
301 this.closeEl.on('click', this.hide, this);
303 Roo.EventManager.onWindowResize(this.resize, this, true);
304 if (this.editableTitle) {
305 this.headerEditEl = this.headerEl.select('.form-control',true).first();
306 this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
307 this.headerEditEl.on('keyup', function(e) {
308 if([ e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
309 this.toggleHeaderInput(false)
312 this.headerEditEl.on('blur', function(e) {
313 this.toggleHeaderInput(false)
323 Roo.lib.Dom.getViewWidth(true),
324 Roo.lib.Dom.getViewHeight(true)
327 if (this.fitwindow) {
329 this.dialogEl.setStyle( { 'max-width' : '100%' });
331 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
332 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
337 if(this.max_width !== 0) {
339 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
342 this.setSize(w, this.height);
346 if(this.max_height) {
347 this.setSize(w,Math.min(
349 Roo.lib.Dom.getViewportHeight(true) - 60
355 if(!this.fit_content) {
356 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
360 this.setSize(w, Math.min(
362 this.headerEl.getHeight() +
363 this.footerEl.getHeight() +
364 this.getChildHeight(this.bodyEl.dom.childNodes),
365 Roo.lib.Dom.getViewportHeight(true) - 60)
371 setSize : function(w,h)
378 // any layout/border etc.. resize..
380 this.items.forEach( function(e) {
381 e.layout ? e.layout() : false;
390 if (!this.rendered) {
393 this.toggleHeaderInput(false);
394 //this.el.setStyle('display', 'block');
395 this.el.removeClass('hideing');
396 this.el.dom.style.display='block';
398 Roo.get(document.body).addClass('modal-open');
400 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
403 this.el.addClass('show');
404 this.el.addClass('in');
407 this.el.addClass('show');
408 this.el.addClass('in');
411 // not sure how we can show data in here..
413 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
416 Roo.get(document.body).addClass("x-body-masked");
418 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
419 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
420 this.maskEl.dom.style.display = 'block';
421 this.maskEl.addClass('show');
426 this.fireEvent('show', this);
428 // set zindex here - otherwise it appears to be ignored...
429 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
432 // this is for children that are... layout.Border
434 this.items.forEach( function(e) {
435 e.layout ? e.layout() : false;
443 if(this.fireEvent("beforehide", this) !== false){
445 this.maskEl.removeClass('show');
447 this.maskEl.dom.style.display = '';
448 Roo.get(document.body).removeClass("x-body-masked");
449 this.el.removeClass('in');
450 this.el.select('.modal-dialog', true).first().setStyle('transform','');
452 if(this.animate){ // why
453 this.el.addClass('hideing');
454 this.el.removeClass('show');
456 if (!this.el.hasClass('hideing')) {
457 return; // it's been shown again...
460 this.el.dom.style.display='';
462 Roo.get(document.body).removeClass('modal-open');
463 this.el.removeClass('hideing');
467 this.el.removeClass('show');
468 this.el.dom.style.display='';
469 Roo.get(document.body).removeClass('modal-open');
472 this.fireEvent('hide', this);
475 isVisible : function()
478 return this.el.hasClass('show') && !this.el.hasClass('hideing');
482 addButton : function(str, cb)
486 var b = Roo.apply({}, { html : str } );
487 b.xns = b.xns || Roo.bootstrap;
488 b.xtype = b.xtype || 'Button';
489 if (typeof(b.listeners) == 'undefined') {
490 b.listeners = { click : cb.createDelegate(this) };
493 var btn = Roo.factory(b);
495 btn.render(this.getButtonContainer());
501 setDefaultButton : function(btn)
503 //this.el.select('.modal-footer').()
506 resizeTo: function(w,h)
508 this.dialogEl.setWidth(w);
510 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
512 this.bodyEl.setHeight(h - diff);
514 this.fireEvent('resize', this);
517 setContentSize : function(w, h)
521 onButtonClick: function(btn,e)
524 this.fireEvent('btnclick', btn.name, e);
527 * Set the title of the Dialog
528 * @param {String} str new Title
530 setTitle: function(str) {
531 this.titleEl.dom.innerHTML = str;
535 * Set the body of the Dialog
536 * @param {String} str new Title
538 setBody: function(str) {
539 this.bodyEl.dom.innerHTML = str;
542 * Set the body of the Dialog using the template
543 * @param {Obj} data - apply this data to the template and replace the body contents.
545 applyBody: function(obj)
548 Roo.log("Error - using apply Body without a template");
551 this.tmpl.overwrite(this.bodyEl, obj);
554 getChildHeight : function(child_nodes)
558 child_nodes.length == 0
563 var child_height = 0;
565 for(var i = 0; i < child_nodes.length; i++) {
568 * for modal with tabs...
569 if(child_nodes[i].classList.contains('roo-layout-panel')) {
571 var layout_childs = child_nodes[i].childNodes;
573 for(var j = 0; j < layout_childs.length; j++) {
575 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
577 var layout_body_childs = layout_childs[j].childNodes;
579 for(var k = 0; k < layout_body_childs.length; k++) {
581 if(layout_body_childs[k].classList.contains('navbar')) {
582 child_height += layout_body_childs[k].offsetHeight;
586 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
588 var layout_body_tab_childs = layout_body_childs[k].childNodes;
590 for(var m = 0; m < layout_body_tab_childs.length; m++) {
592 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
593 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
608 child_height += child_nodes[i].offsetHeight;
609 // Roo.log(child_nodes[i].offsetHeight);
614 toggleHeaderInput : function(is_edit)
616 if (!this.editableTitle) {
617 return; // not editable.
619 if (is_edit && this.is_header_editing) {
620 return; // already editing..
624 this.headerEditEl.dom.value = this.title;
625 this.headerEditEl.removeClass('d-none');
626 this.headerEditEl.dom.focus();
627 this.titleEl.addClass('d-none');
629 this.is_header_editing = true;
632 // flip back to not editing.
633 this.title = this.headerEditEl.dom.value;
634 this.headerEditEl.addClass('d-none');
635 this.titleEl.removeClass('d-none');
636 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
637 this.is_header_editing = false;
638 this.fireEvent('titlechanged', this, this.title);
647 Roo.apply(Roo.bootstrap.Modal, {
649 * Button config that displays a single OK button
658 * Button config that displays Yes and No buttons
674 * Button config that displays OK and Cancel buttons
689 * Button config that displays Yes, No and Cancel buttons