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