-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuow.js
More file actions
189 lines (175 loc) · 5.44 KB
/
uow.js
File metadata and controls
189 lines (175 loc) · 5.44 KB
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
/*
* Factory functions for all UOW services.
*
* Copyright UNC Open Web Team 2010. All Rights Reserved.
**/
dojo.provide('uow');
dojo.registerModulePath('uow', '/libs/uow');
dojo.require('uow.data.MongoStore');
dojo.require('dojo.cookie');
// Gets the singleton JSonic audio manager
uow._audio = null;
uow.getAudio = function(args) {
dojo.require('uow.audio.JSonic');
var def = new dojo.Deferred();
dojo.ready(function() {
if(!uow._audio) {
args = args || {};
args.jsonicURI = '/jsonic/';
uow._audio = new uow.audio.JSonic(args);
}
def.callback(uow._audio);
});
return def;
};
// Gets a MongoStore instance (like dojox.data.JSONRestStore)
uow.getDatabase = function(args) { return uow.data.getDatabase(args); };
// Ask the server to return the current user
uow.getUser = function(args) { return uow.data.getUser(args); };
// Triggers an OpenID login using whatever provider the server has configured
uow.triggerLogin = function() {
var loginDeferred = new dojo.Deferred();
uow._handleOpenIDResponse = function(flag) {
uow.getUser().addCallback(function(user) {
loginDeferred.callback({ flag: flag, user: user });
});
};
popup = window.open('/data/_auth', 'Login_Popup', 'width=790,height=580');
return loginDeferred;
};
// Logs the user out and refreshes the page to clear private info.
uow.logout = function() {
dojo.cookie('user', null, {path : '/', expires: -1});
window.location.reload();
};
// Check browser compatibility
uow.ui = {};
uow.ui.checkBrowser = function() {
dojo.require('uow.ui.BrowserDialog');
var def = new dojo.Deferred();
dojo.ready(function() {
var ok = !dojo.isIE;
if(!ok) {
uow.ui.BrowserDialog.show();
}
def.callback(ok);
});
return def;
};
// Show a busy overlay
uow.ui.showBusy = function(args) {
dojo.require('uow.ui.BusyOverlay');
var def = new dojo.Deferred();
dojo.ready(function() {
var inst = uow.ui.BusyOverlay.show(args);
def.callback(inst);
});
return def;
};
// Hide a busy overlay
uow.ui.hideBusy = function(args) {
dojo.require('uow.ui.BusyOverlay');
var def = new dojo.Deferred();
dojo.ready(function() {
uow.ui.BusyOverlay.hide(args.overlay);
def.callback();
});
return def;
};
// Listen for global keys
uow.ui.connectKeys = function(context) {
if(!context || !context.window) {
// support listening on different windows
context = uow.ui;
context.window = window;
}
if(context._keyToks) {
throw new Error('keys connected');
}
context._keyToks = [];
// keep track of key state to avoid down repeat
context._keyState = {};
var _isIgnoredKey = function(event) {
if(!context._keyIgnores) {return false;}
for(var i=0, l=context._keyIgnores.length; i<l; i++) {
var ignore = context._keyIgnores[i];
var match = true;
for(var key in ignore) {
if(ignore[key] !== event[key]) {
// does not match this ignore pattern, skip to next
match = false;
break;
}
}
if(match) {
// pattern matches, ignore
return true;
}
}
// no pattern matches, do not ignore
return false;
};
var tok;
tok = dojo.connect(context.window, 'onkeyup', function(event) {
// cleanup any tracked key state
delete context._keyState[event.keyCode];
if(!_isIgnoredKey(event)) {
// publish a key up event
dojo.publish('/uow/key/up', [event]);
}
});
context._keyToks.push(tok);
tok = dojo.connect(context.window, 'onkeydown', function(event) {
var ignore = _isIgnoredKey(event);
if (!context._keyState[event.keyCode]) {
// first down event for a key
context._keyState[event.keyCode] = 'd';
if(!ignore) {
// fire an initial event
dojo.publish('/uow/key/down/initial', [event]);
// set a flag on the event for down handlers to read too
event.initialDown = true;
}
}
if(!ignore) {
// publish a down event
dojo.publish('/uow/key/down', [event]);
}
});
context._keyToks.push(tok);
tok = dojo.connect(context.window, 'onkeypress', function(event) {
if(!_isIgnoredKey(event)) {
// publish a press event
dojo.publish('/uow/key/press', [event]);
}
});
context._keyToks.push(tok);
};
// Stop listening for global keys
uow.ui.disconnectKeys = function(context) {
if(!context) {
context = uow.ui;
}
if(!context._keyToks) {
throw new Error('keys not connected');
}
dojo.forEach(context._keyToks, dojo.disconnect, dojo);
context._keyToks = null;
};
// Add key events to ignore (i.e., not publish events for them).
uow.ui.ignoreKeys = function(ignores, context) {
if(!context) {
context = uow.ui;
}
if(!context._keyIgnores) {
context._keyIgnores = [];
}
context._keyIgnores = context._keyIgnores.concat(ignores);
};
// Remove all key events from the ignore list.
uow.ui.clearIgnoredKeys = function(context, keys) {
if(!context) {
context = uow.ui;
}
delete context._keyIgnores;
};