Skip to content

Commit

Permalink
- Added support for downloading open files
Browse files Browse the repository at this point in the history
  • Loading branch information
dufour committed Oct 14, 2012
1 parent fbe486c commit 6c368d5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions download.php
@@ -0,0 +1,15 @@
<?php
if ($_POST["filename"]) {
$filename = $_POST["filename"];
$content = $_POST["content"];
} else {
$filename = $_GET["filename"];
$content = $_GET["content"];
}

header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="' . $filename. '"');
header("Content-Length: " . strlen($content));

echo $content;
?>
18 changes: 18 additions & 0 deletions include/fs.js
Expand Up @@ -299,6 +299,14 @@ cp.deleteFile = function (filename) {
cp.fs.deleteFile(filename);
};

function basename(filename) {
return filename.replace(/\\/g,'/').replace( /.*\//, '');
}

function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

cp.makeEditorToolbar = function (filename) {
var $toolbar = makeToolbar();

Expand All @@ -315,6 +323,16 @@ cp.makeEditorToolbar = function (filename) {
});
$loadButton.appendTo($group);

$saveButton = makeTBButton($('<i class="icon-download-alt"/>'), {"title" : "Download"});
$saveButton.click(function () {
var name = basename(filename);
if (!endsWith(name, ".js")) {
name = name + ".js";
}
saveAs(cp.fs.getContent(filename), name);
});
$saveButton.appendTo($group);

return $toolbar;
};

Expand Down
12 changes: 12 additions & 0 deletions index.html
Expand Up @@ -256,6 +256,13 @@
function focusREPL() {
cp.repl.focus();
}

function saveAs(content, filename) {
$("#form-download-content").val(content);
$("#form-download-filename").val(filename);

$("#form-download").submit();
}
</script>
<script src="include/actions.js"></script>
<script src="include/editors.js"></script>
Expand All @@ -265,5 +272,10 @@
<script src="include/mousetrap.min.js"></script>
<script src="include/tutorial.js"></script>
<script>cp.query(null);</script>

<form id="form-download" class="hide" action="download.php" method="post">
<textarea id="form-download-content" name="content" style="width: 0; height: 0;"></textarea>
<textarea id="form-download-filename" name="filename" style="width: 0; height: 0;"></textarea>
</form>
</body>
</html>

0 comments on commit 6c368d5

Please sign in to comment.