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