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