scratchx/js/scratchx.js

176 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-05-01 09:32:37 -04:00
// Simulate the bare minimum of the view that exists on the main site
var Scratch = Scratch || {};
Scratch.FlashApp = Scratch.FlashApp || {};
function handleEmbedStatus(e) {
$('#scratch-loader').hide();
var scratch = $('#editor');
2015-05-01 09:32:37 -04:00
if (!e.success) {
scratch.css('marginTop', '10');
scratch.find('IMG.proj_thumb').css('width', '179px');
scratch.find('DIV.scratch_unsupported').show();
scratch.find('DIV.scratch_loading').hide();
} else {
Scratch.FlashApp.ASobj = scratch[0];
}
}
// enables the SWF to log errors
function JSthrowError(e) {
if (window.onerror) window.onerror(e, 'swf', 0);
else console.error(e);
}
function JSeditorReady() {
try {
handleParameters();
2015-05-06 14:30:40 -04:00
JSeditorReadyCallback();
2015-05-01 09:32:37 -04:00
return true;
} catch (error) {
console.error(error.message, "\n", error.stack);
throw error;
}
}
function JSprojectLoaded() {
loadExtensionQueue();
}
var extensionQueue = [];
function handleParameters() {
var project;
var queryString = window.location.search.substring(1);
var queryVars = queryString.split(/[&;]/);
for (var i = 0; i < queryVars.length; i++) {
var nameVal = queryVars[i].split('=');
switch(nameVal[0]){
case 'ext':
extensionQueue.push(nameVal[1]);
break;
case 'proj':
project = nameVal[1];
break;
}
}
if (project) {
Scratch.FlashApp.ASobj.ASloadSBXFromURL(project);
}
else {
loadExtensionQueue();
}
}
function loadExtensionQueue() {
for (var i = 0; i < extensionQueue.length; ++i) {
var extensionURL = extensionQueue[i];
ScratchExtensions.loadExternalJS(extensionURL);
}
extensionQueue = [];
}
var flashVars = {
autostart: 'false',
extensionDevMode: 'true',
server: encodeURIComponent(location.host),
cloudToken: '4af4863d-a921-4004-b2cb-e0ad00ee1927',
cdnToken: '34f16bc63e8ada7dfd7ec12c715d0c94',
urlOverrides: {
sitePrefix: "http://scratch.mit.edu/",
siteCdnPrefix: "http://cdn.scratch.mit.edu/",
assetPrefix: "http://assets.scratch.mit.edu/",
assetCdnPrefix: "http://cdn.assets.scratch.mit.edu/",
projectPrefix: "http://projects.scratch.mit.edu/",
projectCdnPrefix: "http://cdn.projects.scratch.mit.edu/",
internalAPI: "internalapi/",
siteAPI: "site-api/",
staticFiles: "scratchr2/static/"
},
inIE: (navigator.userAgent.indexOf('MSIE') > -1)
};
var params = {
allowscriptaccess: 'always',
allowfullscreen: 'true',
wmode: 'direct',
menu: 'false'
};
$.each(flashVars, function (prop, val) {
if ($.isPlainObject(val))
flashVars[prop] = encodeURIComponent(JSON.stringify(val));
});
swfobject.switchOffAutoHideShow();
swfobject.embedSWF('Scratch.swf', 'editor', '100%', '100%', '11.7.0', 'libs/expressInstall.swf',
2015-05-01 09:32:37 -04:00
flashVars, params, null, handleEmbedStatus);
2015-05-05 16:53:44 -04:00
2015-05-06 10:18:15 -04:00
/* File uploads */
2015-05-06 14:30:40 -04:00
var JSeditorReadyCallback = function(){};
2015-05-06 10:18:15 -04:00
function sendFileToFlash() {
2015-05-06 13:58:03 -04:00
var fileReader = new FileReader();
2015-05-06 14:30:40 -04:00
fileReader.onload = function (e) {
var fileAsB64 = ab_to_b64(fileReader.result);
if (Scratch.FlashApp.ASobj.ASloadBase64SBX !== undefined) {
Scratch.FlashApp.ASobj.ASloadBase64SBX(fileAsB64);
} else {
JSeditorReadyCallback = function() {
Scratch.FlashApp.ASobj.ASloadBase64SBX(fileAsB64);
}
}
}
fileReader.readAsArrayBuffer(this.files[0]);
2015-05-06 13:58:03 -04:00
showPage("editor");
2015-05-06 10:18:15 -04:00
}
$("[data-action='load-file']").click(function(e) {
$('<input type="file" />').on('change', sendFileToFlash).click();
});
function sendURLtoFlash(url) {
2015-05-06 13:58:03 -04:00
showPage("editor");
2015-05-06 14:30:40 -04:00
if (Scratch.FlashApp.ASobj.ASloadSBXFromURL !== undefined) {
Scratch.FlashApp.ASobj.ASloadSBXFromURL(url);
} else {
JSeditorReadyCallback = function() {
Scratch.FlashApp.ASobj.ASloadSBXFromURL(url);
}
}
2015-05-06 10:18:15 -04:00
}
$("[data-action='load-url']").submit(function(e) {
e.preventDefault();
sendURLtoFlash($('input[type="text"]', this).val());
2015-05-06 10:18:15 -04:00
});
/* Page switching */
$("[data-staticlink]").click(function(e) {
e.preventDefault();
2015-05-06 10:45:05 -04:00
var path = $(this).attr("data-staticlink");
showPage(path);
window.location.hash = $(this).attr('href');
if ($("body > main > article").has(path).length == 0) {
document.body.scrollTop = 0;
}
});
2015-05-05 16:53:44 -04:00
function showPage(path) {
var toHide = "body > main, body > main > article, #editor";
var toShow = "#" + path;
$(toHide).hide();
2015-05-06 09:53:59 -04:00
$("body > main, body > main > article").has(toShow).show();
2015-05-05 16:53:44 -04:00
$(toShow).show();
}
2015-05-06 09:53:59 -04:00
var initialID = "home";
function initPage() {
if (window.location.hash) initialID = window.location.hash.substr(1);
showPage(initialID);
}
$(initPage);