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();
299 closeClick : function()
304 initEvents : function()
306 if (this.allow_close) {
307 this.closeEl.on('click', this.closeClick, this);
309 Roo.EventManager.onWindowResize(this.resize, this, true);
310 if (this.editableTitle) {
311 this.headerEditEl = this.headerEl.select('.form-control',true).first();
312 this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
313 this.headerEditEl.on('keyup', function(e) {
314 if([ e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
315 this.toggleHeaderInput(false)
318 this.headerEditEl.on('blur', function(e) {
319 this.toggleHeaderInput(false)
329 Roo.lib.Dom.getViewWidth(true),
330 Roo.lib.Dom.getViewHeight(true)
333 if (this.fitwindow) {
335 this.dialogEl.setStyle( { 'max-width' : '100%' });
337 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
338 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
343 if(this.max_width !== 0) {
345 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
348 this.setSize(w, this.height);
352 if(this.max_height) {
353 this.setSize(w,Math.min(
355 Roo.lib.Dom.getViewportHeight(true) - 60
361 if(!this.fit_content) {
362 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
366 this.setSize(w, Math.min(
368 this.headerEl.getHeight() +
369 this.footerEl.getHeight() +
370 this.getChildHeight(this.bodyEl.dom.childNodes),
371 Roo.lib.Dom.getViewportHeight(true) - 60)
377 setSize : function(w,h)
384 // any layout/border etc.. resize..
386 this.items.forEach( function(e) {
387 e.layout ? e.layout() : false;
396 if (!this.rendered) {
399 this.toggleHeaderInput(false);
400 //this.el.setStyle('display', 'block');
401 this.el.removeClass('hideing');
402 this.el.dom.style.display='block';
404 Roo.get(document.body).addClass('modal-open');
406 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
409 this.el.addClass('show');
410 this.el.addClass('in');
413 this.el.addClass('show');
414 this.el.addClass('in');
417 // not sure how we can show data in here..
419 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
422 Roo.get(document.body).addClass("x-body-masked");
424 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
425 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
426 this.maskEl.dom.style.display = 'block';
427 this.maskEl.addClass('show');
432 this.fireEvent('show', this);
434 // set zindex here - otherwise it appears to be ignored...
435 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
438 // this is for children that are... layout.Border
440 this.items.forEach( function(e) {
441 e.layout ? e.layout() : false;
449 if(this.fireEvent("beforehide", this) !== false){
451 this.maskEl.removeClass('show');
453 this.maskEl.dom.style.display = '';
454 Roo.get(document.body).removeClass("x-body-masked");
455 this.el.removeClass('in');
456 this.el.select('.modal-dialog', true).first().setStyle('transform','');
458 if(this.animate){ // why
459 this.el.addClass('hideing');
460 this.el.removeClass('show');
462 if (!this.el.hasClass('hideing')) {
463 return; // it's been shown again...
466 this.el.dom.style.display='';
468 Roo.get(document.body).removeClass('modal-open');
469 this.el.removeClass('hideing');
473 this.el.removeClass('show');
474 this.el.dom.style.display='';
475 Roo.get(document.body).removeClass('modal-open');
478 this.fireEvent('hide', this);
481 isVisible : function()
484 return this.el.hasClass('show') && !this.el.hasClass('hideing');
488 addButton : function(str, cb)
492 var b = Roo.apply({}, { html : str } );
493 b.xns = b.xns || Roo.bootstrap;
494 b.xtype = b.xtype || 'Button';
495 if (typeof(b.listeners) == 'undefined') {
496 b.listeners = { click : cb.createDelegate(this) };
499 var btn = Roo.factory(b);
501 btn.render(this.getButtonContainer());
507 setDefaultButton : function(btn)
509 //this.el.select('.modal-footer').()
512 resizeTo: function(w,h)
514 this.dialogEl.setWidth(w);
516 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
518 this.bodyEl.setHeight(h - diff);
520 this.fireEvent('resize', this);
523 setContentSize : function(w, h)
527 onButtonClick: function(btn,e)
530 this.fireEvent('btnclick', btn.name, e);
533 * Set the title of the Dialog
534 * @param {String} str new Title
536 setTitle: function(str) {
537 this.titleEl.dom.innerHTML = str;
541 * Set the body of the Dialog
542 * @param {String} str new Title
544 setBody: function(str) {
545 this.bodyEl.dom.innerHTML = str;
548 * Set the body of the Dialog using the template
549 * @param {Obj} data - apply this data to the template and replace the body contents.
551 applyBody: function(obj)
554 Roo.log("Error - using apply Body without a template");
557 this.tmpl.overwrite(this.bodyEl, obj);
560 getChildHeight : function(child_nodes)
564 child_nodes.length == 0
569 var child_height = 0;
571 for(var i = 0; i < child_nodes.length; i++) {
574 * for modal with tabs...
575 if(child_nodes[i].classList.contains('roo-layout-panel')) {
577 var layout_childs = child_nodes[i].childNodes;
579 for(var j = 0; j < layout_childs.length; j++) {
581 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
583 var layout_body_childs = layout_childs[j].childNodes;
585 for(var k = 0; k < layout_body_childs.length; k++) {
587 if(layout_body_childs[k].classList.contains('navbar')) {
588 child_height += layout_body_childs[k].offsetHeight;
592 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
594 var layout_body_tab_childs = layout_body_childs[k].childNodes;
596 for(var m = 0; m < layout_body_tab_childs.length; m++) {
598 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
599 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
614 child_height += child_nodes[i].offsetHeight;
615 // Roo.log(child_nodes[i].offsetHeight);
620 toggleHeaderInput : function(is_edit)
622 if (!this.editableTitle) {
623 return; // not editable.
625 if (is_edit && this.is_header_editing) {
626 return; // already editing..
630 this.headerEditEl.dom.value = this.title;
631 this.headerEditEl.removeClass('d-none');
632 this.headerEditEl.dom.focus();
633 this.titleEl.addClass('d-none');
635 this.is_header_editing = true;
638 // flip back to not editing.
639 this.title = this.headerEditEl.dom.value;
640 this.headerEditEl.addClass('d-none');
641 this.titleEl.removeClass('d-none');
642 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
643 this.is_header_editing = false;
644 this.fireEvent('titlechanged', this, this.title);
653 Roo.apply(Roo.bootstrap.Modal, {
655 * Button config that displays a single OK button
664 * Button config that displays Yes and No buttons
680 * Button config that displays OK and Cancel buttons
695 * Button config that displays Yes, No and Cancel buttons