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