Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39550
1
###############################################################################
2
#
3
# CoCalc: Collaborative Calculation in the Cloud
4
#
5
# Copyright (C) 2016, Sagemath Inc.
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
###############################################################################
21
22
# window? is so this can be imported in the backend for testing...
23
$ = window?.$
24
misc = require('smc-util/misc')
25
{defaults, to_json} = misc
26
{webapp_client} = require('./webapp_client')
27
28
types = ['error', 'default', 'success', 'info']
29
default_timeout =
30
error : 5
31
default : 2
32
success : 2
33
info : 3
34
35
$?("#alert-templates").hide()
36
37
last_shown = {}
38
39
exports.alert_message = (opts={}) ->
40
opts = defaults opts,
41
type : 'default'
42
title : undefined
43
message : defaults.required
44
block : undefined
45
timeout : undefined # time in seconds
46
if not opts.timeout?
47
opts.timeout = default_timeout[opts.type]
48
49
if typeof opts.message != "string"
50
opts.message = to_json(opts.message)
51
52
# Don't show the exact same alert message more than once per 5s.
53
# This prevents a screenful of identical useless messages, which
54
# is just annoying and useless.
55
hash = misc.hash_string(opts.message + opts.type)
56
if last_shown[hash] >= misc.server_seconds_ago(5)
57
return
58
last_shown[hash] = misc.server_time()
59
60
if not opts.block?
61
if opts.type == 'error'
62
opts.block = true
63
else
64
opts.block = false
65
66
if opts.type not in types
67
alert("Unknown alert_message type #{opts.type}.")
68
return
69
70
$.pnotify
71
title : opts.title ? ''
72
type : opts.type
73
text : opts.message
74
nonblock : false
75
animation_speed : "fast"
76
closer_hover : false
77
opacity : 0.9
78
delay : opts.timeout*1000
79
80
if opts.type == 'error'
81
# Send the same error message to the backend hub so
82
# that us developers know what errors people are hitting.
83
# There really should be no situation where users *regularly*
84
# get error alert messages.
85
webapp_client.log_error(opts.message)
86
87
return
88
89
# c = $("#alert-templates .alert-#{opts.type}").clone()
90
91
# if opts.block
92
# c.addClass('alert-block')
93
# c.find(".message").text(opts.message)
94
# c.prependTo("#alert-messages")
95
# c.click(() -> $(this).remove())
96
97
# setTimeout((()->c.remove()), opts.timeout*1000)
98
99
check_for_clock_skew = () ->
100
local_time = new Date()
101
s = Math.ceil(Math.abs(webapp_client.server_time() - local_time)/1000)
102
if s > 30
103
exports.alert_message(type:'error', timeout:9999, message:"Your computer's clock is off by about #{s} seconds! You MUST set it correctly then refresh your browser. Expect nothing to work until you fix this.")
104
105
# Wait until after the page is loaded and clock sync'd before checking for skew.
106
setTimeout(check_for_clock_skew, 60000)
107
108
# for testing/development
109
# alert_message(type:'error', message:"This is an error")
110
# alert_message(type:'default', message:"This is a default alert")
111
# alert_message(type:'success', message:"This is a success alert")
112
# alert_message(type:'info', message:"This is an info alert")
113
114
# Make it so alert_message can be used by user code, e.g., in sage worksheets.
115
window?.alert_message = exports.alert_message
116