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