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