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  *
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          * @event titlechanged
49          * Fire when the editable title has been changed
50          * @param {Roo.bootstrap.Modal} this
51          * @param {Roo.EventObject} e
52          * @param {Roo.EventObject} value
53          */
54         "titlechanged" : true,
55         
56     });
57     this.buttons = this.buttons || [];
58
59     if (this.tmpl) {
60         this.tmpl = Roo.factory(this.tmpl);
61     }
62
63 };
64
65 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component,  {
66
67     title : 'test dialog',
68
69     buttons : false,
70
71     // set on load...
72
73     html: false,
74
75     tmp: false,
76
77     specificTitle: false,
78
79     buttonPosition: 'right',
80
81     allow_close : true,
82
83     animate : true,
84
85     fitwindow: false,
86     
87      // private
88     dialogEl: false,
89     bodyEl:  false,
90     footerEl:  false,
91     titleEl:  false,
92     closeEl:  false,
93
94     size: '',
95     
96     max_width: 0,
97     
98     max_height: 0,
99     
100     fit_content: false,
101
102     onRender : function(ct, position)
103     {
104         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
105
106         if(!this.el){
107             var cfg = Roo.apply({},  this.getAutoCreate());
108             cfg.id = Roo.id();
109             //if(!cfg.name){
110             //    cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
111             //}
112             //if (!cfg.name.length) {
113             //    delete cfg.name;
114            // }
115             if (this.cls) {
116                 cfg.cls += ' ' + this.cls;
117             }
118             if (this.style) {
119                 cfg.style = this.style;
120             }
121             this.el = Roo.get(document.body).createChild(cfg, position);
122         }
123         //var type = this.el.dom.type;
124
125
126         if(this.tabIndex !== undefined){
127             this.el.dom.setAttribute('tabIndex', this.tabIndex);
128         }
129
130         this.dialogEl = this.el.select('.modal-dialog',true).first();
131         this.bodyEl = this.el.select('.modal-body',true).first();
132         this.closeEl = this.el.select('.modal-header .close', true).first();
133         this.headerEl = this.el.select('.modal-header',true).first();
134         this.titleEl = this.el.select('.modal-title',true).first();
135         this.footerEl = this.el.select('.modal-footer',true).first();
136
137         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
138         
139         //this.el.addClass("x-dlg-modal");
140
141         if (this.buttons.length) {
142             Roo.each(this.buttons, function(bb) {
143                 var b = Roo.apply({}, bb);
144                 b.xns = b.xns || Roo.bootstrap;
145                 b.xtype = b.xtype || 'Button';
146                 if (typeof(b.listeners) == 'undefined') {
147                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
148                 }
149
150                 var btn = Roo.factory(b);
151
152                 btn.render(this.getButtonContainer());
153
154             },this);
155         }
156         // render the children.
157         var nitems = [];
158
159         if(typeof(this.items) != 'undefined'){
160             var items = this.items;
161             delete this.items;
162
163             for(var i =0;i < items.length;i++) {
164                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
165             }
166         }
167
168         this.items = nitems;
169
170         // where are these used - they used to be body/close/footer
171
172
173         this.initEvents();
174         //this.el.addClass([this.fieldClass, this.cls]);
175
176     },
177
178     getAutoCreate : function()
179     {
180         // we will default to modal-body-overflow - might need to remove or make optional later.
181         var bdy = {
182                 cls : 'modal-body enable-modal-body-overflow ', 
183                 html : this.html || ''
184         };
185
186         var title = {
187             tag: 'h4',
188             cls : 'modal-title',
189             html : this.title
190         };
191
192         if(this.specificTitle){
193             title = this.title;
194
195         }
196
197         var header = [];
198         if (this.allow_close && Roo.bootstrap.version == 3) {
199             header.push({
200                 tag: 'button',
201                 cls : 'close',
202                 html : '&times'
203             });
204         }
205
206         header.push(title);
207
208         if (this.allow_close && Roo.bootstrap.version == 4) {
209             header.push({
210                 tag: 'button',
211                 cls : 'close',
212                 html : '&times'
213             });
214         }
215         
216         var size = '';
217
218         if(this.size.length){
219             size = 'modal-' + this.size;
220         }
221         
222         var footer = Roo.bootstrap.version == 3 ?
223             {
224                 cls : 'modal-footer',
225                 cn : [
226                     {
227                         tag: 'div',
228                         cls: 'btn-' + this.buttonPosition
229                     }
230                 ]
231
232             } :
233             {  // BS4 uses mr-auto on left buttons....
234                 cls : 'modal-footer'
235             };
236
237             
238
239         
240         
241         var modal = {
242             cls: "modal",
243              cn : [
244                 {
245                     cls: "modal-dialog " + size,
246                     cn : [
247                         {
248                             cls : "modal-content",
249                             cn : [
250                                 {
251                                     cls : 'modal-header',
252                                     cn : header
253                                 },
254                                 bdy,
255                                 footer
256                             ]
257
258                         }
259                     ]
260
261                 }
262             ]
263         };
264
265         if(this.animate){
266             modal.cls += ' fade';
267         }
268
269         return modal;
270
271     },
272     getChildContainer : function() {
273
274          return this.bodyEl;
275
276     },
277     getButtonContainer : function() {
278         
279          return Roo.bootstrap.version == 4 ?
280             this.el.select('.modal-footer',true).first()
281             : this.el.select('.modal-footer div',true).first();
282
283     },
284     initEvents : function()
285     {
286         if (this.allow_close) {
287             this.closeEl.on('click', this.hide, this);
288         }
289         Roo.EventManager.onWindowResize(this.resize, this, true);
290
291
292     },
293   
294
295     resize : function()
296     {
297         this.maskEl.setSize(
298             Roo.lib.Dom.getViewWidth(true),
299             Roo.lib.Dom.getViewHeight(true)
300         );
301         
302         if (this.fitwindow) {
303             
304            
305             this.setSize(
306                 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
307                 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
308             );
309             return;
310         }
311         
312         if(this.max_width !== 0) {
313             
314             var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
315             
316             if(this.height) {
317                 this.setSize(w, this.height);
318                 return;
319             }
320             
321             if(this.max_height) {
322                 this.setSize(w,Math.min(
323                     this.max_height,
324                     Roo.lib.Dom.getViewportHeight(true) - 60
325                 ));
326                 
327                 return;
328             }
329             
330             if(!this.fit_content) {
331                 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
332                 return;
333             }
334             
335             this.setSize(w, Math.min(
336                 60 +
337                 this.headerEl.getHeight() + 
338                 this.footerEl.getHeight() + 
339                 this.getChildHeight(this.bodyEl.dom.childNodes),
340                 Roo.lib.Dom.getViewportHeight(true) - 60)
341             );
342         }
343         
344     },
345
346     setSize : function(w,h)
347     {
348         if (!w && !h) {
349             return;
350         }
351         
352         this.resizeTo(w,h);
353     },
354
355     show : function() {
356
357         if (!this.rendered) {
358             this.render();
359         }
360
361         //this.el.setStyle('display', 'block');
362         this.el.removeClass('hideing');
363         this.el.dom.style.display='block';
364         
365         Roo.get(document.body).addClass('modal-open');
366  
367         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
368             
369             (function(){
370                 this.el.addClass('show');
371                 this.el.addClass('in');
372             }).defer(50, this);
373         }else{
374             this.el.addClass('show');
375             this.el.addClass('in');
376         }
377
378         // not sure how we can show data in here..
379         //if (this.tmpl) {
380         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
381         //}
382
383         Roo.get(document.body).addClass("x-body-masked");
384         
385         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
386         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
387         this.maskEl.dom.style.display = 'block';
388         this.maskEl.addClass('show');
389         
390         
391         this.resize();
392         
393         this.fireEvent('show', this);
394
395         // set zindex here - otherwise it appears to be ignored...
396         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
397
398         (function () {
399             this.items.forEach( function(e) {
400                 e.layout ? e.layout() : false;
401
402             });
403         }).defer(100,this);
404
405     },
406     hide : function()
407     {
408         if(this.fireEvent("beforehide", this) !== false){
409             
410             this.maskEl.removeClass('show');
411             
412             this.maskEl.dom.style.display = '';
413             Roo.get(document.body).removeClass("x-body-masked");
414             this.el.removeClass('in');
415             this.el.select('.modal-dialog', true).first().setStyle('transform','');
416
417             if(this.animate){ // why
418                 this.el.addClass('hideing');
419                 this.el.removeClass('show');
420                 (function(){
421                     if (!this.el.hasClass('hideing')) {
422                         return; // it's been shown again...
423                     }
424                     
425                     this.el.dom.style.display='';
426
427                     Roo.get(document.body).removeClass('modal-open');
428                     this.el.removeClass('hideing');
429                 }).defer(150,this);
430                 
431             }else{
432                 this.el.removeClass('show');
433                 this.el.dom.style.display='';
434                 Roo.get(document.body).removeClass('modal-open');
435
436             }
437             this.fireEvent('hide', this);
438         }
439     },
440     isVisible : function()
441     {
442         
443         return this.el.hasClass('show') && !this.el.hasClass('hideing');
444         
445     },
446
447     addButton : function(str, cb)
448     {
449
450
451         var b = Roo.apply({}, { html : str } );
452         b.xns = b.xns || Roo.bootstrap;
453         b.xtype = b.xtype || 'Button';
454         if (typeof(b.listeners) == 'undefined') {
455             b.listeners = { click : cb.createDelegate(this)  };
456         }
457
458         var btn = Roo.factory(b);
459
460         btn.render(this.getButtonContainer());
461
462         return btn;
463
464     },
465
466     setDefaultButton : function(btn)
467     {
468         //this.el.select('.modal-footer').()
469     },
470
471     resizeTo: function(w,h)
472     {
473         this.dialogEl.setWidth(w);
474         
475         var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30  
476
477         this.bodyEl.setHeight(h - 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 0;
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 });
645