You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
6.2 KiB

  1. function SocialSharing() {
  2. }
  3. // Override this method (after deviceready) to set the location where you want the iPad popup arrow to appear.
  4. // If not overridden with different values, the popup is not used. Example:
  5. //
  6. // window.plugins.socialsharing.iPadPopupCoordinates = function() {
  7. // return "100,100,200,300";
  8. // };
  9. SocialSharing.prototype.iPadPopupCoordinates = function () {
  10. // left,top,width,height
  11. return "-1,-1,-1,-1";
  12. };
  13. SocialSharing.prototype.setIPadPopupCoordinates = function (coords) {
  14. // left,top,width,height
  15. cordova.exec(function() {}, this._getErrorCallback(function() {}, "setIPadPopupCoordinates"), "SocialSharing", "setIPadPopupCoordinates", [coords]);
  16. };
  17. SocialSharing.prototype.available = function (callback) {
  18. cordova.exec(function (avail) {
  19. callback(avail ? true : false);
  20. }, null, "SocialSharing", "available", []);
  21. };
  22. // this is the recommended way to share as it is the most feature-rich with respect to what you pass in and get back
  23. SocialSharing.prototype.shareWithOptions = function (options, successCallback, errorCallback) {
  24. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareWithOptions"), "SocialSharing", "shareWithOptions", [options]);
  25. };
  26. SocialSharing.prototype.share = function (message, subject, fileOrFileArray, url, successCallback, errorCallback) {
  27. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "share"), "SocialSharing", "share", [message, subject, this._asArray(fileOrFileArray), url]);
  28. };
  29. SocialSharing.prototype.shareViaTwitter = function (message, file /* multiple not allowed by twitter */, url, successCallback, errorCallback) {
  30. var fileArray = this._asArray(file);
  31. var ecb = this._getErrorCallback(errorCallback, "shareViaTwitter");
  32. if (fileArray.length > 1) {
  33. ecb("shareViaTwitter supports max one file");
  34. } else {
  35. cordova.exec(successCallback, ecb, "SocialSharing", "shareViaTwitter", [message, null, fileArray, url]);
  36. }
  37. };
  38. SocialSharing.prototype.shareViaFacebook = function (message, fileOrFileArray, url, successCallback, errorCallback) {
  39. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaFacebook"), "SocialSharing", "shareViaFacebook", [message, null, this._asArray(fileOrFileArray), url]);
  40. };
  41. SocialSharing.prototype.shareViaFacebookWithPasteMessageHint = function (message, fileOrFileArray, url, pasteMessageHint, successCallback, errorCallback) {
  42. pasteMessageHint = pasteMessageHint || "If you like you can paste a message from your clipboard";
  43. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaFacebookWithPasteMessageHint"), "SocialSharing", "shareViaFacebookWithPasteMessageHint", [message, null, this._asArray(fileOrFileArray), url, pasteMessageHint]);
  44. };
  45. SocialSharing.prototype.shareViaWhatsApp = function (message, fileOrFileArray, url, successCallback, errorCallback) {
  46. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaWhatsApp"), "SocialSharing", "shareViaWhatsApp", [message, null, this._asArray(fileOrFileArray), url, null]);
  47. };
  48. SocialSharing.prototype.shareViaWhatsAppToReceiver = function (receiver, message, fileOrFileArray, url, successCallback, errorCallback) {
  49. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaWhatsAppToReceiver"), "SocialSharing", "shareViaWhatsApp", [message, null, this._asArray(fileOrFileArray), url, receiver]);
  50. };
  51. SocialSharing.prototype.shareViaSMS = function (options, phonenumbers, successCallback, errorCallback) {
  52. var opts = options;
  53. if (typeof options == "string") {
  54. opts = {"message":options}; // for backward compatibility as the options param used to be the message
  55. }
  56. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaSMS"), "SocialSharing", "shareViaSMS", [opts, phonenumbers]);
  57. };
  58. SocialSharing.prototype.shareViaEmail = function (message, subject, toArray, ccArray, bccArray, fileOrFileArray, successCallback, errorCallback) {
  59. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaEmail"), "SocialSharing", "shareViaEmail", [message, subject, this._asArray(toArray), this._asArray(ccArray), this._asArray(bccArray), this._asArray(fileOrFileArray)]);
  60. };
  61. SocialSharing.prototype.canShareVia = function (via, message, subject, fileOrFileArray, url, successCallback, errorCallback) {
  62. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "canShareVia"), "SocialSharing", "canShareVia", [message, subject, this._asArray(fileOrFileArray), url, via]);
  63. };
  64. SocialSharing.prototype.canShareViaEmail = function (successCallback, errorCallback) {
  65. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "canShareViaEmail"), "SocialSharing", "canShareViaEmail", []);
  66. };
  67. SocialSharing.prototype.shareViaInstagram = function (message, fileOrFileArray, successCallback, errorCallback) {
  68. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareViaInstagram"), "SocialSharing", "shareViaInstagram", [message, null, this._asArray(fileOrFileArray), null]);
  69. };
  70. SocialSharing.prototype.shareVia = function (via, message, subject, fileOrFileArray, url, successCallback, errorCallback) {
  71. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "shareVia"), "SocialSharing", "shareVia", [message, subject, this._asArray(fileOrFileArray), url, via]);
  72. };
  73. SocialSharing.prototype.saveToPhotoAlbum = function (fileOrFileArray, successCallback, errorCallback) {
  74. cordova.exec(successCallback, this._getErrorCallback(errorCallback, "saveToPhotoAlbum"), "SocialSharing", "saveToPhotoAlbum", [this._asArray(fileOrFileArray)]);
  75. };
  76. SocialSharing.prototype._asArray = function (param) {
  77. if (param == null) {
  78. param = [];
  79. } else if (typeof param === 'string') {
  80. param = new Array(param);
  81. }
  82. return param;
  83. };
  84. SocialSharing.prototype._getErrorCallback = function (ecb, functionName) {
  85. if (typeof ecb === 'function') {
  86. return ecb;
  87. } else {
  88. return function (result) {
  89. console.log("The injected error callback of '" + functionName + "' received: " + JSON.stringify(result));
  90. }
  91. }
  92. };
  93. SocialSharing.install = function () {
  94. if (!window.plugins) {
  95. window.plugins = {};
  96. }
  97. window.plugins.socialsharing = new SocialSharing();
  98. return window.plugins.socialsharing;
  99. };
  100. cordova.addConstructor(SocialSharing.install);