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