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