Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files and directories are downloadable #146

Merged
merged 2 commits into from
Apr 21, 2016
Merged
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
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"browserfs": "0.5.8",
"bootstrap-contextmenu": "^0.3.4",
"bootbox.js": "^4.4.0",
"github-api": "~0.10.7"
"github-api": "~0.10.7",
"Stuk/jszip": "3.0.0"
},
"devDependencies": {},
"resolutions": {
Expand Down
1 change: 1 addition & 0 deletions src/app/require.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require.config({
"bootstrap-contextmenu":"bower_modules/bootstrap-contextmenu/bootstrap-contextmenu",
"bootbox": "bower_modules/bootbox.js/bootbox",
"github-api": "bower_modules/github-api/github",
"jszip": "bower_modules/jszip/dist/jszip.min",

// Application-specific modules
"app/config": "app/config/config.dev", // overridden to 'config.dist' in build config
Expand Down
7 changes: 1 addition & 6 deletions src/components/editor/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<label class="control-label sr-only" for="editor-font-size">font size (in pixels)</label>
<input data-bind="value: prefs.fontSize" id="editor-font-size" type="number" class="form-control input-sm">
</div>
<div class="col-md-2">
<div class="col-md-4">
<button id="current-code-btn" type="button" class="btn btn-default btn-sm center-block">
<span class="glyphicon glyphicon-file"></span> <span class="file-name" data-bind="text: currentFileName">untitled</span>
</button>
Expand All @@ -19,11 +19,6 @@
<span class="glyphicon glyphicon-indent-left"></span> Indent code
</button>
</div>
<div class="col-md-2">
<button id="download-file-btn" type="button" class="btn btn-default btn-sm center-block">
<span class="glyphicon glyphicon-save"></span> Download file
</button>
</div>
<div class="col-md-2">
<a href="#" tabindex="0" id="editor-settings-btn" type="button" class="btn btn-default btn-sm center-block"
data-toggle="popover" data-trigger="manual" data-container="body" data-html="true" data-placement="bottom">
Expand Down
10 changes: 1 addition & 9 deletions src/components/editor/editor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*global Bloodhound:false, saveAs:false*/
/*global Bloodhound:false */
import ko from 'knockout';
import templateMarkup from 'text!./editor.html';
import ace from 'ace/ace';
import 'bloodhound';
import TokenHighlighter from 'components/editor/token-highlighter';
import 'Blob';
import 'FileSaver';
import * as SysGlobalObservables from 'app/sys-global-observables';

class Editor {
Expand Down Expand Up @@ -43,12 +41,6 @@ class Editor {
e.stopPropagation();
});

$('#download-file-btn').click(() => {
var text = this.getText();
var blob = new Blob([text]);
saveAs(blob, SysGlobalObservables.currentFileName());
});

$('#autoindent-code-btn').click(() => {
this.autoIndentCode();
});
Expand Down
60 changes: 50 additions & 10 deletions src/components/file-browser/file-browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Buffer */
/*global Buffer, saveAs:false*/
import ko from 'knockout';
import templateMarkup from 'text!./file-browser.html';
import 'knockout-projections';
Expand All @@ -7,7 +7,10 @@ import SysRuntime from 'app/sys-runtime';
import SysFileSystem from 'app/sys-filesystem';
import bootbox from 'bootbox';
import 'bootstrap-contextmenu';
import 'Blob';
import 'FileSaver';
import * as SysGlobalObservables from 'app/sys-global-observables';
import JSZip from 'jszip';

// notification options
var warningNotific8Options = {
Expand Down Expand Up @@ -110,7 +113,11 @@ class Filebrowser {
}
menuHtml += '<li><a data-action="rename">Rename</a></li>';
menuHtml += '<li><a data-action="delete">Delete</a></li>';
if (!self.metaData[itemId].isDirectory) {
menuHtml += '<li><a data-action="downloadFile">Download \''+ self.metaData[itemId].name +'\'</a></li>';
}
if (self.metaData[itemId].isDirectory) {
menuHtml += '<li><a data-action="downloadDir">Download \''+ self.metaData[itemId].name +'\' as .zip</a></li>';
menuHtml += '<li><a data-action="clone">Clone a repo into \'' + self.metaData[itemId].name + '\'...</a></li>';
menuHtml += '<li><a data-action="push">Push \''+ self.metaData[itemId].name + '\' to a repo...</a></li>';
}
Expand Down Expand Up @@ -139,17 +146,21 @@ class Filebrowser {
});
}
else if (action === 'rename') {
bootbox.prompt('Insert new name', function (result) {
if (result === null || result.trim().length === 0) {
// do nothing
}
else {
index = itemPath.indexOf(itemName);
if (index === -1) {
return;
bootbox.prompt({
title: 'Insert a new name for \'' + itemName + '\'',
value: itemName,
callback: function (result) {
if (result === null || result.trim().length === 0) {
// do nothing
}
else {
self.fs.rename(itemPath, itemPath.slice(0, index) + result);
index = itemPath.indexOf(itemName);
if (index === -1) {
return;
}
else {
self.fs.rename(itemPath, itemPath.slice(0, index) + result);
}
}
}
});
Expand Down Expand Up @@ -180,6 +191,35 @@ class Filebrowser {
}
});
}
else if (action === 'downloadFile') {
var text = self.fs.readFileSync(itemPath).toString('binary');
var blob = new Blob([text]);
saveAs(blob, itemName);
}
else if (action === 'downloadDir') {
var zip = new JSZip();

var addChildren = function(zipNode, itemPath) {
var children = self.fs.getDirectoryChildren(itemPath);

for(var i = 0; i < children.length; i++) {
if(children[i].isDirectory){
var fol = zipNode.folder(children[i].name);
addChildren(fol, itemPath + '/' + children[i].name);
}
else{
var fileData = self.fs.readFileSync(itemPath + '/' + children[i].name).toString('binary');
zipNode.file(children[i].name, fileData);
}
}
};

addChildren(zip, itemPath);

zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, itemName + '.zip');
});
}
else if (action === 'clone') {
bootbox.dialog({
title: 'Clone a GitHub repo into \'' + itemName + '\'...',
Expand Down