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 nitems.push(this.addxtype(Roo.apply({}, items[i])));
175 // where are these used - they used to be body/close/footer
179 //this.el.addClass([this.fieldClass, this.cls]);
183 getAutoCreate : function()
185 // we will default to modal-body-overflow - might need to remove or make optional later.
187 cls : 'modal-body ' + (this.bodyOverflow ? 'overflow-auto' : ''),
188 html : this.html || ''
197 if(this.specificTitle){ // WTF is this?
202 if (this.allow_close && Roo.bootstrap.version == 3) {
212 if (this.editableTitle) {
214 cls: 'form-control roo-editable-title d-none',
220 if (this.allow_close && Roo.bootstrap.version == 4) {
230 if(this.size.length){
231 size = 'modal-' + this.size;
234 var footer = Roo.bootstrap.version == 3 ?
236 cls : 'modal-footer',
240 cls: 'btn-' + this.buttonPosition
245 { // BS4 uses mr-auto on left buttons....
257 cls: "modal-dialog " + size,
260 cls : "modal-content",
263 cls : 'modal-header',
278 modal.cls += ' fade';
284 getChildContainer : function() {
289 getButtonContainer : function() {
291 return Roo.bootstrap.version == 4 ?
292 this.el.select('.modal-footer',true).first()
293 : this.el.select('.modal-footer div',true).first();
296 initEvents : function()
298 if (this.allow_close) {
299 this.closeEl.on('click', this.hide, this);
301 Roo.EventManager.onWindowResize(this.resize, this, true);
302 if (this.editableTitle) {
303 this.headerEditEl = this.headerEl.select('.form-control',true).first();
304 this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
305 this.headerEditEl.on('keyup', function(e) {
306 if([ e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
307 this.toggleHeaderInput(false)
310 this.headerEditEl.on('blur', function(e) {
311 this.toggleHeaderInput(false)
321 Roo.lib.Dom.getViewWidth(true),
322 Roo.lib.Dom.getViewHeight(true)
325 if (this.fitwindow) {
327 this.dialogEl.setStyle( { 'max-width' : '100%' });
329 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
330 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
335 if(this.max_width !== 0) {
337 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
340 this.setSize(w, this.height);
344 if(this.max_height) {
345 this.setSize(w,Math.min(
347 Roo.lib.Dom.getViewportHeight(true) - 60
353 if(!this.fit_content) {
354 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
358 this.setSize(w, Math.min(
360 this.headerEl.getHeight() +
361 this.footerEl.getHeight() +
362 this.getChildHeight(this.bodyEl.dom.childNodes),
363 Roo.lib.Dom.getViewportHeight(true) - 60)
369 setSize : function(w,h)
380 if (!this.rendered) {
383 this.toggleHeaderInput(false);
384 //this.el.setStyle('display', 'block');
385 this.el.removeClass('hideing');
386 this.el.dom.style.display='block';
388 Roo.get(document.body).addClass('modal-open');
390 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
393 this.el.addClass('show');
394 this.el.addClass('in');
397 this.el.addClass('show');
398 this.el.addClass('in');
401 // not sure how we can show data in here..
403 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
406 Roo.get(document.body).addClass("x-body-masked");
408 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
409 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
410 this.maskEl.dom.style.display = 'block';
411 this.maskEl.addClass('show');
416 this.fireEvent('show', this);
418 // set zindex here - otherwise it appears to be ignored...
419 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
422 this.items.forEach( function(e) {
423 e.layout ? e.layout() : false;
431 if(this.fireEvent("beforehide", this) !== false){
433 this.maskEl.removeClass('show');
435 this.maskEl.dom.style.display = '';
436 Roo.get(document.body).removeClass("x-body-masked");
437 this.el.removeClass('in');
438 this.el.select('.modal-dialog', true).first().setStyle('transform','');
440 if(this.animate){ // why
441 this.el.addClass('hideing');
442 this.el.removeClass('show');
444 if (!this.el.hasClass('hideing')) {
445 return; // it's been shown again...
448 this.el.dom.style.display='';
450 Roo.get(document.body).removeClass('modal-open');
451 this.el.removeClass('hideing');
455 this.el.removeClass('show');
456 this.el.dom.style.display='';
457 Roo.get(document.body).removeClass('modal-open');
460 this.fireEvent('hide', this);
463 isVisible : function()
466 return this.el.hasClass('show') && !this.el.hasClass('hideing');
470 addButton : function(str, cb)
474 var b = Roo.apply({}, { html : str } );
475 b.xns = b.xns || Roo.bootstrap;
476 b.xtype = b.xtype || 'Button';
477 if (typeof(b.listeners) == 'undefined') {
478 b.listeners = { click : cb.createDelegate(this) };
481 var btn = Roo.factory(b);
483 btn.render(this.getButtonContainer());
489 setDefaultButton : function(btn)
491 //this.el.select('.modal-footer').()
494 resizeTo: function(w,h)
496 this.dialogEl.setWidth(w);
498 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
500 this.bodyEl.setHeight(h - diff);
502 this.fireEvent('resize', this);
505 setContentSize : function(w, h)
509 onButtonClick: function(btn,e)
512 this.fireEvent('btnclick', btn.name, e);
515 * Set the title of the Dialog
516 * @param {String} str new Title
518 setTitle: function(str) {
519 this.titleEl.dom.innerHTML = str;
523 * Set the body of the Dialog
524 * @param {String} str new Title
526 setBody: function(str) {
527 this.bodyEl.dom.innerHTML = str;
530 * Set the body of the Dialog using the template
531 * @param {Obj} data - apply this data to the template and replace the body contents.
533 applyBody: function(obj)
536 Roo.log("Error - using apply Body without a template");
539 this.tmpl.overwrite(this.bodyEl, obj);
542 getChildHeight : function(child_nodes)
546 child_nodes.length == 0
551 var child_height = 0;
553 for(var i = 0; i < child_nodes.length; i++) {
556 * for modal with tabs...
557 if(child_nodes[i].classList.contains('roo-layout-panel')) {
559 var layout_childs = child_nodes[i].childNodes;
561 for(var j = 0; j < layout_childs.length; j++) {
563 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
565 var layout_body_childs = layout_childs[j].childNodes;
567 for(var k = 0; k < layout_body_childs.length; k++) {
569 if(layout_body_childs[k].classList.contains('navbar')) {
570 child_height += layout_body_childs[k].offsetHeight;
574 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
576 var layout_body_tab_childs = layout_body_childs[k].childNodes;
578 for(var m = 0; m < layout_body_tab_childs.length; m++) {
580 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
581 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
596 child_height += child_nodes[i].offsetHeight;
597 // Roo.log(child_nodes[i].offsetHeight);
602 toggleHeaderInput : function(is_edit)
604 if (!this.editableTitle) {
605 return; // not editable.
607 if (is_edit && this.is_header_editing) {
608 return; // already editing..
612 this.headerEditEl.dom.value = this.title;
613 this.headerEditEl.removeClass('d-none');
614 this.headerEditEl.dom.focus();
615 this.titleEl.addClass('d-none');
617 this.is_header_editing = true;
620 // flip back to not editing.
621 this.title = this.headerEditEl.dom.value;
622 this.headerEditEl.addClass('d-none');
623 this.titleEl.removeClass('d-none');
624 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
625 this.is_header_editing = false;
626 this.fireEvent('titlechanged', this, this.title);
635 Roo.apply(Roo.bootstrap.Modal, {
637 * Button config that displays a single OK button
646 * Button config that displays Yes and No buttons
662 * Button config that displays OK and Cancel buttons
677 * Button config that displays Yes, No and Cancel buttons