7 * @class Roo.bootstrap.Modal
8 * @extends Roo.bootstrap.Component
10 * Bootstrap Modal class
11 * @cfg {String} title Title of dialog
12 * @cfg {String} html - the body of the dialog (for simple ones) - you can also use template..
13 * @cfg {Roo.Template} tmpl - a template with variables. to use it, add a handler in show:method adn
14 * @cfg {Boolean} specificTitle default false
15 * @cfg {Array} buttons Array of buttons or standard button set..
16 * @cfg {String} buttonPosition (left|right|center) default right (DEPRICATED) - use mr-auto on buttons to put them on the left
17 * @cfg {Boolean} animate default true
18 * @cfg {Boolean} allow_close default true
19 * @cfg {Boolean} fitwindow default false
20 * @cfg {Boolean} bodyOverflow should the body element have overflow auto added default false
21 * @cfg {Number} width fixed width - usefull for chrome extension only really.
22 * @cfg {Number} height fixed height - usefull for chrome extension only really.
23 * @cfg {String} size (sm|lg|xl) default empty
24 * @cfg {Number} max_width set the max width of modal
25 * @cfg {Boolean} editableTitle can the title be edited
30 * Create a new Modal Dialog
31 * @param {Object} config The config object
34 Roo.bootstrap.Modal = function(config){
35 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
40 * The raw btnclick event for the button
41 * @param {Roo.EventObject} e
46 * Fire when dialog resize
47 * @param {Roo.bootstrap.Modal} this
48 * @param {Roo.EventObject} e
53 * Fire when the editable title has been changed
54 * @param {Roo.bootstrap.Modal} this
55 * @param {Roo.EventObject} value
60 this.buttons = this.buttons || [];
63 this.tmpl = Roo.factory(this.tmpl);
68 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
70 title : 'test dialog',
82 buttonPosition: 'right',
104 editableTitle : false,
106 onRender : function(ct, position)
108 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
111 var cfg = Roo.apply({}, this.getAutoCreate());
114 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
116 //if (!cfg.name.length) {
120 cfg.cls += ' ' + this.cls;
123 cfg.style = this.style;
125 this.el = Roo.get(document.body).createChild(cfg, position);
127 //var type = this.el.dom.type;
130 if(this.tabIndex !== undefined){
131 this.el.dom.setAttribute('tabIndex', this.tabIndex);
134 this.dialogEl = this.el.select('.modal-dialog',true).first();
135 this.bodyEl = this.el.select('.modal-body',true).first();
136 this.closeEl = this.el.select('.modal-header .close', true).first();
137 this.headerEl = this.el.select('.modal-header',true).first();
138 this.titleEl = this.el.select('.modal-title',true).first();
139 this.footerEl = this.el.select('.modal-footer',true).first();
141 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
143 //this.el.addClass("x-dlg-modal");
145 if (this.buttons.length) {
146 Roo.each(this.buttons, function(bb) {
147 var b = Roo.apply({}, bb);
148 b.xns = b.xns || Roo.bootstrap;
149 b.xtype = b.xtype || 'Button';
150 if (typeof(b.listeners) == 'undefined') {
151 b.listeners = { click : this.onButtonClick.createDelegate(this) };
154 var btn = Roo.factory(b);
156 btn.render(this.getButtonContainer());
160 // render the children.
163 if(typeof(this.items) != 'undefined'){
164 var items = this.items;
167 for(var i =0;i < items.length;i++) {
168 nitems.push(this.addxtype(Roo.apply({}, items[i])));
174 // where are these used - they used to be body/close/footer
178 //this.el.addClass([this.fieldClass, this.cls]);
182 getAutoCreate : function()
184 // we will default to modal-body-overflow - might need to remove or make optional later.
186 cls : 'modal-body ' + (this.bodyOverflow ? 'overflow-auto' : ''),
187 html : this.html || ''
196 if(this.specificTitle){ // WTF is this?
201 if (this.allow_close && Roo.bootstrap.version == 3) {
211 if (this.editableTitle) {
213 cls: 'form-control roo-editable-title d-none',
219 if (this.allow_close && Roo.bootstrap.version == 4) {
229 if(this.size.length){
230 size = 'modal-' + this.size;
233 var footer = Roo.bootstrap.version == 3 ?
235 cls : 'modal-footer',
239 cls: 'btn-' + this.buttonPosition
244 { // BS4 uses mr-auto on left buttons....
256 cls: "modal-dialog " + size,
259 cls : "modal-content",
262 cls : 'modal-header',
277 modal.cls += ' fade';
283 getChildContainer : function() {
288 getButtonContainer : function() {
290 return Roo.bootstrap.version == 4 ?
291 this.el.select('.modal-footer',true).first()
292 : this.el.select('.modal-footer div',true).first();
295 initEvents : function()
297 if (this.allow_close) {
298 this.closeEl.on('click', this.hide, this);
300 Roo.EventManager.onWindowResize(this.resize, this, true);
301 if (this.editableTitle) {
302 this.headerEditEl = this.headerEl.select('.form-control',true).first();
303 this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
304 this.headerEditEl.on('keyup', function(e) {
305 if([ e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
306 this.toggleHeaderInput(false)
309 this.headerEditEl.on('blur', function(e) {
310 this.toggleHeaderInput(false)
320 Roo.lib.Dom.getViewWidth(true),
321 Roo.lib.Dom.getViewHeight(true)
324 if (this.fitwindow) {
326 this.dialogEl.setStyle( { 'max-width' : '100%' });
328 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
329 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
334 if(this.max_width !== 0) {
336 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
339 this.setSize(w, this.height);
343 if(this.max_height) {
344 this.setSize(w,Math.min(
346 Roo.lib.Dom.getViewportHeight(true) - 60
352 if(!this.fit_content) {
353 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
357 this.setSize(w, Math.min(
359 this.headerEl.getHeight() +
360 this.footerEl.getHeight() +
361 this.getChildHeight(this.bodyEl.dom.childNodes),
362 Roo.lib.Dom.getViewportHeight(true) - 60)
368 setSize : function(w,h)
379 if (!this.rendered) {
382 this.toggleHeaderInput(false);
383 //this.el.setStyle('display', 'block');
384 this.el.removeClass('hideing');
385 this.el.dom.style.display='block';
387 Roo.get(document.body).addClass('modal-open');
389 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
392 this.el.addClass('show');
393 this.el.addClass('in');
396 this.el.addClass('show');
397 this.el.addClass('in');
400 // not sure how we can show data in here..
402 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
405 Roo.get(document.body).addClass("x-body-masked");
407 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
408 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
409 this.maskEl.dom.style.display = 'block';
410 this.maskEl.addClass('show');
415 this.fireEvent('show', this);
417 // set zindex here - otherwise it appears to be ignored...
418 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
421 this.items.forEach( function(e) {
422 e.layout ? e.layout() : false;
430 if(this.fireEvent("beforehide", this) !== false){
432 this.maskEl.removeClass('show');
434 this.maskEl.dom.style.display = '';
435 Roo.get(document.body).removeClass("x-body-masked");
436 this.el.removeClass('in');
437 this.el.select('.modal-dialog', true).first().setStyle('transform','');
439 if(this.animate){ // why
440 this.el.addClass('hideing');
441 this.el.removeClass('show');
443 if (!this.el.hasClass('hideing')) {
444 return; // it's been shown again...
447 this.el.dom.style.display='';
449 Roo.get(document.body).removeClass('modal-open');
450 this.el.removeClass('hideing');
454 this.el.removeClass('show');
455 this.el.dom.style.display='';
456 Roo.get(document.body).removeClass('modal-open');
459 this.fireEvent('hide', this);
462 isVisible : function()
465 return this.el.hasClass('show') && !this.el.hasClass('hideing');
469 addButton : function(str, cb)
473 var b = Roo.apply({}, { html : str } );
474 b.xns = b.xns || Roo.bootstrap;
475 b.xtype = b.xtype || 'Button';
476 if (typeof(b.listeners) == 'undefined') {
477 b.listeners = { click : cb.createDelegate(this) };
480 var btn = Roo.factory(b);
482 btn.render(this.getButtonContainer());
488 setDefaultButton : function(btn)
490 //this.el.select('.modal-footer').()
493 resizeTo: function(w,h)
495 this.dialogEl.setWidth(w);
497 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
499 this.bodyEl.setHeight(h - diff);
501 this.fireEvent('resize', this);
504 setContentSize : function(w, h)
508 onButtonClick: function(btn,e)
511 this.fireEvent('btnclick', btn.name, e);
514 * Set the title of the Dialog
515 * @param {String} str new Title
517 setTitle: function(str) {
518 this.titleEl.dom.innerHTML = str;
522 * Set the body of the Dialog
523 * @param {String} str new Title
525 setBody: function(str) {
526 this.bodyEl.dom.innerHTML = str;
529 * Set the body of the Dialog using the template
530 * @param {Obj} data - apply this data to the template and replace the body contents.
532 applyBody: function(obj)
535 Roo.log("Error - using apply Body without a template");
538 this.tmpl.overwrite(this.bodyEl, obj);
541 getChildHeight : function(child_nodes)
545 child_nodes.length == 0
550 var child_height = 0;
552 for(var i = 0; i < child_nodes.length; i++) {
555 * for modal with tabs...
556 if(child_nodes[i].classList.contains('roo-layout-panel')) {
558 var layout_childs = child_nodes[i].childNodes;
560 for(var j = 0; j < layout_childs.length; j++) {
562 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
564 var layout_body_childs = layout_childs[j].childNodes;
566 for(var k = 0; k < layout_body_childs.length; k++) {
568 if(layout_body_childs[k].classList.contains('navbar')) {
569 child_height += layout_body_childs[k].offsetHeight;
573 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
575 var layout_body_tab_childs = layout_body_childs[k].childNodes;
577 for(var m = 0; m < layout_body_tab_childs.length; m++) {
579 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
580 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
595 child_height += child_nodes[i].offsetHeight;
596 // Roo.log(child_nodes[i].offsetHeight);
601 toggleHeaderInput : function(is_edit)
603 if (!this.editableTitle) {
604 return; // not editable.
606 if (is_edit && this.is_header_editing) {
607 return; // already editing..
611 this.headerEditEl.dom.value = this.title;
612 this.headerEditEl.removeClass('d-none');
613 this.headerEditEl.dom.focus();
614 this.titleEl.addClass('d-none');
616 this.is_header_editing = true;
619 // flip back to not editing.
620 this.title = this.headerEditEl.dom.value;
621 this.headerEditEl.addClass('d-none');
622 this.titleEl.removeClass('d-none');
623 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
624 this.is_header_editing = false;
625 this.fireEvent('titlechanged', this, this.title);
634 Roo.apply(Roo.bootstrap.Modal, {
636 * Button config that displays a single OK button
645 * Button config that displays Yes and No buttons
661 * Button config that displays OK and Cancel buttons
676 * Button config that displays Yes, No and Cancel buttons