Roo/bootstrap/Modal.js
[roojs1] / Roo / bootstrap / Modal.js
1
2 /*
3 * Licence: LGPL
4 */
5
6 /**
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  *
21  *
22  * @constructor
23  * Create a new Modal Dialog
24  * @param {Object} config The config object
25  */
26
27 Roo.bootstrap.Modal = function(config){
28     Roo.bootstrap.Modal.superclass.constructor.call(this, config);
29     this.addEvents({
30         // raw events
31         /**
32          * @event btnclick
33          * The raw btnclick event for the button
34          * @param {Roo.EventObject} e
35          */
36         "btnclick" : true,
37         /**
38          * @event resize
39          * Fire when dialog resize
40          * @param {Roo.bootstrap.Modal} this
41          * @param {Roo.EventObject} e
42          */
43         "resize" : true
44     });
45     this.buttons = this.buttons || [];
46
47     if (this.tmpl) {
48         this.tmpl = Roo.factory(this.tmpl);
49     }
50
51 };
52
53 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component,  {
54
55     title : 'test dialog',
56
57     buttons : false,
58
59     // set on load...
60
61     html: false,
62
63     tmp: false,
64
65     specificTitle: false,
66
67     buttonPosition: 'right',
68
69     allow_close : true,
70
71     animate : true,
72
73     fitwindow: false,
74
75
76      // private
77     dialogEl: false,
78     bodyEl:  false,
79     footerEl:  false,
80     titleEl:  false,
81     closeEl:  false,
82
83     size: '',
84
85
86     onRender : function(ct, position)
87     {
88         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
89
90         if(!this.el){
91             var cfg = Roo.apply({},  this.getAutoCreate());
92             cfg.id = Roo.id();
93             //if(!cfg.name){
94             //    cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
95             //}
96             //if (!cfg.name.length) {
97             //    delete cfg.name;
98            // }
99             if (this.cls) {
100                 cfg.cls += ' ' + this.cls;
101             }
102             if (this.style) {
103                 cfg.style = this.style;
104             }
105             this.el = Roo.get(document.body).createChild(cfg, position);
106         }
107         //var type = this.el.dom.type;
108
109
110         if(this.tabIndex !== undefined){
111             this.el.dom.setAttribute('tabIndex', this.tabIndex);
112         }
113
114         this.dialogEl = this.el.select('.modal-dialog',true).first();
115         this.bodyEl = this.el.select('.modal-body',true).first();
116         this.closeEl = this.el.select('.modal-header .close', true).first();
117         this.headerEl = this.el.select('.modal-header',true).first();
118         this.titleEl = this.el.select('.modal-title',true).first();
119         this.footerEl = this.el.select('.modal-footer',true).first();
120
121         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
122         
123         //this.el.addClass("x-dlg-modal");
124
125         if (this.buttons.length) {
126             Roo.each(this.buttons, function(bb) {
127                 var b = Roo.apply({}, bb);
128                 b.xns = b.xns || Roo.bootstrap;
129                 b.xtype = b.xtype || 'Button';
130                 if (typeof(b.listeners) == 'undefined') {
131                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
132                 }
133
134                 var btn = Roo.factory(b);
135
136                 btn.render(this.el.select('.modal-footer div').first());
137
138             },this);
139         }
140         // render the children.
141         var nitems = [];
142
143         if(typeof(this.items) != 'undefined'){
144             var items = this.items;
145             delete this.items;
146
147             for(var i =0;i < items.length;i++) {
148                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
149             }
150         }
151
152         this.items = nitems;
153
154         // where are these used - they used to be body/close/footer
155
156
157         this.initEvents();
158         //this.el.addClass([this.fieldClass, this.cls]);
159
160     },
161
162     getAutoCreate : function()
163     {
164         var bdy = {
165                 cls : 'modal-body',
166                 html : this.html || ''
167         };
168
169         var title = {
170             tag: 'h4',
171             cls : 'modal-title',
172             html : this.title
173         };
174
175         if(this.specificTitle){
176             title = this.title;
177
178         };
179
180         var header = [];
181         if (this.allow_close) {
182             header.push({
183                 tag: 'button',
184                 cls : 'close',
185                 html : '&times'
186             });
187         }
188
189         header.push(title);
190
191         var size = '';
192
193         if(this.size.length){
194             size = 'modal-' + this.size;
195         }
196
197         var modal = {
198             cls: "modal",
199              cn : [
200                 {
201                     cls: "modal-dialog " + size,
202                     cn : [
203                         {
204                             cls : "modal-content",
205                             cn : [
206                                 {
207                                     cls : 'modal-header',
208                                     cn : header
209                                 },
210                                 bdy,
211                                 {
212                                     cls : 'modal-footer',
213                                     cn : [
214                                         {
215                                             tag: 'div',
216                                             cls: 'btn-' + this.buttonPosition
217                                         }
218                                     ]
219
220                                 }
221
222
223                             ]
224
225                         }
226                     ]
227
228                 }
229             ]
230         };
231
232         if(this.animate){
233             modal.cls += ' fade';
234         }
235
236         return modal;
237
238     },
239     getChildContainer : function() {
240
241          return this.bodyEl;
242
243     },
244     getButtonContainer : function() {
245          return this.el.select('.modal-footer div',true).first();
246
247     },
248     initEvents : function()
249     {
250         if (this.allow_close) {
251             this.closeEl.on('click', this.hide, this);
252         }
253         Roo.EventManager.onWindowResize(this.resize, this, true);
254
255
256     },
257
258     resize : function()
259     {
260         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),  Roo.lib.Dom.getViewHeight(true));
261         
262         if (this.fitwindow) {
263             var w = this.width || Roo.lib.Dom.getViewportWidth(true) - 30;
264             var h = this.height || Roo.lib.Dom.getViewportHeight(true) - 60;
265             this.setSize(w,h);
266         }
267         
268         
269     },
270
271     setSize : function(w,h)
272     {
273         if (!w && !h) {
274             return;
275         }
276         this.resizeTo(w,h);
277     },
278
279     show : function() {
280
281         if (!this.rendered) {
282             this.render();
283         }
284
285         //this.el.setStyle('display', 'block');
286         this.el.removeClass('hideing');        
287         this.el.addClass('show');
288  
289         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
290             var _this = this;
291             (function(){
292                 this.el.addClass('in');
293             }).defer(50, this);
294         }else{
295             this.el.addClass('in');
296         }
297
298         // not sure how we can show data in here..
299         //if (this.tmpl) {
300         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
301         //}
302
303         Roo.get(document.body).addClass("x-body-masked");
304         
305         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
306         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
307         this.maskEl.addClass('show');
308         
309         this.resize();
310         
311         this.fireEvent('show', this);
312
313         // set zindex here - otherwise it appears to be ignored...
314         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
315
316         (function () {
317             this.items.forEach( function(e) {
318                 e.layout ? e.layout() : false;
319
320             });
321         }).defer(100,this);
322
323     },
324     hide : function()
325     {
326         if(this.fireEvent("beforehide", this) !== false){
327             this.maskEl.removeClass('show');
328             Roo.get(document.body).removeClass("x-body-masked");
329             this.el.removeClass('in');
330             this.el.select('.modal-dialog', true).first().setStyle('transform','');
331
332             if(this.animate){ // why
333                 this.el.addClass('hideing');
334                 (function(){
335                     if (!this.el.hasClass('hideing')) {
336                         return; // it's been shown again...
337                     }
338                     this.el.removeClass('show');
339                     this.el.removeClass('hideing');
340                 }).defer(150,this);
341                 
342             }else{
343                  this.el.removeClass('show');
344             }
345             this.fireEvent('hide', this);
346         }
347     },
348     isVisible : function()
349     {
350         
351         return this.el.hasClass('show') && !this.el.hasClass('hideing');
352         
353     },
354
355     addButton : function(str, cb)
356     {
357
358
359         var b = Roo.apply({}, { html : str } );
360         b.xns = b.xns || Roo.bootstrap;
361         b.xtype = b.xtype || 'Button';
362         if (typeof(b.listeners) == 'undefined') {
363             b.listeners = { click : cb.createDelegate(this)  };
364         }
365
366         var btn = Roo.factory(b);
367
368         btn.render(this.el.select('.modal-footer div').first());
369
370         return btn;
371
372     },
373
374     setDefaultButton : function(btn)
375     {
376         //this.el.select('.modal-footer').()
377     },
378     diff : false,
379
380     resizeTo: function(w,h)
381     {
382         // skip.. ?? why??
383
384         this.dialogEl.setWidth(w);
385         if (this.diff === false) {
386             this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
387         }
388
389         this.bodyEl.setHeight(h-this.diff);
390
391         this.fireEvent('resize', this);
392
393     },
394     setContentSize  : function(w, h)
395     {
396
397     },
398     onButtonClick: function(btn,e)
399     {
400         //Roo.log([a,b,c]);
401         this.fireEvent('btnclick', btn.name, e);
402     },
403      /**
404      * Set the title of the Dialog
405      * @param {String} str new Title
406      */
407     setTitle: function(str) {
408         this.titleEl.dom.innerHTML = str;
409     },
410     /**
411      * Set the body of the Dialog
412      * @param {String} str new Title
413      */
414     setBody: function(str) {
415         this.bodyEl.dom.innerHTML = str;
416     },
417     /**
418      * Set the body of the Dialog using the template
419      * @param {Obj} data - apply this data to the template and replace the body contents.
420      */
421     applyBody: function(obj)
422     {
423         if (!this.tmpl) {
424             Roo.log("Error - using apply Body without a template");
425             //code
426         }
427         this.tmpl.overwrite(this.bodyEl, obj);
428     }
429
430 });
431
432
433 Roo.apply(Roo.bootstrap.Modal,  {
434     /**
435          * Button config that displays a single OK button
436          * @type Object
437          */
438         OK :  [{
439             name : 'ok',
440             weight : 'primary',
441             html : 'OK'
442         }],
443         /**
444          * Button config that displays Yes and No buttons
445          * @type Object
446          */
447         YESNO : [
448             {
449                 name  : 'no',
450                 html : 'No'
451             },
452             {
453                 name  :'yes',
454                 weight : 'primary',
455                 html : 'Yes'
456             }
457         ],
458
459         /**
460          * Button config that displays OK and Cancel buttons
461          * @type Object
462          */
463         OKCANCEL : [
464             {
465                name : 'cancel',
466                 html : 'Cancel'
467             },
468             {
469                 name : 'ok',
470                 weight : 'primary',
471                 html : 'OK'
472             }
473         ],
474         /**
475          * Button config that displays Yes, No and Cancel buttons
476          * @type Object
477          */
478         YESNOCANCEL : [
479             {
480                 name : 'yes',
481                 weight : 'primary',
482                 html : 'Yes'
483             },
484             {
485                 name : 'no',
486                 html : 'No'
487             },
488             {
489                 name : 'cancel',
490                 html : 'Cancel'
491             }
492         ],
493         
494         zIndex : 10001
495 });