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