diff --git a/src/js/app/index.js b/src/js/app/index.js index 67b07755..f6936368 100644 --- a/src/js/app/index.js +++ b/src/js/app/index.js @@ -202,6 +202,30 @@ var initDemo = function(sandbox) { 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) { var command = unescape(params.command); sandbox.mainVis.customEvents.on('gitEngineReady', function() { diff --git a/src/js/sandbox/commands.js b/src/js/sandbox/commands.js index 266a54f1..09e66ffd 100644 --- a/src/js/sandbox/commands.js +++ b/src/js/sandbox/commands.js @@ -120,6 +120,7 @@ var regexMap = { 'build level': /^build +level($|\s)/, 'export tree': /^export +tree$/, 'importTreeNow': /^importTreeNow($|\s)/, + 'importLevelNow': /^importLevelNow($|\s)/, 'import tree': /^import +tree$/, 'import level': /^import +level$/, 'undo': /^undo($|\s)/ diff --git a/src/js/sandbox/index.js b/src/js/sandbox/index.js index 43a459d5..badb78b8 100644 --- a/src/js/sandbox/index.js +++ b/src/js/sandbox/index.js @@ -249,7 +249,8 @@ var Sandbox = Backbone.View.extend({ 'export tree': this.exportTree, 'import tree': this.importTree, 'importTreeNow': this.importTreeNow, - 'import level': this.importLevel + 'import level': this.importLevel, + 'importLevelNow': this.importLevelNow, }; var method = commandMap[command.get('method')]; @@ -270,23 +271,55 @@ var Sandbox = Backbone.View.extend({ 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) { var options = command.get('regexResults') || []; if (options.length < 2) { command.set('error', new Errors.GitError({ msg: intl.str('git-error-options') })); - } else { - var string = options.input.replace(/importTreeNow\s+/g, ''); - try { - this.mainVis.gitEngine.loadTreeFromString(string); - } catch (e) { - command.set('error', new Errors.GitError({ - msg: String(e) - })); - } + command.finishWith(deferred); + } + var string = options.input.replace(/importTreeNow\s+/g, ''); + try { + this.mainVis.gitEngine.loadTreeFromString(string); + } catch (e) { + command.set('error', new Errors.GitError({ + msg: String(e) + })); } - command.finishWith(deferred); },