Stackedit/public/res/providers/gplusProvider.js

102 lines
3.2 KiB
JavaScript
Raw Normal View History

2013-06-02 00:38:23 +00:00
define([
2013-11-05 23:03:38 +00:00
"jquery",
2013-06-02 00:38:23 +00:00
"underscore",
"utils",
2013-11-05 23:03:38 +00:00
"storage",
2013-06-22 23:48:57 +00:00
"classes/Provider",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-10 21:22:32 +00:00
"helpers/googleHelper"
2013-11-05 23:03:38 +00:00
], function($, _, utils, storage, Provider, eventMgr, googleHelper) {
2013-06-02 00:38:23 +00:00
var PROVIDER_GPLUS = "gplus";
2013-06-22 23:48:57 +00:00
var gplusProvider = new Provider(PROVIDER_GPLUS, "Google+");
2013-06-02 00:38:23 +00:00
function getThumbnailUrl(doc, size) {
2013-11-05 23:03:38 +00:00
var result;
2013-06-02 00:38:23 +00:00
_.find(doc.thumbnails, function(thumbnail) {
var found = false;
thumbnail.url.replace(/(.*\/s)\d.*?(\/[^\/]+)/, function(match, sub1, sub2) {
result = sub1 + size + sub2;
found = true;
});
return found;
});
return result;
}
2013-07-16 23:54:56 +00:00
2013-11-05 23:03:38 +00:00
var imageDoc;
2013-06-03 22:19:52 +00:00
var importImagePreferences = utils.retrieveIgnoreError(PROVIDER_GPLUS + ".importImagePreferences");
2013-11-05 23:03:38 +00:00
var importImageCallback;
2013-07-16 23:54:56 +00:00
function showImportImgDialog() {
if(!imageDoc.thumbnails) {
2013-07-30 08:46:36 +00:00
eventMgr.onError("Image " + imageDoc.name + " is not accessible.");
2013-11-05 23:03:38 +00:00
importImageCallback(true);
2013-07-16 23:54:56 +00:00
return;
}
utils.resetModalInputs();
2013-08-11 00:52:05 +00:00
$(".modal-import-image img").prop("src", getThumbnailUrl(imageDoc, 128));
2013-07-16 23:54:56 +00:00
utils.setInputValue("#input-import-image-title", imageDoc.name);
// Load preferences
if(importImagePreferences) {
utils.setInputValue("#input-import-image-size", importImagePreferences.size);
}
2013-08-11 00:52:05 +00:00
$(".modal-import-image").modal();
2013-07-16 23:54:56 +00:00
}
2013-06-02 00:38:23 +00:00
gplusProvider.importImage = function(callback) {
importImageCallback = callback;
googleHelper.picker(function(error, docs) {
if(error || docs.length === 0) {
callback(error);
return;
}
imageDoc = docs[0];
2013-07-16 23:54:56 +00:00
showImportImgDialog();
2013-08-23 23:50:14 +00:00
}, 'img');
2013-07-16 23:54:56 +00:00
};
gplusProvider.uploadImage = function(imgName, imgContent, callback) {
importImageCallback = callback;
googleHelper.uploadImg(imgName, imgContent, "default", function(error, image) {
if(error || !image) {
callback(error);
2013-06-02 00:38:23 +00:00
return;
}
2013-07-16 23:54:56 +00:00
imageDoc = {
name: imgName,
thumbnails: []
};
$(image).find("thumbnail").each(function() {
imageDoc.thumbnails.push({
url: $(this).attr("url")
});
});
showImportImgDialog();
});
2013-06-02 00:38:23 +00:00
};
2013-08-04 00:53:46 +00:00
eventMgr.addListener("onReady", function() {
2013-11-05 23:03:38 +00:00
$(".action-import-image").click(function() {
2013-06-02 00:38:23 +00:00
var size = utils.getInputIntValue("#input-import-image-size", undefined, 0) || 0;
var title = utils.getInputTextValue("#input-import-image-title");
var image = getThumbnailUrl(imageDoc, size);
if(title) {
image += ' \"' + title + '"';
}
importImageCallback(undefined, image);
// Store import preferences for next time
2013-06-03 22:19:52 +00:00
importImagePreferences = {};
2013-06-02 00:38:23 +00:00
if(size) {
importImagePreferences.size = size;
}
2013-11-05 23:03:38 +00:00
storage[PROVIDER_GPLUS + ".importImagePreferences"] = JSON.stringify(importImagePreferences);
2013-06-02 00:38:23 +00:00
});
});
return gplusProvider;
});