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