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  * 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  *
21  *
22  * @constructor
23  * Create a new Modal Dialog
24  * @param {Object} config The config object
25  */
26
27 Roo.bootstrap.Modal = function(config){
28     Roo.bootstrap.Modal.superclass.constructor.call(this, config);
29     this.addEvents({
30         // raw events
31         /**
32          * @event btnclick
33          * The raw btnclick event for the button
34          * @param {Roo.EventObject} e
35          */
36         "btnclick" : true,
37         /**
38          * @event resize
39          * Fire when dialog resize
40          * @param {Roo.bootstrap.Modal} this
41          * @param {Roo.EventObject} e
42          */
43         "resize" : true
44     });
45     this.buttons = this.buttons || [];
46
47     if (this.tmpl) {
48         this.tmpl = Roo.factory(this.tmpl);
49     }
50
51 };
52
53 Roo.extend(Roo.bootstrap.Modal, Roo.bootstrap.Component,  {
54
55     title : 'test dialog',
56
57     buttons : false,
58
59     // set on load...
60
61     html: false,
62
63     tmp: false,
64
65     specificTitle: false,
66
67     buttonPosition: 'right',
68
69     allow_close : true,
70
71     animate : true,
72
73     fitwindow: false,
74
75
76      // private
77     dialogEl: false,
78     bodyEl:  false,
79     footerEl:  false,
80     titleEl:  false,
81     closeEl:  false,
82
83     size: '',
84
85
86     onRender : function(ct, position)
87     {
88         Roo.bootstrap.Component.superclass.onRender.call(this, ct, position);
89
90         if(!this.el){
91             var cfg = Roo.apply({},  this.getAutoCreate());
92             cfg.id = Roo.id();
93             //if(!cfg.name){
94             //    cfg.name = typeof(this.name) == 'undefined' ? this.id : this.name;
95             //}
96             //if (!cfg.name.length) {
97             //    delete cfg.name;
98            // }
99             if (this.cls) {
100                 cfg.cls += ' ' + this.cls;
101             }
102             if (this.style) {
103                 cfg.style = this.style;
104             }
105             this.el = Roo.get(document.body).createChild(cfg, position);
106         }
107         //var type = this.el.dom.type;
108
109
110         if(this.tabIndex !== undefined){
111             this.el.dom.setAttribute('tabIndex', this.tabIndex);
112         }
113
114         this.dialogEl = this.el.select('.modal-dialog',true).first();
115         this.bodyEl = this.el.select('.modal-body',true).first();
116         this.closeEl = this.el.select('.modal-header .close', true).first();
117         this.headerEl = this.el.select('.modal-header',true).first();
118         this.titleEl = this.el.select('.modal-title',true).first();
119         this.footerEl = this.el.select('.modal-footer',true).first();
120
121         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
122         this.maskEl.enableDisplayMode("block");
123         this.maskEl.hide();
124         //this.el.addClass("x-dlg-modal");
125
126         if (this.buttons.length) {
127             Roo.each(this.buttons, function(bb) {
128                 var b = Roo.apply({}, bb);
129                 b.xns = b.xns || Roo.bootstrap;
130                 b.xtype = b.xtype || 'Button';
131                 if (typeof(b.listeners) == 'undefined') {
132                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
133                 }
134
135                 var btn = Roo.factory(b);
136                 Roo.log(btn);
137                 btn.render(this.el.select('.modal-footer div').first());
138
139             },this);
140         }
141         // render the children.
142         var nitems = [];
143
144         if(typeof(this.items) != 'undefined'){
145             var items = this.items;
146             delete this.items;
147
148             for(var i =0;i < items.length;i++) {
149                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
150             }
151         }
152
153         this.items = nitems;
154
155         // where are these used - they used to be body/close/footer
156
157
158         this.initEvents();
159         //this.el.addClass([this.fieldClass, this.cls]);
160
161     },
162
163     getAutoCreate : function(){
164
165
166         var bdy = {
167                 cls : 'modal-body',
168                 html : this.html || ''
169         };
170
171         var title = {
172             tag: 'h4',
173             cls : 'modal-title',
174             html : this.title
175         };
176
177         if(this.specificTitle){
178             title = this.title;
179
180         };
181
182         var header = [];
183         if (this.allow_close) {
184             header.push({
185                 tag: 'button',
186                 cls : 'close',
187                 html : '&times'
188             });
189         }
190
191         header.push(title);
192
193         var size = '';
194
195         if(this.size.length){
196             size = 'modal-' + this.size;
197         }
198
199         var modal = {
200             cls: "modal",
201             style : 'display: none',
202             cn : [
203                 {
204                     cls: "modal-dialog " + size,
205                     cn : [
206                         {
207                             cls : "modal-content",
208                             cn : [
209                                 {
210                                     cls : 'modal-header',
211                                     cn : header
212                                 },
213                                 bdy,
214                                 {
215                                     cls : 'modal-footer',
216                                     cn : [
217                                         {
218                                             tag: 'div',
219                                             cls: 'btn-' + this.buttonPosition
220                                         }
221                                     ]
222
223                                 }
224
225
226                             ]
227
228                         }
229                     ]
230
231                 }
232             ]
233         };
234
235         if(this.animate){
236             modal.cls += ' fade';
237         }
238
239         return modal;
240
241     },
242     getChildContainer : function() {
243
244          return this.bodyEl;
245
246     },
247     getButtonContainer : function() {
248          return this.el.select('.modal-footer div',true).first();
249
250     },
251     initEvents : function()
252     {
253         if (this.allow_close) {
254             this.closeEl.on('click', this.hide, this);
255         }
256         Roo.EventManager.onWindowResize(this.resize, this, true);
257
258
259     },
260
261     resize : function()
262     {
263         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),  Roo.lib.Dom.getViewHeight(true));
264         if (this.fitwindow) {
265             var w = this.width || Roo.lib.Dom.getViewportWidth(true) - 30;
266             var h = this.height || Roo.lib.Dom.getViewportHeight(true) - 60;
267             this.setSize(w,h);
268         }
269     },
270
271     setSize : function(w,h)
272     {
273         if (!w && !h) {
274             return;
275         }
276         this.resizeTo(w,h);
277     },
278
279     show : function() {
280
281         if (!this.rendered) {
282             this.render();
283         }
284
285         this.el.setStyle('display', 'block');
286
287         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
288             var _this = this;
289             (function(){
290                 this.el.addClass('in');
291             }).defer(50, this);
292         }else{
293             this.el.addClass('in');
294
295         }
296
297         // not sure how we can show data in here..
298         //if (this.tmpl) {
299         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
300         //}
301
302         Roo.get(document.body).addClass("x-body-masked");
303         
304         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
305         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
306         this.maskEl.show();
307         
308         this.resize();
309         
310         this.fireEvent('show', this);
311
312         // set zindex here - otherwise it appears to be ignored...
313         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
314
315         (function () {
316             this.items.forEach( function(e) {
317                 e.layout ? e.layout() : false;
318
319             });
320         }).defer(100,this);
321
322     },
323     hide : function()
324     {
325         if(this.fireEvent("beforehide", this) !== false){
326             this.maskEl.hide();
327             Roo.get(document.body).removeClass("x-body-masked");
328             this.el.removeClass('in');
329             this.el.select('.modal-dialog', true).first().setStyle('transform','');
330
331             if(this.animate){ // why
332                 var _this = this;
333                 (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
334             }else{
335                 this.el.setStyle('display', 'none');
336             }
337             this.fireEvent('hide', this);
338         }
339     },
340
341     addButton : function(str, cb)
342     {
343
344
345         var b = Roo.apply({}, { html : str } );
346         b.xns = b.xns || Roo.bootstrap;
347         b.xtype = b.xtype || 'Button';
348         if (typeof(b.listeners) == 'undefined') {
349             b.listeners = { click : cb.createDelegate(this)  };
350         }
351
352         var btn = Roo.factory(b);
353
354         btn.render(this.el.select('.modal-footer div').first());
355
356         return btn;
357
358     },
359
360     setDefaultButton : function(btn)
361     {
362         //this.el.select('.modal-footer').()
363     },
364     diff : false,
365
366     resizeTo: function(w,h)
367     {
368         // skip.. ?? why??
369
370         this.dialogEl.setWidth(w);
371         if (this.diff === false) {
372             this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
373         }
374
375         this.bodyEl.setHeight(h-this.diff);
376
377         this.fireEvent('resize', this);
378
379     },
380     setContentSize  : function(w, h)
381     {
382
383     },
384     onButtonClick: function(btn,e)
385     {
386         //Roo.log([a,b,c]);
387         this.fireEvent('btnclick', btn.name, e);
388     },
389      /**
390      * Set the title of the Dialog
391      * @param {String} str new Title
392      */
393     setTitle: function(str) {
394         this.titleEl.dom.innerHTML = str;
395     },
396     /**
397      * Set the body of the Dialog
398      * @param {String} str new Title
399      */
400     setBody: function(str) {
401         this.bodyEl.dom.innerHTML = str;
402     },
403     /**
404      * Set the body of the Dialog using the template
405      * @param {Obj} data - apply this data to the template and replace the body contents.
406      */
407     applyBody: function(obj)
408     {
409         if (!this.tmpl) {
410             Roo.log("Error - using apply Body without a template");
411             //code
412         }
413         this.tmpl.overwrite(this.bodyEl, obj);
414     }
415
416 });
417
418
419 Roo.apply(Roo.bootstrap.Modal,  {
420     /**
421          * Button config that displays a single OK button
422          * @type Object
423          */
424         OK :  [{
425             name : 'ok',
426             weight : 'primary',
427             html : 'OK'
428         }],
429         /**
430          * Button config that displays Yes and No buttons
431          * @type Object
432          */
433         YESNO : [
434             {
435                 name  : 'no',
436                 html : 'No'
437             },
438             {
439                 name  :'yes',
440                 weight : 'primary',
441                 html : 'Yes'
442             }
443         ],
444
445         /**
446          * Button config that displays OK and Cancel buttons
447          * @type Object
448          */
449         OKCANCEL : [
450             {
451                name : 'cancel',
452                 html : 'Cancel'
453             },
454             {
455                 name : 'ok',
456                 weight : 'primary',
457                 html : 'OK'
458             }
459         ],
460         /**
461          * Button config that displays Yes, No and Cancel buttons
462          * @type Object
463          */
464         YESNOCANCEL : [
465             {
466                 name : 'yes',
467                 weight : 'primary',
468                 html : 'Yes'
469             },
470             {
471                 name : 'no',
472                 html : 'No'
473             },
474             {
475                 name : 'cancel',
476                 html : 'Cancel'
477             }
478         ],
479         
480         zIndex : 10001
481 });