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