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