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