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  * 
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         this.dialgEl = this.el.select('.modal-dialog',true).first();
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.render(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     setSize : function(w,h)
248     {
249         
250     },
251     
252     show : function() {
253         
254         if (!this.rendered) {
255             this.render();
256         }
257         
258         this.el.setStyle('display', 'block');
259         
260         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
261             var _this = this;
262             (function(){
263                 this.el.addClass('in');
264             }).defer(50, this);
265         }else{
266             this.el.addClass('in');
267             
268         }
269         
270         // not sure how we can show data in here.. 
271         //if (this.tmpl) {
272         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
273         //}
274         
275         Roo.get(document.body).addClass("x-body-masked");
276         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
277         this.maskEl.show();
278         this.el.setStyle('zIndex', '10001');
279        
280         this.fireEvent('show', this);
281         this.items.forEach(function(e) {
282             e.layout ? e.layout() : false;
283                 
284         });
285         
286         
287         
288     },
289     hide : function()
290     {
291         this.maskEl.hide();
292         Roo.get(document.body).removeClass("x-body-masked");
293         this.el.removeClass('in');
294         this.el.select('.modal-dialog', true).first().setStyle('transform','');
295         
296         if(this.animate){ // why
297             var _this = this;
298             (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
299         }else{
300             this.el.setStyle('display', 'none');
301         }
302         
303         this.fireEvent('hide', this);
304     },
305     
306     addButton : function(str, cb)
307     {
308          
309         
310         var b = Roo.apply({}, { html : str } );
311         b.xns = b.xns || Roo.bootstrap;
312         b.xtype = b.xtype || 'Button';
313         if (typeof(b.listeners) == 'undefined') {
314             b.listeners = { click : cb.createDelegate(this)  };
315         }
316         
317         var btn = Roo.factory(b);
318            
319         btn.render(this.el.select('.modal-footer div').first());
320         
321         return btn;   
322        
323     },
324     
325     setDefaultButton : function(btn)
326     {
327         //this.el.select('.modal-footer').()
328     },
329     diff : false,
330     
331     resizeTo: function(w,h)
332     {
333         // skip.. ?? why??
334         
335         this.el.select('.modal-dialog',true).first().setWidth(w);
336         if (this.diff === false) {
337             this.diff = this.el.select('.modal-dialog',true).first().getHeight() - this.el.select('.modal-body',true).first().getHeight();
338         }
339         
340         this.el.select('.modal-body',true).first().setHeight(h-this.diff);
341         
342         
343     },
344     setContentSize  : function(w, h)
345     {
346         
347     },
348     onButtonClick: function(btn,e)
349     {
350         //Roo.log([a,b,c]);
351         this.fireEvent('btnclick', btn.name, e);
352     },
353      /**
354      * Set the title of the Dialog
355      * @param {String} str new Title
356      */
357     setTitle: function(str) {
358         this.titleEl.dom.innerHTML = str;    
359     },
360     /**
361      * Set the body of the Dialog
362      * @param {String} str new Title
363      */
364     setBody: function(str) {
365         this.bodyEl.dom.innerHTML = str;    
366     },
367     /**
368      * Set the body of the Dialog using the template
369      * @param {Obj} data - apply this data to the template and replace the body contents.
370      */
371     applyBody: function(obj)
372     {
373         if (!this.tmpl) {
374             Roo.log("Error - using apply Body without a template");
375             //code
376         }
377         this.tmpl.overwrite(this.bodyEl, obj);
378     }
379     
380 });
381
382
383 Roo.apply(Roo.bootstrap.Modal,  {
384     /**
385          * Button config that displays a single OK button
386          * @type Object
387          */
388         OK :  [{
389             name : 'ok',
390             weight : 'primary',
391             html : 'OK'
392         }], 
393         /**
394          * Button config that displays Yes and No buttons
395          * @type Object
396          */
397         YESNO : [
398             {
399                 name  : 'no',
400                 html : 'No'
401             },
402             {
403                 name  :'yes',
404                 weight : 'primary',
405                 html : 'Yes'
406             }
407         ],
408         
409         /**
410          * Button config that displays OK and Cancel buttons
411          * @type Object
412          */
413         OKCANCEL : [
414             {
415                name : 'cancel',
416                 html : 'Cancel'
417             },
418             {
419                 name : 'ok',
420                 weight : 'primary',
421                 html : 'OK'
422             }
423         ],
424         /**
425          * Button config that displays Yes, No and Cancel buttons
426          * @type Object
427          */
428         YESNOCANCEL : [
429             {
430                 name : 'yes',
431                 weight : 'primary',
432                 html : 'Yes'
433             },
434             {
435                 name : 'no',
436                 html : 'No'
437             },
438             {
439                 name : 'cancel',
440                 html : 'Cancel'
441             }
442         ]
443 });
444  
445