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