Stackedit/public/res/providers/downloadProvider.js

67 lines
2.0 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
2013-11-05 23:03:38 +00:00
"constants",
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-11-05 23:03:38 +00:00
], function($, constants, 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) {
2013-11-05 23:03:38 +00:00
var title;
var content;
2013-10-13 19:58:42 +00:00
var task = new AsyncTask(true);
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({
2013-11-05 23:03:38 +00:00
url: constants.DOWNLOAD_PROXY_URL + "download?url=" + url,
2013-05-29 19:55:23 +00:00
type: "GET",
dataType: "text",
2013-11-05 23:03:38 +00:00
timeout: constants.AJAX_TIMEOUT
}).done(function(result) {
2013-05-29 19:55:23 +00:00
content = result;
task.chain();
2013-11-05 23:03:38 +00:00
}).fail(function() {
2013-05-29 19:55:23 +00:00
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;
});