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',
90 onRender : function(ct, position)
92 Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
95 var cfg = Roo.apply({}, this.getAutoCreate());
98 // cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
100 //if (!cfg.name.length) {
104 cfg.cls += ' ' + this.cls;
107 cfg.style = this.style;
109 this.el = Roo.get(document.body).createChild(cfg, position);
111 //var type = this.el.dom.type;
114 if(this.tabIndex !== undefined){
115 this.el.dom.setAttribute('tabIndex', this.tabIndex);
118 this.dialogEl = this.el.select('.modal-dialog',true).first();
119 this.bodyEl = this.el.select('.modal-body',true).first();
120 this.closeEl = this.el.select('.modal-header .close', true).first();
121 this.headerEl = this.el.select('.modal-header',true).first();
122 this.titleEl = this.el.select('.modal-title',true).first();
123 this.footerEl = this.el.select('.modal-footer',true).first();
125 this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
127 //this.el.addClass("x-dlg-modal");
129 if (this.buttons.length) {
130 Roo.each(this.buttons, function(bb) {
131 var b = Roo.apply({}, bb);
132 b.xns = b.xns || Roo.bootstrap;
133 b.xtype = b.xtype || 'Button';
134 if (typeof(b.listeners) == 'undefined') {
135 b.listeners = { click : this.onButtonClick.createDelegate(this) };
138 var btn = Roo.factory(b);
140 btn.render(this.el.select('.modal-footer div').first());
144 // render the children.
147 if(typeof(this.items) != 'undefined'){
148 var items = this.items;
151 for(var i =0;i < items.length;i++) {
152 nitems.push(this.addxtype(Roo.apply({}, items[i])));
158 // where are these used - they used to be body/close/footer
162 //this.el.addClass([this.fieldClass, this.cls]);
166 getAutoCreate : function()
170 html : this.html || ''
179 if(this.specificTitle){
185 if (this.allow_close) {
197 if(this.size.length){
198 size = 'modal-' + this.size;
205 cls: "modal-dialog " + size,
208 cls : "modal-content",
211 cls : 'modal-header',
216 cls : 'modal-footer',
220 cls: 'btn-' + this.buttonPosition
237 modal.cls += ' fade';
243 getChildContainer : function() {
248 getButtonContainer : function() {
249 return this.el.select('.modal-footer div',true).first();
252 initEvents : function()
254 if (this.allow_close) {
255 this.closeEl.on('click', this.hide, this);
257 Roo.EventManager.onWindowResize(this.resize, this, true);
265 Roo.lib.Dom.getViewWidth(true),
266 Roo.lib.Dom.getViewHeight(true)
269 if (this.fitwindow) {
271 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
272 this.height || Roo.lib.Dom.getViewportHeight(true) - 60
277 if(this.max_width !== 0) {
279 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
282 this.setSize(w, this.height);
286 if(!this.fit_content) {
287 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
291 var body_childs = this.bodyEl.dom.childNodes;
292 // does not seem to give enough space...
293 var full_height = 60 + this.headerEl.getHeight() + this.footerEl.getHeight();
294 for(var i = 0; i < body_childs.length; i++) {
295 full_height += body_childs[i].offsetHeight;
298 this.setSize(w, Math.min(full_height, Roo.lib.Dom.getViewportHeight(true) - 60));
303 setSize : function(w,h)
313 if (!this.rendered) {
317 //this.el.setStyle('display', 'block');
318 this.el.removeClass('hideing');
319 this.el.addClass('show');
321 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
324 this.el.addClass('in');
327 this.el.addClass('in');
330 // not sure how we can show data in here..
332 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
335 Roo.get(document.body).addClass("x-body-masked");
337 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
338 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
339 this.maskEl.addClass('show');
343 this.fireEvent('show', this);
345 // set zindex here - otherwise it appears to be ignored...
346 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
349 this.items.forEach( function(e) {
350 e.layout ? e.layout() : false;
358 if(this.fireEvent("beforehide", this) !== false){
359 this.maskEl.removeClass('show');
360 Roo.get(document.body).removeClass("x-body-masked");
361 this.el.removeClass('in');
362 this.el.select('.modal-dialog', true).first().setStyle('transform','');
364 if(this.animate){ // why
365 this.el.addClass('hideing');
367 if (!this.el.hasClass('hideing')) {
368 return; // it's been shown again...
370 this.el.removeClass('show');
371 this.el.removeClass('hideing');
375 this.el.removeClass('show');
377 this.fireEvent('hide', this);
380 isVisible : function()
383 return this.el.hasClass('show') && !this.el.hasClass('hideing');
387 addButton : function(str, cb)
391 var b = Roo.apply({}, { html : str } );
392 b.xns = b.xns || Roo.bootstrap;
393 b.xtype = b.xtype || 'Button';
394 if (typeof(b.listeners) == 'undefined') {
395 b.listeners = { click : cb.createDelegate(this) };
398 var btn = Roo.factory(b);
400 btn.render(this.el.select('.modal-footer div').first());
406 setDefaultButton : function(btn)
408 //this.el.select('.modal-footer').()
412 resizeTo: function(w,h)
416 this.dialogEl.setWidth(w);
417 if (this.diff === false) {
418 this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
421 this.bodyEl.setHeight(h-this.diff);
423 this.fireEvent('resize', this);
426 setContentSize : function(w, h)
430 onButtonClick: function(btn,e)
433 this.fireEvent('btnclick', btn.name, e);
436 * Set the title of the Dialog
437 * @param {String} str new Title
439 setTitle: function(str) {
440 this.titleEl.dom.innerHTML = str;
443 * Set the body of the Dialog
444 * @param {String} str new Title
446 setBody: function(str) {
447 this.bodyEl.dom.innerHTML = str;
450 * Set the body of the Dialog using the template
451 * @param {Obj} data - apply this data to the template and replace the body contents.
453 applyBody: function(obj)
456 Roo.log("Error - using apply Body without a template");
459 this.tmpl.overwrite(this.bodyEl, obj);
465 Roo.apply(Roo.bootstrap.Modal, {
467 * Button config that displays a single OK button
476 * Button config that displays Yes and No buttons
492 * Button config that displays OK and Cancel buttons
507 * Button config that displays Yes, No and Cancel buttons