-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasq-exercise-progress.html
669 lines (594 loc) · 20.6 KB
/
asq-exercise-progress.html
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../asq-base/asq-base.html">
<link rel="import" href="../paper-progress/paper-progress.html">
<link rel="import" href="asq-exercise-progress-style.html">
<!--
### ASQ Exercise Progress:
The ASQ exercise progress bar consists of a progress bar and a progress counter
(submissions/connected users).
The progress bar shows the cumulative viewers' activity states for the corresponding
exercise. Each user can be in one of the following activitystates: submitted, working,
focused or idle.
Those states are represented in 4 different colors as partial progress bars in the
order they were mentioned.
To display the gradient of change for a certain activity state (e.g submitted),
we use a slightly bigger progress bar of the same style in the background of the main
progress bar. The faster the animation from left to right, the more activity occurred for
a given state in the last 5 seconds.
The presenter can choose if they want instant updates for the progress bar or
if the progress bar should update simultaneously with the momentum effect
by using the `directUpdate` property.
### Styling progress bar (in presenter's view):
The followings are available for styling (presenter's view):
Custom property | Description | Default
---------------------------------------------------|---------------------------------------------|----------
--asq-exercise-progress-height | The height of the progress bar | 4px
--asq-exercise-progress-submitted-color | The color of the submission bar. | --google-green-500
--asq-exercise-progress-working-color | The color of the working users bar. | --paper-orange-500
--asq-exercise-progress-focused-color | The color of the focused users bar. | --paper-blue-200
--asq-exercise-progress-idle-color | The color of the idle users bar. | --google-grey-500
--asq-exercise-progressbar-background-color | The color of the empty progress bar. | --google-grey-300
--asq-exercise-progress-num-mixin | The mixin to style the progress numbers of the progress bar. | { }
Examples:
- To change the submitted progress bar color:
asq-exercise {
--asq-exercise-progress-submitted-color: #e91e63;
}
- To change the style of the progress numbers:
asq-exercise {
--asq-exercise-progress-progress-num-mixin: {
font-size: 20px;
padding: 2px;
};
}
@element asq-exercise-progress
@demo demo/asq-exercise-progress-demo.html
@group ASQ Elements
@blurb Element Container the progress bar.
@homepage http://github.com/ASQ-USI-Elements/asq-exercise/asq-exercise-progress.html
-->
<dom-module id="asq-exercise-progress">
<template>
<style include="iron-flex asq-exercise-progress-style">
:host {
display: flex;
position: relative;
@apply(--layout-horizontal);
@apply(--layout-center);
margin: 10px 0 5px 0;
font-size: 14px;
}
</style>
<div class="progress-bar-container background-progress">
<paper-progress id="background-submitted-progress" indeterminate style$=[[_calcFlex(_submissionsNum)]]></paper-progress>
<paper-progress id="background-working-progress" indeterminate style$=[[_calcFlex(_workingViewersNum)]]></paper-progress>
<paper-progress id="background-focused-progress" indeterminate style$=[[_calcFlex(_focusedViewersNum)]]></paper-progress>
<paper-progress id="background-idle-progress" indeterminate style$=[[_calcFlex(_idleViewersNum)]]></paper-progress>
</div>
<div class="progress-bar-container foreground-progress">
<paper-progress id="submitted-progress" value="100" style$=[[_calcFlex(_submissionsNum)]]></paper-progress>
<paper-progress id="working-progress" value="100" style$=[[_calcFlex(_workingViewersNum)]]></paper-progress>
<paper-progress id="focused-progress" value="100" style$=[[_calcFlex(_focusedViewersNum)]]></paper-progress>
<paper-progress id="idle-progress" value="100" style$=[[_calcFlex(_idleViewersNum)]]></paper-progress>
</div>
<div id="progress-num">
<span>[[_submissionsNum]]/[[_connectedViewersNum]]</span>
</div>
</template>
<script>
Polymer({
is: 'asq-exercise-progress',
behaviors: [ASQ.asqElementBehavior],
properties: {
/**
* Object that contains viewers with their activity information.
*
* @attribute _viewers
* @type Object
* @default {}
*/
_viewers: {
type: Object,
value: function() { return {}; },
},
/**
* Object that contains the submissions.
*
* @attribute _submissions
* @type Object
* @default {}
*/
_submissions: {
type: Object,
value: function() { return {}; },
},
/**
* Counts the number of currently connected viewers.
*
* @attribute _connectedViewersNum
* @type Number
* @default 0
*/
_connectedViewersNum: {
type: Number,
value: 0,
},
/**
* Counts the number of submissions, inherited from asq-exercise.
*
* @attribute _submissionsNum
* @type Number
* @default 0
*/
_submissionsNum: {
type: Number,
value: 0,
computed: '_computeSubmittedViewersNum(_submissions.*, _update)',
},
/**
* Counts the number of submissions during a given time interval
* by the _visualizeChange function.
*
* @attribute _submissionCounter
* @type Number
* @default 0
*/
_submissionCounter: {
type: Number,
value: 0,
},
/**
* Counts the number of working users, calculated with the function given
* in its computed property.
* @attribute _workingViewersNum
* @type Number
* @default 0
*/
_workingViewersNum:{
type: Number,
value: 0,
computed: '_computeWorkingViewersNum(_viewers.*, _update)',
},
/**
* Counts the number of working users during a given time interval
* by the _visualizeChange function.
*
* @attribute _workingCounter
* @type Number
* @default 0
*/
_workingCounter: {
type: Number,
value: 0,
},
/**
* Counts the number of focused users, calculated with the function given
* in its computed property.
* @attribute _focusedViewersNum
* @type Number
* @default 0
*/
_focusedViewersNum:{
type: Number,
value: 0,
computed: '_computeFocusedViewersNum(_viewers.*, _update)',
},
/**
* Counts the number of focused users during a given time interval
* by the _visualizeChange function.
*
* @attribute _focusedCounter
* @type Number
* @default 0
*/
_focusedCounter: {
type: Number,
value: 0,
},
/**
* Counts the number of idle users, calculated with the function given
* in its computed property.
*
* @attribute _focusedViewersNum
* @type Number
* @default 0
*/
_idleViewersNum:{
type: Number,
value: 0,
computed: '_computeIdleViewersNum(_viewers.*, _update)',
},
/**
* Counts the number of idle users during a given time interval
* by the _visualizeChange function.
*
* @attribute _focusedCounter
* @type Number
* @default 0
*/
_idleCounter: {
type: Number,
value: 0,
},
/**
* Flag to trigger the computed function at the right moment.
*
* @attribute _update
* @type Boolean
* @default false
*/
_update: {
type: Boolean,
value: true,
},
},
/**
* Computes the amount of users that have submitted from the _viewers object.
*
* @return {number} Number of working users.
*/
_computeSubmittedViewersNum: function(submissions, update) {
this._makeSureSubmissionsExist();
if (update) {
var num = Object.keys(this._submissions[this.exUid]).reduce(function(acc, userId) {
if(this._submissions[this.exUid][userId]) return ++acc;
return acc;
}.bind(this), 0);
return num;
}
else return this._submissionsNum;
},
/**
* Computes the amount of working users from the _viewers object.
*
* @return {number} Number of working users.
*/
_computeWorkingViewersNum: function(viewers, update) {
this._makeSureSubmissionsExist();
if (update) {
var num = Object.keys(this._viewers).reduce(function(acc, userId) {
if(this._viewers[userId].working && !this._submissions[this.exUid][userId]) return ++acc;
return acc;
}.bind(this), 0);
return num;
}
else return this._workingViewersNum;
},
/**
* Computes the amount of focused users from the _viewers object.
*
* @return {number} Number of focused users.
*/
_computeFocusedViewersNum: function(viewers, update) {
this._makeSureSubmissionsExist();
if (update) {
return Object.keys(this._viewers).reduce(function(acc, userId) {
if(this._viewers[userId].focused && !this._viewers[userId].idle && !this._submissions[this.exUid][userId]) return ++acc;
return acc;
}.bind(this), 0);
}
else return this._focusedViewersNum;
},
/**
* Computes the amount of idle users from the _viewers object.
*
* @return {number} Number of idle users.
*/
_computeIdleViewersNum: function(viewers, update) {
this._makeSureSubmissionsExist();
if (update) {
return Object.keys(this._viewers).reduce(function(acc, userId) {
if(this._viewers[userId].idle && !this._submissions[this.exUid][userId]) return ++acc
return acc;
}.bind(this), 0);
}
else return this._idleViewersNum;
},
/**
* Returns a CSS declaration as a string to change the width of the progress bar
* by changing its flex property.
*
* @param {number} The new flex value.
* @return {string} String of the form 'flex: num;'
*/
_calcFlex: function(num) {
return 'flex:' + num + ';';
},
/**
* Initializes the _submissions object if it's not already done.
*
*/
_makeSureSubmissionsExist: function() {
this._submissions[this.exUid] = this._submissions[this.exUid] || {};
},
/**
* If the given user is not already in the _viewers object, add him and notify
* the observers that the _viewers object changed.
*
* @param {object} A user object.
*/
_makeSureViewerExists: function(viewerToFind) {
if (viewerToFind.role === 'viewer') {
this._viewers[viewerToFind.id] = this._viewers[viewerToFind.id] || viewerToFind;
this.notifyPath('_viewers.' + viewerToFind.id);
}
return this._viewers[viewerToFind.id];
},
/**
* Mark the given user as working.
*
* @param {object} A user object.
*/
_userWorked: function(user) {
if (this._makeSureViewerExists(user)) {
if (!this._viewers[user.id].working) this._workingCounter++;
this._resetUser(user);
this._viewers[user.id].working = true;
this.notifyPath('_viewers.' + user.id + '.' + 'working');
}
},
/**
* Mark the given user as focused.
*
* @param {object} A user object.
*/
_userFocused: function(user) {
if (this._makeSureViewerExists(user)) {
if (!this._viewers[user.id].focused) this._focusedCounter++;
this._resetUser(user);
this._viewers[user.id].focused = true;
this.notifyPath('_viewers.' + user.id + '.' + 'focused');
}
},
/**
* Mark the given user as not idle, working or focused.
*
* @param {object} A user object.
*/
_userMadeTabVisible: function(user) {
if (this._makeSureViewerExists(user)) {
this._resetUser(user);
}
},
/**
* Mark the given user as idle.
*
* @param {object} A user object.
*/
_userIdled: function(user) {
if (this._makeSureViewerExists(user)) {
if (!this._viewers[user.id].idle) this._idleCounter++;
this._resetUser(user);
this._viewers[user.id].idle = true;
this.notifyPath('_viewers.' + user.id + '.' + 'idle');
}
},
/**
* Mark the given user as disconnected.
*
* @param {object} A user object.
*/
_userDisconnected: function(user) {
if (this._makeSureViewerExists(user)) {
this._resetUser(user);
this._viewers[user.id].disconnected = true;
}
},
/**
* Mark the given user as not idle, working or focused.
*
* @param {object} A user object.
*/
_userMadeTabHidden: function(user) {
if (this._makeSureViewerExists(user)) {
this._resetUser(user);
}
},
/**
* If the given blurred, make sure that he's already in the _viewers object.
*
* @param {object} A user object.
*/
_userBlurred: function(user) {
this._makeSureViewerExists(user)
},
/**
* Reset all the activity properties of the given user.
*
* @param {object} A user object.
*/
_resetUser: function(user) {
this._viewers[user.id].disconnected = false;
this._viewers[user.id].working = false;
this.notifyPath('_viewers.' + user.id + '.' + 'working');
this._viewers[user.id].focused = false;
this.notifyPath('_viewers.' + user.id + '.' + 'focused');
this._viewers[user.id].idle = false;
this.notifyPath('_viewers.' + user.id + '.' + 'idle');
},
/**
* Mark that the given user submitted and change the corresponding counter.
*
* @param {object} Data from the session event.
*/
_userSubmitted: function(data) {
if (data.answer.exercise.id == this.exUid && this._makeSureViewerExists(data.answeree)) {
this._resetUser(data.answeree);
this._submissions[this.exUid][data.answeree.id] = true;
var path = '_submissions.' + this.exUid + '.' + data.answeree.id;
this.notifyPath(path, true);
this._submissionCounter++;
}
},
/**
* Change the number of connected users.
*
* @param {object} An event object.
*/
_onConnectedClients: function (evt) {
this._connectedViewersNum = evt.connectedViewers.length;
},
/**
* Event handler for session event beamer events.
*
* @param {object} An event object.
*/
_onSessionEventBeamer: function(evt) {
const user = evt.data.sessionEvent.user;
if (user) user.id = user.whiteListEntry || user.whitelistEntry;
switch (evt.data.sessionEvent.type) {
case 'answer-submitted':
evt.data.sessionEvent.answeree.id = evt.data.sessionEvent.answeree.whiteListEntry || evt.data.sessionEvent.answeree.whitelistEntry;
return this._userSubmitted(evt.data.sessionEvent);
case 'viewer-idle':
return this._userIdled(user);
case 'questioninput':
case 'copy':
case 'paste':
case 'cut':
case 'input':
return this._userWorked(user);
case 'tabvisible':
return this._userMadeTabVisible(user);
case 'tabhidden':
return this._userMadeTabHidden(user);
case 'focusin':
case 'exercisefocus':
case 'windowfocus':
return this._userFocused(user);
case 'exerciseblur':
case 'windowblur':
return this._userBlurred(user);
case 'folo-disconnected':
return this._userDisconnected(user);
}
},
/**
* Changes the css variable for the paper progress indeterminate cycle duration.
*
* @param {string} The css variable that will be changed.
* @param {number} Duration of the animation cycle, given in seconds.
*/
_setVelocity: function(property, velocity) {
this.customStyle[property] = velocity + 's';
this.updateStyles();
setTimeout(function() {
this.customStyle[property] = '0s';
this.updateStyles();
}.bind(this), velocity * 1000);
},
/**
* Animates the progress bar every 5 seconds depending on the activity.
*
*/
_visualizeChange: function() {
setInterval(function() {
if (!this.directUpdate) this._update = true;
var submissionChange = this._calcChange(this._submissionCounter);
var workingChange = this._calcChange(this._workingCounter);
var focusedChange = this._calcChange(this._focusedCounter);
var idleChange = this._calcChange(this._idleCounter);
this._setVelocity('--submitted-bar-duration', this._determineVelocity(submissionChange));
this._setVelocity('--working-bar-duration', this._determineVelocity(workingChange));
this._setVelocity('--focused-bar-duration', this._determineVelocity(focusedChange));
this._setVelocity('--idle-bar-duration', this._determineVelocity(idleChange));
this._submissionCounter = 0;
this._workingCounter = 0;
this._focusedCounter = 0;
this._idleCounter = 0;
if (!this.directUpdate) this._update = false;
}.bind(this), 5000);
},
/**
* Calculate the amount of change of a given activity.
*
* @param {number} A number that is smaller than or equal to the total number of users.
* @return {number} The amount of change in percent and in proportion to the number of users.
*/
_calcChange: function(num) {
var total = this._submissionsNum + this._workingViewersNum + this._focusedViewersNum + this._idleViewersNum;
return num/total * 100;
},
/**
* Get the velocity for the paper progress indeterminate cycle.
*
* @param {number} A number in percent.
* @return {number} Returns a number in seconds for the duration of one paper progress
* indeterminate cycle.
*/
_determineVelocity: function(num) {
if (num == 0) return 0;
else if(num < 5) return 10;
else if(num < 15) return 8;
else if(num < 30) return 6;
else if(num < 50) return 4;
else if(num < 75) return 2;
else if(num <= 100) return 1;
else return 0 // change was higher than 100%
},
created: function() {
document.addEventListener('asq-ready', function(evt) {
try {
this._subscribeToEvents(evt.detail.asqEventBus);
} catch (err) {
console.debug('failed to _subscribeToEvents');
}
}.bind(this));
this._visualizeChange();
},
attached: function() {
this._changeBackgroundProgressHeight();
},
/**
* Subscribe to events to listen to them.
*
* @param {object} The eventbus who takes care of the propagation.
*/
_subscribeToEvents: function(eventBus) {
eventBus.on('asq:connected-clients', this._onConnectedClients.bind(this));
eventBus.on('asq:sessionEventBeamer', this._onSessionEventBeamer.bind(this));
eventBus.on('asq:question_type', this._onQuestionType.bind(this));
},
/**
* Dynamically adjusts the height of the backgorund effect. It's always two
* pixel bigger than the height of the progress bar.
*/
_changeBackgroundProgressHeight: function() {
var backgroundProgressContainer = this.$$('.background-progress');
var foreGroundHeight = window.getComputedStyle(this.$$('.foreground-progress'), null).height;
var newHeight = parseInt(foreGroundHeight) + 2 + 'px';
[].forEach.call(backgroundProgressContainer.children, function(progressBar) {
progressBar.customStyle['--paper-progress-height'] = newHeight;
this.updateStyles();
}.bind(this));
},
/**
* Handle question type events.
*/
_onQuestionType: function(evt) {
if (evt && evt.questionType && evt.questionType == 'asq-exercise') {
if (evt.type == 'restorePresenter') {
this._onRestorePresenter(evt);
}
}
},
/**
* Restore the number of submissions on the progress bar when the presenter
* reloads.
*/
_onRestorePresenter: function(evt) {
evt.exercises.forEach(function(exercise) {
if (exercise.uid == this.exUid) {
exercise.submissions.forEach(function(submission) {
this._viewers[submission] = {};
this._makeSureSubmissionsExist();
this._submissions[this.exUid][submission] = true;
var path = '_submissions.' + this.exUid + '.' + submission;
this.notifyPath(path, true);
}.bind(this));
}
}.bind(this));
},
});
</script>
</dom-module>