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