Skip to content

Commit

Permalink
add file via gist, #170
Browse files Browse the repository at this point in the history
  • Loading branch information
mapsam committed Sep 13, 2015
1 parent 21feca3 commit 8b90c5e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/js/dropzone.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var dropchop = (function(dc) {
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = function() {
$(dc.layers).trigger('file:added', [file, JSON.parse(reader.result)]);
$(dc.layers).trigger('file:added', [file.name, JSON.parse(reader.result)]);
};
};

Expand Down
5 changes: 5 additions & 0 deletions src/js/form.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ var dropchop = (function(dc) {
dc.form = dc.form || {};
dc.form.init = function() {
$(dc.form).on('form:geo', dc.form.geo);
$(dc.form).on('form:file', dc.form.file);
};

dc.form.geo = function(event, operation) {
dc.form.create(operation, dc.ops.geo[operation], 'geo');
};

dc.form.file = function(event, operation) {
dc.form.create(operation, dc.ops.file[operation], 'file:' + operation);
};

dc.form.create = function(name, info, trigger) {
dc.form.remove();
var $html = $('<form>').addClass('dropchop-form dropchop-form-'+trigger)
Expand Down
14 changes: 7 additions & 7 deletions src/js/layers.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var dropchop = (function(dc) {
* @param {object} file object
* @param {string} file blob to be converted with JSON.parse()
*/
dc.layers.add = function(event, file, blob) {
var l = _makeLayer(file, blob);
dc.layers.add = function(event, name, blob) {
var l = _makeLayer(name, blob);
dc.layers.list[l.stamp] = l;

dc.notify('success', l.name + ' has been added!', 5000);
Expand Down Expand Up @@ -47,15 +47,15 @@ var dropchop = (function(dc) {

};

function _makeLayer(f, b) {
var fl = L.mapbox.featureLayer(b);
function _makeLayer(name, json) {
var fl = L.mapbox.featureLayer(json);

var layer = {
name: dc.util.removeFileExtension(f.name),
name: dc.util.removeFileExtension(name),
stamp: L.stamp(fl),
raw: b,
raw: json,
featurelayer: fl,
dateAdded: f.lastModifiedDate
dateAdded: new Date()
};
return layer;
}
Expand Down
9 changes: 2 additions & 7 deletions src/js/ops.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var dropchop = (function(dc) {
$(dc.ops).on('layer:selected', dc.ops.geoCheck);
$(dc.ops).on('layer:unselected', dc.ops.geoCheck);
$(dc.ops).on('operation:geo', dc.ops.geoExecute);
$(dc.ops).on('operation:file:load-gist', dc.ops.file['load-gist'].get);

// setup ops file
var leftMenu = $('<div>').addClass('dropchop-menu-left');
Expand Down Expand Up @@ -89,14 +90,8 @@ var dropchop = (function(dc) {
dc.notify('error', err);
throw err;
}


var newFile = {
name: prep.name,
lastModifiedDate: new Date()
};

$(dc.layers).trigger('file:added', [newFile, result]);
$(dc.layers).trigger('file:added', [prep.name, result]);
};

/**
Expand Down
38 changes: 29 additions & 9 deletions src/js/ops.file.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,35 @@ var dropchop = (function(dc) {
},

'load-gist': {
description: 'Import files from Gist',
icon: '<i class="fa fa-github"></i>',
parameters: [
{
name: 'gist',
description :'Gist ID or URL',
type: 'text',
},
],
description: 'Import files from Gist',
icon: '<i class="fa fa-github"></i>',
parameters: [
{
name: 'gist',
description :'Gist ID or URL',
type: 'text',
},
],
execute: function() {
$(dc.form).trigger('form:file', ['load-gist']);
},
get: function(event, name, parameters) {
var gist = parameters[0].split('/')[parameters[0].split('/').length-1];
var url = 'https://api.github.com/gists/' + gist;
dc.util.xhr(url, this.file['load-gist'].callback);
},
callback: function(xhr, xhrEvent) {
if (xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
for (var filename in data.files) {
var file = data.files[filename];
$(dc.layers).trigger('file:added', [file.filename, JSON.parse(file.content)]);
}
// dc.notify('success', 'Succesfully retrieved gist')
} else {
dc.notify('error', xhr.status + ': could not retrieve Gist. Please check your URL');
}
}
},

'break1': { type: 'break' },
Expand Down
11 changes: 11 additions & 0 deletions src/js/util.dropchop.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ var dropchop = (function(dc) {
return string;
};

dc.util.xhr = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', encodeURI(url));
xhr.onload = callback.bind(this, xhr);
xhr.onerror = function( xhr ) {
console.error(xhr);
dc.notify('error', 'Unable to access ' + url, 2500);
};
xhr.send();
};

return dc;


Expand Down

0 comments on commit 8b90c5e

Please sign in to comment.