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