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.headerEl = this.el.select('.modal-header',true).first();
111         this.titleEl = this.el.select('.modal-title',true).first();
112         this.footerEl = this.el.select('.modal-footer',true).first();
113
114         this.maskEl = Roo.DomHelper.append(document.body, {tag: "div", cls:"x-dlg-mask"}, true);
115         this.maskEl.enableDisplayMode("block");
116         this.maskEl.hide();
117         //this.el.addClass("x-dlg-modal");
118
119         if (this.buttons.length) {
120             Roo.each(this.buttons, function(bb) {
121                 var b = Roo.apply({}, bb);
122                 b.xns = b.xns || Roo.bootstrap;
123                 b.xtype = b.xtype || 'Button';
124                 if (typeof(b.listeners) == 'undefined') {
125                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
126                 }
127
128                 var btn = Roo.factory(b);
129
130                 btn.render(this.el.select('.modal-footer div').first());
131
132             },this);
133         }
134         // render the children.
135         var nitems = [];
136
137         if(typeof(this.items) != 'undefined'){
138             var items = this.items;
139             delete this.items;
140
141             for(var i =0;i < items.length;i++) {
142                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
143             }
144         }
145
146         this.items = nitems;
147
148         // where are these used - they used to be body/close/footer
149
150
151         this.initEvents();
152         //this.el.addClass([this.fieldClass, this.cls]);
153
154     },
155
156     getAutoCreate : function(){
157
158
159         var bdy = {
160                 cls : 'modal-body',
161                 html : this.html || ''
162         };
163
164         var title = {
165             tag: 'h4',
166             cls : 'modal-title',
167             html : this.title
168         };
169
170         if(this.specificTitle){
171             title = this.title;
172
173         };
174
175         var header = [];
176         if (this.allow_close) {
177             header.push({
178                 tag: 'button',
179                 cls : 'close',
180                 html : '&times'
181             });
182         }
183
184         header.push(title);
185
186         var size = '';
187
188         if(this.size.length){
189             size = 'modal-' + this.size;
190         }
191
192         var modal = {
193             cls: "modal",
194             style : 'display: none',
195             cn : [
196                 {
197                     cls: "modal-dialog " + size,
198                     cn : [
199                         {
200                             cls : "modal-content",
201                             cn : [
202                                 {
203                                     cls : 'modal-header',
204                                     cn : header
205                                 },
206                                 bdy,
207                                 {
208                                     cls : 'modal-footer',
209                                     cn : [
210                                         {
211                                             tag: 'div',
212                                             cls: 'btn-' + this.buttonPosition
213                                         }
214                                     ]
215
216                                 }
217
218
219                             ]
220
221                         }
222                     ]
223
224                 }
225             ]
226         };
227
228         if(this.animate){
229             modal.cls += ' fade';
230         }
231
232         return modal;
233
234     },
235     getChildContainer : function() {
236
237          return this.bodyEl;
238
239     },
240     getButtonContainer : function() {
241          return this.el.select('.modal-footer div',true).first();
242
243     },
244     initEvents : function()
245     {
246         if (this.allow_close) {
247             this.closeEl.on('click', this.hide, this);
248         }
249         Roo.EventManager.onWindowResize(this.resize, this, true);
250
251
252     },
253
254     resize : function()
255     {
256         Roo.log('resizing');
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('z-index', Roo.bootstrap.Modal.zIndex++);
300         this.maskEl.show();
301         
302         this.resize();
303         
304         this.fireEvent('show', this);
305
306         // set zindex here - otherwise it appears to be ignored...
307         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
308
309         (function () {
310             this.items.forEach( function(e) {
311                 e.layout ? e.layout() : false;
312
313             });
314         }).defer(100,this);
315
316     },
317     hide : function()
318     {
319         if(this.fireEvent("beforehide", this) !== false){
320             this.maskEl.hide();
321             Roo.get(document.body).removeClass("x-body-masked");
322             this.el.removeClass('in');
323             this.el.select('.modal-dialog', true).first().setStyle('transform','');
324
325             if(this.animate){ // why
326                 var _this = this;
327                 (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
328             }else{
329                 this.el.setStyle('display', 'none');
330             }
331             this.fireEvent('hide', this);
332         }
333     },
334
335     addButton : function(str, cb)
336     {
337
338
339         var b = Roo.apply({}, { html : str } );
340         b.xns = b.xns || Roo.bootstrap;
341         b.xtype = b.xtype || 'Button';
342         if (typeof(b.listeners) == 'undefined') {
343             b.listeners = { click : cb.createDelegate(this)  };
344         }
345
346         var btn = Roo.factory(b);
347
348         btn.render(this.el.select('.modal-footer div').first());
349
350         return btn;
351
352     },
353
354     setDefaultButton : function(btn)
355     {
356         //this.el.select('.modal-footer').()
357     },
358     diff : false,
359
360     resizeTo: function(w,h)
361     {
362         // skip.. ?? why??
363
364         this.dialogEl.setWidth(w);
365         if (this.diff === false) {
366             this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
367         }
368
369         this.bodyEl.setHeight(h-this.diff);
370
371
372     },
373     setContentSize  : function(w, h)
374     {
375
376     },
377     onButtonClick: function(btn,e)
378     {
379         //Roo.log([a,b,c]);
380         this.fireEvent('btnclick', btn.name, e);
381     },
382      /**
383      * Set the title of the Dialog
384      * @param {String} str new Title
385      */
386     setTitle: function(str) {
387         this.titleEl.dom.innerHTML = str;
388     },
389     /**
390      * Set the body of the Dialog
391      * @param {String} str new Title
392      */
393     setBody: function(str) {
394         this.bodyEl.dom.innerHTML = str;
395     },
396     /**
397      * Set the body of the Dialog using the template
398      * @param {Obj} data - apply this data to the template and replace the body contents.
399      */
400     applyBody: function(obj)
401     {
402         if (!this.tmpl) {
403             Roo.log("Error - using apply Body without a template");
404             //code
405         }
406         this.tmpl.overwrite(this.bodyEl, obj);
407     }
408
409 });
410
411
412 Roo.apply(Roo.bootstrap.Modal,  {
413     /**
414          * Button config that displays a single OK button
415          * @type Object
416          */
417         OK :  [{
418             name : 'ok',
419             weight : 'primary',
420             html : 'OK'
421         }],
422         /**
423          * Button config that displays Yes and No buttons
424          * @type Object
425          */
426         YESNO : [
427             {
428                 name  : 'no',
429                 html : 'No'
430             },
431             {
432                 name  :'yes',
433                 weight : 'primary',
434                 html : 'Yes'
435             }
436         ],
437
438         /**
439          * Button config that displays OK and Cancel buttons
440          * @type Object
441          */
442         OKCANCEL : [
443             {
444                name : 'cancel',
445                 html : 'Cancel'
446             },
447             {
448                 name : 'ok',
449                 weight : 'primary',
450                 html : 'OK'
451             }
452         ],
453         /**
454          * Button config that displays Yes, No and Cancel buttons
455          * @type Object
456          */
457         YESNOCANCEL : [
458             {
459                 name : 'yes',
460                 weight : 'primary',
461                 html : 'Yes'
462             },
463             {
464                 name : 'no',
465                 html : 'No'
466             },
467             {
468                 name : 'cancel',
469                 html : 'Cancel'
470             }
471         ],
472         
473         zIndex : 10001
474 });