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