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