Stackedit/res/providers/downloadProvider.js

66 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
2013-09-03 10:37:59 +00:00
"eventMgr",
"utils",
"fileMgr",
2013-06-22 23:48:57 +00:00
"classes/Provider",
2013-06-16 10:47:35 +00:00
"classes/AsyncTask"
2013-09-03 10:37:59 +00:00
], function($, eventMgr, utils, fileMgr, Provider, AsyncTask) {
2013-06-22 23:48:57 +00:00
var downloadProvider = new Provider("download");
downloadProvider.sharingAttributes = [
"url"
];
2013-05-29 19:55:23 +00:00
downloadProvider.importPublic = function(importParameters, callback) {
var title = undefined;
var content = undefined;
2013-06-16 10:47:35 +00:00
var task = new AsyncTask();
2013-05-29 19:55:23 +00:00
task.onRun(function() {
var url = importParameters.url;
var slashUrl = url.lastIndexOf("/");
if(slashUrl === -1) {
task.error(new Error("Invalid URL parameter."));
return;
}
title = url.substring(slashUrl + 1);
$.ajax({
url: DOWNLOAD_PROXY_URL + "download?url=" + url,
type: "GET",
dataType: "text",
timeout: AJAX_TIMEOUT
}).done(function(result, textStatus, jqXHR) {
content = result;
task.chain();
}).fail(function(jqXHR) {
task.error(new Error("Unable to access URL " + url));
});
});
task.onSuccess(function() {
callback(undefined, title, content);
});
task.onError(function(error) {
callback(error);
});
2013-06-16 10:47:35 +00:00
task.enqueue();
2013-05-29 19:55:23 +00:00
};
2013-09-03 10:37:59 +00:00
eventMgr.addListener("onReady", function() {
$('.action-import-url').click(function(e) {
var url = utils.getInputTextValue('#input-import-url', e);
if(url) {
downloadProvider.importPublic({
url: url
}, function(error, title, content) {
if(error) {
return;
}
var fileDesc = fileMgr.createFile(title, content);
fileMgr.selectFile(fileDesc);
});
}
});
});
2013-05-29 19:55:23 +00:00
return downloadProvider;
});