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         header.push(title);
182         
183         var modal = {
184             cls: "modal",
185             style : 'display: none',
186             cn : [
187                 {
188                     cls: "modal-dialog",
189                     cn : [
190                         {
191                             cls : "modal-content",
192                             cn : [
193                                 {
194                                     cls : 'modal-header',
195                                     cn : header
196                                 },
197                                 bdy,
198                                 {
199                                     cls : 'modal-footer',
200                                     cn : [
201                                         {
202                                             tag: 'div',
203                                             cls: 'btn-' + this.buttonPosition
204                                         }
205                                     ]
206                                     
207                                 }
208                                 
209                                 
210                             ]
211                             
212                         }
213                     ]
214                         
215                 }
216             ]
217         };
218         
219         if(this.animate){
220             modal.cls += ' fade';
221         }
222         
223         return modal;
224           
225     },
226     getChildContainer : function() {
227          
228          return this.bodyEl;
229         
230     },
231     getButtonContainer : function() {
232          return this.el.select('.modal-footer div',true).first();
233         
234     },
235     initEvents : function()
236     {
237         if (this.allow_close) {
238             this.closeEl.on('click', this.hide, this);
239         }
240         
241         
242         window.addEventListener("resize", this.resize , this );
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.getViewWidth(true) - 30;
251             var h = this.height || Roo.lib.Dom.getViewHeight(true) - 30;
252             this.setSize(w,h)
253         }
254     },
255     
256     setSize : function(w,h)
257     {
258         this.resizeTo(w,h);
259     },
260     
261     show : function() {
262         
263         if (!this.rendered) {
264             this.render();
265         }
266         
267         this.el.setStyle('display', 'block');
268         
269         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
270             var _this = this;
271             (function(){
272                 this.el.addClass('in');
273             }).defer(50, this);
274         }else{
275             this.el.addClass('in');
276             
277         }
278         
279         // not sure how we can show data in here.. 
280         //if (this.tmpl) {
281         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
282         //}
283         
284         Roo.get(document.body).addClass("x-body-masked");
285         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
286         this.maskEl.show();
287         this.el.setStyle('zIndex', '10001');
288        
289         this.fireEvent('show', this);
290         this.items.forEach(function(e) {
291             e.layout ? e.layout() : false;
292                 
293         });
294         this.resize();
295         
296         
297         
298     },
299     hide : function()
300     {
301         this.maskEl.hide();
302         Roo.get(document.body).removeClass("x-body-masked");
303         this.el.removeClass('in');
304         this.el.select('.modal-dialog', true).first().setStyle('transform','');
305         
306         if(this.animate){ // why
307             var _this = this;
308             (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
309         }else{
310             this.el.setStyle('display', 'none');
311         }
312         
313         this.fireEvent('hide', this);
314     },
315     
316     addButton : function(str, cb)
317     {
318          
319         
320         var b = Roo.apply({}, { html : str } );
321         b.xns = b.xns || Roo.bootstrap;
322         b.xtype = b.xtype || 'Button';
323         if (typeof(b.listeners) == 'undefined') {
324             b.listeners = { click : cb.createDelegate(this)  };
325         }
326         
327         var btn = Roo.factory(b);
328            
329         btn.render(this.el.select('.modal-footer div').first());
330         
331         return btn;   
332        
333     },
334     
335     setDefaultButton : function(btn)
336     {
337         //this.el.select('.modal-footer').()
338     },
339     diff : false,
340     
341     resizeTo: function(w,h)
342     {
343         // skip.. ?? why??
344         
345         this.dialogEl.setWidth(w);
346         if (this.diff === false) {
347             this.diff = this.dialogEl.getHeight() - this.el.select('.modal-body',true).first().getHeight();
348         }
349         
350         this.el.select('.modal-body',true).first().setHeight(h-this.diff);
351         
352         
353     },
354     setContentSize  : function(w, h)
355     {
356         
357     },
358     onButtonClick: function(btn,e)
359     {
360         //Roo.log([a,b,c]);
361         this.fireEvent('btnclick', btn.name, e);
362     },
363      /**
364      * Set the title of the Dialog
365      * @param {String} str new Title
366      */
367     setTitle: function(str) {
368         this.titleEl.dom.innerHTML = str;    
369     },
370     /**
371      * Set the body of the Dialog
372      * @param {String} str new Title
373      */
374     setBody: function(str) {
375         this.bodyEl.dom.innerHTML = str;    
376     },
377     /**
378      * Set the body of the Dialog using the template
379      * @param {Obj} data - apply this data to the template and replace the body contents.
380      */
381     applyBody: function(obj)
382     {
383         if (!this.tmpl) {
384             Roo.log("Error - using apply Body without a template");
385             //code
386         }
387         this.tmpl.overwrite(this.bodyEl, obj);
388     }
389     
390 });
391
392
393 Roo.apply(Roo.bootstrap.Modal,  {
394     /**
395          * Button config that displays a single OK button
396          * @type Object
397          */
398         OK :  [{
399             name : 'ok',
400             weight : 'primary',
401             html : 'OK'
402         }], 
403         /**
404          * Button config that displays Yes and No buttons
405          * @type Object
406          */
407         YESNO : [
408             {
409                 name  : 'no',
410                 html : 'No'
411             },
412             {
413                 name  :'yes',
414                 weight : 'primary',
415                 html : 'Yes'
416             }
417         ],
418         
419         /**
420          * Button config that displays OK and Cancel buttons
421          * @type Object
422          */
423         OKCANCEL : [
424             {
425                name : 'cancel',
426                 html : 'Cancel'
427             },
428             {
429                 name : 'ok',
430                 weight : 'primary',
431                 html : 'OK'
432             }
433         ],
434         /**
435          * Button config that displays Yes, No and Cancel buttons
436          * @type Object
437          */
438         YESNOCANCEL : [
439             {
440                 name : 'yes',
441                 weight : 'primary',
442                 html : 'Yes'
443             },
444             {
445                 name : 'no',
446                 html : 'No'
447             },
448             {
449                 name : 'cancel',
450                 html : 'Cancel'
451             }
452         ]
453 });
454  
455