Roo/bootstrap/Modal.js
[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  * @cfg {Boolean} fitwindow default false
19  * @cfg {String} size (sm|lg) default empty
20  *
21  *
22  * @constructor
23  * Create a new Modal Dialog
24  * @param {Object} config The config object
25  */
26
27 Roo.bootstrap.Modal = function(config){
28     Roo.bootstrap.Modal.superclass.constructor.call(this, config);
29     this.addEvents({
30         // raw events
31         /**
32          * @event btnclick
33          * The raw btnclick event for the button
34          * @param {Roo.EventObject} e
35          */
36         "btnclick" : true,
37         /**
38          * @event resize
39          * Fire when dialog resize
40          * @param {Roo.bootstrap.Modal} this
41          * @param {Roo.EventObject} e
42          */
43         "resize" : true
44     });
45     this.buttons = this.buttons || [];
46
47     if (this.tmpl) {
48         this.tmpl = Roo.factory(this.tmpl);
49     }
50
51 };
52
53 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component,  {
54
55     title : 'test dialog',
56
57     buttons : false,
58
59     // set on load...
60
61     html: false,
62
63     tmp: false,
64
65     specificTitle: false,
66
67     buttonPosition: 'right',
68
69     allow_close : true,
70
71     animate : true,
72
73     fitwindow: false,
74
75
76      // private
77     dialogEl: false,
78     bodyEl:  false,
79     footerEl:  false,
80     titleEl:  false,
81     closeEl:  false,
82
83     size: '',
84
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
110         if(this.tabIndex !== undefined){
111             this.el.dom.setAttribute('tabIndex', this.tabIndex);
112         }
113
114         this.dialogEl = this.el.select('.modal-dialog',true).first();
115         this.bodyEl = this.el.select('.modal-body',true).first();
116         this.closeEl = this.el.select('.modal-header .close', true).first();
117         this.headerEl = this.el.select('.modal-header',true).first();
118         this.titleEl = this.el.select('.modal-title',true).first();
119         this.footerEl = this.el.select('.modal-footer',true).first();
120
121         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
122         
123         //this.el.addClass("x-dlg-modal");
124
125         if (this.buttons.length) {
126             Roo.each(this.buttons, function(bb) {
127                 var b = Roo.apply({}, bb);
128                 b.xns = b.xns || Roo.bootstrap;
129                 b.xtype = b.xtype || 'Button';
130                 if (typeof(b.listeners) == 'undefined') {
131                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
132                 }
133
134                 var btn = Roo.factory(b);
135
136                 btn.render(this.el.select('.modal-footer div').first());
137
138             },this);
139         }
140         // render the children.
141         var nitems = [];
142
143         if(typeof(this.items) != 'undefined'){
144             var items = this.items;
145             delete this.items;
146
147             for(var i =0;i < items.length;i++) {
148                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
149             }
150         }
151
152         this.items = nitems;
153
154         // where are these used - they used to be body/close/footer
155
156
157         this.initEvents();
158         //this.el.addClass([this.fieldClass, this.cls]);
159
160     },
161
162     getAutoCreate : function()
163     {
164         var bdy = {
165                 cls : 'modal-body',
166                 html : this.html || ''
167         };
168
169         var title = {
170             tag: 'h4',
171             cls : 'modal-title',
172             html : this.title
173         };
174
175         if(this.specificTitle){
176             title = this.title;
177
178         };
179
180         var header = [];
181         if (this.allow_close) {
182             header.push({
183                 tag: 'button',
184                 cls : 'close',
185                 html : '&times'
186             });
187         }
188
189         header.push(title);
190
191         var size = '';
192
193         if(this.size.length){
194             size = 'modal-' + this.size;
195         }
196
197         var modal = {
198             cls: "modal",
199              cn : [
200                 {
201                     cls: "modal-dialog " + size,
202                     cn : [
203                         {
204                             cls : "modal-content",
205                             cn : [
206                                 {
207                                     cls : 'modal-header',
208                                     cn : header
209                                 },
210                                 bdy,
211                                 {
212                                     cls : 'modal-footer',
213                                     cn : [
214                                         {
215                                             tag: 'div',
216                                             cls: 'btn-' + this.buttonPosition
217                                         }
218                                     ]
219
220                                 }
221
222
223                             ]
224
225                         }
226                     ]
227
228                 }
229             ]
230         };
231
232         if(this.animate){
233             modal.cls += ' fade';
234         }
235
236         return modal;
237
238     },
239     getChildContainer : function() {
240
241          return this.bodyEl;
242
243     },
244     getButtonContainer : function() {
245          return this.el.select('.modal-footer div',true).first();
246
247     },
248     initEvents : function()
249     {
250         if (this.allow_close) {
251             this.closeEl.on('click', this.hide, this);
252         }
253         Roo.EventManager.onWindowResize(this.resize, this, true);
254
255
256     },
257
258     resize : function()
259     {
260         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),  Roo.lib.Dom.getViewHeight(true));
261         if (this.fitwindow) {
262             var w = this.width || Roo.lib.Dom.getViewportWidth(true) - 30;
263             var h = this.height || Roo.lib.Dom.getViewportHeight(true) - 60;
264             this.setSize(w,h);
265         }
266     },
267
268     setSize : function(w,h)
269     {
270         if (!w && !h) {
271             return;
272         }
273         this.resizeTo(w,h);
274     },
275
276     show : function() {
277
278         if (!this.rendered) {
279             this.render();
280         }
281
282         //this.el.setStyle('display', 'block');
283         this.el.removeClass('hideing');        
284         this.el.addClass('show');
285  
286         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
287             var _this = this;
288             (function(){
289                 this.el.addClass('in');
290             }).defer(50, this);
291         }else{
292             this.el.addClass('in');
293
294         }
295
296         // not sure how we can show data in here..
297         //if (this.tmpl) {
298         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
299         //}
300
301         Roo.get(document.body).addClass("x-body-masked");
302         
303         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
304         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
305         this.maskEl.addClass('show');
306         
307         this.resize();
308         
309         this.fireEvent('show', this);
310
311         // set zindex here - otherwise it appears to be ignored...
312         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
313
314         (function () {
315             this.items.forEach( function(e) {
316                 e.layout ? e.layout() : false;
317
318             });
319         }).defer(100,this);
320
321     },
322     hide : function()
323     {
324         if(this.fireEvent("beforehide", this) !== false){
325             this.maskEl.removeClass('show');
326             Roo.get(document.body).removeClass("x-body-masked");
327             this.el.removeClass('in');
328             this.el.select('.modal-dialog', true).first().setStyle('transform','');
329
330             if(this.animate){ // why
331                 this.el.addClass('hideing');
332                 (function(){
333                     if (!this.el.hasClass('hideing')) {
334                         return; // it's been shown again...
335                     }
336                     this.el.removeClass('show');
337                     this.el.removeClass('hideing');
338                 }).defer(150,this);
339                 
340             }else{
341                  this.el.removeClass('show');
342             }
343             this.fireEvent('hide', this);
344         }
345     },
346     isVisible : function()
347     {
348         
349         return this.el.hasClass('show') && !this.el.hasClass('hideing');
350         
351     },
352
353     addButton : function(str, cb)
354     {
355
356
357         var b = Roo.apply({}, { html : str } );
358         b.xns = b.xns || Roo.bootstrap;
359         b.xtype = b.xtype || 'Button';
360         if (typeof(b.listeners) == 'undefined') {
361             b.listeners = { click : cb.createDelegate(this)  };
362         }
363
364         var btn = Roo.factory(b);
365
366         btn.render(this.el.select('.modal-footer div').first());
367
368         return btn;
369
370     },
371
372     setDefaultButton : function(btn)
373     {
374         //this.el.select('.modal-footer').()
375     },
376     diff : false,
377
378     resizeTo: function(w,h)
379     {
380         // skip.. ?? why??
381
382         this.dialogEl.setWidth(w);
383         if (this.diff === false) {
384             this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
385         }
386
387         this.bodyEl.setHeight(h-this.diff);
388
389         this.fireEvent('resize', this);
390
391     },
392     setContentSize  : function(w, h)
393     {
394
395     },
396     onButtonClick: function(btn,e)
397     {
398         //Roo.log([a,b,c]);
399         this.fireEvent('btnclick', btn.name, e);
400     },
401      /**
402      * Set the title of the Dialog
403      * @param {String} str new Title
404      */
405     setTitle: function(str) {
406         this.titleEl.dom.innerHTML = str;
407     },
408     /**
409      * Set the body of the Dialog
410      * @param {String} str new Title
411      */
412     setBody: function(str) {
413         this.bodyEl.dom.innerHTML = str;
414     },
415     /**
416      * Set the body of the Dialog using the template
417      * @param {Obj} data - apply this data to the template and replace the body contents.
418      */
419     applyBody: function(obj)
420     {
421         if (!this.tmpl) {
422             Roo.log("Error - using apply Body without a template");
423             //code
424         }
425         this.tmpl.overwrite(this.bodyEl, obj);
426     }
427
428 });
429
430
431 Roo.apply(Roo.bootstrap.Modal,  {
432     /**
433          * Button config that displays a single OK button
434          * @type Object
435          */
436         OK :  [{
437             name : 'ok',
438             weight : 'primary',
439             html : 'OK'
440         }],
441         /**
442          * Button config that displays Yes and No buttons
443          * @type Object
444          */
445         YESNO : [
446             {
447                 name  : 'no',
448                 html : 'No'
449             },
450             {
451                 name  :'yes',
452                 weight : 'primary',
453                 html : 'Yes'
454             }
455         ],
456
457         /**
458          * Button config that displays OK and Cancel buttons
459          * @type Object
460          */
461         OKCANCEL : [
462             {
463                name : 'cancel',
464                 html : 'Cancel'
465             },
466             {
467                 name : 'ok',
468                 weight : 'primary',
469                 html : 'OK'
470             }
471         ],
472         /**
473          * Button config that displays Yes, No and Cancel buttons
474          * @type Object
475          */
476         YESNOCANCEL : [
477             {
478                 name : 'yes',
479                 weight : 'primary',
480                 html : 'Yes'
481             },
482             {
483                 name : 'no',
484                 html : 'No'
485             },
486             {
487                 name : 'cancel',
488                 html : 'Cancel'
489             }
490         ],
491         
492         zIndex : 10001
493 });