d01b306f59b8c48ea42348c88bf2385f4140b2c9
[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 {Number} width fixed width - usefull for chrome extension only really.
20  * @cfg {Number} height fixed height - usefull for chrome extension only really.
21  * @cfg {String} size (sm|lg) default empty
22  * @cfg {Number} max_width set the max width of modal
23  * @cfg {Boolean} editable_title can the title be edited
24
25  *
26  *
27  * @constructor
28  * Create a new Modal Dialog
29  * @param {Object} config The config object
30  */
31
32 Roo.bootstrap.Modal = function(config){
33     Roo.bootstrap.Modal.superclass.constructor.call(this, config);
34     this.addEvents({
35         // raw events
36         /**
37          * @event btnclick
38          * The raw btnclick event for the button
39          * @param {Roo.EventObject} e
40          */
41         "btnclick" : true,
42         /**
43          * @event resize
44          * Fire when dialog resize
45          * @param {Roo.bootstrap.Modal} this
46          * @param {Roo.EventObject} e
47          */
48         "resize" : true,
49         /**
50          * @event titlechanged
51          * Fire when the editable title has been changed
52          * @param {Roo.bootstrap.Modal} this
53          * @param {Roo.EventObject} e
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     editable_title  : 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 enable-modal-body-overflow ', 
186                 html : this.html || ''
187         };
188
189         var title = {
190             tag: 'h4',
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.editable_title) {
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.editable_title) {
301             this.headerEditEl =  this.headerEl.select('.form-control',true).first();
302             this.headerEl.on('click', this.showHeaderInput, this);
303         }
304
305     },
306   
307
308     resize : function()
309     {
310         this.maskEl.setSize(
311             Roo.lib.Dom.getViewWidth(true),
312             Roo.lib.Dom.getViewHeight(true)
313         );
314         
315         if (this.fitwindow) {
316             
317            
318             this.setSize(
319                 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
320                 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
321             );
322             return;
323         }
324         
325         if(this.max_width !== 0) {
326             
327             var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
328             
329             if(this.height) {
330                 this.setSize(w, this.height);
331                 return;
332             }
333             
334             if(this.max_height) {
335                 this.setSize(w,Math.min(
336                     this.max_height,
337                     Roo.lib.Dom.getViewportHeight(true) - 60
338                 ));
339                 
340                 return;
341             }
342             
343             if(!this.fit_content) {
344                 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
345                 return;
346             }
347             
348             this.setSize(w, Math.min(
349                 60 +
350                 this.headerEl.getHeight() + 
351                 this.footerEl.getHeight() + 
352                 this.getChildHeight(this.bodyEl.dom.childNodes),
353                 Roo.lib.Dom.getViewportHeight(true) - 60)
354             );
355         }
356         
357     },
358
359     setSize : function(w,h)
360     {
361         if (!w && !h) {
362             return;
363         }
364         
365         this.resizeTo(w,h);
366     },
367
368     show : function() {
369
370         if (!this.rendered) {
371             this.render();
372         }
373
374         //this.el.setStyle('display', 'block');
375         this.el.removeClass('hideing');
376         this.el.dom.style.display='block';
377         
378         Roo.get(document.body).addClass('modal-open');
379  
380         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
381             
382             (function(){
383                 this.el.addClass('show');
384                 this.el.addClass('in');
385             }).defer(50, this);
386         }else{
387             this.el.addClass('show');
388             this.el.addClass('in');
389         }
390
391         // not sure how we can show data in here..
392         //if (this.tmpl) {
393         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
394         //}
395
396         Roo.get(document.body).addClass("x-body-masked");
397         
398         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
399         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
400         this.maskEl.dom.style.display = 'block';
401         this.maskEl.addClass('show');
402         
403         
404         this.resize();
405         
406         this.fireEvent('show', this);
407
408         // set zindex here - otherwise it appears to be ignored...
409         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
410
411         (function () {
412             this.items.forEach( function(e) {
413                 e.layout ? e.layout() : false;
414
415             });
416         }).defer(100,this);
417
418     },
419     hide : function()
420     {
421         if(this.fireEvent("beforehide", this) !== false){
422             
423             this.maskEl.removeClass('show');
424             
425             this.maskEl.dom.style.display = '';
426             Roo.get(document.body).removeClass("x-body-masked");
427             this.el.removeClass('in');
428             this.el.select('.modal-dialog', true).first().setStyle('transform','');
429
430             if(this.animate){ // why
431                 this.el.addClass('hideing');
432                 this.el.removeClass('show');
433                 (function(){
434                     if (!this.el.hasClass('hideing')) {
435                         return; // it's been shown again...
436                     }
437                     
438                     this.el.dom.style.display='';
439
440                     Roo.get(document.body).removeClass('modal-open');
441                     this.el.removeClass('hideing');
442                 }).defer(150,this);
443                 
444             }else{
445                 this.el.removeClass('show');
446                 this.el.dom.style.display='';
447                 Roo.get(document.body).removeClass('modal-open');
448
449             }
450             this.fireEvent('hide', this);
451         }
452     },
453     isVisible : function()
454     {
455         
456         return this.el.hasClass('show') && !this.el.hasClass('hideing');
457         
458     },
459
460     addButton : function(str, cb)
461     {
462
463
464         var b = Roo.apply({}, { html : str } );
465         b.xns = b.xns || Roo.bootstrap;
466         b.xtype = b.xtype || 'Button';
467         if (typeof(b.listeners) == 'undefined') {
468             b.listeners = { click : cb.createDelegate(this)  };
469         }
470
471         var btn = Roo.factory(b);
472
473         btn.render(this.getButtonContainer());
474
475         return btn;
476
477     },
478
479     setDefaultButton : function(btn)
480     {
481         //this.el.select('.modal-footer').()
482     },
483
484     resizeTo: function(w,h)
485     {
486         this.dialogEl.setWidth(w);
487         
488         var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30  
489
490         this.bodyEl.setHeight(h - diff);
491         
492         this.fireEvent('resize', this);
493     },
494     
495     setContentSize  : function(w, h)
496     {
497
498     },
499     onButtonClick: function(btn,e)
500     {
501         //Roo.log([a,b,c]);
502         this.fireEvent('btnclick', btn.name, e);
503     },
504      /**
505      * Set the title of the Dialog
506      * @param {String} str new Title
507      */
508     setTitle: function(str) {
509         this.titleEl.dom.innerHTML = str;
510     },
511     /**
512      * Set the body of the Dialog
513      * @param {String} str new Title
514      */
515     setBody: function(str) {
516         this.bodyEl.dom.innerHTML = str;
517     },
518     /**
519      * Set the body of the Dialog using the template
520      * @param {Obj} data - apply this data to the template and replace the body contents.
521      */
522     applyBody: function(obj)
523     {
524         if (!this.tmpl) {
525             Roo.log("Error - using apply Body without a template");
526             //code
527         }
528         this.tmpl.overwrite(this.bodyEl, obj);
529     },
530     
531     getChildHeight : function(child_nodes)
532     {
533         if(
534             !child_nodes ||
535             child_nodes.length == 0
536         ) {
537             return 0;
538         }
539         
540         var child_height = 0;
541         
542         for(var i = 0; i < child_nodes.length; i++) {
543             
544             /*
545             * for modal with tabs...
546             if(child_nodes[i].classList.contains('roo-layout-panel')) {
547                 
548                 var layout_childs = child_nodes[i].childNodes;
549                 
550                 for(var j = 0; j < layout_childs.length; j++) {
551                     
552                     if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
553                         
554                         var layout_body_childs = layout_childs[j].childNodes;
555                         
556                         for(var k = 0; k < layout_body_childs.length; k++) {
557                             
558                             if(layout_body_childs[k].classList.contains('navbar')) {
559                                 child_height += layout_body_childs[k].offsetHeight;
560                                 continue;
561                             }
562                             
563                             if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
564                                 
565                                 var layout_body_tab_childs = layout_body_childs[k].childNodes;
566                                 
567                                 for(var m = 0; m < layout_body_tab_childs.length; m++) {
568                                     
569                                     if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
570                                         child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
571                                         continue;
572                                     }
573                                     
574                                 }
575                                 
576                             }
577                             
578                         }
579                     }
580                 }
581                 continue;
582             }
583             */
584             
585             child_height += child_nodes[i].offsetHeight;
586             // Roo.log(child_nodes[i].offsetHeight);
587         }
588         
589         return child_height;
590     },
591     toggleHeaderInput : function(is_edit)
592     {
593         
594         if (is_edit && this.is_header_editing) {
595             return; // already editing..
596         }
597         if (is_edit) {
598     
599             this.headerEditEl.setValue(this.title);
600             this.headerEditEl.removeClass('d-none');
601             this.titleEl.addClass('d-none');
602             this.is_header_editing = true;
603             return
604         }
605         // flip back to not editing.
606         this.title = this.headerEditEl.getValue(this.title);
607         this.headerEditEl.addClass('d-none');
608         this.titleEl.removeClass('d-none');
609         this.titleEl.dom.innerHTML = String.format('{0}', this.title);
610         this.is_header_editing = false;
611         this.fireEvent('title-changed', this, this.title);
612     
613             
614         
615     }
616
617 });
618
619
620 Roo.apply(Roo.bootstrap.Modal,  {
621     /**
622          * Button config that displays a single OK button
623          * @type Object
624          */
625         OK :  [{
626             name : 'ok',
627             weight : 'primary',
628             html : 'OK'
629         }],
630         /**
631          * Button config that displays Yes and No buttons
632          * @type Object
633          */
634         YESNO : [
635             {
636                 name  : 'no',
637                 html : 'No'
638             },
639             {
640                 name  :'yes',
641                 weight : 'primary',
642                 html : 'Yes'
643             }
644         ],
645
646         /**
647          * Button config that displays OK and Cancel buttons
648          * @type Object
649          */
650         OKCANCEL : [
651             {
652                name : 'cancel',
653                 html : 'Cancel'
654             },
655             {
656                 name : 'ok',
657                 weight : 'primary',
658                 html : 'OK'
659             }
660         ],
661         /**
662          * Button config that displays Yes, No and Cancel buttons
663          * @type Object
664          */
665         YESNOCANCEL : [
666             {
667                 name : 'yes',
668                 weight : 'primary',
669                 html : 'Yes'
670             },
671             {
672                 name : 'no',
673                 html : 'No'
674             },
675             {
676                 name : 'cancel',
677                 html : 'Cancel'
678             }
679         ],
680         
681         zIndex : 10001
682 });
683