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