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