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