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',
89 onRender : function(ct, position)
91 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
94 var cfg = Roo.apply({}, this.getAutoCreate());
97 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
99 //if (!cfg.name.length) {
103 cfg.cls += ' ' + this.cls;
106 cfg.style = this.style;
108 this.el = Roo.get(document.body).createChild(cfg, position);
110 //var type = this.el.dom.type;
113 if(this.tabIndex !== undefined){
114 this.el.dom.setAttribute('tabIndex', this.tabIndex);
117 this.dialogEl = this.el.select('.modal-dialog',true).first();
118 this.bodyEl = this.el.select('.modal-body',true).first();
119 this.closeEl = this.el.select('.modal-header .close', true).first();
120 this.headerEl = this.el.select('.modal-header',true).first();
121 this.titleEl = this.el.select('.modal-title',true).first();
122 this.footerEl = this.el.select('.modal-footer',true).first();
124 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
126 //this.el.addClass("x-dlg-modal");
128 if (this.buttons.length) {
129 Roo.each(this.buttons, function(bb) {
130 var b = Roo.apply({}, bb);
131 b.xns = b.xns || Roo.bootstrap;
132 b.xtype = b.xtype || 'Button';
133 if (typeof(b.listeners) == 'undefined') {
134 b.listeners = { click : this.onButtonClick.createDelegate(this) };
137 var btn = Roo.factory(b);
139 btn.render(this.el.select('.modal-footer div').first());
143 // render the children.
146 if(typeof(this.items) != 'undefined'){
147 var items = this.items;
150 for(var i =0;i < items.length;i++) {
151 nitems.push(this.addxtype(Roo.apply({}, items[i])));
157 // where are these used - they used to be body/close/footer
161 //this.el.addClass([this.fieldClass, this.cls]);
165 getAutoCreate : function()
169 html : this.html || ''
178 if(this.specificTitle){
184 if (this.allow_close) {
196 if(this.size.length){
197 size = 'modal-' + this.size;
204 cls: "modal-dialog " + size,
207 cls : "modal-content",
210 cls : 'modal-header',
215 cls : 'modal-footer',
219 cls: 'btn-' + this.buttonPosition
236 modal.cls += ' fade';
242 getChildContainer : function() {
247 getButtonContainer : function() {
248 return this.el.select('.modal-footer div',true).first();
251 initEvents : function()
253 if (this.allow_close) {
254 this.closeEl.on('click', this.hide, this);
256 Roo.EventManager.onWindowResize(this.resize, this, true);
263 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
265 if (this.fitwindow) {
266 var w = this.width || Roo.lib.Dom.getViewportWidth(true) - 30;
267 var h = this.height || Roo.lib.Dom.getViewportHeight(true) - 60;
271 if(!this.fitwindow && this.max_width !== 0){
273 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
276 this.setSize(w, this.height);
280 var view_height = Roo.lib.Dom.getViewportHeight(true) - 60;
281 var body_childs = this.bodyEl.dom.childNodes;
282 var full_height = this.headerEl.getHeight() + this.footerEl.getHeight();
283 for(var i = 0; i < body_childs.length; i++) {
284 full_height += body_childs[i].offsetHeight;
287 this.setSize(w, Math.min(full_height, view_height));
292 setSize : function(w,h)
302 if (!this.rendered) {
306 //this.el.setStyle('display', 'block');
307 this.el.removeClass('hideing');
308 this.el.addClass('show');
310 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
313 this.el.addClass('in');
316 this.el.addClass('in');
319 // not sure how we can show data in here..
321 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
324 Roo.get(document.body).addClass("x-body-masked");
326 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
327 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
328 this.maskEl.addClass('show');
332 this.fireEvent('show', this);
334 // set zindex here - otherwise it appears to be ignored...
335 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
338 this.items.forEach( function(e) {
339 e.layout ? e.layout() : false;
347 if(this.fireEvent("beforehide", this) !== false){
348 this.maskEl.removeClass('show');
349 Roo.get(document.body).removeClass("x-body-masked");
350 this.el.removeClass('in');
351 this.el.select('.modal-dialog', true).first().setStyle('transform','');
353 if(this.animate){ // why
354 this.el.addClass('hideing');
356 if (!this.el.hasClass('hideing')) {
357 return; // it's been shown again...
359 this.el.removeClass('show');
360 this.el.removeClass('hideing');
364 this.el.removeClass('show');
366 this.fireEvent('hide', this);
369 isVisible : function()
372 return this.el.hasClass('show') && !this.el.hasClass('hideing');
376 addButton : function(str, cb)
380 var b = Roo.apply({}, { html : str } );
381 b.xns = b.xns || Roo.bootstrap;
382 b.xtype = b.xtype || 'Button';
383 if (typeof(b.listeners) == 'undefined') {
384 b.listeners = { click : cb.createDelegate(this) };
387 var btn = Roo.factory(b);
389 btn.render(this.el.select('.modal-footer div').first());
395 setDefaultButton : function(btn)
397 //this.el.select('.modal-footer').()
401 resizeTo: function(w,h)
405 this.dialogEl.setWidth(w);
406 if (this.diff === false) {
407 this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
410 this.bodyEl.setHeight(h-this.diff);
412 this.fireEvent('resize', this);
415 setContentSize : function(w, h)
419 onButtonClick: function(btn,e)
422 this.fireEvent('btnclick', btn.name, e);
425 * Set the title of the Dialog
426 * @param {String} str new Title
428 setTitle: function(str) {
429 this.titleEl.dom.innerHTML = str;
432 * Set the body of the Dialog
433 * @param {String} str new Title
435 setBody: function(str) {
436 this.bodyEl.dom.innerHTML = str;
439 * Set the body of the Dialog using the template
440 * @param {Obj} data - apply this data to the template and replace the body contents.
442 applyBody: function(obj)
445 Roo.log("Error - using apply Body without a template");
448 this.tmpl.overwrite(this.bodyEl, obj);
454 Roo.apply(Roo.bootstrap.Modal, {
456 * Button config that displays a single OK button
465 * Button config that displays Yes and No buttons
481 * Button config that displays OK and Cancel buttons
496 * Button config that displays Yes, No and Cancel buttons