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} 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
55 "titlechanged" : true,
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 enable-modal-body-overflow ',
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.showHeaderInput(true) } , this);
302 this.headerEditEl .on('specialkey', function() { this.showHeaderInput(false) } , this);
311 Roo.lib.Dom.getViewWidth(true),
312 Roo.lib.Dom.getViewHeight(true)
315 if (this.fitwindow) {
319 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
320 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
325 if(this.max_width !== 0) {
327 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
330 this.setSize(w, this.height);
334 if(this.max_height) {
335 this.setSize(w,Math.min(
337 Roo.lib.Dom.getViewportHeight(true) - 60
343 if(!this.fit_content) {
344 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
348 this.setSize(w, Math.min(
350 this.headerEl.getHeight() +
351 this.footerEl.getHeight() +
352 this.getChildHeight(this.bodyEl.dom.childNodes),
353 Roo.lib.Dom.getViewportHeight(true) - 60)
359 setSize : function(w,h)
370 if (!this.rendered) {
374 //this.el.setStyle('display', 'block');
375 this.el.removeClass('hideing');
376 this.el.dom.style.display='block';
378 Roo.get(document.body).addClass('modal-open');
380 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
383 this.el.addClass('show');
384 this.el.addClass('in');
387 this.el.addClass('show');
388 this.el.addClass('in');
391 // not sure how we can show data in here..
393 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
396 Roo.get(document.body).addClass("x-body-masked");
398 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
399 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
400 this.maskEl.dom.style.display = 'block';
401 this.maskEl.addClass('show');
406 this.fireEvent('show', this);
408 // set zindex here - otherwise it appears to be ignored...
409 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
412 this.items.forEach( function(e) {
413 e.layout ? e.layout() : false;
421 if(this.fireEvent("beforehide", this) !== false){
423 this.maskEl.removeClass('show');
425 this.maskEl.dom.style.display = '';
426 Roo.get(document.body).removeClass("x-body-masked");
427 this.el.removeClass('in');
428 this.el.select('.modal-dialog', true).first().setStyle('transform','');
430 if(this.animate){ // why
431 this.el.addClass('hideing');
432 this.el.removeClass('show');
434 if (!this.el.hasClass('hideing')) {
435 return; // it's been shown again...
438 this.el.dom.style.display='';
440 Roo.get(document.body).removeClass('modal-open');
441 this.el.removeClass('hideing');
445 this.el.removeClass('show');
446 this.el.dom.style.display='';
447 Roo.get(document.body).removeClass('modal-open');
450 this.fireEvent('hide', this);
453 isVisible : function()
456 return this.el.hasClass('show') && !this.el.hasClass('hideing');
460 addButton : function(str, cb)
464 var b = Roo.apply({}, { html : str } );
465 b.xns = b.xns || Roo.bootstrap;
466 b.xtype = b.xtype || 'Button';
467 if (typeof(b.listeners) == 'undefined') {
468 b.listeners = { click : cb.createDelegate(this) };
471 var btn = Roo.factory(b);
473 btn.render(this.getButtonContainer());
479 setDefaultButton : function(btn)
481 //this.el.select('.modal-footer').()
484 resizeTo: function(w,h)
486 this.dialogEl.setWidth(w);
488 var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30
490 this.bodyEl.setHeight(h - diff);
492 this.fireEvent('resize', this);
495 setContentSize : function(w, h)
499 onButtonClick: function(btn,e)
502 this.fireEvent('btnclick', btn.name, e);
505 * Set the title of the Dialog
506 * @param {String} str new Title
508 setTitle: function(str) {
509 this.titleEl.dom.innerHTML = str;
512 * Set the body of the Dialog
513 * @param {String} str new Title
515 setBody: function(str) {
516 this.bodyEl.dom.innerHTML = str;
519 * Set the body of the Dialog using the template
520 * @param {Obj} data - apply this data to the template and replace the body contents.
522 applyBody: function(obj)
525 Roo.log("Error - using apply Body without a template");
528 this.tmpl.overwrite(this.bodyEl, obj);
531 getChildHeight : function(child_nodes)
535 child_nodes.length == 0
540 var child_height = 0;
542 for(var i = 0; i < child_nodes.length; i++) {
545 * for modal with tabs...
546 if(child_nodes[i].classList.contains('roo-layout-panel')) {
548 var layout_childs = child_nodes[i].childNodes;
550 for(var j = 0; j < layout_childs.length; j++) {
552 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
554 var layout_body_childs = layout_childs[j].childNodes;
556 for(var k = 0; k < layout_body_childs.length; k++) {
558 if(layout_body_childs[k].classList.contains('navbar')) {
559 child_height += layout_body_childs[k].offsetHeight;
563 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
565 var layout_body_tab_childs = layout_body_childs[k].childNodes;
567 for(var m = 0; m < layout_body_tab_childs.length; m++) {
569 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
570 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
585 child_height += child_nodes[i].offsetHeight;
586 // Roo.log(child_nodes[i].offsetHeight);
591 toggleHeaderInput : function(is_edit)
594 if (is_edit && this.is_header_editing) {
595 return; // already editing..
599 this.headerEditEl.setValue(this.title);
600 this.headerEditEl.removeClass('d-none');
601 this.titleEl.addClass('d-none');
602 this.is_header_editing = true;
605 // flip back to not editing.
606 this.title = this.headerEditEl.getValue(this.title);
607 this.headerEditEl.addClass('d-none');
608 this.titleEl.removeClass('d-none');
609 this.titleEl.dom.innerHTML = String.format('{0}', this.title);
610 this.is_header_editing = false;
611 this.fireEvent('titlechanged', this, this.title);
620 Roo.apply(Roo.bootstrap.Modal, {
622 * Button config that displays a single OK button
631 * Button config that displays Yes and No buttons
647 * Button config that displays OK and Cancel buttons
662 * Button config that displays Yes, No and Cancel buttons