Merge branch 'master' of http://git.roojs.com/roojs1
[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                 // we force children not to montor widnow resize  - as we do that for them.
170                 items[i].monitorWindowResize = false;
171                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
172             }
173         }
174
175         this.items = nitems;
176
177         // where are these used - they used to be body/close/footer
178
179
180         this.initEvents();
181         //this.el.addClass([this.fieldClass, this.cls]);
182
183     },
184
185     getAutoCreate : function()
186     {
187         // we will default to modal-body-overflow - might need to remove or make optional later.
188         var bdy = {
189                 cls : 'modal-body ' + (this.bodyOverflow ? 'overflow-auto' : ''), 
190                 html : this.html || ''
191         };
192
193         var title = {
194             tag: 'h5',
195             cls : 'modal-title',
196             html : this.title
197         };
198
199         if(this.specificTitle){ // WTF is this?
200             title = this.title;
201         }
202
203         var header = [];
204         if (this.allow_close && Roo.bootstrap.version == 3) {
205             header.push({
206                 tag: 'button',
207                 cls : 'close',
208                 html : '&times'
209             });
210         }
211
212         header.push(title);
213
214         if (this.editableTitle) {
215             header.push({
216                 cls: 'form-control roo-editable-title d-none',
217                 tag: 'input',
218                 type: 'text'
219             });
220         }
221         
222         if (this.allow_close && Roo.bootstrap.version == 4) {
223             header.push({
224                 tag: 'button',
225                 cls : 'close',
226                 html : '&times'
227             });
228         }
229         
230         var size = '';
231
232         if(this.size.length){
233             size = 'modal-' + this.size;
234         }
235         
236         var footer = Roo.bootstrap.version == 3 ?
237             {
238                 cls : 'modal-footer',
239                 cn : [
240                     {
241                         tag: 'div',
242                         cls: 'btn-' + this.buttonPosition
243                     }
244                 ]
245
246             } :
247             {  // BS4 uses mr-auto on left buttons....
248                 cls : 'modal-footer'
249             };
250
251             
252
253         
254         
255         var modal = {
256             cls: "modal",
257              cn : [
258                 {
259                     cls: "modal-dialog " + size,
260                     cn : [
261                         {
262                             cls : "modal-content",
263                             cn : [
264                                 {
265                                     cls : 'modal-header',
266                                     cn : header
267                                 },
268                                 bdy,
269                                 footer
270                             ]
271
272                         }
273                     ]
274
275                 }
276             ]
277         };
278
279         if(this.animate){
280             modal.cls += ' fade';
281         }
282
283         return modal;
284
285     },
286     getChildContainer : function() {
287
288          return this.bodyEl;
289
290     },
291     getButtonContainer : function() {
292         
293          return Roo.bootstrap.version == 4 ?
294             this.el.select('.modal-footer',true).first()
295             : this.el.select('.modal-footer div',true).first();
296
297     },
298     
299     closeClick : function()
300     {
301         this.hide();
302     },
303     
304     initEvents : function()
305     {
306         if (this.allow_close) {
307             this.closeEl.on('click', this.closeClick, this);
308         }
309         Roo.EventManager.onWindowResize(this.resize, this, true);
310         if (this.editableTitle) {
311             this.headerEditEl =  this.headerEl.select('.form-control',true).first();
312             this.headerEl.on('click', function() { this.toggleHeaderInput(true) } , this);
313             this.headerEditEl.on('keyup', function(e) {
314                     if([  e.RETURN , e.TAB , e.ESC ].indexOf(e.keyCode) > -1) {
315                         this.toggleHeaderInput(false)
316                     }
317                 }, this);
318             this.headerEditEl.on('blur', function(e) {
319                 this.toggleHeaderInput(false)
320             },this);
321         }
322
323     },
324   
325
326     resize : function()
327     {
328         this.maskEl.setSize(
329             Roo.lib.Dom.getViewWidth(true),
330             Roo.lib.Dom.getViewHeight(true)
331         );
332         
333         if (this.fitwindow) {
334             
335            this.dialogEl.setStyle( { 'max-width' : '100%' });
336             this.setSize(
337                 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
338                 this.height || Roo.lib.Dom.getViewportHeight(true) // catering margin-top 30 margin-bottom 30
339             );
340             return;
341         }
342         
343         if(this.max_width !== 0) {
344             
345             var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
346             
347             if(this.height) {
348                 this.setSize(w, this.height);
349                 return;
350             }
351             
352             if(this.max_height) {
353                 this.setSize(w,Math.min(
354                     this.max_height,
355                     Roo.lib.Dom.getViewportHeight(true) - 60
356                 ));
357                 
358                 return;
359             }
360             
361             if(!this.fit_content) {
362                 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
363                 return;
364             }
365             
366             this.setSize(w, Math.min(
367                 60 +
368                 this.headerEl.getHeight() + 
369                 this.footerEl.getHeight() + 
370                 this.getChildHeight(this.bodyEl.dom.childNodes),
371                 Roo.lib.Dom.getViewportHeight(true) - 60)
372             );
373         }
374         
375     },
376
377     setSize : function(w,h)
378     {
379         if (!w && !h) {
380             return;
381         }
382         
383         this.resizeTo(w,h);
384         // any layout/border etc.. resize..
385         (function () {
386             this.items.forEach( function(e) {
387                 e.layout ? e.layout() : false;
388
389             });
390         }).defer(100,this);
391         
392     },
393
394     show : function() {
395
396         if (!this.rendered) {
397             this.render();
398         }
399         this.toggleHeaderInput(false);
400         //this.el.setStyle('display', 'block');
401         this.el.removeClass('hideing');
402         this.el.dom.style.display='block';
403         
404         Roo.get(document.body).addClass('modal-open');
405  
406         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
407             
408             (function(){
409                 this.el.addClass('show');
410                 this.el.addClass('in');
411             }).defer(50, this);
412         }else{
413             this.el.addClass('show');
414             this.el.addClass('in');
415         }
416
417         // not sure how we can show data in here..
418         //if (this.tmpl) {
419         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
420         //}
421
422         Roo.get(document.body).addClass("x-body-masked");
423         
424         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
425         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
426         this.maskEl.dom.style.display = 'block';
427         this.maskEl.addClass('show');
428         
429         
430         this.resize();
431         
432         this.fireEvent('show', this);
433
434         // set zindex here - otherwise it appears to be ignored...
435         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
436         
437         
438         // this is for children that are... layout.Border 
439         (function () {
440             this.items.forEach( function(e) {
441                 e.layout ? e.layout() : false;
442
443             });
444         }).defer(100,this);
445
446     },
447     hide : function()
448     {
449         if(this.fireEvent("beforehide", this) !== false){
450             
451             this.maskEl.removeClass('show');
452             
453             this.maskEl.dom.style.display = '';
454             Roo.get(document.body).removeClass("x-body-masked");
455             this.el.removeClass('in');
456             this.el.select('.modal-dialog', true).first().setStyle('transform','');
457
458             if(this.animate){ // why
459                 this.el.addClass('hideing');
460                 this.el.removeClass('show');
461                 (function(){
462                     if (!this.el.hasClass('hideing')) {
463                         return; // it's been shown again...
464                     }
465                     
466                     this.el.dom.style.display='';
467
468                     Roo.get(document.body).removeClass('modal-open');
469                     this.el.removeClass('hideing');
470                 }).defer(150,this);
471                 
472             }else{
473                 this.el.removeClass('show');
474                 this.el.dom.style.display='';
475                 Roo.get(document.body).removeClass('modal-open');
476
477             }
478             this.fireEvent('hide', this);
479         }
480     },
481     isVisible : function()
482     {
483         
484         return this.el.hasClass('show') && !this.el.hasClass('hideing');
485         
486     },
487
488     addButton : function(str, cb)
489     {
490
491
492         var b = Roo.apply({}, { html : str } );
493         b.xns = b.xns || Roo.bootstrap;
494         b.xtype = b.xtype || 'Button';
495         if (typeof(b.listeners) == 'undefined') {
496             b.listeners = { click : cb.createDelegate(this)  };
497         }
498
499         var btn = Roo.factory(b);
500
501         btn.render(this.getButtonContainer());
502
503         return btn;
504
505     },
506
507     setDefaultButton : function(btn)
508     {
509         //this.el.select('.modal-footer').()
510     },
511
512     resizeTo: function(w,h)
513     {
514         this.dialogEl.setWidth(w);
515         
516         var diff = this.headerEl.getHeight() + this.footerEl.getHeight() + 60; // dialog margin-bottom: 30  
517
518         this.bodyEl.setHeight(h - diff);
519         
520         this.fireEvent('resize', this);
521     },
522     
523     setContentSize  : function(w, h)
524     {
525
526     },
527     onButtonClick: function(btn,e)
528     {
529         //Roo.log([a,b,c]);
530         this.fireEvent('btnclick', btn.name, e);
531     },
532      /**
533      * Set the title of the Dialog
534      * @param {String} str new Title
535      */
536     setTitle: function(str) {
537         this.titleEl.dom.innerHTML = str;
538         this.title = str;
539     },
540     /**
541      * Set the body of the Dialog
542      * @param {String} str new Title
543      */
544     setBody: function(str) {
545         this.bodyEl.dom.innerHTML = str;
546     },
547     /**
548      * Set the body of the Dialog using the template
549      * @param {Obj} data - apply this data to the template and replace the body contents.
550      */
551     applyBody: function(obj)
552     {
553         if (!this.tmpl) {
554             Roo.log("Error - using apply Body without a template");
555             //code
556         }
557         this.tmpl.overwrite(this.bodyEl, obj);
558     },
559     
560     getChildHeight : function(child_nodes)
561     {
562         if(
563             !child_nodes ||
564             child_nodes.length == 0
565         ) {
566             return 0;
567         }
568         
569         var child_height = 0;
570         
571         for(var i = 0; i < child_nodes.length; i++) {
572             
573             /*
574             * for modal with tabs...
575             if(child_nodes[i].classList.contains('roo-layout-panel')) {
576                 
577                 var layout_childs = child_nodes[i].childNodes;
578                 
579                 for(var j = 0; j < layout_childs.length; j++) {
580                     
581                     if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
582                         
583                         var layout_body_childs = layout_childs[j].childNodes;
584                         
585                         for(var k = 0; k < layout_body_childs.length; k++) {
586                             
587                             if(layout_body_childs[k].classList.contains('navbar')) {
588                                 child_height += layout_body_childs[k].offsetHeight;
589                                 continue;
590                             }
591                             
592                             if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
593                                 
594                                 var layout_body_tab_childs = layout_body_childs[k].childNodes;
595                                 
596                                 for(var m = 0; m < layout_body_tab_childs.length; m++) {
597                                     
598                                     if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
599                                         child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
600                                         continue;
601                                     }
602                                     
603                                 }
604                                 
605                             }
606                             
607                         }
608                     }
609                 }
610                 continue;
611             }
612             */
613             
614             child_height += child_nodes[i].offsetHeight;
615             // Roo.log(child_nodes[i].offsetHeight);
616         }
617         
618         return child_height;
619     },
620     toggleHeaderInput : function(is_edit)
621     {
622         if (!this.editableTitle) {
623             return; // not editable.
624         }
625         if (is_edit && this.is_header_editing) {
626             return; // already editing..
627         }
628         if (is_edit) {
629     
630             this.headerEditEl.dom.value = this.title;
631             this.headerEditEl.removeClass('d-none');
632             this.headerEditEl.dom.focus();
633             this.titleEl.addClass('d-none');
634             
635             this.is_header_editing = true;
636             return
637         }
638         // flip back to not editing.
639         this.title = this.headerEditEl.dom.value;
640         this.headerEditEl.addClass('d-none');
641         this.titleEl.removeClass('d-none');
642         this.titleEl.dom.innerHTML = String.format('{0}', this.title);
643         this.is_header_editing = false;
644         this.fireEvent('titlechanged', this, this.title);
645     
646             
647         
648     }
649
650 });
651
652
653 Roo.apply(Roo.bootstrap.Modal,  {
654     /**
655          * Button config that displays a single OK button
656          * @type Object
657          */
658         OK :  [{
659             name : 'ok',
660             weight : 'primary',
661             html : 'OK'
662         }],
663         /**
664          * Button config that displays Yes and No buttons
665          * @type Object
666          */
667         YESNO : [
668             {
669                 name  : 'no',
670                 html : 'No'
671             },
672             {
673                 name  :'yes',
674                 weight : 'primary',
675                 html : 'Yes'
676             }
677         ],
678
679         /**
680          * Button config that displays OK and Cancel buttons
681          * @type Object
682          */
683         OKCANCEL : [
684             {
685                name : 'cancel',
686                 html : 'Cancel'
687             },
688             {
689                 name : 'ok',
690                 weight : 'primary',
691                 html : 'OK'
692             }
693         ],
694         /**
695          * Button config that displays Yes, No and Cancel buttons
696          * @type Object
697          */
698         YESNOCANCEL : [
699             {
700                 name : 'yes',
701                 weight : 'primary',
702                 html : 'Yes'
703             },
704             {
705                 name : 'no',
706                 html : 'No'
707             },
708             {
709                 name : 'cancel',
710                 html : 'Cancel'
711             }
712         ],
713         
714         zIndex : 10001
715 });
716