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