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