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