2013-05-25 18:13:59 +00:00
|
|
|
define(["jquery", "core", "utils", "async-runner", "download-provider", "gist-provider", "underscore"], function($, core, utils, asyncRunner) {
|
2013-05-02 00:03:34 +00:00
|
|
|
var sharing = {};
|
|
|
|
|
|
|
|
// Create a map with providerId: providerObject
|
|
|
|
var providerMap = _.chain(arguments)
|
|
|
|
.map(function(argument) {
|
|
|
|
return argument && argument.providerId && [argument.providerId, argument];
|
|
|
|
}).compact().object().value();
|
|
|
|
|
2013-05-04 00:05:58 +00:00
|
|
|
// Used to populate the "Sharing" dropdown box
|
|
|
|
var lineTemplate = ['<div class="input-prepend">',
|
|
|
|
'<a href="<%= link %>" class="add-on" title="Sharing location"><i class="icon-link"></i></a>',
|
|
|
|
'<input class="span2" type="text" value="<%= link %>" readonly />',
|
|
|
|
'</div>'].join("");
|
|
|
|
sharing.refreshDocumentSharing = function(attributesList) {
|
|
|
|
var linkList = $("#link-container .link-list").empty();
|
|
|
|
$("#link-container .no-link").show();
|
|
|
|
_.each(attributesList, function(attributes) {
|
|
|
|
if(attributes.sharingLink) {
|
|
|
|
var lineElement = $(_.template(lineTemplate, {
|
|
|
|
link: attributes.sharingLink
|
|
|
|
}));
|
|
|
|
lineElement.click(function(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
linkList.append(lineElement);
|
|
|
|
$("#link-container .no-link").hide();
|
|
|
|
}
|
2013-05-02 00:03:34 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
sharing.createLink = function(attributes, callback) {
|
|
|
|
var provider = providerMap[attributes.provider];
|
2013-05-04 00:05:58 +00:00
|
|
|
// Don't create link if link already exists or provider is not compatible for sharing
|
|
|
|
if(attributes.sharingLink !== undefined || provider === undefined
|
|
|
|
// Or document is not published in markdown format
|
|
|
|
|| attributes.format != "markdown") {
|
2013-05-02 00:03:34 +00:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
var shortUrl = undefined;
|
|
|
|
task.onRun(function() {
|
|
|
|
if(core.isOffline === true) {
|
|
|
|
task.chain();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var url = [MAIN_URL, 'viewer.html?provider=', attributes.provider];
|
|
|
|
_.each(provider.sharingAttributes, function(attributeName) {
|
|
|
|
url.push('&');
|
|
|
|
url.push(attributeName);
|
|
|
|
url.push('=');
|
2013-05-04 00:05:58 +00:00
|
|
|
url.push(encodeURIComponent(attributes[attributeName]));
|
2013-05-02 00:03:34 +00:00
|
|
|
});
|
|
|
|
$.getJSON(
|
|
|
|
"https://api-ssl.bitly.com/v3/shorten",
|
|
|
|
{
|
|
|
|
"access_token": BITLY_ACCESS_TOKEN,
|
|
|
|
"longUrl": url.join("")
|
|
|
|
},
|
|
|
|
function(response)
|
|
|
|
{
|
|
|
|
if(response.data) {
|
|
|
|
shortUrl = response.data.url;
|
2013-05-04 00:05:58 +00:00
|
|
|
attributes.sharingLink = shortUrl;
|
2013-05-02 00:03:34 +00:00
|
|
|
}
|
|
|
|
task.chain();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2013-05-04 00:05:58 +00:00
|
|
|
function onFinish() {
|
|
|
|
if(shortUrl === undefined) {
|
|
|
|
localStorage["missingSharingLink"] = true;
|
|
|
|
}
|
2013-05-02 00:03:34 +00:00
|
|
|
callback(shortUrl);
|
2013-05-04 00:05:58 +00:00
|
|
|
}
|
|
|
|
task.onSuccess(onFinish);
|
|
|
|
task.onError(onFinish);
|
2013-05-02 00:03:34 +00:00
|
|
|
asyncRunner.addTask(task);
|
|
|
|
};
|
|
|
|
|
2013-05-04 00:05:58 +00:00
|
|
|
// Create the possible missing links
|
|
|
|
function checkMissingLinks() {
|
|
|
|
if(core.isOffline === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!_.has(localStorage, "missingSharingLink")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
localStorage.removeItem("missingSharingLink");
|
2013-05-26 22:59:03 +00:00
|
|
|
var fileDescList = core.fileManager.getFileList();
|
|
|
|
_.each(fileDescList, function(fileDesc) {
|
|
|
|
_.each(fileDescList.publishLocations, function(publishAttributes, publishIndex) {
|
|
|
|
sharing.createLink(publishAttributes, function(shortUrl) {
|
2013-05-04 00:05:58 +00:00
|
|
|
if(shortUrl !== undefined) {
|
2013-05-26 22:59:03 +00:00
|
|
|
localStorage[publishIndex] = utils.serializeAttributes(attributes);
|
2013-05-04 00:05:58 +00:00
|
|
|
}
|
2013-05-02 00:03:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-05-04 00:05:58 +00:00
|
|
|
}
|
|
|
|
// Periodically check that links are not missing
|
|
|
|
if(viewerMode === false) {
|
|
|
|
core.addPeriodicCallback(checkMissingLinks);
|
|
|
|
}
|
|
|
|
|
|
|
|
core.onReady(function() {
|
|
|
|
if(viewerMode === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check parameters to see if we have to download a shared document
|
2013-05-25 18:13:59 +00:00
|
|
|
var providerId = utils.getURLParameter("provider");
|
2013-05-04 00:05:58 +00:00
|
|
|
if(providerId === undefined) {
|
2013-05-10 23:49:50 +00:00
|
|
|
providerId = "download";
|
2013-05-04 00:05:58 +00:00
|
|
|
}
|
|
|
|
var provider = providerMap[providerId];
|
|
|
|
if(provider === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var importParameters = {};
|
|
|
|
_.each(provider.sharingAttributes, function(attributeName) {
|
2013-05-25 18:13:59 +00:00
|
|
|
var parameter = utils.getURLParameter(attributeName);
|
2013-05-10 23:49:50 +00:00
|
|
|
if(!parameter) {
|
2013-05-13 23:10:02 +00:00
|
|
|
importParameters = undefined;
|
2013-05-10 23:49:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
importParameters[attributeName] = parameter;
|
2013-05-04 00:05:58 +00:00
|
|
|
});
|
2013-05-13 23:10:02 +00:00
|
|
|
if(importParameters === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-04 00:05:58 +00:00
|
|
|
$("#wmd-preview, #file-title").hide();
|
|
|
|
provider.importPublic(importParameters, function(error, title, content) {
|
|
|
|
$("#wmd-preview, #file-title").show();
|
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-26 22:59:03 +00:00
|
|
|
var fileDesc = core.fileManager.createFile(title, content, undefined, true);
|
|
|
|
core.fileManager.selectFile(fileDesc);
|
2013-05-04 00:05:58 +00:00
|
|
|
});
|
2013-05-02 00:03:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return sharing;
|
|
|
|
});
|