Resolves #181 share levels easily with gist IDs and jsonp

This commit is contained in:
Peter Cottle 2014-06-29 16:22:48 -07:00
parent d01f1806b4
commit 507291a0c4
3 changed files with 69 additions and 11 deletions

View file

@ -202,6 +202,30 @@ var initDemo = function(sandbox) {
tryLocaleDetect(); tryLocaleDetect();
} }
if (params.gist_level_id) {
$.ajax({
url: 'https://api.github.com/gists/' + params.gist_level_id,
type: 'GET',
dataType: 'jsonp',
success: function(response) {
var data = response.data || {};
var files = data.files || {};
if (!Object.keys(files).length) {
console.warn('no files found');
return;
}
var file = files[Object.keys(files)[0]];
if (!file.content) {
console.warn('file empty');
}
eventBaton.trigger(
'commandSubmitted',
'importLevelNow ' + escape(file.content) + '; clear'
);
}
});
}
if (params.command) { if (params.command) {
var command = unescape(params.command); var command = unescape(params.command);
sandbox.mainVis.customEvents.on('gitEngineReady', function() { sandbox.mainVis.customEvents.on('gitEngineReady', function() {

View file

@ -120,6 +120,7 @@ var regexMap = {
'build level': /^build +level($|\s)/, 'build level': /^build +level($|\s)/,
'export tree': /^export +tree$/, 'export tree': /^export +tree$/,
'importTreeNow': /^importTreeNow($|\s)/, 'importTreeNow': /^importTreeNow($|\s)/,
'importLevelNow': /^importLevelNow($|\s)/,
'import tree': /^import +tree$/, 'import tree': /^import +tree$/,
'import level': /^import +level$/, 'import level': /^import +level$/,
'undo': /^undo($|\s)/ 'undo': /^undo($|\s)/

View file

@ -249,7 +249,8 @@ var Sandbox = Backbone.View.extend({
'export tree': this.exportTree, 'export tree': this.exportTree,
'import tree': this.importTree, 'import tree': this.importTree,
'importTreeNow': this.importTreeNow, 'importTreeNow': this.importTreeNow,
'import level': this.importLevel 'import level': this.importLevel,
'importLevelNow': this.importLevelNow,
}; };
var method = commandMap[command.get('method')]; var method = commandMap[command.get('method')];
@ -270,23 +271,55 @@ var Sandbox = Backbone.View.extend({
this.mainVis.show(); this.mainVis.show();
}, },
importLevelNow: function(command, deferred) {
var options = command.get('regexResults') || [];
if (options.length < 2) {
command.set('error', new Errors.GitError({
msg: intl.str('git-error-options')
}));
command.finishWith(deferred);
return;
}
var string = options.input.replace(/importLevelNow\s+/g, '');
var Level = require('../level').Level;
try {
var levelJSON = JSON.parse(unescape(string));
console.log(levelJSON);
var whenLevelOpen = Q.defer();
this.currentLevel = new Level({
level: levelJSON,
deferred: whenLevelOpen,
command: command
});
this.hide();
whenLevelOpen.promise.then(function() {
command.finishWith(deferred);
});
} catch(e) {
command.set('error', new Errors.GitError({
msg: 'Something went wrong ' + String(e)
}));
}
command.finishWith(deferred);
},
importTreeNow: function(command, deferred) { importTreeNow: function(command, deferred) {
var options = command.get('regexResults') || []; var options = command.get('regexResults') || [];
if (options.length < 2) { if (options.length < 2) {
command.set('error', new Errors.GitError({ command.set('error', new Errors.GitError({
msg: intl.str('git-error-options') msg: intl.str('git-error-options')
})); }));
} else { command.finishWith(deferred);
var string = options.input.replace(/importTreeNow\s+/g, ''); }
try { var string = options.input.replace(/importTreeNow\s+/g, '');
this.mainVis.gitEngine.loadTreeFromString(string); try {
} catch (e) { this.mainVis.gitEngine.loadTreeFromString(string);
command.set('error', new Errors.GitError({ } catch (e) {
msg: String(e) command.set('error', new Errors.GitError({
})); msg: String(e)
} }));
} }
command.finishWith(deferred); command.finishWith(deferred);
}, },