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',
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 enable-modal-body-overflow ',
185 html : this.html || ''
194 if(this.specificTitle){
200 if (this.allow_close && Roo.bootstrap.version == 3) {
210 if (this.allow_close && Roo.bootstrap.version == 4) {
220 if(this.size.length){
221 size = 'modal-' + this.size;
224 var footer = Roo.bootstrap.version == 3 ?
226 cls : 'modal-footer',
230 cls: 'btn-' + this.buttonPosition
235 { // BS4 uses mr-auto on left buttons....
247 cls: "modal-dialog " + size,
250 cls : "modal-content",
253 cls : 'modal-header',
268 modal.cls += ' fade';
274 getChildContainer : function() {
279 getButtonContainer : function() {
281 return Roo.bootstrap.version == 4 ?
282 this.el.select('.modal-footer',true).first()
283 : this.el.select('.modal-footer div',true).first();
286 initEvents : function()
288 if (this.allow_close) {
289 this.closeEl.on('click', this.hide, this);
291 Roo.EventManager.onWindowResize(this.resize, this, true);
300 Roo.lib.Dom.getViewWidth(true),
301 Roo.lib.Dom.getViewHeight(true)
304 if (this.fitwindow) {
308 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
309 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
314 if(this.max_width !== 0) {
316 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
319 this.setSize(w, this.height);
323 if(this.max_height) {
324 this.setSize(w,Math.min(
326 Roo.lib.Dom.getViewportHeight(true) - 60
332 if(!this.fit_content) {
333 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
337 this.setSize(w, Math.min(
339 this.headerEl.getHeight() +
340 this.footerEl.getHeight() +
341 this.getChildHeight(this.bodyEl.dom.childNodes),
342 Roo.lib.Dom.getViewportHeight(true) - 60)
348 setSize : function(w,h)
359 if (!this.rendered) {
363 //this.el.setStyle('display', 'block');
364 this.el.removeClass('hideing');
365 this.el.dom.style.display='block';
367 Roo.get(document.body).addClass('modal-open');
369 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
372 this.el.addClass('show');
373 this.el.addClass('in');
376 this.el.addClass('show');
377 this.el.addClass('in');
380 // not sure how we can show data in here..
382 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
385 Roo.get(document.body).addClass("x-body-masked");
387 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
388 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
389 this.maskEl.dom.style.display = 'block';
390 this.maskEl.addClass('show');
395 this.fireEvent('show', this);
397 // set zindex here - otherwise it appears to be ignored...
398 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
401 this.items.forEach( function(e) {
402 e.layout ? e.layout() : false;
410 if(this.fireEvent("beforehide", this) !== false){
412 this.maskEl.removeClass('show');
414 this.maskEl.dom.style.display = '';
415 Roo.get(document.body).removeClass("x-body-masked");
416 this.el.removeClass('in');
417 this.el.select('.modal-dialog', true).first().setStyle('transform','');
419 if(this.animate){ // why
420 this.el.addClass('hideing');
421 this.el.removeClass('show');
423 if (!this.el.hasClass('hideing')) {
424 return; // it's been shown again...
427 this.el.dom.style.display='';
429 Roo.get(document.body).removeClass('modal-open');
430 this.el.removeClass('hideing');
434 this.el.removeClass('show');
435 this.el.dom.style.display='';
436 Roo.get(document.body).removeClass('modal-open');
439 this.fireEvent('hide', this);
442 isVisible : function()
445 return this.el.hasClass('show') && !this.el.hasClass('hideing');
449 addButton : function(str, cb)
453 var b = Roo.apply({}, { html : str } );
454 b.xns = b.xns || Roo.bootstrap;
455 b.xtype = b.xtype || 'Button';
456 if (typeof(b.listeners) == 'undefined') {
457 b.listeners = { click : cb.createDelegate(this) };
460 var btn = Roo.factory(b);
462 btn.render(this.getButtonContainer());
468 setDefaultButton : function(btn)
470 //this.el.select('.modal-footer').()
473 resizeTo: function(w,h)
475 this.dialogEl.setWidth(w);
477 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
479 this.bodyEl.setHeight(h - diff);
481 this.fireEvent('resize', this);
484 setContentSize : function(w, h)
488 onButtonClick: function(btn,e)
491 this.fireEvent('btnclick', btn.name, e);
494 * Set the title of the Dialog
495 * @param {String} str new Title
497 setTitle: function(str) {
498 this.titleEl.dom.innerHTML = str;
501 * Set the body of the Dialog
502 * @param {String} str new Title
504 setBody: function(str) {
505 this.bodyEl.dom.innerHTML = str;
508 * Set the body of the Dialog using the template
509 * @param {Obj} data - apply this data to the template and replace the body contents.
511 applyBody: function(obj)
514 Roo.log("Error - using apply Body without a template");
517 this.tmpl.overwrite(this.bodyEl, obj);
520 getChildHeight : function(child_nodes)
524 child_nodes.length == 0
529 var child_height = 0;
531 for(var i = 0; i < child_nodes.length; i++) {
534 * for modal with tabs...
535 if(child_nodes[i].classList.contains('roo-layout-panel')) {
537 var layout_childs = child_nodes[i].childNodes;
539 for(var j = 0; j < layout_childs.length; j++) {
541 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
543 var layout_body_childs = layout_childs[j].childNodes;
545 for(var k = 0; k < layout_body_childs.length; k++) {
547 if(layout_body_childs[k].classList.contains('navbar')) {
548 child_height += layout_body_childs[k].offsetHeight;
552 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
554 var layout_body_tab_childs = layout_body_childs[k].childNodes;
556 for(var m = 0; m < layout_body_tab_childs.length; m++) {
558 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
559 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
574 child_height += child_nodes[i].offsetHeight;
575 // Roo.log(child_nodes[i].offsetHeight);
584 Roo.apply(Roo.bootstrap.Modal, {
586 * Button config that displays a single OK button
595 * Button config that displays Yes and No buttons
611 * Button config that displays OK and Cancel buttons
626 * Button config that displays Yes, No and Cancel buttons