76003a54dc791ec5116f287895880f8531c24865
[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) {
187             header.push({
188                 tag: 'button',
189                 cls : 'close',
190                 html : '&times'
191             });
192         }
193
194         header.push(title);
195
196         var size = '';
197
198         if(this.size.length){
199             size = 'modal-' + this.size;
200         }
201
202         var modal = {
203             cls: "modal",
204              cn : [
205                 {
206                     cls: "modal-dialog " + size,
207                     cn : [
208                         {
209                             cls : "modal-content",
210                             cn : [
211                                 {
212                                     cls : 'modal-header',
213                                     cn : header
214                                 },
215                                 bdy,
216                                 {
217                                     cls : 'modal-footer',
218                                     cn : [
219                                         {
220                                             tag: 'div',
221                                             cls: 'btn-' + this.buttonPosition
222                                         }
223                                     ]
224
225                                 }
226
227
228                             ]
229
230                         }
231                     ]
232
233                 }
234             ]
235         };
236
237         if(this.animate){
238             modal.cls += ' fade';
239         }
240
241         return modal;
242
243     },
244     getChildContainer : function() {
245
246          return this.bodyEl;
247
248     },
249     getButtonContainer : function() {
250          return this.el.select('.modal-footer div',true).first();
251
252     },
253     initEvents : function()
254     {
255         if (this.allow_close) {
256             this.closeEl.on('click', this.hide, this);
257         }
258         Roo.EventManager.onWindowResize(this.resize, this, true);
259
260
261     },
262
263     resize : function()
264     {
265         this.maskEl.setSize(
266             Roo.lib.Dom.getViewWidth(true),
267             Roo.lib.Dom.getViewHeight(true)
268         );
269         
270         if (this.fitwindow) {
271             this.setSize(
272                 this.width || Roo.lib.Dom.getViewportWidth(true) - 30,
273                 this.height || Roo.lib.Dom.getViewportHeight(true) - 60
274             );
275             return;
276         }
277         
278         if(this.max_width !== 0) {
279             
280             var w = Math.min(this.max_width, Roo.lib.Dom.getViewportWidth(true) - 30);
281             
282             if(this.height) {
283                 this.setSize(w, this.height);
284                 return;
285             }
286             
287             if(this.max_height) {
288                 this.setSize(w,Math.min(
289                     this.max_height,
290                     Roo.lib.Dom.getViewportHeight(true) - 60
291                 ));
292                 
293                 return;
294             }
295             
296             if(!this.fit_content) {
297                 this.setSize(w, Roo.lib.Dom.getViewportHeight(true) - 60);
298                 return;
299             }
300             
301             this.setSize(w, Math.min(
302                 60 +
303                 this.headerEl.getHeight() + 
304                 this.footerEl.getHeight() + 
305                 this.getChildHeight(this.bodyEl.dom.childNodes),
306                 Roo.lib.Dom.getViewportHeight(true) - 60)
307             );
308         }
309         
310     },
311
312     setSize : function(w,h)
313     {
314         if (!w && !h) {
315             return;
316         }
317         
318         this.resizeTo(w,h);
319     },
320
321     show : function() {
322
323         if (!this.rendered) {
324             this.render();
325         }
326
327         //this.el.setStyle('display', 'block');
328         this.el.removeClass('hideing');        
329         this.el.addClass('show');
330  
331         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
332             var _this = this;
333             (function(){
334                 this.el.addClass('in');
335             }).defer(50, this);
336         }else{
337             this.el.addClass('in');
338         }
339
340         // not sure how we can show data in here..
341         //if (this.tmpl) {
342         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
343         //}
344
345         Roo.get(document.body).addClass("x-body-masked");
346         
347         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
348         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
349         this.maskEl.addClass('show');
350         
351         this.resize();
352         
353         this.fireEvent('show', this);
354
355         // set zindex here - otherwise it appears to be ignored...
356         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
357
358         (function () {
359             this.items.forEach( function(e) {
360                 e.layout ? e.layout() : false;
361
362             });
363         }).defer(100,this);
364
365     },
366     hide : function()
367     {
368         if(this.fireEvent("beforehide", this) !== false){
369             this.maskEl.removeClass('show');
370             Roo.get(document.body).removeClass("x-body-masked");
371             this.el.removeClass('in');
372             this.el.select('.modal-dialog', true).first().setStyle('transform','');
373
374             if(this.animate){ // why
375                 this.el.addClass('hideing');
376                 (function(){
377                     if (!this.el.hasClass('hideing')) {
378                         return; // it's been shown again...
379                     }
380                     this.el.removeClass('show');
381                     this.el.removeClass('hideing');
382                 }).defer(150,this);
383                 
384             }else{
385                  this.el.removeClass('show');
386             }
387             this.fireEvent('hide', this);
388         }
389     },
390     isVisible : function()
391     {
392         
393         return this.el.hasClass('show') && !this.el.hasClass('hideing');
394         
395     },
396
397     addButton : function(str, cb)
398     {
399
400
401         var b = Roo.apply({}, { html : str } );
402         b.xns = b.xns || Roo.bootstrap;
403         b.xtype = b.xtype || 'Button';
404         if (typeof(b.listeners) == 'undefined') {
405             b.listeners = { click : cb.createDelegate(this)  };
406         }
407
408         var btn = Roo.factory(b);
409
410         btn.render(this.el.select('.modal-footer div').first());
411
412         return btn;
413
414     },
415
416     setDefaultButton : function(btn)
417     {
418         //this.el.select('.modal-footer').()
419     },
420     diff : false,
421
422     resizeTo: function(w,h)
423     {
424         // skip.. ?? why??
425
426         this.dialogEl.setWidth(w);
427         if (this.diff === false) {
428             this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
429         }
430
431         this.bodyEl.setHeight(h - this.diff);
432
433         this.fireEvent('resize', this);
434
435     },
436     setContentSize  : function(w, h)
437     {
438
439     },
440     onButtonClick: function(btn,e)
441     {
442         //Roo.log([a,b,c]);
443         this.fireEvent('btnclick', btn.name, e);
444     },
445      /**
446      * Set the title of the Dialog
447      * @param {String} str new Title
448      */
449     setTitle: function(str) {
450         this.titleEl.dom.innerHTML = str;
451     },
452     /**
453      * Set the body of the Dialog
454      * @param {String} str new Title
455      */
456     setBody: function(str) {
457         this.bodyEl.dom.innerHTML = str;
458     },
459     /**
460      * Set the body of the Dialog using the template
461      * @param {Obj} data - apply this data to the template and replace the body contents.
462      */
463     applyBody: function(obj)
464     {
465         if (!this.tmpl) {
466             Roo.log("Error - using apply Body without a template");
467             //code
468         }
469         this.tmpl.overwrite(this.bodyEl, obj);
470     },
471     
472     getChildHeight : function(child_nodes)
473     {
474         if(
475             !child_nodes ||
476             child_nodes.length == 0
477         ) {
478             return;
479         }
480         
481         var child_height = 0;
482         
483         for(var i = 0; i < child_nodes.length; i++) {
484             
485             /*
486             * for modal with tabs...
487             if(child_nodes[i].classList.contains('roo-layout-panel')) {
488                 
489                 var layout_childs = child_nodes[i].childNodes;
490                 
491                 for(var j = 0; j < layout_childs.length; j++) {
492                     
493                     if(layout_childs[j].classList.contains('roo-layout-panel-body')) {
494                         
495                         var layout_body_childs = layout_childs[j].childNodes;
496                         
497                         for(var k = 0; k < layout_body_childs.length; k++) {
498                             
499                             if(layout_body_childs[k].classList.contains('navbar')) {
500                                 child_height += layout_body_childs[k].offsetHeight;
501                                 continue;
502                             }
503                             
504                             if(layout_body_childs[k].classList.contains('roo-layout-tabs-body')) {
505                                 
506                                 var layout_body_tab_childs = layout_body_childs[k].childNodes;
507                                 
508                                 for(var m = 0; m < layout_body_tab_childs.length; m++) {
509                                     
510                                     if(layout_body_tab_childs[m].classList.contains('roo-layout-active-content')) {
511                                         child_height += this.getChildHeight(layout_body_tab_childs[m].childNodes);
512                                         continue;
513                                     }
514                                     
515                                 }
516                                 
517                             }
518                             
519                         }
520                     }
521                 }
522                 continue;
523             }
524             */
525             
526             child_height += child_nodes[i].offsetHeight;
527             // Roo.log(child_nodes[i].offsetHeight);
528         }
529         
530         return child_height;
531     }
532
533 });
534
535
536 Roo.apply(Roo.bootstrap.Modal,  {
537     /**
538          * Button config that displays a single OK button
539          * @type Object
540          */
541         OK :  [{
542             name : 'ok',
543             weight : 'primary',
544             html : 'OK'
545         }],
546         /**
547          * Button config that displays Yes and No buttons
548          * @type Object
549          */
550         YESNO : [
551             {
552                 name  : 'no',
553                 html : 'No'
554             },
555             {
556                 name  :'yes',
557                 weight : 'primary',
558                 html : 'Yes'
559             }
560         ],
561
562         /**
563          * Button config that displays OK and Cancel buttons
564          * @type Object
565          */
566         OKCANCEL : [
567             {
568                name : 'cancel',
569                 html : 'Cancel'
570             },
571             {
572                 name : 'ok',
573                 weight : 'primary',
574                 html : 'OK'
575             }
576         ],
577         /**
578          * Button config that displays Yes, No and Cancel buttons
579          * @type Object
580          */
581         YESNOCANCEL : [
582             {
583                 name : 'yes',
584                 weight : 'primary',
585                 html : 'Yes'
586             },
587             {
588                 name : 'no',
589                 html : 'No'
590             },
591             {
592                 name : 'cancel',
593                 html : 'Cancel'
594             }
595         ],
596         
597         zIndex : 10001
598 });