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
16 * @cfg {Boolean} animate default true
17 * @cfg {Boolean} allow_close default true
18 * @cfg {Boolean} fitwindow default false
19 * @cfg {String} size (sm|lg) default empty
20 * @cfg {Number} max_width set the max width of modal
24 * Create a new Modal Dialog
25 * @param {Object} config The config object
28 Roo.bootstrap.Modal = function(config){
29 Roo.bootstrap.Modal.superclass.constructor.call(this, config);
34 * The raw btnclick event for the button
35 * @param {Roo.EventObject} e
40 * Fire when dialog resize
41 * @param {Roo.bootstrap.Modal} this
42 * @param {Roo.EventObject} e
46 this.buttons = this.buttons || [];
49 this.tmpl = Roo.factory(this.tmpl);
54 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component, {
56 title : 'test dialog',
68 buttonPosition: 'right',
91 onRender : function(ct, position)
93 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
96 var cfg = Roo.apply({}, this.getAutoCreate());
99 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
101 //if (!cfg.name.length) {
105 cfg.cls += ' ' + this.cls;
108 cfg.style = this.style;
110 this.el = Roo.get(document.body).createChild(cfg, position);
112 //var type = this.el.dom.type;
115 if(this.tabIndex !== undefined){
116 this.el.dom.setAttribute('tabIndex', this.tabIndex);
119 this.dialogEl = this.el.select('.modal-dialog',true).first();
120 this.bodyEl = this.el.select('.modal-body',true).first();
121 this.closeEl = this.el.select('.modal-header .close', true).first();
122 this.headerEl = this.el.select('.modal-header',true).first();
123 this.titleEl = this.el.select('.modal-title',true).first();
124 this.footerEl = this.el.select('.modal-footer',true).first();
126 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
128 //this.el.addClass("x-dlg-modal");
130 if (this.buttons.length) {
131 Roo.each(this.buttons, function(bb) {
132 var b = Roo.apply({}, bb);
133 b.xns = b.xns || Roo.bootstrap;
134 b.xtype = b.xtype || 'Button';
135 if (typeof(b.listeners) == 'undefined') {
136 b.listeners = { click : this.onButtonClick.createDelegate(this) };
139 var btn = Roo.factory(b);
141 btn.render(this.el.select('.modal-footer div').first());
145 // render the children.
148 if(typeof(this.items) != 'undefined'){
149 var items = this.items;
152 for(var i =0;i < items.length;i++) {
153 nitems.push(this.addxtype(Roo.apply({}, items[i])));
159 // where are these used - they used to be body/close/footer
163 //this.el.addClass([this.fieldClass, this.cls]);
167 getAutoCreate : function()
171 html : this.html || ''
180 if(this.specificTitle){
186 if (this.allow_close) {
198 if(this.size.length){
199 size = 'modal-' + this.size;
206 cls: "modal-dialog " + size,
209 cls : "modal-content",
212 cls : 'modal-header',
217 cls : 'modal-footer',
221 cls: 'btn-' + this.buttonPosition
238 modal.cls += ' fade';
244 getChildContainer : function() {
249 getButtonContainer : function() {
250 return this.el.select('.modal-footer div',true).first();
253 initEvents : function()
255 if (this.allow_close) {
256 this.closeEl.on('click', this.hide, this);
258 Roo.EventManager.onWindowResize(this.resize, this, true);
266 Roo.lib.Dom.getViewWidth(true),
267 Roo.lib.Dom.getViewHeight(true)
270 if (this.fitwindow) {
272 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
273 this.height || Roo.lib.Dom.getViewportHeight(true) - 60
278 if(this.max_width !== 0) {
280 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
283 this.setSize(w, this.height);
287 if(this.max_height) {
288 this.setSize(w,Math.min(
290 Roo.lib.Dom.getViewportHeight(true) - 60
296 if(!this.fit_content) {
297 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
301 this.setSize(w, Math.min(
303 this.headerEl.getHeight() +
304 this.footerEl.getHeight() +
305 this.getChildHeight(this.bodyEl.dom.childNodes),
306 Roo.lib.Dom.getViewportHeight(true) - 60)
312 setSize : function(w,h)
323 if (!this.rendered) {
327 //this.el.setStyle('display', 'block');
328 this.el.removeClass('hideing');
329 this.el.addClass('show');
331 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
334 this.el.addClass('in');
337 this.el.addClass('in');
340 // not sure how we can show data in here..
342 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
345 Roo.get(document.body).addClass("x-body-masked");
347 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
348 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
349 this.maskEl.addClass('show');
353 this.fireEvent('show', this);
355 // set zindex here - otherwise it appears to be ignored...
356 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
359 this.items.forEach( function(e) {
360 e.layout ? e.layout() : false;
368 if(this.fireEvent("beforehide", this) !== false){
369 this.maskEl.removeClass('show');
370 Roo.get(document.body).removeClass("x-body-masked");
371 this.el.removeClass('in');
372 this.el.select('.modal-dialog', true).first().setStyle('transform','');
374 if(this.animate){ // why
375 this.el.addClass('hideing');
377 if (!this.el.hasClass('hideing')) {
378 return; // it's been shown again...
380 this.el.removeClass('show');
381 this.el.removeClass('hideing');
385 this.el.removeClass('show');
387 this.fireEvent('hide', this);
390 isVisible : function()
393 return this.el.hasClass('show') && !this.el.hasClass('hideing');
397 addButton : function(str, cb)
401 var b = Roo.apply({}, { html : str } );
402 b.xns = b.xns || Roo.bootstrap;
403 b.xtype = b.xtype || 'Button';
404 if (typeof(b.listeners) == 'undefined') {
405 b.listeners = { click : cb.createDelegate(this) };
408 var btn = Roo.factory(b);
410 btn.render(this.el.select('.modal-footer div').first());
416 setDefaultButton : function(btn)
418 //this.el.select('.modal-footer').()
422 resizeTo: function(w,h)
426 this.dialogEl.setWidth(w);
427 if (this.diff === false) {
428 this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
431 this.bodyEl.setHeight(h - this.diff);
433 this.fireEvent('resize', this);
436 setContentSize : function(w, h)
440 onButtonClick: function(btn,e)
443 this.fireEvent('btnclick', btn.name, e);
446 * Set the title of the Dialog
447 * @param {String} str new Title
449 setTitle: function(str) {
450 this.titleEl.dom.innerHTML = str;
453 * Set the body of the Dialog
454 * @param {String} str new Title
456 setBody: function(str) {
457 this.bodyEl.dom.innerHTML = str;
460 * Set the body of the Dialog using the template
461 * @param {Obj} data - apply this data to the template and replace the body contents.
463 applyBody: function(obj)
466 Roo.log("Error - using apply Body without a template");
469 this.tmpl.overwrite(this.bodyEl, obj);
472 getChildHeight : function(child_nodes)
476 child_nodes.length == 0
481 var child_height = 0;
483 for(var i = 0; i < child_nodes.length; i++) {
486 * for modal with tabs...
487 if(child_nodes[i].classList.contains('roo-layout-panel')) {
489 var layout_childs = child_nodes[i].childNodes;
491 for(var j = 0; j < layout_childs.length; j++) {
493 if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
495 var layout_body_childs = layout_childs[j].childNodes;
497 for(var k = 0; k < layout_body_childs.length; k++) {
499 if(layout_body_childs[k].classList.contains('navbar')) {
500 child_height += layout_body_childs[k].offsetHeight;
504 if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
506 var layout_body_tab_childs = layout_body_childs[k].childNodes;
508 for(var m = 0; m < layout_body_tab_childs.length; m++) {
510 if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
511 child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
526 child_height += child_nodes[i].offsetHeight;
527 // Roo.log(child_nodes[i].offsetHeight);
536 Roo.apply(Roo.bootstrap.Modal, {
538 * Button config that displays a single OK button
547 * Button config that displays Yes and No buttons
563 * Button config that displays OK and Cancel buttons
578 * Button config that displays Yes, No and Cancel buttons