Roo/bootstrap/Button.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         
96         
97         if(this.tabIndex !== undefined){
98             this.el.dom.setAttribute('tabIndex', this.tabIndex);
99         }
100         
101         
102         this.bodyEl = this.el.select('.modal-body',true).first();
103         this.closeEl = this.el.select('.modal-header .close', true).first();
104         this.footerEl = this.el.select('.modal-footer',true).first();
105         this.titleEl = this.el.select('.modal-title',true).first();
106         
107         
108          
109         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
110         this.maskEl.enableDisplayMode("block");
111         this.maskEl.hide();
112         //this.el.addClass("x-dlg-modal");
113     
114         if (this.buttons.length) {
115             Roo.each(this.buttons, function(bb) {
116                 var b = Roo.apply({}, bb);
117                 b.xns = b.xns || Roo.bootstrap;
118                 b.xtype = b.xtype || 'Button';
119                 if (typeof(b.listeners) == 'undefined') {
120                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
121                 }
122                 
123                 var btn = Roo.factory(b);
124                 
125                 btn.onRender(this.el.select('.modal-footer div').first());
126                 
127             },this);
128         }
129         // render the children.
130         var nitems = [];
131         
132         if(typeof(this.items) != 'undefined'){
133             var items = this.items;
134             delete this.items;
135
136             for(var i =0;i < items.length;i++) {
137                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
138             }
139         }
140         
141         this.items = nitems;
142         
143         // where are these used - they used to be body/close/footer
144         
145        
146         this.initEvents();
147         //this.el.addClass([this.fieldClass, this.cls]);
148         
149     },
150     
151     getAutoCreate : function(){
152         
153         
154         var bdy = {
155                 cls : 'modal-body',
156                 html : this.html || ''
157         };
158         
159         var title = {
160             tag: 'h4',
161             cls : 'modal-title',
162             html : this.title
163         };
164         
165         if(this.specificTitle){
166             title = this.title;
167             
168         };
169         
170         var header = [];
171         if (this.allow_close) {
172             header.push({
173                 tag: 'button',
174                 cls : 'close',
175                 html : '&times'
176             });
177         }
178         header.push(title);
179         
180         var modal = {
181             cls: "modal",
182             style : 'display: none',
183             cn : [
184                 {
185                     cls: "modal-dialog",
186                     cn : [
187                         {
188                             cls : "modal-content",
189                             cn : [
190                                 {
191                                     cls : 'modal-header',
192                                     cn : header
193                                 },
194                                 bdy,
195                                 {
196                                     cls : 'modal-footer',
197                                     cn : [
198                                         {
199                                             tag: 'div',
200                                             cls: 'btn-' + this.buttonPosition
201                                         }
202                                     ]
203                                     
204                                 }
205                                 
206                                 
207                             ]
208                             
209                         }
210                     ]
211                         
212                 }
213             ]
214         };
215         
216         if(this.animate){
217             modal.cls += ' fade';
218         }
219         
220         return modal;
221           
222     },
223     getChildContainer : function() {
224          
225          return this.bodyEl;
226         
227     },
228     getButtonContainer : function() {
229          return this.el.select('.modal-footer div',true).first();
230         
231     },
232     initEvents : function()
233     {
234         if (this.allow_close) {
235             this.closeEl.on('click', this.hide, this);
236         }
237         
238         var _this = this;
239         
240         window.addEventListener("resize", function() { _this.resize(); } );
241
242     },
243     
244     resize : function()
245     {
246         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
247     },
248     
249     show : function() {
250         
251         if (!this.rendered) {
252             this.render();
253         }
254         
255         this.el.setStyle('display', 'block');
256         
257         if(this.animate){
258             var _this = this;
259             (function(){ _this.el.addClass('in'); }).defer(50);
260         }else{
261             this.el.addClass('in');
262         }
263         
264         // not sure how we can show data in here.. 
265         //if (this.tmpl) {
266         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
267         //}
268         
269         Roo.get(document.body).addClass("x-body-masked");
270         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
271         this.maskEl.show();
272         this.el.setStyle('zIndex', '10001');
273        
274         this.fireEvent('show', this);
275         
276         
277     },
278     hide : function()
279     {
280         this.maskEl.hide();
281         Roo.get(document.body).removeClass("x-body-masked");
282         this.el.removeClass('in');
283         
284         if(this.animate){
285             var _this = this;
286             (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
287         }else{
288             this.el.setStyle('display', 'none');
289         }
290         
291         this.fireEvent('hide', this);
292     },
293     
294     addButton : function(str, cb)
295     {
296          
297         
298         var b = Roo.apply({}, { html : str } );
299         b.xns = b.xns || Roo.bootstrap;
300         b.xtype = b.xtype || 'Button';
301         if (typeof(b.listeners) == 'undefined') {
302             b.listeners = { click : cb.createDelegate(this)  };
303         }
304         
305         var btn = Roo.factory(b);
306            
307         btn.onRender(this.el.select('.modal-footer div').first());
308         
309         return btn;   
310        
311     },
312     
313     setDefaultButton : function(btn)
314     {
315         //this.el.select('.modal-footer').()
316     },
317     resizeTo: function(w,h)
318     {
319         // skip..
320     },
321     setContentSize  : function(w, h)
322     {
323         
324     },
325     onButtonClick: function(btn,e)
326     {
327         //Roo.log([a,b,c]);
328         this.fireEvent('btnclick', btn.name, e);
329     },
330      /**
331      * Set the title of the Dialog
332      * @param {String} str new Title
333      */
334     setTitle: function(str) {
335         this.titleEl.dom.innerHTML = str;    
336     },
337     /**
338      * Set the body of the Dialog
339      * @param {String} str new Title
340      */
341     setBody: function(str) {
342         this.bodyEl.dom.innerHTML = str;    
343     },
344     /**
345      * Set the body of the Dialog using the template
346      * @param {Obj} data - apply this data to the template and replace the body contents.
347      */
348     applyBody: function(obj)
349     {
350         if (!this.tmpl) {
351             Roo.log("Error - using apply Body without a template");
352             //code
353         }
354         this.tmpl.overwrite(this.bodyEl, obj);
355     }
356     
357 });
358
359
360 Roo.apply(Roo.bootstrap.Modal,  {
361     /**
362          * Button config that displays a single OK button
363          * @type Object
364          */
365         OK :  [{
366             name : 'ok',
367             weight : 'primary',
368             html : 'OK'
369         }], 
370         /**
371          * Button config that displays Yes and No buttons
372          * @type Object
373          */
374         YESNO : [
375             {
376                 name  : 'no',
377                 html : 'No'
378             },
379             {
380                 name  :'yes',
381                 weight : 'primary',
382                 html : 'Yes'
383             }
384         ],
385         
386         /**
387          * Button config that displays OK and Cancel buttons
388          * @type Object
389          */
390         OKCANCEL : [
391             {
392                name : 'cancel',
393                 html : 'Cancel'
394             },
395             {
396                 name : 'ok',
397                 weight : 'primary',
398                 html : 'OK'
399             }
400         ],
401         /**
402          * Button config that displays Yes, No and Cancel buttons
403          * @type Object
404          */
405         YESNOCANCEL : [
406             {
407                 name : 'yes',
408                 weight : 'primary',
409                 html : 'Yes'
410             },
411             {
412                 name : 'no',
413                 html : 'No'
414             },
415             {
416                 name : 'cancel',
417                 html : 'Cancel'
418             }
419         ]
420 });
421  
422