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();
|
2015-05-01 15:50:11 -04:00
|
|
|
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 20:17:43 -04:00
|
|
|
$("#editor").trigger("editorReady");
|
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();
|
|
|
|
|
2015-05-01 15:50:11 -04:00
|
|
|
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 19:36:53 -04:00
|
|
|
function sendFileToFlash(file) {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Use the HTML5 FileReader API to send base-64 encoded file
|
|
|
|
* contents to Flash via ASloadBase64SBX (or do it when the SWF
|
|
|
|
* is ready).
|
|
|
|
*/
|
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);
|
2015-05-06 19:36:53 -04:00
|
|
|
showPage("editor");
|
2015-05-06 14:30:40 -04:00
|
|
|
if (Scratch.FlashApp.ASobj.ASloadBase64SBX !== undefined) {
|
|
|
|
Scratch.FlashApp.ASobj.ASloadBase64SBX(fileAsB64);
|
|
|
|
} else {
|
2015-05-06 20:17:43 -04:00
|
|
|
$(document).on("editorReady", function(e) {
|
2015-05-06 14:30:40 -04:00
|
|
|
Scratch.FlashApp.ASobj.ASloadBase64SBX(fileAsB64);
|
2015-05-06 20:17:43 -04:00
|
|
|
$(this).off(e);
|
|
|
|
});
|
2015-05-06 14:30:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-05-06 19:36:53 -04:00
|
|
|
fileReader.readAsArrayBuffer(file);
|
2015-05-06 10:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$("[data-action='load-file']").click(function(e) {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Buttons with data-action="load-file" trigger a file input
|
|
|
|
* prompt, passed to a handler that passes the file to Flash.
|
|
|
|
*/
|
|
|
|
$('<input type="file" />').on('change', function(){
|
|
|
|
sendFileToFlash(this.files[0])
|
|
|
|
}).click();
|
2015-05-06 10:18:15 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
function sendURLtoFlash(url) {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Send a URL to Flash with ASloadGithubURL, or do it when the
|
|
|
|
* editor is ready.
|
|
|
|
*/
|
2015-05-06 19:36:53 -04:00
|
|
|
if (Scratch.FlashApp.ASobj.ASloadGithubURL !== undefined) {
|
|
|
|
Scratch.FlashApp.ASobj.ASloadGithubURL(url);
|
2015-05-06 14:30:40 -04:00
|
|
|
} else {
|
2015-05-06 20:17:43 -04:00
|
|
|
$(document).on("editorReady", function(e) {
|
2015-05-06 19:36:53 -04:00
|
|
|
Scratch.FlashApp.ASobj.ASloadGithubURL(url);
|
2015-05-06 20:17:43 -04:00
|
|
|
$(this).off(e);
|
|
|
|
});
|
2015-05-06 14:30:40 -04:00
|
|
|
}
|
2015-05-06 10:18:15 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 20:44:24 -04:00
|
|
|
|
|
|
|
/* Load from URL */
|
2015-05-06 19:55:28 -04:00
|
|
|
$("[data-action='load-url']").click(function(e) {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Links with data-action="load-url" send their href to Flash
|
|
|
|
* So use like...
|
|
|
|
* <a href="?url=urlToLoad" data-action="load-url">Load this</a>
|
|
|
|
*/
|
2015-05-06 19:55:28 -04:00
|
|
|
e.preventDefault();
|
|
|
|
showPage("editor");
|
|
|
|
sendURLtoFlash($(this).attr("href"));
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".url-load-form").submit(function(e) {
|
2015-05-06 20:44:24 -04:00
|
|
|
// Load text input value on submit
|
2015-05-06 14:59:37 -04:00
|
|
|
e.preventDefault();
|
2015-05-06 19:36:53 -04:00
|
|
|
showPage("editor");
|
2015-05-06 14:59:37 -04:00
|
|
|
sendURLtoFlash($('input[type="text"]', this).val());
|
2015-05-06 10:18:15 -04:00
|
|
|
});
|
|
|
|
|
2015-05-06 19:36:53 -04:00
|
|
|
function loadFromURLParameter() {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Get all url=urlToLoad from the querystring and send to Flash
|
|
|
|
* Use like...
|
|
|
|
* http://scratchx.org/?url=urlToLoad1&url=urlToLoad2
|
|
|
|
*/
|
2015-05-06 19:36:53 -04:00
|
|
|
var paramString = window.location.search.replace(/^\?|\/$/g, '');
|
|
|
|
var vars = paramString.split("&");
|
|
|
|
var showedEditor = false;
|
|
|
|
for (var i=0; i<vars.length; i++) {
|
|
|
|
var pair = vars[i].split("=");
|
|
|
|
if (pair.length > 1 && pair[0]=="url") {
|
|
|
|
if (!showedEditor) {
|
2015-05-06 20:44:24 -04:00
|
|
|
// Only try to switch to the editor once
|
2015-05-06 19:36:53 -04:00
|
|
|
showPage("editor");
|
|
|
|
showedEditor = true;
|
|
|
|
}
|
|
|
|
sendURLtoFlash(pair[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 10:18:15 -04:00
|
|
|
|
2015-05-06 10:17:51 -04:00
|
|
|
/* Page switching */
|
|
|
|
|
2015-05-06 19:56:15 -04:00
|
|
|
$("[data-action='static-link']").click(function(e) {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Links with data-action="static-link" should switch the view
|
|
|
|
* to that page. Works like tabs sort of. Use like...
|
|
|
|
* <!-- Makes a link to the Privacy Policy section -->
|
|
|
|
* <a href="#privacy-policy" data-action="static-link">Privacy Policy</a>
|
|
|
|
*
|
|
|
|
*/
|
2015-05-06 10:17:51 -04:00
|
|
|
e.preventDefault();
|
2015-05-06 19:56:15 -04:00
|
|
|
var path = $(this).attr("href").substring(1);
|
2015-05-06 10:45:05 -04:00
|
|
|
showPage(path);
|
2015-05-06 10:17:51 -04:00
|
|
|
});
|
|
|
|
|
2015-05-05 16:53:44 -04:00
|
|
|
function showPage(path) {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* Show a part of the page. The site is set up like
|
|
|
|
* body
|
|
|
|
* main
|
|
|
|
* article#home
|
|
|
|
* article#privacy-policy
|
|
|
|
* ...
|
|
|
|
* editor
|
|
|
|
*
|
|
|
|
* Each <article> is a "page" of the site, plus one special
|
|
|
|
* view, which is the editor.
|
|
|
|
*
|
|
|
|
* The editor is not actually hidden, but located -9999px above
|
|
|
|
* the viewport. This is because if it's hidden, it doesn't load
|
|
|
|
* when the page is loaded.
|
|
|
|
*
|
|
|
|
* So first we have to hide everything that we're not going to show
|
|
|
|
* or move the editor up, then display everything we're going to show
|
|
|
|
* if it's hidden.
|
|
|
|
*
|
|
|
|
* If we are linking to an anchor within a page, then show its parent.
|
|
|
|
*/
|
2015-05-06 15:06:32 -04:00
|
|
|
var toHide = "body > main, body > main > article";
|
2015-05-05 16:53:44 -04:00
|
|
|
var toShow = "#" + path;
|
2015-05-06 15:06:32 -04:00
|
|
|
var $toShow = $(toShow);
|
2015-05-05 16:53:44 -04:00
|
|
|
|
2015-05-06 19:36:53 -04:00
|
|
|
$(toHide).filter(":visible").hide();
|
|
|
|
if (toShow != "#editor") $("#editor").css({top: "-9999px"});
|
2015-05-06 15:06:32 -04:00
|
|
|
$("body > main, body > main > article").has($toShow).show();
|
|
|
|
$toShow.show();
|
|
|
|
if (path == "editor") {
|
|
|
|
$toShow.css({top: 0});
|
|
|
|
}
|
2015-05-06 14:59:55 -04:00
|
|
|
|
|
|
|
window.location.hash = toShow;
|
|
|
|
if ($("body > main > article").has(toShow).length == 0) {
|
|
|
|
document.body.scrollTop = 0;
|
|
|
|
}
|
2015-05-05 16:53:44 -04:00
|
|
|
}
|
|
|
|
|
2015-05-06 09:53:59 -04:00
|
|
|
var initialID = "home";
|
|
|
|
function initPage() {
|
2015-05-06 20:44:24 -04:00
|
|
|
/*
|
|
|
|
* On load, show the page identified by the URL fragment. Default to #home.
|
|
|
|
*/
|
2015-05-06 09:53:59 -04:00
|
|
|
if (window.location.hash) initialID = window.location.hash.substr(1);
|
|
|
|
showPage(initialID);
|
2015-05-06 19:36:53 -04:00
|
|
|
loadFromURLParameter();
|
2015-05-06 09:53:59 -04:00
|
|
|
}
|
|
|
|
$(initPage);
|