Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic image pick for browser #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions www/imagepicker.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*global cordova,window,console*/
/**
* An Image Picker plugin for Cordova
*
*
* Developed by Wymsee for Sync OnSet
*/

var ImagePicker = function() {

this.hiddenInputElement = null;
};

/*
* success - success callback
* fail - error callback
* options
* .maximumImagesCount - max images to be selected, defaults to 15. If this is set to 1,
* .maximumImagesCount - max images to be selected, defaults to 15. If this is set to 1,
* upon selection of a single image, the plugin will return it.
* .width - width to resize image to (if one of height/width is 0, will resize to fit the
* other while keeping aspect ratio, if both height and width are 0, the full size
Expand All @@ -25,15 +25,35 @@ ImagePicker.prototype.getPictures = function(success, fail, options) {
if (!options) {
options = {};
}

var params = {
maximumImagesCount: options.maximumImagesCount ? options.maximumImagesCount : 15,
width: options.width ? options.width : 0,
height: options.height ? options.height : 0,
quality: options.quality ? options.quality : 100
};

return cordova.exec(success, fail, "ImagePicker", "getPictures", [params]);
if (cordova.platformId === 'browser') {
function readURL() {
var reader = new FileReader();
reader.onerror = fail;
reader.onloadend = function() {
success([reader.result]);
}
reader.readAsDataURL(event.target.files[0]);
}
if (!this.hiddenInputElement) {
this.hiddenInputElement = document.createElement('input');
this.hiddenInputElement.setAttribute('type', 'file');
this.hiddenInputElement.style.display = 'none';
document.body.appendChild(this.hiddenInputElement);
this.hiddenInputElement.addEventListener('change', readURL, true);
}
this.hiddenInputElement.click();

} else {
return cordova.exec(success, fail, 'ImagePicker', 'getPictures', [params]);
}
};

window.imagePicker = new ImagePicker();