summaryrefslogtreecommitdiff
path: root/frontend/beta/js/YUI-extensions/widgets/MessageBox.js
blob: 01b168dc5bf48991913c01a96974364a255e3fcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
YAHOO.ext.MessageBox = function(){
    var dlg, opt, mask;
    var bodyEl, msgEl, textboxEl, textareaEl, progressEl, pp;
    var buttons, activeTextEl, bwidth;
    
    var handleButton = function(button){
        if(typeof opt.fn == 'function'){
            if(opt.fn.call(opt.scope||window, button, activeTextEl.dom.value) !== false){
                dlg.hide();
            }
        }else{
            dlg.hide();
        }
    };
    var updateButtons = function(b){
        var width = 0;
        if(!b){
            buttons['ok'].hide();
            buttons['cancel'].hide();
            buttons['yes'].hide();
            buttons['no'].hide();
            return width;
        }
        for(var k in buttons){
            if(typeof buttons[k] != 'function'){
                if(b[k]){
                    buttons[k].show();
                    buttons[k].setText(typeof b[k] == 'string' ? b[k] : YAHOO.ext.MessageBox.buttonText[k]);
                    width += buttons[k].el.getWidth()+15;
                }else{
                    buttons[k].hide();
                }
            }
        }
        return width;
    };
    
    return {
        getDialog : function(){
           if(!dlg){
                dlg = new YAHOO.ext.BasicDialog('mb-dlg', {
                    autoCreate : true,
                    shadow: true,
                    draggable: true,
                    resizable:false,
                    constraintoviewport:true,
                    fixedcenter:true,
                    shim:true,
                    modal: true,
                    width:400, height:100,
                    buttonAlign:'center',
                    closeClick : function(){
                        if(opt && opt.buttons && opt.buttons.no && !opt.buttons.cancel){
                            handleButton('no');
                        }else{
                            handleButton('cancel');
                        }
                    }
                });
                dlg.closeClick = function(){
                    alert('wtf');
                };
                mask = dlg.mask;
                dlg.addKeyListener(27, dlg.hide, dlg);
                buttons = {};
                buttons['ok'] = dlg.addButton(this.buttonText['ok'], handleButton.createCallback('ok'));
                buttons['yes'] = dlg.addButton(this.buttonText['yes'], handleButton.createCallback('yes'));
                buttons['no'] = dlg.addButton(this.buttonText['no'], handleButton.createCallback('no'));
                buttons['cancel'] = dlg.addButton(this.buttonText['cancel'], handleButton.createCallback('cancel'));
                bodyEl = dlg.body.createChild({
                    tag:'div', 
                    html:'<span class="ext-mb-text"></span><br /><input type="text" class="ext-mb-input"><textarea class="ext-mb-textarea"></textarea><div class="ext-mb-progress-wrap"><div class="ext-mb-progress"><div class="ext-mb-progress-bar">&#160;</div></div></div>'
                });
                msgEl = bodyEl.dom.firstChild;
                textboxEl = getEl(bodyEl.dom.childNodes[2]);
                textboxEl.enableDisplayMode();
                textboxEl.addKeyListener([10,13], function(){
                    if(dlg.isVisible() && opt && opt.buttons){
                        if(opt.buttons.ok){
                            handleButton('ok');
                        }else if(opt.buttons.yes){
                            handleButton('yes');
                        }
                    }
                });
                textareaEl = getEl(bodyEl.dom.childNodes[3]);
                textareaEl.enableDisplayMode();
                progressEl = getEl(bodyEl.dom.childNodes[4]);
                progressEl.enableDisplayMode();
                pp = getEl(progressEl.dom.firstChild.firstChild);
            }
            return dlg;
        },
        
        updateText : function(text){
            if(!dlg.isVisible() && !opt.width){
                dlg.resizeTo(this.maxWidth, 100); // resize first so content is never clipped from previous shows
            }
            msgEl.innerHTML = text;
            var w = Math.max(Math.min(opt.width || msgEl.offsetWidth, this.maxWidth), 
                        Math.max(opt.minWidth || this.minWidth, bwidth));
            if(opt.prompt){
                activeTextEl.setWidth(w);
            }
            dlg.setContentSize(w, bodyEl.getHeight());
        },        
        
        updateProgress : function(value, text){
            if(text){
                this.updateText(text);
            }
            pp.setWidth(value*progressEl.dom.firstChild.offsetWidth);
        },        
        
        isVisible : function(){
            return dlg && dlg.isVisible();  
        },
        
        hide : function(){
            if(this.isVisible()){
                dlg.hide();
            }  
        },
        
        show : function(options){
            var d = this.getDialog();
            opt = options;
            d.setTitle(opt.title || '&#160;');
            d.close.setDisplayed(opt.closable !== false);
            activeTextEl = textboxEl;
            opt.prompt = opt.prompt || (opt.multiline ? true : false)
            if(opt.prompt){
                if(opt.multiline){
                    textboxEl.hide();
                    textareaEl.show();
                    textareaEl.setHeight(typeof opt.multiline == 'number' ? 
                        opt.multiline : this.defaultTextHeight);
                    activeTextEl = textareaEl;
                }else{
                    textboxEl.show();
                    textareaEl.hide();
                }
            }else{
                textboxEl.hide();
                textareaEl.hide();
            }
            progressEl.setDisplayed(opt.progress === true);
            this.updateProgress(0);
            activeTextEl.dom.value = opt.value || '';
            if(opt.prompt){
                dlg.setDefaultButton(activeTextEl);
            }else{
                var bs = opt.buttons;
                var db = null;
                if(bs && bs.ok){
                    db = buttons['ok'];
                }else if(bs && bs.yes){
                    db = buttons['yes'];
                }
                dlg.setDefaultButton(db);
            }
            bwidth = updateButtons(opt.buttons);
            this.updateText(opt.msg);
            d.modal = opt.modal !== false;
            d.mask = opt.modal !== false ? mask : false;
            d.animateTarget = null;
            d.show(options.animEl);
        },
        
        progress : function(title, msg){
            this.show({
                title : title,
                msg : msg,
                buttons: false,
                progress:true,
                closable:false
            });
        },
        
        alert : function(title, msg, fn, scope){
            this.show({
                title : title,
                msg : msg,
                buttons: this.OK,
                fn: fn,
                scope : scope
            });
        },
        
        confirm : function(title, msg, fn, scope){
            this.show({
                title : title,
                msg : msg,
                buttons: this.YESNO,
                fn: fn,
                scope : scope
            });
        },
        
        prompt : function(title, msg, fn, scope, multiline){
            this.show({
                title : title,
                msg : msg,
                buttons: this.OKCANCEL,
                fn: fn,
                minWidth:250,
                scope : scope,
                prompt:true,
                multiline: multiline
            });
        },
        
        OK : {ok:true},
        YESNO : {yes:true, no:true},
        OKCANCEL : {ok:true, cancel:true},
        YESNOCANCEL : {yes:true, no:true, cancel:true},
        
        defaultTextHeight:75,
        maxWidth : 500,
        minWidth : 100,
        buttonText : {
            ok : 'OK',
            cancel : 'Cancel',
            yes : 'Yes',
            no : 'No'
        }
    };
}();

YAHOO.ext.Msg = YAHOO.ext.MessageBox;