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