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.log(this.buttons);
128             Roo.each(this.buttons, function(bb) {
129                 var b = Roo.apply({}, bb);
130                 b.xns = b.xns || Roo.bootstrap;
131                 b.xtype = b.xtype || 'Button';
132                 if (typeof(b.listeners) == 'undefined') {
133                     b.listeners = { click : this.onButtonClick.createDelegate(this)  };
134                 }
135
136                 var btn = Roo.factory(b);
137
138                 btn.render(this.el.select('.modal-footer div').first());
139
140             },this);
141         }
142         // render the children.
143         var nitems = [];
144
145         if(typeof(this.items) != 'undefined'){
146             var items = this.items;
147             delete this.items;
148
149             for(var i =0;i < items.length;i++) {
150                 nitems.push(this.addxtype(Roo.apply({}, items[i])));
151             }
152         }
153
154         this.items = nitems;
155
156         // where are these used - they used to be body/close/footer
157
158
159         this.initEvents();
160         //this.el.addClass([this.fieldClass, this.cls]);
161
162     },
163
164     getAutoCreate : function(){
165
166
167         var bdy = {
168                 cls : 'modal-body',
169                 html : this.html || ''
170         };
171
172         var title = {
173             tag: 'h4',
174             cls : 'modal-title',
175             html : this.title
176         };
177
178         if(this.specificTitle){
179             title = this.title;
180
181         };
182
183         var header = [];
184         if (this.allow_close) {
185             header.push({
186                 tag: 'button',
187                 cls : 'close',
188                 html : '&times'
189             });
190         }
191
192         header.push(title);
193
194         var size = '';
195
196         if(this.size.length){
197             size = 'modal-' + this.size;
198         }
199
200         var modal = {
201             cls: "modal",
202             style : 'display: none',
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(Roo.lib.Dom.getViewWidth(true),  Roo.lib.Dom.getViewHeight(true));
265         if (this.fitwindow) {
266             var w = this.width || Roo.lib.Dom.getViewportWidth(true) - 30;
267             var h = this.height || Roo.lib.Dom.getViewportHeight(true) - 60;
268             this.setSize(w,h);
269         }
270     },
271
272     setSize : function(w,h)
273     {
274         if (!w && !h) {
275             return;
276         }
277         this.resizeTo(w,h);
278     },
279
280     show : function() {
281
282         if (!this.rendered) {
283             this.render();
284         }
285
286         this.el.setStyle('display', 'block');
287
288         if(this.animate){  // element has 'fade'  - so stuff happens after .3s ?- not sure why the delay?
289             var _this = this;
290             (function(){
291                 this.el.addClass('in');
292             }).defer(50, this);
293         }else{
294             this.el.addClass('in');
295
296         }
297
298         // not sure how we can show data in here..
299         //if (this.tmpl) {
300         //    this.getChildContainer().dom.innerHTML = this.tmpl.applyTemplate(this);
301         //}
302
303         Roo.get(document.body).addClass("x-body-masked");
304         
305         this.maskEl.setSize(Roo.lib.Dom.getViewWidth(true),   Roo.lib.Dom.getViewHeight(true));
306         this.maskEl.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
307         this.maskEl.show();
308         
309         this.resize();
310         
311         this.fireEvent('show', this);
312
313         // set zindex here - otherwise it appears to be ignored...
314         this.el.setStyle('z-index', Roo.bootstrap.Modal.zIndex++);
315
316         (function () {
317             this.items.forEach( function(e) {
318                 e.layout ? e.layout() : false;
319
320             });
321         }).defer(100,this);
322
323     },
324     hide : function()
325     {
326         if(this.fireEvent("beforehide", this) !== false){
327             this.maskEl.hide();
328             Roo.get(document.body).removeClass("x-body-masked");
329             this.el.removeClass('in');
330             this.el.select('.modal-dialog', true).first().setStyle('transform','');
331
332             if(this.animate){ // why
333                 var _this = this;
334                 (function(){ _this.el.setStyle('display', 'none'); }).defer(150);
335             }else{
336                 this.el.setStyle('display', 'none');
337             }
338             this.fireEvent('hide', this);
339         }
340     },
341
342     addButton : function(str, cb)
343     {
344
345
346         var b = Roo.apply({}, { html : str } );
347         b.xns = b.xns || Roo.bootstrap;
348         b.xtype = b.xtype || 'Button';
349         if (typeof(b.listeners) == 'undefined') {
350             b.listeners = { click : cb.createDelegate(this)  };
351         }
352
353         var btn = Roo.factory(b);
354
355         btn.render(this.el.select('.modal-footer div').first());
356
357         return btn;
358
359     },
360
361     setDefaultButton : function(btn)
362     {
363         //this.el.select('.modal-footer').()
364     },
365     diff : false,
366
367     resizeTo: function(w,h)
368     {
369         // skip.. ?? why??
370
371         this.dialogEl.setWidth(w);
372         if (this.diff === false) {
373             this.diff = this.dialogEl.getHeight() - this.bodyEl.getHeight();
374         }
375
376         this.bodyEl.setHeight(h-this.diff);
377
378         this.fireEvent('resize', this);
379
380     },
381     setContentSize  : function(w, h)
382     {
383
384     },
385     onButtonClick: function(btn,e)
386     {
387         //Roo.log([a,b,c]);
388         this.fireEvent('btnclick', btn.name, e);
389     },
390      /**
391      * Set the title of the Dialog
392      * @param {String} str new Title
393      */
394     setTitle: function(str) {
395         this.titleEl.dom.innerHTML = str;
396     },
397     /**
398      * Set the body of the Dialog
399      * @param {String} str new Title
400      */
401     setBody: function(str) {
402         this.bodyEl.dom.innerHTML = str;
403     },
404     /**
405      * Set the body of the Dialog using the template
406      * @param {Obj} data - apply this data to the template and replace the body contents.
407      */
408     applyBody: function(obj)
409     {
410         if (!this.tmpl) {
411             Roo.log("Error - using apply Body without a template");
412             //code
413         }
414         this.tmpl.overwrite(this.bodyEl, obj);
415     }
416
417 });
418
419
420 Roo.apply(Roo.bootstrap.Modal,  {
421     /**
422          * Button config that displays a single OK button
423          * @type Object
424          */
425         OK :  [{
426             name : 'ok',
427             weight : 'primary',
428             html : 'OK'
429         }],
430         /**
431          * Button config that displays Yes and No buttons
432          * @type Object
433          */
434         YESNO : [
435             {
436                 name  : 'no',
437                 html : 'No'
438             },
439             {
440                 name  :'yes',
441                 weight : 'primary',
442                 html : 'Yes'
443             }
444         ],
445
446         /**
447          * Button config that displays OK and Cancel buttons
448          * @type Object
449          */
450         OKCANCEL : [
451             {
452                name : 'cancel',
453                 html : 'Cancel'
454             },
455             {
456                 name : 'ok',
457                 weight : 'primary',
458                 html : 'OK'
459             }
460         ],
461         /**
462          * Button config that displays Yes, No and Cancel buttons
463          * @type Object
464          */
465         YESNOCANCEL : [
466             {
467                 name : 'yes',
468                 weight : 'primary',
469                 html : 'Yes'
470             },
471             {
472                 name : 'no',
473                 html : 'No'
474             },
475             {
476                 name : 'cancel',
477                 html : 'Cancel'
478             }
479         ],
480         
481         zIndex : 10001
482 });