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