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         if(this.fireEvent("beforehide", this) === false){
251             return;
252         }
253         
254         this.maskEl.hide();
255         Roo.get(document.body).removeClass("x-body-masked");
256         this.el.removeClass('in');
257         
258         if(this.animate){
259             var _this = this;
260             (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
261         }else{
262             this.el.setStyle('display', 'none');
263         }
264         
265         this.fireEvent('hide', this);
266     },
267     
268     addButton : function(str, cb)
269     {
270          
271         
272         var b = Roo.apply({}, { html : str } );
273         b.xns = b.xns || Roo.bootstrap;
274         b.xtype = b.xtype || 'Button';
275         if (typeof(b.listeners) == 'undefined') {
276             b.listeners = { click : cb.createDelegate(this)  };
277         }
278         
279         var btn = Roo.factory(b);
280            
281         btn.onRender(this.el.select('.modal-footer div').first());
282         
283         return btn;   
284        
285     },
286     
287     setDefaultButton : function(btn)
288     {
289         //this.el.select('.modal-footer').()
290     },
291     resizeTo: function(w,h)
292     {
293         // skip..
294     },
295     setContentSize  : function(w, h)
296     {
297         
298     },
299     onButtonClick: function(btn,e)
300     {
301         //Roo.log([a,b,c]);
302         this.fireEvent('btnclick', btn.name, e);
303     },
304     setTitle: function(str) {
305         this.el.select('.modal-title',true).first().dom.innerHTML = str;
306         
307     }
308 });
309
310
311 Roo.apply(Roo.bootstrap.Modal,  {
312     /**
313          * Button config that displays a single OK button
314          * @type Object
315          */
316         OK :  [{
317             name : 'ok',
318             weight : 'primary',
319             html : 'OK'
320         }], 
321         /**
322          * Button config that displays Yes and No buttons
323          * @type Object
324          */
325         YESNO : [
326             {
327                 name  : 'no',
328                 html : 'No'
329             },
330             {
331                 name  :'yes',
332                 weight : 'primary',
333                 html : 'Yes'
334             }
335         ],
336         
337         /**
338          * Button config that displays OK and Cancel buttons
339          * @type Object
340          */
341         OKCANCEL : [
342             {
343                name : 'cancel',
344                 html : 'Cancel'
345             },
346             {
347                 name : 'ok',
348                 weight : 'primary',
349                 html : 'OK'
350             }
351         ],
352         /**
353          * Button config that displays Yes, No and Cancel buttons
354          * @type Object
355          */
356         YESNOCANCEL : [
357             {
358                 name : 'yes',
359                 weight : 'primary',
360                 html : 'Yes'
361             },
362             {
363                 name : 'no',
364                 html : 'No'
365             },
366             {
367                 name : 'cancel',
368                 html : 'Cancel'
369             }
370         ]
371 });
372