7 * @class Roo.bootstrap.Modal
8 * @extends Roo.bootstrap.Component
9 * Bootstrap Modal class
10 * @cfg {String} title Title of dialog
11 * @cfg {String} html - the body of the dialog (for simple ones) - you can also use template..
12 * @cfg {Roo.Template} tmpl - a template with variables. to use it, add a handler in show:method adn
13 * @cfg {Boolean} specificTitle default false
14 * @cfg {Array} buttons Array of buttons or standard button set..
15 * @cfg {String} buttonPosition (left|right|center) default right (DEPRICATED) - use mr-auto on buttons to put them on the left
16 * @cfg {Boolean} animate default true
17 * @cfg {Boolean} allow_close default true
18 * @cfg {Boolean} fitwindow default false
19 * @cfg {Number} width fixed width - usefull for chrome extension only really.
20 * @cfg {Number} height fixed height - usefull for chrome extension only really.
21 * @cfg {String} size (sm|lg|xl) default empty
22 * @cfg {Number} max_width set the max width of modal
23 * @cfg {Boolean} editableTitle can the title be edited
28 * Create a new Modal Dialog
29 * @param {Object} config The config object
32 Roo.bootstrap.Modal = function(config){
33 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
38 * The raw btnclick event for the button
39 * @param {Roo.EventObject} e
44 * Fire when dialog resize
45 * @param {Roo.bootstrap.Modal} this
46 * @param {Roo.EventObject} e
51 * Fire when the editable title has been changed
52 * @param {Roo.bootstrap.Modal} this
53 * @param {Roo.EventObject} value
58 this.buttons = this.buttons || [];
61 this.tmpl = Roo.factory(this.tmpl);
66 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
68 title : 'test dialog',
80 buttonPosition: 'right',
102 editableTitle : false,
104 onRender : function(ct, position)
106 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
109 var cfg = Roo.apply({}, this.getAutoCreate());
112 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
114 //if (!cfg.name.length) {
118 cfg.cls += ' ' + this.cls;
121 cfg.style = this.style;
123 this.el = Roo.get(document.body).createChild(cfg, position);
125 //var type = this.el.dom.type;
128 if(this.tabIndex !== undefined){
129 this.el.dom.setAttribute('tabIndex', this.tabIndex);
132 this.dialogEl = this.el.select('.modal-dialog',true).first();
133 this.bodyEl = this.el.select('.modal-body',true).first();
134 this.closeEl = this.el.select('.modal-header .close', true).first();
135 this.headerEl = this.el.select('.modal-header',true).first();
136 this.titleEl = this.el.select('.modal-title',true).first();
137 this.footerEl = this.el.select('.modal-footer',true).first();
139 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
141 //this.el.addClass("x-dlg-modal");
143 if (this.buttons.length) {
144 Roo.each(this.buttons, function(bb) {
145 var b = Roo.apply({}, bb);
146 b.xns = b.xns || Roo.bootstrap;
147 b.xtype = b.xtype || 'Button';
148 if (typeof(b.listeners) == 'undefined') {
149 b.listeners = { click : this.onButtonClick.createDelegate(this) };
152 var btn = Roo.factory(b);
154 btn.render(this.getButtonContainer());
158 // render the children.
161 if(typeof(this.items) != 'undefined'){
162 var items = this.items;
165 for(var i =0;i < items.length;i++) {
166 nitems.push(this.addxtype(Roo.apply({}, items[i])));
172 // where are these used - they used to be body/close/footer
176 //this.el.addClass([this.fieldClass, this.cls]);
180 getAutoCreate : function()
182 // we will default to modal-body-overflow - might need to remove or make optional later.
184 cls : 'modal-body ' + this.fitwindow ? 'overflow-auto' : '',
185 html : this.html || ''
194 if(this.specificTitle){ // WTF is this?
199 if (this.allow_close && Roo.bootstrap.version == 3) {
209 if (this.editableTitle) {
211 cls: 'form-control roo-editable-title d-none',
217 if (this.allow_close && Roo.bootstrap.version == 4) {
227 if(this.size.length){
228 size = 'modal-' + this.size;
231 var footer = Roo.bootstrap.version == 3 ?
233 cls : 'modal-footer',
237 cls: 'btn-' + this.buttonPosition
242 { // BS4 uses mr-auto on left buttons....
254 cls: "modal-dialog " + size,
257 cls : "modal-content",
260 cls : 'modal-header',
275 modal.cls += ' fade';
281 getChildContainer : function() {
286 getButtonContainer : function() {
288 return Roo.bootstrap.version == 4 ?
289 this.el.select('.modal-footer',true).first()
290 : this.el.select('.modal-footer div',true).first();
293 initEvents : function()
295 if (this.allow_close) {
296 this.closeEl.on('click', this.hide, this);
298 Roo.EventManager.onWindowResize(this.resize, this, true);
299 if (this.editableTitle) {
300 this.headerEditEl = this.headerEl.select('.form-control',true).first();
301 this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
302 this.headerEditEl.on('keyup', function(e) {
303 if([ e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
304 this.toggleHeaderInput(false)
307 this.headerEditEl.on('blur', function(e) {
308 this.toggleHeaderInput(false)
318 Roo.lib.Dom.getViewWidth(true),
319 Roo.lib.Dom.getViewHeight(true)
322 if (this.fitwindow) {
326 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
327 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
332 if(this.max_width !== 0) {
334 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
337 this.setSize(w, this.height);
341 if(this.max_height) {
342 this.setSize(w,Math.min(
344 Roo.lib.Dom.getViewportHeight(true) - 60
350 if(!this.fit_content) {
351 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
355 this.setSize(w, Math.min(
357 this.headerEl.getHeight() +
358 this.footerEl.getHeight() +
359 this.getChildHeight(this.bodyEl.dom.childNodes),
360 Roo.lib.Dom.getViewportHeight(true) - 60)
366 setSize : function(w,h)
377 if (!this.rendered) {
380 this.toggleHeaderInput(false);
381 //this.el.setStyle('display', 'block');
382 this.el.removeClass('hideing');
383 this.el.dom.style.display='block';
385 Roo.get(document.body).addClass('modal-open');
387 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
390 this.el.addClass('show');
391 this.el.addClass('in');
394 this.el.addClass('show');
395 this.el.addClass('in');
398 // not sure how we can show data in here..
400 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
403 Roo.get(document.body).addClass("x-body-masked");
405 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
406 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
407 this.maskEl.dom.style.display = 'block';
408 this.maskEl.addClass('show');
413 this.fireEvent('show', this);
415 // set zindex here - otherwise it appears to be ignored...
416 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
419 this.items.forEach( function(e) {
420 e.layout ? e.layout() : false;
428 if(this.fireEvent("beforehide", this) !== false){
430 this.maskEl.removeClass('show');
432 this.maskEl.dom.style.display = '';
433 Roo.get(document.body).removeClass("x-body-masked");
434 this.el.removeClass('in');
435 this.el.select('.modal-dialog', true).first().setStyle('transform','');
437 if(this.animate){ // why
438 this.el.addClass('hideing');
439 this.el.removeClass('show');
441 if (!this.el.hasClass('hideing')) {
442 return; // it's been shown again...
445 this.el.dom.style.display='';
447 Roo.get(document.body).removeClass('modal-open');
448 this.el.removeClass('hideing');
452 this.el.removeClass('show');
453 this.el.dom.style.display='';
454 Roo.get(document.body).removeClass('modal-open');
457 this.fireEvent('hide', this);
460 isVisible : function()
463 return this.el.hasClass('show') && !this.el.hasClass('hideing');
467 addButton : function(str, cb)
471 var b = Roo.apply({}, { html : str } );
472 b.xns = b.xns || Roo.bootstrap;
473 b.xtype = b.xtype || 'Button';
474 if (typeof(b.listeners) == 'undefined') {
475 b.listeners = { click : cb.createDelegate(this) };
478 var btn = Roo.factory(b);
480 btn.render(this.getButtonContainer());
486 setDefaultButton : function(btn)
488 //this.el.select('.modal-footer').()
491 resizeTo: function(w,h)
493 this.dialogEl.setWidth(w);
495 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
497 this.bodyEl.setHeight(h - diff);
499 this.fireEvent('resize', this);
502 setContentSize : function(w, h)
506 onButtonClick: function(btn,e)
509 this.fireEvent('btnclick', btn.name, e);
512 * Set the title of the Dialog
513 * @param {String} str new Title
515 setTitle: function(str) {
516 this.titleEl.dom.innerHTML = str;
520 * Set the body of the Dialog
521 * @param {String} str new Title
523 setBody: function(str) {
524 this.bodyEl.dom.innerHTML = str;
527 * Set the body of the Dialog using the template
528 * @param {Obj} data - apply this data to the template and replace the body contents.
530 applyBody: function(obj)
533 Roo.log("Error - using apply Body without a template");
536 this.tmpl.overwrite(this.bodyEl, obj);
539 getChildHeight : function(child_nodes)
543 child_nodes.length == 0
548 var child_height = 0;
550 for(var i = 0; i < child_nodes.length; i++) {
553 * for modal with tabs...
554 if(child_nodes[i].classList.contains('roo-layout-panel')) {
556 var layout_childs = child_nodes[i].childNodes;
558 for(var j = 0; j < layout_childs.length; j++) {
560 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
562 var layout_body_childs = layout_childs[j].childNodes;
564 for(var k = 0; k < layout_body_childs.length; k++) {
566 if(layout_body_childs[k].classList.contains('navbar')) {
567 child_height += layout_body_childs[k].offsetHeight;
571 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
573 var layout_body_tab_childs = layout_body_childs[k].childNodes;
575 for(var m = 0; m < layout_body_tab_childs.length; m++) {
577 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
578 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
593 child_height += child_nodes[i].offsetHeight;
594 // Roo.log(child_nodes[i].offsetHeight);
599 toggleHeaderInput : function(is_edit)
601 if (!this.editableTitle) {
602 return; // not editable.
604 if (is_edit && this.is_header_editing) {
605 return; // already editing..
609 this.headerEditEl.dom.value = this.title;
610 this.headerEditEl.removeClass('d-none');
611 this.headerEditEl.dom.focus();
612 this.titleEl.addClass('d-none');
614 this.is_header_editing = true;
617 // flip back to not editing.
618 this.title = this.headerEditEl.dom.value;
619 this.headerEditEl.addClass('d-none');
620 this.titleEl.removeClass('d-none');
621 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
622 this.is_header_editing = false;
623 this.fireEvent('titlechanged', this, this.title);
632 Roo.apply(Roo.bootstrap.Modal, {
634 * Button config that displays a single OK button
643 * Button config that displays Yes and No buttons
659 * Button config that displays OK and Cancel buttons
674 * Button config that displays Yes, No and Cancel buttons