Extract playground samples from mdoc, add possiblity to run them from source

This commit is contained in:
Alex Dima 2016-09-06 12:34:53 +02:00
parent df6136ea12
commit 5b86272f55
103 changed files with 3126 additions and 21 deletions

View file

@ -176,22 +176,23 @@ function load() {
var sampleSwitcher = document.createElement('select');
var sampleChapter;
ALL_SAMPLES.forEach(function (sample) {
PLAY_SAMPLES.forEach(function (sample) {
if (!sampleChapter || sampleChapter.label !== sample.chapter) {
sampleChapter = document.createElement('optgroup');
sampleChapter.label = sample.chapter;
sampleSwitcher.appendChild(sampleChapter);
}
var sampleOption = document.createElement('option');
sampleOption.value = sample.sampleId;
sampleOption.value = sample.id;
sampleOption.appendChild(document.createTextNode(sample.name));
sampleChapter.appendChild(sampleOption);
});
sampleSwitcher.className = 'sample-switcher';
function findSample(sampleId) {
for (var i = 0; i < SAMPLES.length; i++) {
var sample = SAMPLES[i];
var LOADED_SAMPLES = [];
function findLoadedSample(sampleId) {
for (var i = 0; i < LOADED_SAMPLES.length; i++) {
var sample = LOADED_SAMPLES[i];
if (sample.id === sampleId) {
return sample;
}
@ -199,22 +200,46 @@ function load() {
return null;
}
function findSamplePath(sampleId) {
for (var i = 0; i < PLAY_SAMPLES.length; i++) {
var sample = PLAY_SAMPLES[i];
if (sample.id === sampleId) {
return sample.path;
}
}
return null;
}
function loadSample(sampleId, callback) {
var sample = findSample(sampleId);
var sample = findLoadedSample(sampleId);
if (sample) {
return callback(null, sample);
}
var script = document.createElement('script');
script.src = 'playground/samples/' + sampleId + '.js';
script.onload = function() {
var sample = findSample(sampleId);
return callback(sample ? null : new Error('sample not found'), sample);
};
script.onerror = function(err) {
return callback(err, null);
};
document.head.appendChild(script);
var samplePath = findSamplePath(sampleId);
if (!samplePath) {
return callback(new Error('sample not found'));
}
samplePath = 'playground/new-samples/' + samplePath;
var js = xhr(samplePath + '/sample.js').then(function(response) { return response.responseText});
var css = xhr(samplePath + '/sample.css').then(function(response) { return response.responseText});
var html = xhr(samplePath + '/sample.html').then(function(response) { return response.responseText});
monaco.Promise.join([js, css, html]).then(function(_) {
var js = _[0];
var css = _[1];
var html = _[2];
LOADED_SAMPLES.push({
id: sampleId,
js: js,
css: css,
html: html
});
return callback(null, findLoadedSample(sampleId));
}, function(err) {
callback(err, null);
});
}
sampleSwitcher.onchange = function() {
@ -243,7 +268,7 @@ function load() {
function parseHash(firstTime) {
var sampleId = window.location.hash.replace(/^#/, '');
if (!sampleId) {
sampleId = ALL_SAMPLES[0].sampleId;
sampleId = PLAY_SAMPLES[0].id;
}
if (firstTime) {