better error messages, more robust csv uploading
This commit is contained in:
@@ -45,12 +45,12 @@ onmessage = function(message) {
|
||||
var docs = parseCSV(message.data.data);
|
||||
|
||||
var req = new XMLHttpRequest();
|
||||
|
||||
|
||||
req.onprogress = req.upload.onprogress = function(e) {
|
||||
if(e.lengthComputable) postMessage(JSON.stringify({ percent: (e.loaded / e.total) * 100 }));
|
||||
if(e.lengthComputable) postMessage({ percent: (e.loaded / e.total) * 100 });
|
||||
};
|
||||
|
||||
req.onreadystatechange = function() { if (req.readyState == 4) postMessage(JSON.stringify( {done: true} )) };
|
||||
req.onreadystatechange = function() { if (req.readyState == 4) postMessage({done: true, response: req.responseText}) };
|
||||
req.open('POST', message.data.url);
|
||||
req.setRequestHeader('Content-Type', 'application/json');
|
||||
req.send(JSON.stringify({docs: docs}));
|
||||
|
||||
@@ -90,7 +90,14 @@ var costco = function() {
|
||||
if(!docs.length) dfd.resolve("Failed: No docs specified");
|
||||
couch.request({url: app.baseURL + "api/_bulk_docs", type: "POST", data: JSON.stringify({docs: docs})})
|
||||
.then(
|
||||
function(resp) {ensureCommit().then(function() { dfd.resolve(resp) })},
|
||||
function(resp) {ensureCommit().then(function() {
|
||||
var error = couch.responseError(resp);
|
||||
if (error) {
|
||||
dfd.reject(error);
|
||||
} else {
|
||||
dfd.resolve(resp);
|
||||
}
|
||||
})},
|
||||
function(err) { dfd.reject(err.responseText) }
|
||||
);
|
||||
return dfd.promise();
|
||||
@@ -119,25 +126,29 @@ var costco = function() {
|
||||
data: event.target.result
|
||||
};
|
||||
var worker = new Worker('script/costco-csv-worker.js');
|
||||
worker.onmessage = function(message) {
|
||||
message = JSON.parse(message.data);
|
||||
console.log(message)
|
||||
|
||||
if (message.done) {
|
||||
util.hide('dialog');
|
||||
util.notify("Data uploaded successfully!");
|
||||
recline.initializeTable(app.offset);
|
||||
} else if (message.percent) {
|
||||
if (message.percent === 100) {
|
||||
util.notify("Waiting for CouchDB...", {persist: true, loader: true})
|
||||
} else {
|
||||
util.notify("Uploading... " + message.percent + "%");
|
||||
}
|
||||
} else {
|
||||
util.notify(JSON.stringify(message));
|
||||
}
|
||||
};
|
||||
worker.postMessage(payload);
|
||||
worker.onmessage = function(event) {
|
||||
var message = event.data;
|
||||
if (message.done) {
|
||||
var error = couch.responseError(JSON.parse(message.response))
|
||||
console.log('e',error)
|
||||
if (error) {
|
||||
app.emitter.emit(error, 'error');
|
||||
} else {
|
||||
util.notify("Data uploaded successfully!");
|
||||
recline.initializeTable(app.offset);
|
||||
}
|
||||
util.hide('dialog');
|
||||
} else if (message.percent) {
|
||||
if (message.percent === 100) {
|
||||
util.notify("Waiting for CouchDB...", {persist: true, loader: true})
|
||||
} else {
|
||||
util.notify("Uploading... " + message.percent + "%");
|
||||
}
|
||||
} else {
|
||||
util.notify(JSON.stringify(message));
|
||||
}
|
||||
};
|
||||
worker.postMessage(payload);
|
||||
};
|
||||
} else {
|
||||
util.notify('File not selected. Please try again');
|
||||
|
||||
19
attachments/script/deps-min.js
vendored
Normal file
19
attachments/script/deps-min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -10,9 +10,32 @@
|
||||
url: "/"
|
||||
};
|
||||
|
||||
couch.errors = {
|
||||
forbidden: "You aren't allowed to do that."
|
||||
}
|
||||
|
||||
couch.responseError = function(response) {
|
||||
if(_.isArray(response) && (response.length > 0) ) response = response[0];
|
||||
if (response.error) return couch.errors[response.error];
|
||||
}
|
||||
|
||||
couch.request = function(opts) {
|
||||
var ajaxOpts = $.extend({}, defaults, opts);
|
||||
return $.ajax(ajaxOpts).promise();
|
||||
var ajaxOpts = $.extend({}, defaults, opts)
|
||||
, dfd = $.Deferred()
|
||||
;
|
||||
|
||||
$.ajax(ajaxOpts).then(
|
||||
function(successResponse) {
|
||||
var error = couch.responseError(successResponse);
|
||||
if (error) app.emitter.emit(error, 'error');
|
||||
dfd.resolve(successResponse);
|
||||
},
|
||||
function(errorResponse) {
|
||||
app.emitter.emit("Fatal XHR Error", 'error');
|
||||
}
|
||||
)
|
||||
|
||||
return dfd.promise();
|
||||
}
|
||||
|
||||
couch.get = function(url) {
|
||||
|
||||
@@ -122,7 +122,12 @@ var recline = function() {
|
||||
}
|
||||
|
||||
function getPageSize() {
|
||||
return parseInt($(".viewpanel-pagesize .selected").text());
|
||||
var pagination = $(".viewpanel-pagesize .selected");
|
||||
if (pagination.length > 0) {
|
||||
return parseInt(pagination.text())
|
||||
} else {
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
|
||||
function fetchRows(id, skip) {
|
||||
@@ -158,9 +163,9 @@ var recline = function() {
|
||||
)
|
||||
}
|
||||
|
||||
function getDbInfo() {
|
||||
function getDbInfo(url) {
|
||||
var dfd = $.Deferred();
|
||||
return couch.request({url: app.baseURL + "api"}).then(function(dbInfo) {
|
||||
return couch.request({url: url}).then(function(dbInfo) {
|
||||
app.dbInfo = dbInfo;
|
||||
|
||||
$.extend(app.dbInfo, {
|
||||
@@ -176,18 +181,15 @@ var recline = function() {
|
||||
}
|
||||
|
||||
|
||||
function bootstrap() {
|
||||
util.registerEmitter();
|
||||
function bootstrap(id) {
|
||||
app.dbPath = app.baseURL + "api/";
|
||||
|
||||
util.listenFor(['esc', 'return']);
|
||||
|
||||
getDbInfo().then(function( dbInfo ) {
|
||||
|
||||
util.render('tableContainer', app.container);
|
||||
util.render('title', 'project-title', app.dbInfo);
|
||||
getDbInfo(app.dbPath).then(function( dbInfo ) {
|
||||
util.render('title', 'project-title', dbInfo);
|
||||
util.render( 'generating', 'project-actions' );
|
||||
|
||||
updateDocCount(app.dbInfo.doc_count);
|
||||
|
||||
|
||||
couch.session().then(function(session) {
|
||||
if ( session.userCtx.name ) {
|
||||
var text = "Sign out";
|
||||
@@ -196,24 +198,26 @@ var recline = function() {
|
||||
}
|
||||
util.render('controls', 'project-controls', {text: text});
|
||||
})
|
||||
|
||||
|
||||
initializeTable();
|
||||
})
|
||||
}
|
||||
|
||||
function initializeTable(offset) {
|
||||
util.render( 'tableContainer', 'right-panel' );
|
||||
showDialog('busy');
|
||||
couch.request({url: app.baseURL + 'api/headers'}).then(function ( headers ) {
|
||||
couch.request({url: app.dbPath + 'headers'}).then(function ( headers ) {
|
||||
util.hide('dialog');
|
||||
getDbInfo().then(function(dbInfo) {
|
||||
getDbInfo(app.dbPath).then(function(dbInfo) {
|
||||
updateDocCount(dbInfo.doc_count);
|
||||
});
|
||||
app.headers = headers;
|
||||
app.csvUrl = app.baseURL + 'api/csv?headers=' + escape(JSON.stringify(headers));
|
||||
app.csvUrl = app.dbPath + 'csv?headers=' + escape(JSON.stringify(headers));
|
||||
util.render( 'actions', 'project-actions', $.extend({}, app.dbInfo, {url: app.csvUrl}) );
|
||||
fetchRows(false, offset);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
formatDiskSize: formatDiskSize,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
var app = {
|
||||
baseURL: util.getBaseURL(document.location.pathname),
|
||||
container: 'main_content'
|
||||
container: 'main_content',
|
||||
emitter: util.registerEmitter()
|
||||
};
|
||||
|
||||
app.handler = function(route) {
|
||||
@@ -227,7 +228,6 @@ app.after = {
|
||||
},
|
||||
function (err) {
|
||||
util.hide('dialog');
|
||||
util.notify("Error uploading: " + err.responseText);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
@@ -246,6 +246,10 @@ app.sammy = $.sammy(function () {
|
||||
});
|
||||
|
||||
$(function() {
|
||||
app.emitter.on('error', function(error) {
|
||||
util.notify("Server error: " + error);
|
||||
})
|
||||
|
||||
util.traverse = require('traverse');
|
||||
app.sammy.run();
|
||||
})
|
||||
@@ -32,7 +32,7 @@ var util = function() {
|
||||
};
|
||||
};
|
||||
MicroEvent.mixin(Emitter);
|
||||
app.emitter = new Emitter();
|
||||
return new Emitter();
|
||||
}
|
||||
|
||||
function listenFor(keys) {
|
||||
|
||||
Reference in New Issue
Block a user