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