From 59bd917a2c2408bcd7ac308e596ae2cef3d59e6a Mon Sep 17 00:00:00 2001 From: Arsen Melikyan Date: Sun, 25 Oct 2015 12:30:44 +0400 Subject: [PATCH] save bonfire solution as a gist and fix search issue button query string --- client/main.js | 55 ++++++++++++++++++++++- server/middlewares/csp.js | 3 +- server/views/coursewares/showBonfire.jade | 6 +++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/client/main.js b/client/main.js index 6f147abcf7..44e8c21d73 100644 --- a/client/main.js +++ b/client/main.js @@ -208,13 +208,66 @@ $(document).ready(function() { $('#search-issue').unbind('click'); $('#search-issue').on('click', function() { - var queryIssue = window.location.href.toString(); + var queryIssue = window.location.href.toString().split('#?')[0]; window.open('https://github.com/FreeCodeCamp/FreeCodeCamp/issues?q=' + 'is:issue is:all ' + (challenge_Name || challengeName) + ' OR ' + queryIssue.substr(queryIssue.lastIndexOf('challenges/') + 11) .replace('/', ''), '_blank'); }); + $('#gist-share').unbind('click'); + $('#gist-share').on('click', function() { + var gistWindow = window.open('', '_blank'); + + $('#gist-share') + .attr('disabled', 'true') + .removeClass('btn-danger') + .addClass('btn-warning disabled'); + + function createCORSRequest(method, url) { + var xhr = new XMLHttpRequest(); + if ('withCredentials' in xhr) { + xhr.open(method, url, true); + } else if (typeof XDomainRequest !== 'undefined') { + xhr = new XDomainRequest(); + xhr.open(method, url); + } else { + xhr = null; + } + xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); + return xhr; + } + + var request = createCORSRequest('post', 'https://api.github.com/gists'); + if (request) { + request.onload = function() { + if (request.readyState === 4 && + request.status === 201 && + request.statusText === 'Created') { + gistWindow.location.href = JSON.parse(request.responseText)['html_url']; + } + }; + var data = { + description: (challenge_Name || challengeName), + public: true, + files: {} + }, + queryIssue = window.location.href.toString().split('#?')[0], + filename = queryIssue + .substr(queryIssue.lastIndexOf('challenges/') + 11) + .replace('/', '') + '.js'; + data['files'][filename] = { + content: '// ' + data.description + '\n' + + (username ? '// Author: @' + username + '\n' : '') + + '// Challenge: ' + queryIssue + '\n' + + '// Learn to Code at Free Code Camp (www.freecodecamp.com)' + + '\n\n' + editor.getValue().trim() + }; + + request.send(JSON.stringify(data)); + } + }); + $('#help-ive-found-a-bug-wiki-article').unbind('click'); $('#help-ive-found-a-bug-wiki-article').on('click', function() { window.open("https://github.com/FreeCodeCamp/FreeCodeCamp/wiki/Help-I've-Found-a-Bug", '_blank'); diff --git a/server/middlewares/csp.js b/server/middlewares/csp.js index 24e1e848cf..391f9bb450 100644 --- a/server/middlewares/csp.js +++ b/server/middlewares/csp.js @@ -48,7 +48,8 @@ const trusted = [ 'http://hn.inspectlet.com/', '*.googleapis.com', '*.gstatic.com', - 'https://hn.inspectlet.com/' + 'https://hn.inspectlet.com/', + 'https://*.github.com' ]; export default function csp() { diff --git a/server/views/coursewares/showBonfire.jade b/server/views/coursewares/showBonfire.jade index 5a2f6c0a11..7ca4a6c74f 100644 --- a/server/views/coursewares/showBonfire.jade +++ b/server/views/coursewares/showBonfire.jade @@ -80,6 +80,10 @@ block content var dashedName = !{JSON.stringify(dashedName)}; var _ = R; var dashed = !{JSON.stringify(dashedName)}; + var username; + if (user) + script(type="text/javascript"). + username = !{JSON.stringify(user.username)}; .col-md-8.col-lg-9 .editorScrollDiv(style = "overflow-y: auto; overflow-x: hidden;") @@ -103,12 +107,14 @@ block content .row if (user) #submit-challenge.animated.fadeIn.btn.btn-lg.btn-primary.btn-block Submit and go to my next challenge (ctrl + enter) + a.animated.fadeIn.btn.btn-lg.btn-danger.btn-block#gist-share Share your solution as a GitHub gist if (user.progressTimestamps.length > 2) a.btn.btn-lg.btn-block.btn-twitter(target="_blank", href="https://twitter.com/intent/tweet?text=I%20just%20#{verb}%20%40FreeCodeCamp%20#{name}&url=http%3A%2F%2Ffreecodecamp.com/challenges/#{dashedName}&hashtags=LearnToCode, JavaScript", onclick="ga('send', 'event', 'twitter', 'share', 'challenge completion share');") i.fa.fa-twitter   = phrase else #next-challenge.btn.btn-lg.btn-primary.btn-block Go to my next challenge (ctrl + enter) + a.btn.btn-lg.btn-danger.btn-block#gist-share Share your solution as a GitHub gist #reset-modal.modal(tabindex='-1') .modal-dialog.animated.fadeInUp.fast-animation .modal-content