7 * @class Roo.bootstrap.Modal
8 * @extends Roo.bootstrap.Component
11 * @children Roo.bootstrap.Component
12 * Bootstrap Modal class
13 * @cfg {String} title Title of dialog
14 * @cfg {String} html - the body of the dialog (for simple ones) - you can also use template..
15 * @cfg {Roo.Template} tmpl - a template with variables. to use it, add a handler in show:method adn
16 * @cfg {Boolean} specificTitle default false
17 * @cfg {Array} buttons Array of buttons or standard button set..
18 * @cfg {String} buttonPosition (left|right|center) default right (DEPRICATED) - use mr-auto on buttons to put them on the left
19 * @cfg {Boolean} animate default true
20 * @cfg {Boolean} allow_close default true
21 * @cfg {Boolean} fitwindow default false
22 * @cfg {Boolean} bodyOverflow should the body element have overflow auto added default false
23 * @cfg {Number} width fixed width - usefull for chrome extension only really.
24 * @cfg {Number} height fixed height - usefull for chrome extension only really.
25 * @cfg {String} size (sm|lg|xl) default empty
26 * @cfg {Number} max_width set the max width of modal
27 * @cfg {Boolean} editableTitle can the title be edited
32 * Create a new Modal Dialog
33 * @param {Object} config The config object
36 Roo.bootstrap.Modal = function(config){
37 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
42 * The raw btnclick event for the button
43 * @param {Roo.EventObject} e
48 * Fire when dialog resize
49 * @param {Roo.bootstrap.Modal} this
50 * @param {Roo.EventObject} e
55 * Fire when the editable title has been changed
56 * @param {Roo.bootstrap.Modal} this
57 * @param {Roo.EventObject} value
62 this.buttons = this.buttons || [];
65 this.tmpl = Roo.factory(this.tmpl);
70 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
72 title : 'test dialog',
84 buttonPosition: 'right',
106 editableTitle : false,
108 onRender : function(ct, position)
110 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
113 var cfg = Roo.apply({}, this.getAutoCreate());
116 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
118 //if (!cfg.name.length) {
122 cfg.cls += ' ' + this.cls;
125 cfg.style = this.style;
127 this.el = Roo.get(document.body).createChild(cfg, position);
129 //var type = this.el.dom.type;
132 if(this.tabIndex !== undefined){
133 this.el.dom.setAttribute('tabIndex', this.tabIndex);
136 this.dialogEl = this.el.select('.modal-dialog',true).first();
137 this.bodyEl = this.el.select('.modal-body',true).first();
138 this.closeEl = this.el.select('.modal-header .close', true).first();
139 this.headerEl = this.el.select('.modal-header',true).first();
140 this.titleEl = this.el.select('.modal-title',true).first();
141 this.footerEl = this.el.select('.modal-footer',true).first();
143 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
145 //this.el.addClass("x-dlg-modal");
147 if (this.buttons.length) {
148 Roo.each(this.buttons, function(bb) {
149 var b = Roo.apply({}, bb);
150 b.xns = b.xns || Roo.bootstrap;
151 b.xtype = b.xtype || 'Button';
152 if (typeof(b.listeners) == 'undefined') {
153 b.listeners = { click : this.onButtonClick.createDelegate(this) };
156 var btn = Roo.factory(b);
158 btn.render(this.getButtonContainer());
162 // render the children.
165 if(typeof(this.items) != 'undefined'){
166 var items = this.items;
169 for(var i =0;i < items.length;i++) {
170 nitems.push(this.addxtype(Roo.apply({}, items[i])));
176 // where are these used - they used to be body/close/footer
180 //this.el.addClass([this.fieldClass, this.cls]);
184 getAutoCreate : function()
186 // we will default to modal-body-overflow - might need to remove or make optional later.
188 cls : 'modal-body ' + (this.bodyOverflow ? 'overflow-auto' : ''),
189 html : this.html || ''
198 if(this.specificTitle){ // WTF is this?
203 if (this.allow_close && Roo.bootstrap.version == 3) {
213 if (this.editableTitle) {
215 cls: 'form-control roo-editable-title d-none',
221 if (this.allow_close && Roo.bootstrap.version == 4) {
231 if(this.size.length){
232 size = 'modal-' + this.size;
235 var footer = Roo.bootstrap.version == 3 ?
237 cls : 'modal-footer',
241 cls: 'btn-' + this.buttonPosition
246 { // BS4 uses mr-auto on left buttons....
258 cls: "modal-dialog " + size,
261 cls : "modal-content",
264 cls : 'modal-header',
279 modal.cls += ' fade';
285 getChildContainer : function() {
290 getButtonContainer : function() {
292 return Roo.bootstrap.version == 4 ?
293 this.el.select('.modal-footer',true).first()
294 : this.el.select('.modal-footer div',true).first();
297 initEvents : function()
299 if (this.allow_close) {
300 this.closeEl.on('click', this.hide, this);
302 Roo.EventManager.onWindowResize(this.resize, this, true);
303 if (this.editableTitle) {
304 this.headerEditEl = this.headerEl.select('.form-control',true).first();
305 this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
306 this.headerEditEl.on('keyup', function(e) {
307 if([ e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
308 this.toggleHeaderInput(false)
311 this.headerEditEl.on('blur', function(e) {
312 this.toggleHeaderInput(false)
322 Roo.lib.Dom.getViewWidth(true),
323 Roo.lib.Dom.getViewHeight(true)
326 if (this.fitwindow) {
328 this.dialogEl.setStyle( { 'max-width' : '100%' });
330 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
331 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
336 if(this.max_width !== 0) {
338 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
341 this.setSize(w, this.height);
345 if(this.max_height) {
346 this.setSize(w,Math.min(
348 Roo.lib.Dom.getViewportHeight(true) - 60
354 if(!this.fit_content) {
355 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
359 this.setSize(w, Math.min(
361 this.headerEl.getHeight() +
362 this.footerEl.getHeight() +
363 this.getChildHeight(this.bodyEl.dom.childNodes),
364 Roo.lib.Dom.getViewportHeight(true) - 60)
370 setSize : function(w,h)
381 if (!this.rendered) {
384 this.toggleHeaderInput(false);
385 //this.el.setStyle('display', 'block');
386 this.el.removeClass('hideing');
387 this.el.dom.style.display='block';
389 Roo.get(document.body).addClass('modal-open');
391 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
394 this.el.addClass('show');
395 this.el.addClass('in');
398 this.el.addClass('show');
399 this.el.addClass('in');
402 // not sure how we can show data in here..
404 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
407 Roo.get(document.body).addClass("x-body-masked");
409 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
410 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
411 this.maskEl.dom.style.display = 'block';
412 this.maskEl.addClass('show');
417 this.fireEvent('show', this);
419 // set zindex here - otherwise it appears to be ignored...
420 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
423 this.items.forEach( function(e) {
424 e.layout ? e.layout() : false;
432 if(this.fireEvent("beforehide", this) !== false){
434 this.maskEl.removeClass('show');
436 this.maskEl.dom.style.display = '';
437 Roo.get(document.body).removeClass("x-body-masked");
438 this.el.removeClass('in');
439 this.el.select('.modal-dialog', true).first().setStyle('transform','');
441 if(this.animate){ // why
442 this.el.addClass('hideing');
443 this.el.removeClass('show');
445 if (!this.el.hasClass('hideing')) {
446 return; // it's been shown again...
449 this.el.dom.style.display='';
451 Roo.get(document.body).removeClass('modal-open');
452 this.el.removeClass('hideing');
456 this.el.removeClass('show');
457 this.el.dom.style.display='';
458 Roo.get(document.body).removeClass('modal-open');
461 this.fireEvent('hide', this);
464 isVisible : function()
467 return this.el.hasClass('show') && !this.el.hasClass('hideing');
471 addButton : function(str, cb)
475 var b = Roo.apply({}, { html : str } );
476 b.xns = b.xns || Roo.bootstrap;
477 b.xtype = b.xtype || 'Button';
478 if (typeof(b.listeners) == 'undefined') {
479 b.listeners = { click : cb.createDelegate(this) };
482 var btn = Roo.factory(b);
484 btn.render(this.getButtonContainer());
490 setDefaultButton : function(btn)
492 //this.el.select('.modal-footer').()
495 resizeTo: function(w,h)
497 this.dialogEl.setWidth(w);
499 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
501 this.bodyEl.setHeight(h - diff);
503 this.fireEvent('resize', this);
506 setContentSize : function(w, h)
510 onButtonClick: function(btn,e)
513 this.fireEvent('btnclick', btn.name, e);
516 * Set the title of the Dialog
517 * @param {String} str new Title
519 setTitle: function(str) {
520 this.titleEl.dom.innerHTML = str;
524 * Set the body of the Dialog
525 * @param {String} str new Title
527 setBody: function(str) {
528 this.bodyEl.dom.innerHTML = str;
531 * Set the body of the Dialog using the template
532 * @param {Obj} data - apply this data to the template and replace the body contents.
534 applyBody: function(obj)
537 Roo.log("Error - using apply Body without a template");
540 this.tmpl.overwrite(this.bodyEl, obj);
543 getChildHeight : function(child_nodes)
547 child_nodes.length == 0
552 var child_height = 0;
554 for(var i = 0; i < child_nodes.length; i++) {
557 * for modal with tabs...
558 if(child_nodes[i].classList.contains('roo-layout-panel')) {
560 var layout_childs = child_nodes[i].childNodes;
562 for(var j = 0; j < layout_childs.length; j++) {
564 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
566 var layout_body_childs = layout_childs[j].childNodes;
568 for(var k = 0; k < layout_body_childs.length; k++) {
570 if(layout_body_childs[k].classList.contains('navbar')) {
571 child_height += layout_body_childs[k].offsetHeight;
575 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
577 var layout_body_tab_childs = layout_body_childs[k].childNodes;
579 for(var m = 0; m < layout_body_tab_childs.length; m++) {
581 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
582 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
597 child_height += child_nodes[i].offsetHeight;
598 // Roo.log(child_nodes[i].offsetHeight);
603 toggleHeaderInput : function(is_edit)
605 if (!this.editableTitle) {
606 return; // not editable.
608 if (is_edit && this.is_header_editing) {
609 return; // already editing..
613 this.headerEditEl.dom.value = this.title;
614 this.headerEditEl.removeClass('d-none');
615 this.headerEditEl.dom.focus();
616 this.titleEl.addClass('d-none');
618 this.is_header_editing = true;
621 // flip back to not editing.
622 this.title = this.headerEditEl.dom.value;
623 this.headerEditEl.addClass('d-none');
624 this.titleEl.removeClass('d-none');
625 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
626 this.is_header_editing = false;
627 this.fireEvent('titlechanged', this, this.title);
636 Roo.apply(Roo.bootstrap.Modal, {
638 * Button config that displays a single OK button
647 * Button config that displays Yes and No buttons
663 * Button config that displays OK and Cancel buttons
678 * Button config that displays Yes, No and Cancel buttons