###############################################################################1#2# CoCalc: Collaborative Calculation in the Cloud3#4# Copyright (C) 2016, Sagemath Inc.5#6# This program is free software: you can redistribute it and/or modify7# it under the terms of the GNU General Public License as published by8# the Free Software Foundation, either version 3 of the License, or9# (at your option) any later version.10#11# This program is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14# GNU General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with this program. If not, see <http://www.gnu.org/licenses/>.18#19###############################################################################2021# window? is so this can be imported in the backend for testing...22$ = window?.$23misc = require('smc-util/misc')24{defaults, to_json} = misc25{webapp_client} = require('./webapp_client')2627types = ['error', 'default', 'success', 'info']28default_timeout =29error : 530default : 231success : 232info : 33334$?("#alert-templates").hide()3536last_shown = {}3738exports.alert_message = (opts={}) ->39opts = defaults opts,40type : 'default'41title : undefined42message : defaults.required43block : undefined44timeout : undefined # time in seconds45if not opts.timeout?46opts.timeout = default_timeout[opts.type]4748if typeof opts.message != "string"49opts.message = to_json(opts.message)5051# Don't show the exact same alert message more than once per 5s.52# This prevents a screenful of identical useless messages, which53# is just annoying and useless.54hash = misc.hash_string(opts.message + opts.type)55if last_shown[hash] >= misc.server_seconds_ago(5)56return57last_shown[hash] = misc.server_time()5859if not opts.block?60if opts.type == 'error'61opts.block = true62else63opts.block = false6465if opts.type not in types66alert("Unknown alert_message type #{opts.type}.")67return6869$.pnotify70title : opts.title ? ''71type : opts.type72text : opts.message73nonblock : false74animation_speed : "fast"75closer_hover : false76opacity : 0.977delay : opts.timeout*10007879if opts.type == 'error'80# Send the same error message to the backend hub so81# that us developers know what errors people are hitting.82# There really should be no situation where users *regularly*83# get error alert messages.84webapp_client.log_error(opts.message)8586return8788# c = $("#alert-templates .alert-#{opts.type}").clone()8990# if opts.block91# c.addClass('alert-block')92# c.find(".message").text(opts.message)93# c.prependTo("#alert-messages")94# c.click(() -> $(this).remove())9596# setTimeout((()->c.remove()), opts.timeout*1000)9798check_for_clock_skew = () ->99local_time = new Date()100s = Math.ceil(Math.abs(webapp_client.server_time() - local_time)/1000)101if s > 30102exports.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.")103104# Wait until after the page is loaded and clock sync'd before checking for skew.105setTimeout(check_for_clock_skew, 60000)106107# for testing/development108# alert_message(type:'error', message:"This is an error")109# alert_message(type:'default', message:"This is a default alert")110# alert_message(type:'success', message:"This is a success alert")111# alert_message(type:'info', message:"This is an info alert")112113# Make it so alert_message can be used by user code, e.g., in sage worksheets.114window?.alert_message = exports.alert_message115116