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