Stackedit/public/res/providers/downloadProvider.js

68 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"constants",
"eventMgr",
"utils",
"fileMgr",
"classes/Provider",
"classes/AsyncTask"
2013-11-05 23:03:38 +00:00
], function($, constants, eventMgr, utils, fileMgr, Provider, AsyncTask) {
var downloadProvider = new Provider("download");
2014-09-28 18:34:30 +00:00
downloadProvider.viewerSharingAttributes = [
"url"
];
2013-05-29 19:55:23 +00:00
downloadProvider.importPublic = function(importParameters, callback) {
var title;
var content;
var task = new AsyncTask(true);
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: constants.DOWNLOAD_IMPORT_URL + '?' + $.param({
url: url
}),
dataType: 'text',
timeout: constants.AJAX_TIMEOUT
}).done(function(result) {
content = result;
task.chain();
}).fail(function() {
task.error(new Error("Unable to access URL " + url));
});
});
task.onSuccess(function() {
callback(undefined, title, content);
});
task.onError(function(error) {
callback(error);
});
task.enqueue();
};
2013-05-29 19:55:23 +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-09-03 10:37:59 +00:00
return downloadProvider;
});