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) default empty
22 * @cfg {Number} max_width set the max width of modal
23 * @cfg {Boolean} editable_title 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} e
54 * @param {Roo.EventObject} value
56 "titlechanged" : true,
59 this.buttons = this.buttons || [];
62 this.tmpl = Roo.factory(this.tmpl);
67 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
69 title : 'test dialog',
81 buttonPosition: 'right',
103 editable_title : false,
105 onRender : function(ct, position)
107 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
110 var cfg = Roo.apply({}, this.getAutoCreate());
113 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
115 //if (!cfg.name.length) {
119 cfg.cls += ' ' + this.cls;
122 cfg.style = this.style;
124 this.el = Roo.get(document.body).createChild(cfg, position);
126 //var type = this.el.dom.type;
129 if(this.tabIndex !== undefined){
130 this.el.dom.setAttribute('tabIndex', this.tabIndex);
133 this.dialogEl = this.el.select('.modal-dialog',true).first();
134 this.bodyEl = this.el.select('.modal-body',true).first();
135 this.closeEl = this.el.select('.modal-header .close', true).first();
136 this.headerEl = this.el.select('.modal-header',true).first();
137 this.titleEl = this.el.select('.modal-title',true).first();
138 this.footerEl = this.el.select('.modal-footer',true).first();
140 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
142 //this.el.addClass("x-dlg-modal");
144 if (this.buttons.length) {
145 Roo.each(this.buttons, function(bb) {
146 var b = Roo.apply({}, bb);
147 b.xns = b.xns || Roo.bootstrap;
148 b.xtype = b.xtype || 'Button';
149 if (typeof(b.listeners) == 'undefined') {
150 b.listeners = { click : this.onButtonClick.createDelegate(this) };
153 var btn = Roo.factory(b);
155 btn.render(this.getButtonContainer());
159 // render the children.
162 if(typeof(this.items) != 'undefined'){
163 var items = this.items;
166 for(var i =0;i < items.length;i++) {
167 nitems.push(this.addxtype(Roo.apply({}, items[i])));
173 // where are these used - they used to be body/close/footer
177 //this.el.addClass([this.fieldClass, this.cls]);
181 getAutoCreate : function()
183 // we will default to modal-body-overflow - might need to remove or make optional later.
185 cls : 'modal-body enable-modal-body-overflow ',
186 html : this.html || ''
195 if(this.specificTitle){
201 if (this.allow_close && Roo.bootstrap.version == 3) {
211 if (this.allow_close && Roo.bootstrap.version == 4) {
221 if(this.size.length){
222 size = 'modal-' + this.size;
225 var footer = Roo.bootstrap.version == 3 ?
227 cls : 'modal-footer',
231 cls: 'btn-' + this.buttonPosition
236 { // BS4 uses mr-auto on left buttons....
248 cls: "modal-dialog " + size,
251 cls : "modal-content",
254 cls : 'modal-header',
269 modal.cls += ' fade';
275 getChildContainer : function() {
280 getButtonContainer : function() {
282 return Roo.bootstrap.version == 4 ?
283 this.el.select('.modal-footer',true).first()
284 : this.el.select('.modal-footer div',true).first();
287 initEvents : function()
289 if (this.allow_close) {
290 this.closeEl.on('click', this.hide, this);
292 Roo.EventManager.onWindowResize(this.resize, this, true);
293 if (this.editable_title) {
294 this.headerEl.on('click', this.showHeaderInput, this);
303 Roo.lib.Dom.getViewWidth(true),
304 Roo.lib.Dom.getViewHeight(true)
307 if (this.fitwindow) {
311 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
312 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
317 if(this.max_width !== 0) {
319 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
322 this.setSize(w, this.height);
326 if(this.max_height) {
327 this.setSize(w,Math.min(
329 Roo.lib.Dom.getViewportHeight(true) - 60
335 if(!this.fit_content) {
336 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
340 this.setSize(w, Math.min(
342 this.headerEl.getHeight() +
343 this.footerEl.getHeight() +
344 this.getChildHeight(this.bodyEl.dom.childNodes),
345 Roo.lib.Dom.getViewportHeight(true) - 60)
351 setSize : function(w,h)
362 if (!this.rendered) {
366 //this.el.setStyle('display', 'block');
367 this.el.removeClass('hideing');
368 this.el.dom.style.display='block';
370 Roo.get(document.body).addClass('modal-open');
372 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
375 this.el.addClass('show');
376 this.el.addClass('in');
379 this.el.addClass('show');
380 this.el.addClass('in');
383 // not sure how we can show data in here..
385 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
388 Roo.get(document.body).addClass("x-body-masked");
390 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
391 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
392 this.maskEl.dom.style.display = 'block';
393 this.maskEl.addClass('show');
398 this.fireEvent('show', this);
400 // set zindex here - otherwise it appears to be ignored...
401 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
404 this.items.forEach( function(e) {
405 e.layout ? e.layout() : false;
413 if(this.fireEvent("beforehide", this) !== false){
415 this.maskEl.removeClass('show');
417 this.maskEl.dom.style.display = '';
418 Roo.get(document.body).removeClass("x-body-masked");
419 this.el.removeClass('in');
420 this.el.select('.modal-dialog', true).first().setStyle('transform','');
422 if(this.animate){ // why
423 this.el.addClass('hideing');
424 this.el.removeClass('show');
426 if (!this.el.hasClass('hideing')) {
427 return; // it's been shown again...
430 this.el.dom.style.display='';
432 Roo.get(document.body).removeClass('modal-open');
433 this.el.removeClass('hideing');
437 this.el.removeClass('show');
438 this.el.dom.style.display='';
439 Roo.get(document.body).removeClass('modal-open');
442 this.fireEvent('hide', this);
445 isVisible : function()
448 return this.el.hasClass('show') && !this.el.hasClass('hideing');
452 addButton : function(str, cb)
456 var b = Roo.apply({}, { html : str } );
457 b.xns = b.xns || Roo.bootstrap;
458 b.xtype = b.xtype || 'Button';
459 if (typeof(b.listeners) == 'undefined') {
460 b.listeners = { click : cb.createDelegate(this) };
463 var btn = Roo.factory(b);
465 btn.render(this.getButtonContainer());
471 setDefaultButton : function(btn)
473 //this.el.select('.modal-footer').()
476 resizeTo: function(w,h)
478 this.dialogEl.setWidth(w);
480 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
482 this.bodyEl.setHeight(h - diff);
484 this.fireEvent('resize', this);
487 setContentSize : function(w, h)
491 onButtonClick: function(btn,e)
494 this.fireEvent('btnclick', btn.name, e);
497 * Set the title of the Dialog
498 * @param {String} str new Title
500 setTitle: function(str) {
501 this.titleEl.dom.innerHTML = str;
504 * Set the body of the Dialog
505 * @param {String} str new Title
507 setBody: function(str) {
508 this.bodyEl.dom.innerHTML = str;
511 * Set the body of the Dialog using the template
512 * @param {Obj} data - apply this data to the template and replace the body contents.
514 applyBody: function(obj)
517 Roo.log("Error - using apply Body without a template");
520 this.tmpl.overwrite(this.bodyEl, obj);
523 getChildHeight : function(child_nodes)
527 child_nodes.length == 0
532 var child_height = 0;
534 for(var i = 0; i < child_nodes.length; i++) {
537 * for modal with tabs...
538 if(child_nodes[i].classList.contains('roo-layout-panel')) {
540 var layout_childs = child_nodes[i].childNodes;
542 for(var j = 0; j < layout_childs.length; j++) {
544 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
546 var layout_body_childs = layout_childs[j].childNodes;
548 for(var k = 0; k < layout_body_childs.length; k++) {
550 if(layout_body_childs[k].classList.contains('navbar')) {
551 child_height += layout_body_childs[k].offsetHeight;
555 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
557 var layout_body_tab_childs = layout_body_childs[k].childNodes;
559 for(var m = 0; m < layout_body_tab_childs.length; m++) {
561 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
562 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
577 child_height += child_nodes[i].offsetHeight;
578 // Roo.log(child_nodes[i].offsetHeight);
583 showHeaderInput : function() {
592 Roo.apply(Roo.bootstrap.Modal, {
594 * Button config that displays a single OK button
603 * Button config that displays Yes and No buttons
619 * Button config that displays OK and Cancel buttons
634 * Button config that displays Yes, No and Cancel buttons