Skip to content
Open

fiddleR #1379

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions htdocs/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,24 @@
<li><button id="fork-notebook" title="Fork" type="button" class="btn btn-link navbar-btn"><i class="icon-code-fork"></i></button></li>
<li><button id="save-notebook" title="Save" type="button" class="btn btn-link navbar-btn disabled" style="display:none;"><i class="icon-save"></i></button></li>
<li><button id="revert-notebook" title="Revert" type="button" class="btn btn-link navbar-btn" style="display:none;"><i class="icon-undo"></i></button></li>
<li>
<span class="button-highlight"></span>
<button id="toggle-view" title="Web View" type="button" class="btn btn-link navbar-btn">
<i class="icon-html5"></i>
</button>
</li>
<li>
<span class="button-highlight"></span>
<button id="run-notebook" title="Run All" type="button" class="btn btn-link navbar-btn">
<i class="icon-play"></i>
</button>
</li>
<li>
<span class="button-highlight"></span>
<button id="new-tab" title="Show in new tab" type="button" class="btn btn-link navbar-btn" style="display:none">
<a target="_blank"><i class="icon-eye-open"></i></a>
</button>
</li>
<li><a href="#"><span id="notebook-author"></span><span id="author-title-dash" style="display:none;">&nbsp;&ndash;&nbsp;</span><span id="rename-notebook" title="Change Title" style="display:none">[<span id="notebook-title"></span>]</a></span><small id="forked-from-desc"></small></li>
<li><a href="#" id="readonly-notebook" style="display:none;">(read-only)</a></li>
<li><a href="#"><span id="loading"><img id="loading-animation" src="/img/processing.gif" style="height: 20px;width:20px;margin:0"></img></span></a></li>
Expand Down Expand Up @@ -264,6 +276,12 @@ <h1 style="vertical-align:middle;position:relative;text-align:center;padding-top
<span class="bar"></span>
</span>

<div id = "external-link" style="width:100%;display:none">
<a>Show in new tab</a>
</div>
<div style="position:relative">
<iframe id="iframe" class="tab-div" style="height:100%;width:100%;display:none" src="fiddle_default.html"></iframe>
</div>
<div id="output" class="tab-div">
</div>

Expand Down
20 changes: 13 additions & 7 deletions htdocs/editor_tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ var editor = function () {
}

