From 32df3c0cbd78a77478eec853c964e8042661cf41 Mon Sep 17 00:00:00 2001 From: SansWord Date: Thu, 26 Dec 2013 11:42:10 +0800 Subject: [PATCH 1/3] Add saveDataStringAsImageDataToLibrary api --- www/Canvas2ImagePlugin.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/www/Canvas2ImagePlugin.js b/www/Canvas2ImagePlugin.js index e46b6db..97ae329 100644 --- a/www/Canvas2ImagePlugin.js +++ b/www/Canvas2ImagePlugin.js @@ -7,12 +7,11 @@ // MIT Licensed // - module.exports = { - - saveImageDataToLibrary:function(successCallback, failureCallback, canvasId) { +module.exports = (function() { + function saveImageDataToLibrary(successCallback, failureCallback, canvasId) { // successCallback required if (typeof successCallback != "function") { - console.log("Canvas2ImagePlugin Error: successCallback is not a function"); + console.log("Canvas2ImagePlugin Error: successCallback is not a function"); } else if (typeof failureCallback != "function") { console.log("Canvas2ImagePlugin Error: failureCallback is not a function"); @@ -20,8 +19,18 @@ else { var canvas = (typeof canvasId === "string") ? document.getElementById(canvasId) : canvasId; var imageData = canvas.toDataURL().replace(/data:image\/png;base64,/,''); - return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); + return saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData); } } - }; + + function saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData) { + return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); + } + + return { + saveImageDataToLibrary: saveImageDataToLibrary, + saveDataStringAsImageDataToLibrary: saveDataStringAsImageDataToLibrary + } + +})(); From 75e98aaaa147b5583158c9f24df22a3d6a65a74c Mon Sep 17 00:00:00 2001 From: Yvonne Yu Date: Thu, 26 Dec 2013 12:05:28 +0800 Subject: [PATCH 2/3] Add validation to call back functions --- www/Canvas2ImagePlugin.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/www/Canvas2ImagePlugin.js b/www/Canvas2ImagePlugin.js index 97ae329..81411b5 100644 --- a/www/Canvas2ImagePlugin.js +++ b/www/Canvas2ImagePlugin.js @@ -8,15 +8,25 @@ // module.exports = (function() { - function saveImageDataToLibrary(successCallback, failureCallback, canvasId) { - // successCallback required + + function validateCallBacks(successCallback, failureCallback){ + + var isValid = true; + if (typeof successCallback != "function") { - console.log("Canvas2ImagePlugin Error: successCallback is not a function"); + console.log("Canvas2ImagePlugin Error: successCallback is not a function"); + isValid = false; } else if (typeof failureCallback != "function") { console.log("Canvas2ImagePlugin Error: failureCallback is not a function"); + isValid = false; } - else { + return isValid; + } + + function saveImageDataToLibrary(successCallback, failureCallback, canvasId) { + // successCallback required + if (validateCallBacks(successCallback, failureCallback)) { var canvas = (typeof canvasId === "string") ? document.getElementById(canvasId) : canvasId; var imageData = canvas.toDataURL().replace(/data:image\/png;base64,/,''); return saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData); @@ -24,7 +34,10 @@ module.exports = (function() { } function saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData) { - return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); + if (validateCallBacks(successCallback, failureCallback)) { + return cordova.exec(successCallback, failureCallback, + "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); + } } return { From a6b4e32b22cbd55a4003cd25309c259ef31c28dd Mon Sep 17 00:00:00 2001 From: Yvonne Yu Date: Fri, 7 Feb 2014 15:42:03 +0800 Subject: [PATCH 3/3] fixed bugs for Android --- www/Canvas2ImagePlugin.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/www/Canvas2ImagePlugin.js b/www/Canvas2ImagePlugin.js index 81411b5..07816af 100644 --- a/www/Canvas2ImagePlugin.js +++ b/www/Canvas2ImagePlugin.js @@ -1,3 +1,5 @@ +cordova.define("org.devgeeks.Canvas2ImagePlugin.Canvas2ImagePlugin", function(require, exports, module) { + // // Canvas2ImagePlugin.js // Canvas2ImagePlugin PhoneGap/Cordova plugin @@ -33,17 +35,20 @@ module.exports = (function() { } } - function saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData) { + function saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData) { if (validateCallBacks(successCallback, failureCallback)) { return cordova.exec(successCallback, failureCallback, - "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); + "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]); } } - return { + return { saveImageDataToLibrary: saveImageDataToLibrary, saveDataStringAsImageDataToLibrary: saveDataStringAsImageDataToLibrary } - -})(); - + +})() + +}); + +