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