Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Views: 39604
1
# CoCalc: Collaborative Calculation in the Cloud
2
# Copyright (C) 2017, Sagemath Inc.
3
# License: AGPLv3+
4
5
# this is embedded into index.pug to do some dynamic changes.
6
# the overall goal is to be slick and simple to avoid any slowdowns whatsoever...
7
8
'use strict'
9
10
stat_rows = [
11
['Modified projects', 'projects_edited'],
12
['Created projects', 'projects_created'],
13
['Created accounts', 'accounts_created'],
14
]
15
opened_files = [
16
['Sage Worksheets', 'sagews'],
17
['Jupyter Notebooks', 'ipynb'],
18
['LaTeX Documents', 'tex']
19
]
20
21
sum_clients = (stats) ->
22
hubs = stats?['hub_servers'] ? []
23
s = 0
24
for h in hubs
25
s += h.clients ? 0
26
return s
27
28
update_stats = (stats) ->
29
#console.log stats
30
table = document.getElementById('statstable')
31
if table.rows.length >= 2
32
for i in [table.rows.length...1]
33
table.deleteRow(i-1)
34
35
for [name, key] in stat_rows
36
row = table.insertRow()
37
cell = row.insertCell()
38
cell.className = 'left'
39
cell.innerHTML = "<strong>#{name}</strong>"
40
for j in window.stat_times
41
cell = row.insertCell()
42
cell.appendChild(document.createTextNode("#{stats[key][j]}"))
43
44
row = table.insertRow()
45
delim = row.insertCell()
46
delim.innerHTML = '&nbsp;'
47
delim.setAttribute("colspan", 5)
48
row = table.insertRow()
49
cell = row.insertCell()
50
cell.className = 'left'
51
cell.innerHTML = '<strong>Number of files</strong>'
52
cell = row.insertCell()
53
cell.setAttribute("colspan", 4)
54
cell.innerHTML = 'opened or edited, counting total and unique file paths'
55
56
for [name, ext] in opened_files
57
row = table.insertRow()
58
cell = row.insertCell()
59
cell.className = 'left'
60
cell.innerHTML = "<strong>#{name}</strong>"
61
for j in window.stat_times
62
cell = row.insertCell()
63
total = stats.files_opened?.total[j]?[ext] ? 0
64
distinct = stats.files_opened?.distinct[j]?[ext] ? 0
65
cell.innerHTML = "<span title='total files opened'>#{total}</span> (<span title='distinct files opened'>#{distinct})</span>"
66
67
document.getElementById("sum_clients").innerHTML = sum_clients(stats)
68
69
get_stats = ->
70
r = new XMLHttpRequest()
71
r.open("GET", "./stats", true)
72
r.onreadystatechange = ->
73
return if r.readyState != 4 or r.status != 200
74
try
75
update_stats(JSON.parse(r.responseText))
76
catch e
77
console.log e
78
r.send()
79
# tail recursive callback
80
setTimeout(get_stats, 10 * 1000)
81
82
init_video = ->
83
for vplayer in document.getElementsByClassName("video-player")
84
vid = vplayer.getElementsByTagName("video")[0]
85
over = vplayer.getElementsByClassName("video-overlay")[0]
86
do (vplayer, vid) ->
87
vplayer.onclick = (el) ->
88
console.log vplayer, over, vid
89
vplayer.removeChild(over)
90
vid.setAttribute("controls", "true")
91
vid.setAttribute("loop", "true")
92
vid.play()
93
94
find_parent = (el, matcher) ->
95
while true
96
el = el.parentElement
97
return null if not el
98
return el if matcher(el)
99
100
init_magic_anchors = ->
101
div_matcher = (el) ->
102
is_div = el.tagName.toUpperCase() == 'DIV'
103
is_anchor = el.getAttribute("id")?
104
return is_div and is_anchor
105
106
for tag in ['h1', 'h2']
107
for header in document.getElementsByTagName(tag)
108
div = find_parent(header, div_matcher)
109
continue if not div
110
a_id = "a-#{div.getAttribute('id')}"
111
anchor = document.querySelector("a##{a_id}")
112
continue if not anchor
113
marker = document.createElement("a")
114
marker.setAttribute("class", "marker")
115
loc = window.location
116
marker_url = loc.href.slice(0, loc.href.length - loc.hash.length) + "##{a_id}"
117
marker.setAttribute("href", marker_url)
118
marker.appendChild(document.createTextNode('¶'))
119
header.appendChild(marker)
120
121
document.addEventListener "DOMContentLoaded", ->
122
get_stats()
123
init_video()
124
init_magic_anchors()
125
126