1 /**
  2  * @class Monk.component.Messenger
  3  * @description Handles messaging for the workbench.  Encapsulates Ext.MessageBox class.
  4  * @extends Workbench.lang.Object
  5  * @author Andrew
  6  */
  7 
  8 Monk.component.Messenger = function(args) {
  9 	Monk.component.Messenger.superclass.constructor.call(this, args);
 10 }
 11 
 12 Workbench.extend(Monk.component.Messenger, Workbench.lang.Object, {
 13 	
 14     label : "Messenger",
 15     "window" : this.window,
 16     
 17     show : function(config, target) {
 18         var window;
 19         
 20         function componentScope(title, msg) {
 21             window = this.Ext.Msg.show(config);
 22         }
 23         
 24         if (target != null) {
 25             componentScope.call(target, config);
 26         } else {
 27             window = Ext.Msg.show(config);
 28         }
 29         
 30         Workbench.component.manager.notify(new Monk.event.MessageSent({
 31             label: 'Message sent: show'
 32         }));
 33         
 34         return window;
 35     },
 36     
 37     progress : function(title, msg, progressTxt, target) {
 38         var window;
 39         
 40         function componentScope(title, msg) {
 41             window = this.Ext.Msg.progress(title, msg, progressTxt);
 42         }
 43         
 44         if (target != null) {
 45             componentScope.call(target, title, msg, progressTxt);
 46         } else {
 47             window = Ext.Msg.progress(title, msg, progressTxt);
 48         }
 49         
 50         Workbench.component.manager.notify(new Monk.event.MessageSent({
 51             label: 'Message sent: progress'
 52         }));
 53         
 54         return window;
 55     },
 56 	
 57 	alert : function(title, msg, target) {
 58 		if (this.useJs()) {
 59 			return alert(msg);
 60 		}
 61 //        var win;
 62         
 63 		target = target ? target : window;
 64 //		target.Ext.Msg.getDialog().keyMap.bindings.push(function(e){console.warn(e)})
 65 		return target.Ext.Msg.alert(title,msg)
 66 		/*
 67 		var keyMap = target.Ext.Msg.getDialog().getKeyMap();
 68 		console.warn(keyMap.okSet)
 69 		if (!keyMap.okSet) {
 70 			var ok = function() {Ext.Msg.hide()}
 71 			keyMap.addBinding({
 72 				key : 'oy', // Ok or Yes
 73 				handler : ok,
 74 				scope : target
 75 			});
 76 			keyMap.okSet = ok;
 77 		}
 78 		console.warn(keyMap.okSet)
 79 		console.warn(keyMap)
 80 		
 81         function componentScope(title, msg) {
 82             win = this.Ext.Msg.alert(title, msg);
 83         }
 84         
 85         if (target != null) {
 86             componentScope.call(target, title, msg);
 87         } else {
 88             win = Ext.Msg.alert(title, msg);    
 89         }
 90         
 91 		Workbench.component.manager.notify(new Monk.event.MessageSent({
 92             label: 'Message sent: alert'
 93         }));
 94         */
 95 		
 96         return win;
 97 	},
 98 
 99     wait : function(title, msg, target) {
100         var window;
101         
102         function componentScope(title, msg) {
103             window = this.Ext.Msg.wait(msg, title);
104         }
105         
106         if (target != null) {
107             componentScope.call(target, title, msg);
108         } else {
109             window = Ext.Msg.wait(msg, title);
110         }
111         
112         Workbench.component.manager.notify(new Monk.event.MessageSent({
113             label: 'Message sent: wait'
114         }));
115         
116         return window;
117     },
118 	
119 	confirm : function(title, msg, fn, scope, target) {
120 		if (this.useJs()) {
121 			return fn.call(scope, confirm(msg) == true ? 'yes' : 'no');
122 		}
123         var window;
124         
125         function componentScope(title, msg, fn, scope) {
126             window = this.Ext.Msg.confirm(title, msg, fn, scope);
127         }
128         
129         if (target != null) {
130             componentScope.call(target, title, msg, fn, scope);
131         } else {
132             window = Ext.Msg.confirm(title, msg, fn, scope);
133         }
134         
135         Workbench.component.manager.notify(new Monk.event.MessageSent({
136             label: 'Message sent: confirm'
137         }));
138         
139         return window;
140     },
141 	
142     prompt : function(title, msg, target) {
143 		 // UNTESTED
144 		if (this.useJs()) {
145 			var i = input(msg);
146 			return fn.call(scope, i ? 'ok' : 'cancel', i);
147 		}
148 		
149         var window;
150         
151         function componentScope(title, msg) {
152             window = this.Ext.Msg.prompt(title, msg);
153         }
154         
155         if (target != null) {
156             componentScope.call(target, title, msg);
157         } else {
158             window = Ext.Msg.prompt(title, msg);
159         }
160         
161         Workbench.component.manager.notify(new Monk.event.MessageSent({
162             label: 'Message sent: prompt'
163         }));
164         
165         return window;
166     },
167     
168     growl : function(title, msg, target) {
169         var msgCt;
170 
171         function doGrowl(title, msg, positionMethod) {
172             function createBox(t, s){
173                 return ['<div class="msg">',
174                         '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
175                         '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
176                         '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
177                         '</div>'].join('');
178             }
179             
180             if(!msgCt){
181                 msgCt = this.Ext.DomHelper.append(this.document.body, {id:'msg-div', style:'z-index:9000'}, true);
182             }
183             var width = this.Ext.lib.Dom.getViewWidth() - 20;
184             msgCt.setWidth(width);
185             if (positionMethod == 'set') {
186                 // need to manually set position when inside tool window
187                 var height = this.Ext.lib.Dom.getViewHeight() - 70;
188                 msgCt.setLeftTop('10px', height+'px');
189             } else {
190                 msgCt.alignTo(this.document, 'br-br', [-10, -70]);        /*<-- pos in bottom-right corner*/
191             }
192             var msg = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
193             var m = this.Ext.DomHelper.append(msgCt, {html:createBox(title, msg)}, true);
194             m.slideIn('b').pause(5).ghost("b", {remove:true});  /*<--move from bottom*/
195         }
196         
197         if (target != null) {
198             doGrowl.call(target, title, msg, 'set');
199         } else {
200             doGrowl.call(this.window, title, msg, 'align');
201         }
202     },
203 	
204 	useJs : function() {
205 		return Workbench.component.manager.hasVisibleApplet();
206 	}
207 	
208 });
209 
210 /**
211  * A static instance of the {@link Monk.component.Messenger} class.
212  */
213 Monk.component.messenger = new Monk.component.Messenger();
214 
215 Ext.onReady(function() {
216 	/*
217 	Ext.Msg.getDialog().getKeyMap().addBinding({
218 		key : 'yo',
219 		handler: function(k,e){
220 			console.warn(e.getTarget(),e.getTarget(null,null,true))
221 //			Ext.Msg.handleButton
222 		}
223 	});
224 	*/
225 })
226