jam-cloud/web/app/assets/javascripts/everywhere/everywhere.js

173 lines
5.0 KiB
JavaScript
Raw Normal View History

// Includes code that should be run on every single page of the site
// !!!! Keep white space after last require !!!!
//
//= require fakeJamClient
//= require fakeJamClientMessages
//= require fakeJamClientRecordings
//= require backend_alerts
2014-09-13 03:30:51 +00:00
//= require stun
(function (context, $) {
"use strict";
context.JK = context.JK || {};
var ALERT_NAMES = context.JK.ALERT_NAMES;
2014-09-13 03:30:51 +00:00
var logger = context.JK.logger;
var stun = null;
$(document).on('JAMKAZAM_CONSTRUCTED', function(e, data) {
var app = data.app;
if(!app) throw "app not found";
// makes window.jamClient / context.jamClient set to something non-null very early on
context.JK.initJamClient(app);
2014-09-13 03:30:51 +00:00
updateScoringIntervals();
})
$(document).on('JAMKAZAM_READY', function() {
// this event is fired when context.app is initialized
var app = context.JK.app;
if(!app) throw "app not found"
initializeDialogs(app);
checkAudioStopped();
2014-09-13 03:30:51 +00:00
checkMacOSXInstalledCorrectly();
2014-09-15 12:45:17 +00:00
watchPreferencesEvent(app);
2014-09-13 03:30:51 +00:00
initializeStun(app);
2014-09-13 03:30:51 +00:00
operationalEvents(app);
});
2014-09-15 12:45:17 +00:00
function watchPreferencesEvent(app) {
context.JK.onBackendEvent(ALERT_NAMES.SHOW_PREFERENCES, 'everywhere', function() {
app.layout.showDialog('client-preferences-dialog')
});
2014-09-13 03:30:51 +00:00
}
function checkMacOSXInstalledCorrectly() {
var os = context.jamClient.GetOSAsString();
// check if method exists at all for migration purposes
if(context.jamClient.IsAppInWritableVolume && os == "MacOSX" && !context.jamClient.IsAppInWritableVolume()) {
context.JK.Banner.showAlert(
2014-08-31 16:57:59 +00:00
{ title: "Drag JamKazam to the Applications Folder!",
buttons: [{name: 'SHUTDOWN APPLICATION', click: function() {context.jamClient.ShutdownApplication()}} ],
html: $('#template-app-in-read-only-volume').html()});
return;
}
}
function initializeDialogs(app) {
var backendAlerts = new JK.BackendAlerts(app);
backendAlerts.initialize();
JK.Banner.initialize();
var shutdownDialog = new JK.ShutdownDialog(app);
shutdownDialog.initialize();
2014-08-14 15:09:33 +00:00
var googleHelper = new JK.GoogleHelper(app);
JK.GoogleHelperInstance = googleHelper;
googleHelper.initialize();
var twitterHelper = new JK.TwitterHelper(app);
JK.TwitterHelperInstance = twitterHelper;
twitterHelper.initialize(gon.global.twitter_public_account);
var facebookHelper = new JK.FacebookHelper(app);
JK.FacebookHelperInstance = facebookHelper;
facebookHelper.initialize(gon.global.facebook_app_id);
var invitationDialog = new JK.InvitationDialog(app);
JK.InvitationDialogInstance = invitationDialog;
invitationDialog.initialize(facebookHelper);
var textMessageDialog = new JK.TextMessageDialog(app);
JK.TextMessageDialogInstance = textMessageDialog;
textMessageDialog.initialize();
var friendSelectorDialog = new JK.FriendSelectorDialog(app);
JK.FriendSelectorDialogInstance = friendSelectorDialog;
friendSelectorDialog.initialize();
var launchAppDialog = new JK.LaunchAppDialog(app);
launchAppDialog.initialize();
var userDropdown = new JK.UserDropdown(app);
JK.UserDropdown = userDropdown;
userDropdown.initialize(invitationDialog);
var whatsNextDialog = new JK.WhatsNextDialog(app);
whatsNextDialog.initialize(invitationDialog);
var videoDialog = new JK.VideoDialog(app);
videoDialog.initialize();
var clientPreferencesDialog = new JK.ClientPreferencesDialog(app);
clientPreferencesDialog.initialize();
}
// wait 10 seconds
function checkAudioStopped() {
if(context.jamClient.ResetPageCounters) {
// upgrade concern
context.jamClient.ResetPageCounters();
context.JK.AudioStopTimeout = setTimeout(function() {
if(context.jamClient.IsAudioStarted()) {
logger.debug("checkAudioStopped: stopping audio ...")
context.jamClient.StopAudio();
}
}, 10000);
}
}
2014-09-13 03:30:51 +00:00
function updateScoringIntervals() {
// set scoring intervals
if(context.jamClient.SetScoreWorkTimingInterval){
var success = context.jamClient.SetScoreWorkTimingInterval(
{
interval: gon.global.scoring_get_work_interval,
backoff: gon.global.scoring_get_work_backoff_interval
})
if(!success) logger.warning("unable to set scoring intervals")
}
}
function initializeStun(app) {
stun = new context.JK.Stun(app);
context.JK.StunInstance = stun;
stun.initialize();
}
function operationalEvents(app) {
if(!JK.JamServer || !JK.JamServer.registerMessageCallback) {return;} //no websocket means no events
JK.JamServer.registerMessageCallback(JK.MessageType.RELOAD, function(header, payload) {
window.location.reload();
});
JK.JamServer.registerMessageCallback(JK.MessageType.RESTART_APPLICATION, function(header, payload) {
context.jamClient.RestartApplication();
});
JK.JamServer.registerMessageCallback(JK.MessageType.STOP_APPLICATION, function(header, payload) {
context.jamClient.ShutdownApplication();
});
}
})(window, jQuery);