Roo/bootstrap/Modal.js
[roojs1] / Roo / bootstrap / Modal.js
1
2 /*
3 <div class="modal fade">
4   <div class="modal-dialog">
5     <div class="modal-content">
6       <div class="modal-header">
7         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
8         <h4 class="modal-title">Modal title</h4>
9       </div>
10       <div class="modal-body">
11         <p>One fine body&hellip;</p>
12       </div>
13       <div class="modal-footer">
14         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
15         <button type="button" class="btn btn-primary">Save changes</button>
16       </div>
17     </div><!-- /.modal-content -->
18   </div><!-- /.modal-dialog -->
19 </div><!-- /.modal -->
20 */
21 /*
22  * - LGPL
23  *
24  * page contgainer.
25  * 
26  */
27
28 /**
29  * @class Roo.bootstrap.Modal
30  * @extends Roo.bootstrap.Component
31  * Bootstrap Modal class
32  * @cfg {String} title Title of dialog
33  * @cfg {Boolean} specificTitle default false
34  * @cfg {Array} buttons Array of buttons or standard button set..
35  * @cfg {String} buttonPosition (left|right|center) default right
36  * @cfg {Boolean} animate default true
37  * @cfg {Boolean} allow_close default true
38  * 
39  * @constructor
40  * Create a new Modal Dialog
41  * @param {Object} config The config object
42  */
43
44 Roo.bootstrap.Modal = function(config){
45     Roo.bootstrap.Modal.superclass.constructor.call(this, config);
46     this.addEvents({
47         // raw events
48         /**
49          * @event btnclick
50          * The raw btnclick event for the button
51          * @param {Roo.EventObject} e
52          */
53         "btnclick" : true
54     });
55     this.buttons = this.buttons || [];
56 };
57
58 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component,  {
59     
60     title : 'test dialog',
61    
62     buttons : false,
63     
64     // set on load...
65     body:  false,
66     
67     specificTitle: false,
68     
69     buttonPosition: 'right',
70     
71     allow_close : true,
72     
73     animate : true,
74     
75     onRender : function(ct, position)
76     {
77         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
78      
79         if(!this.el){
80             var cfg = Roo.apply({},  this.getAutoCreate());
81             cfg.id = Roo.id();
82             //if(!cfg.name){
83             //    cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
84             //}
85             //if (!cfg.name.length) {
86             //    delete cfg.name;
87            // }
88             if (this.cls) {
89                 cfg.cls += ' ' + this.cls;
90             }
91             if (this.style) {
92                 cfg.style = this.style;
93             }
94             this.el = Roo.get(document.body).createChild(cfg, position);
95         }
96         //var type = this.el.dom.type;
97         
98         if(this.tabIndex !== undefined){
99             this.el.dom.setAttribute('tabIndex', this.tabIndex);
100         }
101         
102         
103         
104         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
105         this.maskEl.enableDisplayMode("block");
106         this.maskEl.hide();
107         //this.el.addClass("x-dlg-modal");
108     
109         if (this.buttons.length) {
110             Roo.each(this.buttons, function(bb) {
111                 b = Roo.apply({}, bb);
112                 b.xns = b.xns || Roo.bootstrap;
113                 b.xtype = b.xtype || 'Button';
114                 if (typeof(b.listeners) == 'undefined') {
115                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
116                 }
117                 
118                 var btn = Roo.factory(b);
119                 
120                 btn.onRender(this.el.select('.modal-footer div').first());
121                 
122             },this);
123         }
124         // render the children.
125         var nitems = [];
126         
127         if(typeof(this.items) != 'undefined'){
128             var items = this.items;
129             delete this.items;
130
131             for(var i =0;i < items.length;i++) {
132                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
133             }
134         }
135         
136         this.items = nitems;
137         
138         this.body = this.el.select('.modal-body',true).first();
139         this.close = this.el.select('.modal-header .close', true).first();
140         this.footer = this.el.select('.modal-footer',true).first();
141         this.initEvents();
142         //this.el.addClass([this.fieldClass, this.cls]);
143         
144     },
145     getAutoCreate : function(){
146         
147         
148         var bdy = {
149                 cls : 'modal-body',
150                 html : this.html || ''
151         };
152         
153         var title = {
154             tag: 'h4',
155             cls : 'modal-title',
156             html : this.title
157         };
158         
159         if(this.specificTitle){
160             title = this.title;
161             
162         };
163         
164         var header = [];
165         if (this.allow_close) {
166             header.push({
167                 tag: 'button',
168                 cls : 'close',
169                 html : '&times'
170             });
171         }
172         header.push(title);
173         
174         var modal = {
175             cls: "modal",
176             style : 'display: none',
177             cn : [
178                 {
179                     cls: "modal-dialog",
180                     cn : [
181                         {
182                             cls : "modal-content",
183                             cn : [
184                                 {
185                                     cls : 'modal-header',
186                                     cn : header
187                                 },
188                                 bdy,
189                                 {
190                                     cls : 'modal-footer',
191                                     cn : [
192                                         {
193                                             tag: 'div',
194                                             cls: 'btn-' + this.buttonPosition
195                                         }
196                                     ]
197                                     
198                                 }
199                                 
200                                 
201                             ]
202                             
203                         }
204                     ]
205                         
206                 }
207             ]
208         };
209         
210         if(this.animate){
211             modal.cls += ' fade';
212         }
213         
214         return modal;
215           
216     },
217     getChildContainer : function() {
218          
219          return this.el.select('.modal-body',true).first();
220         
221     },
222     getButtonContainer : function() {
223          return this.el.select('.modal-footer div',true).first();
224         
225     },
226     initEvents : function()
227     {
228         this.el.select('.modal-header .close').on('click', this.hide, this);
229 //        
230 //        this.addxtype(this);
231     },
232     show : function() {
233         
234         if (!this.rendered) {
235             this.render();
236         }
237         
238         this.el.setStyle('display', 'block');
239         
240         if(this.animate){
241             var _this = this;
242             (function(){ _this.el.addClass('in'); }).defer(50);
243         }else{
244             this.el.addClass('in');
245         }
246         
247         Roo.get(document.body).addClass("x-body-masked");
248         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
249         this.maskEl.show();
250         this.el.setStyle('zIndex', '10001');
251         this.fireEvent('show', this);
252         
253         
254     },
255     hide : function()
256     {
257         this.maskEl.hide();
258         Roo.get(document.body).removeClass("x-body-masked");
259         this.el.removeClass('in');
260         
261         if(this.animate){
262             var _this = this;
263             (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
264         }else{
265             this.el.setStyle('display', 'none');
266         }
267         
268         this.fireEvent('hide', this);
269     },
270     
271     addButton : function(str, cb)
272     {
273          
274         
275         var b = Roo.apply({}, { html : str } );
276         b.xns = b.xns || Roo.bootstrap;
277         b.xtype = b.xtype || 'Button';
278         if (typeof(b.listeners) == 'undefined') {
279             b.listeners = { click : cb.createDelegate(this)  };
280         }
281         
282         var btn = Roo.factory(b);
283            
284         btn.onRender(this.el.select('.modal-footer div').first());
285         
286         return btn;   
287        
288     },
289     
290     setDefaultButton : function(btn)
291     {
292         //this.el.select('.modal-footer').()
293     },
294     resizeTo: function(w,h)
295     {
296         // skip..
297     },
298     setContentSize  : function(w, h)
299     {
300         
301     },
302     onButtonClick: function(btn,e)
303     {
304         //Roo.log([a,b,c]);
305         this.fireEvent('btnclick', btn.name, e);
306     },
307     setTitle: function(str) {
308         this.el.select('.modal-title',true).first().dom.innerHTML = str;
309         
310     }
311 });
312
313
314 Roo.apply(Roo.bootstrap.Modal,  {
315     /**
316          * Button config that displays a single OK button
317          * @type Object
318          */
319         OK :  [{
320             name : 'ok',
321             weight : 'primary',
322             html : 'OK'
323         }], 
324         /**
325          * Button config that displays Yes and No buttons
326          * @type Object
327          */
328         YESNO : [
329             {
330                 name  : 'no',
331                 html : 'No'
332             },
333             {
334                 name  :'yes',
335                 weight : 'primary',
336                 html : 'Yes'
337             }
338         ],
339         
340         /**
341          * Button config that displays OK and Cancel buttons
342          * @type Object
343          */
344         OKCANCEL : [
345             {
346                name : 'cancel',
347                 html : 'Cancel'
348             },
349             {
350                 name : 'ok',
351                 weight : 'primary',
352                 html : 'OK'
353             }
354         ],
355         /**
356          * Button config that displays Yes, No and Cancel buttons
357          * @type Object
358          */
359         YESNOCANCEL : [
360             {
361                 name : 'yes',
362                 weight : 'primary',
363                 html : 'Yes'
364             },
365             {
366                 name : 'no',
367                 html : 'No'
368             },
369             {
370                 name : 'cancel',
371                 html : 'Cancel'
372             }
373         ]
374 });
375