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