Commit f001dc153f4effc13813254bbfa9c01d0c173ae2
1 parent
a635102a
Exists in
master
and in
1 other branch
- handle focus/unfocus events
- handle resize event - report initial screen and window size
Showing
3 changed files
with
49 additions
and
16 deletions
Show diff stats
perguntations/app.py
| ... | ... | @@ -257,6 +257,17 @@ class App(): |
| 257 | 257 | return test |
| 258 | 258 | |
| 259 | 259 | # ------------------------------------------------------------------------ |
| 260 | + def event_test(self, uid, cmd, value): | |
| 261 | + '''handle browser events the occur during the test''' | |
| 262 | + if cmd == 'focus': | |
| 263 | + logger.info('Student %s: focus %s', uid, value) | |
| 264 | + elif cmd == 'size': | |
| 265 | + scr_y, scr_x, win_y, win_x = value | |
| 266 | + area = win_x * win_y / (scr_x * scr_y) * 100 | |
| 267 | + logger.info('Student %s: area=%g%%, window=%dx%d, screen=%dx%d', | |
| 268 | + uid, area, win_x, win_y, scr_x, scr_y) | |
| 269 | + | |
| 270 | + # ------------------------------------------------------------------------ | |
| 260 | 271 | |
| 261 | 272 | # --- helpers (getters) |
| 262 | 273 | # def get_student_name(self, uid): | ... | ... |
perguntations/serve.py
| ... | ... | @@ -154,11 +154,18 @@ class BaseHandler(tornado.web.RequestHandler): |
| 154 | 154 | # AdminSocketHandler.send_updates(chat) # send to clients |
| 155 | 155 | |
| 156 | 156 | class StudentWebservice(BaseHandler): |
| 157 | + ''' | |
| 158 | + Receive ajax from students in the test: | |
| 159 | + focus, unfocus | |
| 160 | + ''' | |
| 161 | + | |
| 157 | 162 | @tornado.web.authenticated |
| 158 | - async def post(self): | |
| 163 | + def post(self): | |
| 164 | + '''handle ajax post''' | |
| 159 | 165 | uid = self.current_user |
| 160 | 166 | cmd = self.get_body_argument('cmd', None) |
| 161 | - logging.info('%s %s', uid, cmd) | |
| 167 | + value = json.loads(self.get_body_argument('value', None)) | |
| 168 | + self.testapp.event_test(uid, cmd, value) | |
| 162 | 169 | |
| 163 | 170 | # ---------------------------------------------------------------------------- |
| 164 | 171 | class AdminHandler(BaseHandler): | ... | ... |
perguntations/static/js/detect_unfocus.js
| ... | ... | @@ -12,28 +12,43 @@ jQuery.postJSON = function(url, args) { |
| 12 | 12 | |
| 13 | 13 | |
| 14 | 14 | $(document).ready(function() { |
| 15 | + $.postJSON("/studentwebservice", { | |
| 16 | + "cmd": "size", | |
| 17 | + "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), | |
| 18 | + }); | |
| 19 | + | |
| 15 | 20 | $(window).focus(function(){ |
| 16 | 21 | $.postJSON("/studentwebservice", { |
| 17 | 22 | "cmd": "focus", |
| 23 | + "value": true, | |
| 18 | 24 | }); |
| 19 | 25 | }); |
| 20 | 26 | |
| 21 | 27 | $(window).blur(function(){ |
| 22 | 28 | $.postJSON("/studentwebservice", { |
| 23 | - "cmd": "unfocus", | |
| 29 | + "cmd": "focus", | |
| 30 | + "value": false, | |
| 24 | 31 | }); |
| 25 | 32 | }); |
| 26 | 33 | |
| 27 | - // $(window).resize(function(){ | |
| 28 | - // var n = $(window).scrollTop(); | |
| 29 | - // $.ajax({ | |
| 30 | - // type: "POST", | |
| 31 | - // url: "/adminwebservice", | |
| 32 | - // data: { | |
| 33 | - // "cmd": "resize", | |
| 34 | - // "name": $("#number").text(), | |
| 35 | - // "scroll": n | |
| 36 | - // } | |
| 37 | - // }); | |
| 38 | - // }); | |
| 39 | -}); | |
| 40 | 34 | \ No newline at end of file |
| 35 | + $(window).resize(function(){ | |
| 36 | + $.postJSON("/studentwebservice", { | |
| 37 | + "cmd": "size", | |
| 38 | + "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), | |
| 39 | + }); | |
| 40 | + | |
| 41 | + | |
| 42 | + // // var n = $(window).scrollTop(); | |
| 43 | + // $.postJSON({"/studentwebservice", { | |
| 44 | + // "cmd": "size", | |
| 45 | + // "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), | |
| 46 | + // }}); | |
| 47 | + // // data: { | |
| 48 | + // // "cmd": "resize", | |
| 49 | + // // "name": $("#number").text(), | |
| 50 | + // // "scroll": n | |
| 51 | + // // } | |
| 52 | + // // }); | |
| 53 | + }); | |
| 54 | + | |
| 55 | +}); | ... | ... |