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