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