From 5550ba310c1bbfc3d943f9f6ce473c6bff114eac Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Thu, 12 Feb 2015 17:26:13 -0500 Subject: [PATCH 1/2] Adding windows support to share plugin --- plugin.xml | 8 +++ src/windows/SocialSharingProxy.js | 114 ++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/windows/SocialSharingProxy.js diff --git a/plugin.xml b/plugin.xml index 93000530..adf07681 100755 --- a/plugin.xml +++ b/plugin.xml @@ -72,5 +72,13 @@ + + + + + + + + diff --git a/src/windows/SocialSharingProxy.js b/src/windows/SocialSharingProxy.js new file mode 100644 index 00000000..4f97bf65 --- /dev/null +++ b/src/windows/SocialSharingProxy.js @@ -0,0 +1,114 @@ +var cordova = require('cordova'); + +module.exports = { + share: function (win, fail, args) { + //Text Message + var message = args[0]; + //Title + var subject = args[1]; + //File(s) Path + var fileOrFileArray = args[2]; + //Web link + var url = args[3]; + + var doShare = function (e) { + e.request.data.properties.title = subject?subject: "Sharing"; + if (message) e.request.data.setText(message); + if (url) e.request.data.setWebLink(new Windows.Foundation.Uri(url)); + if (fileOrFileArray.length > 0) { + var deferral = e.request.getDeferral(); + var storageItems = []; + var filesCount = fileOrFileArray.length; + for (var i = 0; i < fileOrFileArray; i++) { + Windows.Storage.StorageFile.getFileFromPathAsync(fileOrFileArray[i]).done( + function(file) { + storageItems.push(file); + if (!--filesCount) { + e.request.data.setStorageItems(storageItems); + deferral.complete(); + } + }, + function() { + if (!--filesCount) { + e.request.data.setStorageItems(storageItems); + deferral.complete(); + } + } + ); + } + } + } + + + var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView(); + + dataTransferManager.addEventListener("datarequested", doShare); + + try { + Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI(); + win(true); + } catch (err) { + fail(err); + } + }, + + canShareViaEmail: function (win, fail, args) { + win(true); + }, + + shareViaEmail: function (win, fail, args) { + //Text Message + var message = args[0]; + //Title + var subject = args[1]; + //File(s) Path + var fileOrFileArray = args[5]; + + var doShare = function (e) { + e.request.data.properties.title = subject ? subject : "Sharing"; + if (message) { + var htmlFormat = Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.createHtmlFormat(message); + e.request.data.setHtmlFormat(htmlFormat); + } + + if (fileOrFileArray.length > 0) { + var deferral = e.request.getDeferral(); + var storageItems = []; + var filesCount = fileOrFileArray.length; + for (var i = 0; i < fileOrFileArray; i++) { + Windows.Storage.StorageFile.getFileFromPathAsync(fileOrFileArray[i]).done( + function (index, file) { + var path = fileOrFileArray[index]; + var streamRef = Windows.Storage.Streams.RandomAccessStreamReference.createFromFile(file); + e.request.data.resourceMap[path] = streamRef; + storageItems.push(file); + if (!--filesCount) { + e.request.data.setStorageItems(storageItems); + deferral.complete(); + } + }.bind(this, i), + function () { + if (!--filesCount) { + e.request.data.setStorageItems(storageItems); + deferral.complete(); + } + } + ); + } + } + } + + var dataTransferManager = Windows.ApplicationModel.DataTransfer.DataTransferManager.getForCurrentView(); + + dataTransferManager.addEventListener("datarequested", doShare); + + try { + Windows.ApplicationModel.DataTransfer.DataTransferManager.showShareUI(); + win(true); + } catch (err) { + fail(err); + } + } +}; + +require("cordova/exec/proxy").add("SocialSharing", module.exports); \ No newline at end of file From 9ac1da9ac737f87f11a387b759abeb11269317a3 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Fri, 13 Feb 2015 16:43:00 -0500 Subject: [PATCH 2/2] bug fix Files sharing bug fix --- src/windows/SocialSharingProxy.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/windows/SocialSharingProxy.js b/src/windows/SocialSharingProxy.js index 4f97bf65..99e0af15 100644 --- a/src/windows/SocialSharingProxy.js +++ b/src/windows/SocialSharingProxy.js @@ -19,9 +19,9 @@ module.exports = { var deferral = e.request.getDeferral(); var storageItems = []; var filesCount = fileOrFileArray.length; - for (var i = 0; i < fileOrFileArray; i++) { + for (var i = 0; i < fileOrFileArray.length; i++) { Windows.Storage.StorageFile.getFileFromPathAsync(fileOrFileArray[i]).done( - function(file) { + function (file) { storageItems.push(file); if (!--filesCount) { e.request.data.setStorageItems(storageItems); @@ -33,7 +33,7 @@ module.exports = { e.request.data.setStorageItems(storageItems); deferral.complete(); } - } + } ); } } @@ -75,7 +75,7 @@ module.exports = { var deferral = e.request.getDeferral(); var storageItems = []; var filesCount = fileOrFileArray.length; - for (var i = 0; i < fileOrFileArray; i++) { + for (var i = 0; i < fileOrFileArray.length; i++) { Windows.Storage.StorageFile.getFileFromPathAsync(fileOrFileArray[i]).done( function (index, file) { var path = fileOrFileArray[index];