function update_url(opts) {
var url = ui_utils.make_url('edit.html', opts);
var url = ui_utils.make_url(window.location.pathname.substr(1), opts);
window.history.replaceState("rcloud.notebook", null, url);
return rcloud.api.set_url(url);
}
Expand Down Expand Up @@ -953,16 +953,22 @@ var editor = function () {
if(event.node.id === 'showmore')
result.show_history(event.node.parent, false);
else if(event.node.gistname) {
if(event.click_event.metaKey || event.click_event.ctrlKey)
if(event.click_event.metaKey || event.click_event.ctrlKey) {
RCloud.UI.toggle_button.init();
$('#iframe').attr('src','fiddle_default.html');
result.open_notebook(event.node.gistname, event.node.version, true, true);
}
else {
// it's weird that a notebook exists in two trees but only one is selected (#220)
// just select - and this enables editability
if(event.node.gistname === current_.notebook &&
event.node.version == current_.version && event.node.version === null) // nulliness ok here
select_node(event.node);
else
else {
RCloud.UI.toggle_button.init();
$('#iframe').attr('src','fiddle_default.html');
result.open_notebook(event.node.gistname, event.node.version || null, event.node.root, false);
}
}
}
else
Expand Down Expand Up @@ -1030,7 +1036,7 @@ var editor = function () {
var message = "Could not open notebook " + opts.notebook;
if(opts.version)
message += " (version " + opts.version + ")";
RCloud.UI.fatal_dialog(message, "Continue", ui_utils.make_url('edit.html'));
RCloud.UI.fatal_dialog(message, "Continue", ui_utils.make_url(window.location.pathname.substr(1)));
throw xep;
});
} else if(!opts.new_notebook && current_.notebook) {
Expand All @@ -1050,7 +1056,7 @@ var editor = function () {
e.preventDefault();
e.stopPropagation();
if(e.metaKey || e.ctrlKey) {
var url = ui_utils.make_url('edit.html', {new_notebook: true});
var url = ui_utils.make_url(window.location.pathname.substr(1), {new_notebook: true});
window.open(url, "_blank");
}
else
Expand Down Expand Up @@ -1081,7 +1087,7 @@ var editor = function () {
},
// missing: friends, featured, histories
fatal_reload: function(message) {
var url = ui_utils.make_url('edit.html', {notebook: current_.notebook, version: current_.version});
var url = ui_utils.make_url(window.location.pathname.substr(1), {notebook: current_.notebook, version: current_.version});
message = "<p>Sorry, RCloud's internal state has become inconsistent. Please reload to return to a working state.</p><p>" + message + "</p>";
RCloud.UI.fatal_dialog(message, "Reload", url);
},
Expand Down Expand Up @@ -1115,7 +1121,7 @@ var editor = function () {
open_notebook: function(gistname, version, selroot, new_window) {
// really just load_notebook except possibly in a new window
if(new_window) {
var url = ui_utils.make_url('edit.html', {notebook: gistname, version: version});
var url = ui_utils.make_url(window.location.pathname.substr(1), {notebook: gistname, version: version});
window.open(url, "_blank");
}
else
Expand Down
49 changes: 49 additions & 0 deletions htdocs/fiddle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
$(function () {
window.fiddle = {
call_notebook: function(notebook) {
//if(args === undefined) {
// args = {};
//}
//args.cookies = document.cookie;
//args.redirecturl = window.location.pathname;

return new Promise(function(resolve,reject) {
rclient = RClient.create({
debug: false,
host: location.href.replace(/^http/,"ws").replace(/#.*$/,""),
on_connect: function(ocaps) {
rcloud = RCloud.create(ocaps.rcloud);

var promise;
if (rcloud.authenticated) {
promise = rcloud.session_init(rcloud.username(), rcloud.github_token());
} else {
promise = rcloud.anonymous_session_init();
}
promise.then(function(hello) {
rclient.post_response(hello);
});

// resolve(rcloud.init_client_side_data()); // what was this for?!?

version = null;
rcloud.call_notebook(notebook, version).then(function(x) {
resolve(x);
});
},
on_error: function(msg, status_code) {
// debugger;
if (msg == 'Login failed. Shutting down!') {
window.location =
(window.location.protocol +
'//' + window.location.host +
'/login.R?redirect=' +
encodeURIComponent(window.location.pathname + window.location.search));
reject(new Error(msg));
} else reject(new Error(msg));
}
});
});
}
}
});
19 changes: 19 additions & 0 deletions htdocs/fiddle_default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
<style>
img
{
position:absolute;
width:256px; /*image width */
height:200px; /*image height */
left:50%;
top:50%;
margin-left:-128px; /*image width/2 */
margin-top:-100px; /*image height/2 */
}
</style>
</head>
<body>
<img src="img/rcloud.png">
</body>
</html>
Binary file added htdocs/img/rcloud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions htdocs/js/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ rcloud_bundle.js: \
ui/progress.js \
ui/right_panel.js \
ui/run_button.js \
ui/toggle_button.js \
ui/scratchpad.js \
ui/search.js \
ui/session_pane.js \
Expand Down
104 changes: 104 additions & 0 deletions htdocs/js/rcloud_bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5976,6 +5976,7 @@ RCloud.UI.init = function() {
shell.notebook.controller.save_button(saveb);

RCloud.UI.run_button.init();
RCloud.UI.toggle_button.init();

//////////////////////////////////////////////////////////////////////////
// allow reordering cells by dragging them
Expand Down Expand Up @@ -6856,10 +6857,16 @@ RCloud.UI.run_button = (function() {
return {
init: function() {
var that = this;

run_button_.click(function() {
if(running_) {
that.stop();
}
else if ($('#iframe').css('display')!=='none') {
shell.save_notebook();
var target_url = window.location.origin+'/notebook.R/'+shell.gistname()+'/index.html';
$('#iframe').attr('src',target_url);
}
else
shell.run_notebook();
});
Expand Down Expand Up @@ -6909,6 +6916,103 @@ RCloud.UI.run_button = (function() {
}
};
})();
RCloud.UI.toggle_button = (function() {
var toggle_button_ = $("#toggle-view"),
external_link_ = $('#external-link'),
iframe_ = $('#iframe'),
output_ = $('#output'),
end_of_output_ = $('#end-of-output');

function display(icon, title) {
$('i', toggle_button_).removeClass().addClass(icon);
toggle_button_.attr('title', title);
}
function highlight(whether) {
toggle_button_.parent().find('.button-highlight').animate({opacity: whether ? 1 : 0}, 250);
}
function createIndex() {
var found = shell.notebook.model.get_asset('index.html');

var indexTemplate =

"<html>\n\
<head>\n\
<script>\n\
start = function() {\n\
fiddle.call_notebook('"+shell.gistname()+"')\n\
.then(function(x) {\n\
$('body').append('<BR>'+x.string);\n\
console.log(x);\n\
})\n\
.catch(function(err) {\n\
console.error(err);\n\
});\n\
}\n\
</script>\n\
</head>\n\
<body>\n\
\n\
<!--\n\
To pass data from R to Javascript, include the following lines:\n\
library(rcloud.web)\n\
mystring <- 'My test string'\n\
myvec <- c(1:10)\n\
mydf <- mtcars\n\
rcw.result(string = mystring, vec = myvec, df = mydf)\n\
-->\n\
\n\
Hello World!\n\
<script src='/lib/js/require-common.js'></script>\n\
<script src='/lib/js/require.js' data-main='/lib/js/require-fiddle-output.js'></script>\n\
</body>\n\
</html>";

if(found) {
found.controller.select();
}
else {
shell.notebook.controller
.append_asset(indexTemplate, 'index.html')
.spread(function(_, controller) {
controller.select();
ui_utils.ace_set_pos(RCloud.UI.scratchpad.widget, 2, 1);
});
}
}

function toggleHTML5() {
toggle_button_.click(function () {});
createIndex();
$('#new-tab a').attr('href',window.location.origin+'/notebook.R/'+shell.gistname()+'/index.html');

output_.hide();
end_of_output_.hide();
$('#prompt-area').hide();
iframe_.show();
$('#new-tab').show();
display('icon-code','Toggle Notebook');
toggle_button_.click(toggleCode);
highlight(false);
}

function toggleCode() {
toggle_button_.click(function () {});
iframe_.hide();
$('#new-tab').hide();
output_.show();
end_of_output_.show();
$('#prompt-area').show();
display('icon-html5','Toggle Web View');
toggle_button_.click(toggleHTML5);
highlight(false);
}

return {
init: function() {
toggleCode();
}
};
})();
RCloud.UI.scratchpad = {
session: null,
widget: null,
Expand Down
4 changes: 2 additions & 2 deletions htdocs/js/rcloud_bundle.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions htdocs/js/ui/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RCloud.UI.init = function() {
shell.notebook.controller.save_button(saveb);

RCloud.UI.run_button.init();
RCloud.UI.toggle_button.init();

//////////////////////////////////////////////////////////////////////////
// allow reordering cells by dragging them
Expand Down
6 changes: 6 additions & 0 deletions htdocs/js/ui/run_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ RCloud.UI.run_button = (function() {
return {
init: function() {
var that = this;

run_button_.click(function() {
if(running_) {
that.stop();
}
else if ($('#iframe').css('display')!=='none') {
shell.save_notebook();
var target_url = window.location.origin+'/notebook.R/'+shell.gistname()+'/index.html';
$('#iframe').attr('src',target_url);
}
else
shell.run_notebook();
});
Expand Down
Loading