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