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         if (this.editable_title) {
294             this.headerEl.on('click', this.showHeaderInput, this);
295         }
296
297     },
298   
299
300     resize : function()
301     {
302         this.maskEl.setSize(
303             Roo.lib.Dom.getViewWidth(true),
304             Roo.lib.Dom.getViewHeight(true)
305         );
306         
307         if (this.fitwindow) {
308             
309            
310             this.setSize(
311                 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
312                 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
313             );
314             return;
315         }
316         
317         if(this.max_width !== 0) {
318             
319             var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
320             
321             if(this.height) {
322                 this.setSize(w, this.height);
323                 return;
324             }
325             
326             if(this.max_height) {
327                 this.setSize(w,Math.min(
328                     this.max_height,
329                     Roo.lib.Dom.getViewportHeight(true) - 60
330                 ));
331                 
332                 return;
333             }
334             
335             if(!this.fit_content) {
336                 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
337                 return;
338             }
339             
340             this.setSize(w, Math.min(
341                 60 +
342                 this.headerEl.getHeight() + 
343                 this.footerEl.getHeight() + 
344                 this.getChildHeight(this.bodyEl.dom.childNodes),
345                 Roo.lib.Dom.getViewportHeight(true) - 60)
346             );
347         }
348         
349     },
350
351     setSize : function(w,h)
352     {
353         if (!w && !h) {
354             return;
355         }
356         
357         this.resizeTo(w,h);
358     },
359
360     show : function() {
361
362         if (!this.rendered) {
363             this.render();
364         }
365
366         //this.el.setStyle('display', 'block');
367         this.el.removeClass('hideing');
368         this.el.dom.style.display='block';
369         
370         Roo.get(document.body).addClass('modal-open');
371  
372         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
373             
374             (function(){
375                 this.el.addClass('show');
376                 this.el.addClass('in');
377             }).defer(50, this);
378         }else{
379             this.el.addClass('show');
380             this.el.addClass('in');
381         }
382
383         // not sure how we can show data in here..
384         //if (this.tmpl) {
385         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
386         //}
387
388         Roo.get(document.body).addClass("x-body-masked");
389         
390         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
391         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
392         this.maskEl.dom.style.display = 'block';
393         this.maskEl.addClass('show');
394         
395         
396         this.resize();
397         
398         this.fireEvent('show', this);
399
400         // set zindex here - otherwise it appears to be ignored...
401         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
402
403         (function () {
404             this.items.forEach( function(e) {
405                 e.layout ? e.layout() : false;
406
407             });
408         }).defer(100,this);
409
410     },
411     hide : function()
412     {
413         if(this.fireEvent("beforehide", this) !== false){
414             
415             this.maskEl.removeClass('show');
416             
417             this.maskEl.dom.style.display = '';
418             Roo.get(document.body).removeClass("x-body-masked");
419             this.el.removeClass('in');
420             this.el.select('.modal-dialog', true).first().setStyle('transform','');
421
422             if(this.animate){ // why
423                 this.el.addClass('hideing');
424                 this.el.removeClass('show');
425                 (function(){
426                     if (!this.el.hasClass('hideing')) {
427                         return; // it's been shown again...
428                     }
429                     
430                     this.el.dom.style.display='';
431
432                     Roo.get(document.body).removeClass('modal-open');
433                     this.el.removeClass('hideing');
434                 }).defer(150,this);
435                 
436             }else{
437                 this.el.removeClass('show');
438                 this.el.dom.style.display='';
439                 Roo.get(document.body).removeClass('modal-open');
440
441             }
442             this.fireEvent('hide', this);
443         }
444     },
445     isVisible : function()
446     {
447         
448         return this.el.hasClass('show') && !this.el.hasClass('hideing');
449         
450     },
451
452     addButton : function(str, cb)
453     {
454
455
456         var b = Roo.apply({}, { html : str } );
457         b.xns = b.xns || Roo.bootstrap;
458         b.xtype = b.xtype || 'Button';
459         if (typeof(b.listeners) == 'undefined') {
460             b.listeners = { click : cb.createDelegate(this)  };
461         }
462
463         var btn = Roo.factory(b);
464
465         btn.render(this.getButtonContainer());
466
467         return btn;
468
469     },
470
471     setDefaultButton : function(btn)
472     {
473         //this.el.select('.modal-footer').()
474     },
475
476     resizeTo: function(w,h)
477     {
478         this.dialogEl.setWidth(w);
479         
480         var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30  
481
482         this.bodyEl.setHeight(h - diff);
483         
484         this.fireEvent('resize', this);
485     },
486     
487     setContentSize  : function(w, h)
488     {
489
490     },
491     onButtonClick: function(btn,e)
492     {
493         //Roo.log([a,b,c]);
494         this.fireEvent('btnclick', btn.name, e);
495     },
496      /**
497      * Set the title of the Dialog
498      * @param {String} str new Title
499      */
500     setTitle: function(str) {
501         this.titleEl.dom.innerHTML = str;
502     },
503     /**
504      * Set the body of the Dialog
505      * @param {String} str new Title
506      */
507     setBody: function(str) {
508         this.bodyEl.dom.innerHTML = str;
509     },
510     /**
511      * Set the body of the Dialog using the template
512      * @param {Obj} data - apply this data to the template and replace the body contents.
513      */
514     applyBody: function(obj)
515     {
516         if (!this.tmpl) {
517             Roo.log("Error - using apply Body without a template");
518             //code
519         }
520         this.tmpl.overwrite(this.bodyEl, obj);
521     },
522     
523     getChildHeight : function(child_nodes)
524     {
525         if(
526             !child_nodes ||
527             child_nodes.length == 0
528         ) {
529             return 0;
530         }
531         
532         var child_height = 0;
533         
534         for(var i = 0; i < child_nodes.length; i++) {
535             
536             /*
537             * for modal with tabs...
538             if(child_nodes[i].classList.contains('roo-layout-panel')) {
539                 
540                 var layout_childs = child_nodes[i].childNodes;
541                 
542                 for(var j = 0; j < layout_childs.length; j++) {
543                     
544                     if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
545                         
546                         var layout_body_childs = layout_childs[j].childNodes;
547                         
548                         for(var k = 0; k < layout_body_childs.length; k++) {
549                             
550                             if(layout_body_childs[k].classList.contains('navbar')) {
551                                 child_height += layout_body_childs[k].offsetHeight;
552                                 continue;
553                             }
554                             
555                             if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
556                                 
557                                 var layout_body_tab_childs = layout_body_childs[k].childNodes;
558                                 
559                                 for(var m = 0; m < layout_body_tab_childs.length; m++) {
560                                     
561                                     if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
562                                         child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
563                                         continue;
564                                     }
565                                     
566                                 }
567                                 
568                             }
569                             
570                         }
571                     }
572                 }
573                 continue;
574             }
575             */
576             
577             child_height += child_nodes[i].offsetHeight;
578             // Roo.log(child_nodes[i].offsetHeight);
579         }
580         
581         return child_height;
582     },
583     showHeaderInput : function() {
584         
585         
586         
587     }
588
589 });
590
591
592 Roo.apply(Roo.bootstrap.Modal,  {
593     /**
594          * Button config that displays a single OK button
595          * @type Object
596          */
597         OK :  [{
598             name : 'ok',
599             weight : 'primary',
600             html : 'OK'
601         }],
602         /**
603          * Button config that displays Yes and No buttons
604          * @type Object
605          */
606         YESNO : [
607             {
608                 name  : 'no',
609                 html : 'No'
610             },
611             {
612                 name  :'yes',
613                 weight : 'primary',
614                 html : 'Yes'
615             }
616         ],
617
618         /**
619          * Button config that displays OK and Cancel buttons
620          * @type Object
621          */
622         OKCANCEL : [
623             {
624                name : 'cancel',
625                 html : 'Cancel'
626             },
627             {
628                 name : 'ok',
629                 weight : 'primary',
630                 html : 'OK'
631             }
632         ],
633         /**
634          * Button config that displays Yes, No and Cancel buttons
635          * @type Object
636          */
637         YESNOCANCEL : [
638             {
639                 name : 'yes',
640                 weight : 'primary',
641                 html : 'Yes'
642             },
643             {
644                 name : 'no',
645                 html : 'No'
646             },
647             {
648                 name : 'cancel',
649                 html : 'Cancel'
650             }
651         ],
652         
653         zIndex : 10001
654 });
655