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