2015-10-15 23:05:57 -04:00
|
|
|
|
var api = require('./mixins/api.jsx').api;
|
|
|
|
|
var jar = require('./lib/jar');
|
2015-10-16 11:04:34 -04:00
|
|
|
|
|
2015-10-15 23:05:57 -04:00
|
|
|
|
var translations = require('../locales/translations.json');
|
|
|
|
|
|
|
|
|
|
require('custom-event-polyfill');
|
|
|
|
|
|
|
|
|
|
// Session
|
|
|
|
|
(function () {
|
|
|
|
|
window._session = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Binds the object to private session variable and dispatches a global
|
|
|
|
|
* "session" event.
|
|
|
|
|
*
|
|
|
|
|
* @param {object} Session object
|
|
|
|
|
*
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
|
|
|
|
window.updateSession = function (session) {
|
|
|
|
|
window._session = session;
|
|
|
|
|
var sessionEvent = new CustomEvent('session', session);
|
|
|
|
|
window.dispatchEvent(sessionEvent);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a session object from the local proxy method. Calls "updateSession"
|
|
|
|
|
* once session has been returned from the proxy.
|
|
|
|
|
*
|
|
|
|
|
* @return {void}
|
|
|
|
|
*/
|
|
|
|
|
window.refreshSession = function () {
|
|
|
|
|
api({
|
|
|
|
|
host: '',
|
|
|
|
|
uri: '/session/'
|
|
|
|
|
}, function (err, body) {
|
2015-10-20 16:17:53 -04:00
|
|
|
|
if (body.banned) {
|
|
|
|
|
return window.location = body.redirectUrl;
|
|
|
|
|
} else {
|
|
|
|
|
window.updateSession(body);
|
|
|
|
|
}
|
2015-10-15 23:05:57 -04:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Fetch session
|
|
|
|
|
window.refreshSession();
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
// L10N
|
|
|
|
|
(function () {
|
|
|
|
|
/**
|
|
|
|
|
* Bind locale code from cookie if available. Uses navigator language API as a fallback.
|
|
|
|
|
*
|
|
|
|
|
* @return {string}
|
|
|
|
|
*/
|
|
|
|
|
function updateLocale () {
|
|
|
|
|
var obj = jar.get('scratchlanguage');
|
|
|
|
|
if (typeof obj === 'undefined') {
|
|
|
|
|
obj = window.navigator.userLanguage || window.navigator.language;
|
|
|
|
|
}
|
|
|
|
|
if (typeof translations[obj] === 'undefined') {
|
|
|
|
|
// Fall back on the split
|
|
|
|
|
obj = obj.split('-')[0];
|
|
|
|
|
}
|
|
|
|
|
if (typeof translations[obj] === 'undefined') {
|
|
|
|
|
// Language appears to not be supported – return `null`
|
|
|
|
|
obj = null;
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window._locale = updateLocale() || 'en';
|
|
|
|
|
window._translations = translations;
|
|
|
|
|
})();
|