2013-05-27 19:45:33 +00:00
|
|
|
define([
|
2014-09-25 23:25:01 +00:00
|
|
|
"jquery",
|
|
|
|
"underscore",
|
|
|
|
"constants",
|
|
|
|
"utils",
|
|
|
|
"eventMgr",
|
|
|
|
"fileMgr",
|
|
|
|
"classes/AsyncTask",
|
|
|
|
"classes/Provider",
|
2014-10-12 17:57:58 +00:00
|
|
|
"providers/couchdbProvider",
|
2014-09-25 23:25:01 +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
|
|
|
|
2014-09-25 23:25:01 +00:00
|
|
|
var sharing = {};
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2014-09-25 23:25:01 +00:00
|
|
|
// Create a map with providerId: providerModule
|
|
|
|
var providerMap = _.chain(arguments).map(function(argument) {
|
|
|
|
return argument instanceof Provider && [
|
|
|
|
argument.providerId,
|
|
|
|
argument
|
|
|
|
];
|
|
|
|
}).compact().object().value();
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2014-09-25 23:25:01 +00:00
|
|
|
// Listen to offline status changes
|
|
|
|
var isOffline = false;
|
|
|
|
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
|
|
|
isOffline = isOfflineParam;
|
|
|
|
});
|
2013-08-04 00:53:46 +00:00
|
|
|
|
2014-09-28 18:34:30 +00:00
|
|
|
sharing.getEditorParams = function(attributes) {
|
2014-10-12 11:14:03 +00:00
|
|
|
var provider = attributes.provider;
|
2014-09-28 18:34:30 +00:00
|
|
|
var params = {
|
|
|
|
provider: provider.providerId
|
|
|
|
};
|
|
|
|
if(!provider.editorSharingAttributes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.each(provider.editorSharingAttributes, function(attributeName) {
|
|
|
|
params[attributeName] = attributes[attributeName];
|
|
|
|
});
|
|
|
|
return params;
|
|
|
|
};
|
|
|
|
|
|
|
|
sharing.getViewerParams = function(attributes) {
|
2014-10-12 11:14:03 +00:00
|
|
|
var provider = attributes.provider;
|
|
|
|
// If document is not published in markdown format
|
|
|
|
if(attributes.format != "markdown") {
|
2014-09-25 23:25:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-28 18:34:30 +00:00
|
|
|
var params = {
|
|
|
|
provider: provider.providerId
|
|
|
|
};
|
|
|
|
if(!provider.viewerSharingAttributes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.each(provider.viewerSharingAttributes, function(attributeName) {
|
|
|
|
params[attributeName] = attributes[attributeName];
|
2014-09-25 23:25:01 +00:00
|
|
|
});
|
2014-09-28 18:34:30 +00:00
|
|
|
return params;
|
2014-09-25 23:25:01 +00:00
|
|
|
};
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2014-09-25 23:25:01 +00:00
|
|
|
eventMgr.addListener("onReady", function() {
|
|
|
|
// Check parameters to see if we have to download a shared document
|
2014-10-12 17:57:58 +00:00
|
|
|
var importParameters, provider, providerId = utils.getURLParameter("provider");
|
2014-09-28 18:34:30 +00:00
|
|
|
if(window.viewerMode) {
|
|
|
|
if(providerId === undefined) {
|
|
|
|
providerId = "download";
|
|
|
|
}
|
2014-10-12 17:57:58 +00:00
|
|
|
provider = providerMap[providerId];
|
2014-09-28 18:34:30 +00:00
|
|
|
if(provider === undefined) {
|
2014-09-25 23:25:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-12 17:57:58 +00:00
|
|
|
importParameters = {};
|
|
|
|
if(_.some(provider.viewerSharingAttributes, function(attributeName) {
|
2014-09-28 18:34:30 +00:00
|
|
|
var parameter = utils.getURLParameter(attributeName);
|
|
|
|
if(!parameter) {
|
2014-10-12 17:57:58 +00:00
|
|
|
return 1;
|
2014-09-28 18:34:30 +00:00
|
|
|
}
|
|
|
|
importParameters[attributeName] = parameter;
|
2014-10-12 17:57:58 +00:00
|
|
|
})) {
|
2014-09-25 23:25:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-12 17:57:58 +00:00
|
|
|
provider.importPublic(importParameters, utils.lockUI(function(error, title, content) {
|
2014-09-28 18:34:30 +00:00
|
|
|
if(error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var fileDesc = fileMgr.createFile(title, content, undefined, undefined, true);
|
|
|
|
fileMgr.selectFile(fileDesc);
|
2014-10-12 17:57:58 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
else if(providerId) {
|
|
|
|
provider = providerMap[providerId];
|
|
|
|
if(provider === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
importParameters = {};
|
|
|
|
if(_.some(provider.editorSharingAttributes, function(attributeName) {
|
|
|
|
var parameter = utils.getURLParameter(attributeName);
|
|
|
|
if(!parameter) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
importParameters[attributeName] = parameter;
|
|
|
|
})) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
provider.importPrivate(importParameters, utils.lockUI());
|
2014-09-28 18:34:30 +00:00
|
|
|
}
|
2014-09-25 23:25:01 +00:00
|
|
|
});
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2014-10-12 11:14:03 +00:00
|
|
|
eventMgr.onSharingCreated(sharing);
|
2014-09-25 23:25:01 +00:00
|
|
|
return sharing;
|
2014-03-28 00:49:49 +00:00
|
|
|
});
|