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 (DEPRICATED) - use mr-auto on buttons to put them on the left
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      // private
77     dialogEl: false,
78     bodyEl:  false,
79     footerEl:  false,
80     titleEl:  false,
81     closeEl:  false,
82
83     size: '',
84     
85     max_width: 0,
86     
87     max_height: 0,
88     
89     fit_content: false,
90
91     onRender : function(ct, position)
92     {
93         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
94
95         if(!this.el){
96             var cfg = Roo.apply({},  this.getAutoCreate());
97             cfg.id = Roo.id();
98             //if(!cfg.name){
99             //    cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
100             //}
101             //if (!cfg.name.length) {
102             //    delete cfg.name;
103            // }
104             if (this.cls) {
105                 cfg.cls += ' ' + this.cls;
106             }
107             if (this.style) {
108                 cfg.style = this.style;
109             }
110             this.el = Roo.get(document.body).createChild(cfg, position);
111         }
112         //var type = this.el.dom.type;
113
114
115         if(this.tabIndex !== undefined){
116             this.el.dom.setAttribute('tabIndex', this.tabIndex);
117         }
118
119         this.dialogEl = this.el.select('.modal-dialog',true).first();
120         this.bodyEl = this.el.select('.modal-body',true).first();
121         this.closeEl = this.el.select('.modal-header .close', true).first();
122         this.headerEl = this.el.select('.modal-header',true).first();
123         this.titleEl = this.el.select('.modal-title',true).first();
124         this.footerEl = this.el.select('.modal-footer',true).first();
125
126         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
127         
128         //this.el.addClass("x-dlg-modal");
129
130         if (this.buttons.length) {
131             Roo.each(this.buttons, function(bb) {
132                 var b = Roo.apply({}, bb);
133                 b.xns = b.xns || Roo.bootstrap;
134                 b.xtype = b.xtype || 'Button';
135                 if (typeof(b.listeners) == 'undefined') {
136                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
137                 }
138
139                 var btn = Roo.factory(b);
140
141                 btn.render(this.getButtonContainer());
142
143             },this);
144         }
145         // render the children.
146         var nitems = [];
147
148         if(typeof(this.items) != 'undefined'){
149             var items = this.items;
150             delete this.items;
151
152             for(var i =0;i < items.length;i++) {
153                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
154             }
155         }
156
157         this.items = nitems;
158
159         // where are these used - they used to be body/close/footer
160
161
162         this.initEvents();
163         //this.el.addClass([this.fieldClass, this.cls]);
164
165     },
166
167     getAutoCreate : function()
168     {
169         var bdy = {
170                 cls : 'modal-body',
171                 html : this.html || ''
172         };
173
174         var title = {
175             tag: 'h4',
176             cls : 'modal-title',
177             html : this.title
178         };
179
180         if(this.specificTitle){
181             title = this.title;
182
183         }
184
185         var header = [];
186         if (this.allow_close && Roo.bootstrap.version == 3) {
187             header.push({
188                 tag: 'button',
189                 cls : 'close',
190                 html : '&times'
191             });
192         }
193
194         header.push(title);
195
196         if (this.allow_close && Roo.bootstrap.version == 4) {
197             header.push({
198                 tag: 'button',
199                 cls : 'close',
200                 html : '&times'
201             });
202         }
203         
204         var size = '';
205
206         if(this.size.length){
207             size = 'modal-' + this.size;
208         }
209         
210         var footer = Roo.bootstrap.version == 3 ?
211             {
212                 cls : 'modal-footer',
213                 cn : [
214                     {
215                         tag: 'div',
216                         cls: 'btn-' + this.buttonPosition
217                     }
218                 ]
219
220             } :
221             {  // BS4 uses mr-auto on left buttons....
222                 cls : 'modal-footer'
223             };
224
225             
226
227         
228         
229         var modal = {
230             cls: "modal",
231              cn : [
232                 {
233                     cls: "modal-dialog " + size,
234                     cn : [
235                         {
236                             cls : "modal-content",
237                             cn : [
238                                 {
239                                     cls : 'modal-header',
240                                     cn : header
241                                 },
242                                 bdy,
243                                 footer
244                             ]
245
246                         }
247                     ]
248
249                 }
250             ]
251         };
252
253         if(this.animate){
254             modal.cls += ' fade';
255         }
256
257         return modal;
258
259     },
260     getChildContainer : function() {
261
262          return this.bodyEl;
263
264     },
265     getButtonContainer : function() {
266         
267          return Roo.bootstrap.version == 4 ?
268             this.el.select('.modal-footer',true).first()
269             : this.el.select('.modal-footer div',true).first();
270
271     },
272     initEvents : function()
273     {
274         if (this.allow_close) {
275             this.closeEl.on('click', this.hide, this);
276         }
277         Roo.EventManager.onWindowResize(this.resize, this, true);
278
279
280     },
281   
282
283     resize : function()
284     {
285         this.maskEl.setSize(
286             Roo.lib.Dom.getViewWidth(true),
287             Roo.lib.Dom.getViewHeight(true)
288         );
289         
290         if (this.fitwindow) {
291             
292            
293             this.setSize(
294                 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
295                 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
296             );
297             return;
298         }
299         
300         if(this.max_width !== 0) {
301             
302             var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
303             
304             if(this.height) {
305                 this.setSize(w, this.height);
306                 return;
307             }
308             
309             if(this.max_height) {
310                 this.setSize(w,Math.min(
311                     this.max_height,
312                     Roo.lib.Dom.getViewportHeight(true) - 60
313                 ));
314                 
315                 return;
316             }
317             
318             if(!this.fit_content) {
319                 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
320                 return;
321             }
322             
323             this.setSize(w, Math.min(
324                 60 +
325                 this.headerEl.getHeight() + 
326                 this.footerEl.getHeight() + 
327                 this.getChildHeight(this.bodyEl.dom.childNodes),
328                 Roo.lib.Dom.getViewportHeight(true) - 60)
329             );
330         }
331         
332     },
333
334     setSize : function(w,h)
335     {
336         if (!w && !h) {
337             return;
338         }
339         
340         this.resizeTo(w,h);
341     },
342
343     show : function() {
344
345         if (!this.rendered) {
346             this.render();
347         }
348
349         //this.el.setStyle('display', 'block');
350         this.el.removeClass('hideing');
351         this.el.dom.style.display='block';
352         
353         Roo.get(document.body).addClass('modal-open');
354  
355         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
356             
357             (function(){
358                 this.el.addClass('show');
359                 this.el.addClass('in');
360             }).defer(50, this);
361         }else{
362             this.el.addClass('show');
363             this.el.addClass('in');
364         }
365
366         // not sure how we can show data in here..
367         //if (this.tmpl) {
368         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
369         //}
370
371         Roo.get(document.body).addClass("x-body-masked");
372         
373         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
374         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
375         this.maskEl.dom.style.display = 'block';
376         this.maskEl.addClass('show');
377         
378         
379         this.resize();
380         
381         this.fireEvent('show', this);
382
383         // set zindex here - otherwise it appears to be ignored...
384         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
385
386         (function () {
387             this.items.forEach( function(e) {
388                 e.layout ? e.layout() : false;
389
390             });
391         }).defer(100,this);
392
393     },
394     hide : function()
395     {
396         if(this.fireEvent("beforehide", this) !== false){
397             
398             this.maskEl.removeClass('show');
399             
400             this.maskEl.dom.style.display = '';
401             Roo.get(document.body).removeClass("x-body-masked");
402             this.el.removeClass('in');
403             this.el.select('.modal-dialog', true).first().setStyle('transform','');
404
405             if(this.animate){ // why
406                 this.el.addClass('hideing');
407                 this.el.removeClass('show');
408                 (function(){
409                     if (!this.el.hasClass('hideing')) {
410                         return; // it's been shown again...
411                     }
412                     
413                     this.el.dom.style.display='';
414
415                     Roo.get(document.body).removeClass('modal-open');
416                     this.el.removeClass('hideing');
417                 }).defer(150,this);
418                 
419             }else{
420                 this.el.removeClass('show');
421                 this.el.dom.style.display='';
422                 Roo.get(document.body).removeClass('modal-open');
423
424             }
425             this.fireEvent('hide', this);
426         }
427     },
428     isVisible : function()
429     {
430         
431         return this.el.hasClass('show') && !this.el.hasClass('hideing');
432         
433     },
434
435     addButton : function(str, cb)
436     {
437
438
439         var b = Roo.apply({}, { html : str } );
440         b.xns = b.xns || Roo.bootstrap;
441         b.xtype = b.xtype || 'Button';
442         if (typeof(b.listeners) == 'undefined') {
443             b.listeners = { click : cb.createDelegate(this)  };
444         }
445
446         var btn = Roo.factory(b);
447
448         btn.render(this.getButtonContainer());
449
450         return btn;
451
452     },
453
454     setDefaultButton : function(btn)
455     {
456         //this.el.select('.modal-footer').()
457     },
458
459     resizeTo: function(w,h)
460     {
461         this.dialogEl.setWidth(w);
462         
463         var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30  
464
465         this.bodyEl.setHeight(h - diff);
466         
467         this.fireEvent('resize', this);
468     },
469     
470     setContentSize  : function(w, h)
471     {
472
473     },
474     onButtonClick: function(btn,e)
475     {
476         //Roo.log([a,b,c]);
477         this.fireEvent('btnclick', btn.name, e);
478     },
479      /**
480      * Set the title of the Dialog
481      * @param {String} str new Title
482      */
483     setTitle: function(str) {
484         this.titleEl.dom.innerHTML = str;
485     },
486     /**
487      * Set the body of the Dialog
488      * @param {String} str new Title
489      */
490     setBody: function(str) {
491         this.bodyEl.dom.innerHTML = str;
492     },
493     /**
494      * Set the body of the Dialog using the template
495      * @param {Obj} data - apply this data to the template and replace the body contents.
496      */
497     applyBody: function(obj)
498     {
499         if (!this.tmpl) {
500             Roo.log("Error - using apply Body without a template");
501             //code
502         }
503         this.tmpl.overwrite(this.bodyEl, obj);
504     },
505     
506     getChildHeight : function(child_nodes)
507     {
508         if(
509             !child_nodes ||
510             child_nodes.length == 0
511         ) {
512             return;
513         }
514         
515         var child_height = 0;
516         
517         for(var i = 0; i < child_nodes.length; i++) {
518             
519             /*
520             * for modal with tabs...
521             if(child_nodes[i].classList.contains('roo-layout-panel')) {
522                 
523                 var layout_childs = child_nodes[i].childNodes;
524                 
525                 for(var j = 0; j < layout_childs.length; j++) {
526                     
527                     if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
528                         
529                         var layout_body_childs = layout_childs[j].childNodes;
530                         
531                         for(var k = 0; k < layout_body_childs.length; k++) {
532                             
533                             if(layout_body_childs[k].classList.contains('navbar')) {
534                                 child_height += layout_body_childs[k].offsetHeight;
535                                 continue;
536                             }
537                             
538                             if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
539                                 
540                                 var layout_body_tab_childs = layout_body_childs[k].childNodes;
541                                 
542                                 for(var m = 0; m < layout_body_tab_childs.length; m++) {
543                                     
544                                     if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
545                                         child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
546                                         continue;
547                                     }
548                                     
549                                 }
550                                 
551                             }
552                             
553                         }
554                     }
555                 }
556                 continue;
557             }
558             */
559             
560             child_height += child_nodes[i].offsetHeight;
561             // Roo.log(child_nodes[i].offsetHeight);
562         }
563         
564         return child_height;
565     }
566
567 });
568
569
570 Roo.apply(Roo.bootstrap.Modal,  {
571     /**
572          * Button config that displays a single OK button
573          * @type Object
574          */
575         OK :  [{
576             name : 'ok',
577             weight : 'primary',
578             html : 'OK'
579         }],
580         /**
581          * Button config that displays Yes and No buttons
582          * @type Object
583          */
584         YESNO : [
585             {
586                 name  : 'no',
587                 html : 'No'
588             },
589             {
590                 name  :'yes',
591                 weight : 'primary',
592                 html : 'Yes'
593             }
594         ],
595
596         /**
597          * Button config that displays OK and Cancel buttons
598          * @type Object
599          */
600         OKCANCEL : [
601             {
602                name : 'cancel',
603                 html : 'Cancel'
604             },
605             {
606                 name : 'ok',
607                 weight : 'primary',
608                 html : 'OK'
609             }
610         ],
611         /**
612          * Button config that displays Yes, No and Cancel buttons
613          * @type Object
614          */
615         YESNOCANCEL : [
616             {
617                 name : 'yes',
618                 weight : 'primary',
619                 html : 'Yes'
620             },
621             {
622                 name : 'no',
623                 html : 'No'
624             },
625             {
626                 name : 'cancel',
627                 html : 'Cancel'
628             }
629         ],
630         
631         zIndex : 10001
632 });