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        this.el.setStyle('display', 'block');
224         (function(){ this.el.addClass('in'); }).defer(100);
225 //        this.el.removeClass('fade');
226 //        this.el.setStyle('display', 'block');
227 //        Roo.get(document.body).addClass("x-body-masked");
228 //        this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true), Roo.lib.Dom.getViewHeight(true));
229 //        this.maskEl.show();
230 //        this.el.setStyle('zIndex', '10001');
231 //        this.fireEvent('show', this);
232         
233         
234     },
235     hide : function()
236     {
237         Roo.log('Modal hide?!');
238 //        this.maskEl.hide();
239 //        Roo.get(document.body).removeClass("x-body-masked");
240         this.el.removeClass('in');
241 //        this.el.addClass('fade');
242         this.el.setStyle('display', 'none');
243         this.fireEvent('hide', this);
244     },
245     
246     addButton : function(str, cb)
247     {
248          
249         
250         var b = Roo.apply({}, { html : str } );
251         b.xns = b.xns || Roo.bootstrap;
252         b.xtype = b.xtype || 'Button';
253         if (typeof(b.listeners) == 'undefined') {
254             b.listeners = { click : cb.createDelegate(this)  };
255         }
256         
257         var btn = Roo.factory(b);
258            
259         btn.onRender(this.el.select('.modal-footer div').first());
260         
261         return btn;   
262        
263     },
264     
265     setDefaultButton : function(btn)
266     {
267         //this.el.select('.modal-footer').()
268     },
269     resizeTo: function(w,h)
270     {
271         // skip..
272     },
273     setContentSize  : function(w, h)
274     {
275         
276     },
277     onButtonClick: function(btn,e)
278     {
279         //Roo.log([a,b,c]);
280         this.fireEvent('btnclick', btn.name, e);
281     },
282     setTitle: function(str) {
283         this.el.select('.modal-title',true).first().dom.innerHTML = str;
284         
285     }
286 });
287
288
289 Roo.apply(Roo.bootstrap.Modal,  {
290     /**
291          * Button config that displays a single OK button
292          * @type Object
293          */
294         OK :  [{
295             name : 'ok',
296             weight : 'primary',
297             html : 'OK'
298         }], 
299         /**
300          * Button config that displays Yes and No buttons
301          * @type Object
302          */
303         YESNO : [
304             {
305                 name  : 'no',
306                 html : 'No'
307             },
308             {
309                 name  :'yes',
310                 weight : 'primary',
311                 html : 'Yes'
312             }
313         ],
314         
315         /**
316          * Button config that displays OK and Cancel buttons
317          * @type Object
318          */
319         OKCANCEL : [
320             {
321                name : 'cancel',
322                 html : 'Cancel'
323             },
324             {
325                 name : 'ok',
326                 weight : 'primary',
327                 html : 'OK'
328             }
329         ],
330         /**
331          * Button config that displays Yes, No and Cancel buttons
332          * @type Object
333          */
334         YESNOCANCEL : [
335             {
336                 name : 'yes',
337                 weight : 'primary',
338                 html : 'Yes'
339             },
340             {
341                 name : 'no',
342                 html : 'No'
343             },
344             {
345                 name : 'cancel',
346                 html : 'Cancel'
347             }
348         ]
349 });
350