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);
264 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
266 if (this.fitwindow) {
267 var w = this.width || Roo.lib.Dom.getViewportWidth(true) - 30;
268 var h = this.height || Roo.lib.Dom.getViewportHeight(true) - 60;
272 if(!this.fitwindow && this.max_width !== 0){
274 var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
278 this.setSize(w, this.height);
282 if(!this.fit_content) {
283 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
286 var body_childs = this.bodyEl.dom.childNodes;
287 var full_height = this.headerEl.getHeight() + this.footerEl.getHeight();
288 for(var i = 0; i < body_childs.length; i++) {
290 // if(body_childs[i].classList.indexOf('roo-layout-region') * 1 != -1) {
291 // var layout_childs = body_childs[i].childNodes;
292 // for(var j = 0; j < layout_childs.length; j++) {
297 full_height += body_childs[i].offsetHeight;
300 this.setSize(w, Math.min(full_height, Roo.lib.Dom.getViewportHeight(true) - 60));
305 setSize : function(w,h)
315 if (!this.rendered) {
319 //this.el.setStyle('display', 'block');
320 this.el.removeClass('hideing');
321 this.el.addClass('show');
323 if(this.animate){ // element has 'fade' - so stuff happens after .3s ?- not sure why the delay?
326 this.el.addClass('in');
329 this.el.addClass('in');
332 // not sure how we can show data in here..
334 // this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
337 Roo.get(document.body).addClass("x-body-masked");
339 this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
340 this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
341 this.maskEl.addClass('show');
345 this.fireEvent('show', this);
347 // set zindex here - otherwise it appears to be ignored...
348 this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
351 this.items.forEach( function(e) {
352 e.layout ? e.layout() : false;
360 if(this.fireEvent("beforehide", this) !== false){
361 this.maskEl.removeClass('show');
362 Roo.get(document.body).removeClass("x-body-masked");
363 this.el.removeClass('in');
364 this.el.select('.modal-dialog', true).first().setStyle('transform','');
366 if(this.animate){ // why
367 this.el.addClass('hideing');
369 if (!this.el.hasClass('hideing')) {
370 return; // it's been shown again...
372 this.el.removeClass('show');
373 this.el.removeClass('hideing');
377 this.el.removeClass('show');
379 this.fireEvent('hide', this);
382 isVisible : function()
385 return this.el.hasClass('show') && !this.el.hasClass('hideing');
389 addButton : function(str, cb)
393 var b = Roo.apply({}, { html : str } );
394 b.xns = b.xns || Roo.bootstrap;
395 b.xtype = b.xtype || 'Button';
396 if (typeof(b.listeners) == 'undefined') {
397 b.listeners = { click : cb.createDelegate(this) };
400 var btn = Roo.factory(b);
402 btn.render(this.el.select('.modal-footer div').first());
408 setDefaultButton : function(btn)
410 //this.el.select('.modal-footer').()
414 resizeTo: function(w,h)
418 this.dialogEl.setWidth(w);
419 if (this.diff === false) {
420 this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
423 this.bodyEl.setHeight(h-this.diff);
425 this.fireEvent('resize', this);
428 setContentSize : function(w, h)
432 onButtonClick: function(btn,e)
435 this.fireEvent('btnclick', btn.name, e);
438 * Set the title of the Dialog
439 * @param {String} str new Title
441 setTitle: function(str) {
442 this.titleEl.dom.innerHTML = str;
445 * Set the body of the Dialog
446 * @param {String} str new Title
448 setBody: function(str) {
449 this.bodyEl.dom.innerHTML = str;
452 * Set the body of the Dialog using the template
453 * @param {Obj} data - apply this data to the template and replace the body contents.
455 applyBody: function(obj)
458 Roo.log("Error - using apply Body without a template");
461 this.tmpl.overwrite(this.bodyEl, obj);
467 Roo.apply(Roo.bootstrap.Modal, {
469 * Button config that displays a single OK button
478 * Button config that displays Yes and No buttons
494 * Button config that displays OK and Cancel buttons
509 * Button config that displays Yes, No and Cancel buttons