2013-05-27 19:45:33 +00:00
|
|
|
define([
|
|
|
|
"jquery",
|
|
|
|
"underscore",
|
2013-11-05 23:03:38 +00:00
|
|
|
"constants",
|
2013-05-27 19:45:33 +00:00
|
|
|
"utils",
|
2013-07-30 08:46:36 +00:00
|
|
|
"eventMgr",
|
2013-06-10 21:22:32 +00:00
|
|
|
"fileMgr",
|
2013-06-16 10:47:35 +00:00
|
|
|
"classes/AsyncTask",
|
2013-06-22 23:48:57 +00:00
|
|
|
"classes/Provider",
|
2013-06-10 21:22:32 +00:00
|
|
|
"providers/downloadProvider",
|
|
|
|
"providers/gistProvider"
|
2013-11-05 23:03:38 +00:00
|
|
|
], function($, _, constants, utils, eventMgr, fileMgr, AsyncTask, Provider) {
|
2013-05-02 00:03:34 +00:00
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
var sharing = {};
|
|
|
|
|
|
|
|
// Create a map with providerId: providerModule
|
|
|
|
var providerMap = _.chain(arguments).map(function(argument) {
|
2013-06-22 23:48:57 +00:00
|
|
|
return argument instanceof Provider && [
|
2013-05-29 19:55:23 +00:00
|
|
|
argument.providerId,
|
|
|
|
argument
|
|
|
|
];
|
|
|
|
}).compact().object().value();
|
|
|
|
|
2013-08-04 00:53:46 +00:00
|
|
|
// Listen to offline status changes
|
|
|
|
var isOffline = false;
|
|
|
|
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
|
|
|
isOffline = isOfflineParam;
|
|
|
|
});
|
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
sharing.createLink = function(attributes, callback) {
|
2013-05-29 23:16:15 +00:00
|
|
|
var provider = providerMap[attributes.provider.providerId];
|
2013-05-29 19:55:23 +00:00
|
|
|
// Don't create link if link already exists or provider is not
|
|
|
|
// compatible for sharing
|
2013-11-05 23:03:38 +00:00
|
|
|
if(attributes.sharingLink !== undefined || provider === undefined ||
|
2013-05-29 19:55:23 +00:00
|
|
|
// Or document is not published in markdown format
|
2013-11-05 23:03:38 +00:00
|
|
|
attributes.format != "markdown") {
|
2013-05-29 19:55:23 +00:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2013-06-16 10:47:35 +00:00
|
|
|
var task = new AsyncTask();
|
2013-11-05 23:03:38 +00:00
|
|
|
var shortUrl;
|
2013-05-29 19:55:23 +00:00
|
|
|
task.onRun(function() {
|
2013-08-04 00:53:46 +00:00
|
|
|
if(isOffline === true) {
|
2013-05-29 19:55:23 +00:00
|
|
|
task.chain();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var url = [
|
2013-11-05 23:03:38 +00:00
|
|
|
constants.MAIN_URL,
|
2013-10-19 22:59:17 +00:00
|
|
|
'viewer#!provider=',
|
2013-05-29 23:16:15 +00:00
|
|
|
provider.providerId
|
2013-05-29 19:55:23 +00:00
|
|
|
];
|
|
|
|
_.each(provider.sharingAttributes, function(attributeName) {
|
|
|
|
url.push('&');
|
|
|
|
url.push(attributeName);
|
|
|
|
url.push('=');
|
|
|
|
url.push(encodeURIComponent(attributes[attributeName]));
|
|
|
|
});
|
|
|
|
url = url.join("");
|
|
|
|
$.getJSON("https://api-ssl.bitly.com/v3/shorten", {
|
2013-11-05 23:03:38 +00:00
|
|
|
"access_token": constants.BITLY_ACCESS_TOKEN,
|
2013-05-29 19:55:23 +00:00
|
|
|
"longUrl": url
|
|
|
|
}, function(response) {
|
|
|
|
if(response.data) {
|
|
|
|
shortUrl = response.data.url;
|
|
|
|
attributes.sharingLink = shortUrl;
|
|
|
|
}
|
|
|
|
else {
|
2013-07-30 08:46:36 +00:00
|
|
|
eventMgr.onError("An error occured while creating sharing link.");
|
2013-05-29 19:55:23 +00:00
|
|
|
attributes.sharingLink = url;
|
|
|
|
}
|
|
|
|
task.chain();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function onFinish() {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
task.onSuccess(onFinish);
|
|
|
|
task.onError(onFinish);
|
2013-06-16 10:47:35 +00:00
|
|
|
task.enqueue();
|
2013-05-29 19:55:23 +00:00
|
|
|
};
|
|
|
|
|
2013-08-04 00:53:46 +00:00
|
|
|
eventMgr.addListener("onReady", function() {
|
2013-11-05 23:03:38 +00:00
|
|
|
if(window.viewerMode === false) {
|
2013-05-29 19:55:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check parameters to see if we have to download a shared document
|
|
|
|
var providerId = utils.getURLParameter("provider");
|
|
|
|
if(providerId === undefined) {
|
|
|
|
providerId = "download";
|
|
|
|
}
|
|
|
|
var provider = providerMap[providerId];
|
|
|
|
if(provider === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var importParameters = {};
|
|
|
|
_.each(provider.sharingAttributes, function(attributeName) {
|
|
|
|
var parameter = utils.getURLParameter(attributeName);
|
|
|
|
if(!parameter) {
|
|
|
|
importParameters = undefined;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
importParameters[attributeName] = parameter;
|
|
|
|
});
|
|
|
|
if(importParameters === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-03 17:28:30 +00:00
|
|
|
$("#preview-contents, .navbar .file-title-navbar").hide();
|
2013-05-29 19:55:23 +00:00
|
|
|
provider.importPublic(importParameters, function(error, title, content) {
|
2013-09-03 17:28:30 +00:00
|
|
|
$("#preview-contents, .navbar .file-title-navbar").show();
|
2013-05-29 19:55:23 +00:00
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var fileDesc = fileMgr.createFile(title, content, undefined, true);
|
|
|
|
fileMgr.selectFile(fileDesc);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return sharing;
|
2013-05-02 00:03:34 +00:00
|
|
|
});
|