Skip to content

Instantly share code, notes, and snippets.

@Studira
Created January 30, 2017 08:40
Show Gist options
  • Save Studira/88db9a7967e040553486b4518661dc24 to your computer and use it in GitHub Desktop.
Save Studira/88db9a7967e040553486b4518661dc24 to your computer and use it in GitHub Desktop.
;(function () {
'use strict';
/**
* @name Camera Device
* @description Directive to add camera functionality to the html page
*/
var module = angular.module( 'Camera' );
var injectables = [ '$window', '$parse', 'lodash', '$cordovaCamera', 'camera.fctr', 'notify.fctr', 'Camera.cnst', '$spinner', '$ionic' ];
function directive ( $window, $parse, _, Camera, Service, Notify, Constant, $spinner,$ionic ) {
function link ( $scope, $elem, $attrs ) {
$ionic.Platform.ready( function () {
// Private Scope
// ==============================
var PLUGIN = $window.Camera || null;
var PLUGIN_READY = _.isObject( PLUGIN ) && !_.isEmpty( PLUGIN );
var GET_IMAGE = _.has( Camera, 'getPicture' ) ? Camera.getPicture : null;
var GLOBAL_CONFIG = $scope.config;
var GLOBAL_DATA = $scope.data;
var IMAGE_TYPE = GLOBAL_CONFIG.image.type || 'jpg';
var IMAGE_TYPE_ID = Service.getImgTypeId( IMAGE_TYPE );
var CACHE_IMAGE = Service.saveImgToLocal;
var REMOVE_CACHED_IMAGE = Service.removeImgFromLocal;
var NOTIFY_USER = Notify.show;
var SPINNER = _.bind($spinner.set, $spinner);
if ( PLUGIN_READY ) {
var DEVICE_OPTIONS = {
quality: 30,
destinationType: PLUGIN.DestinationType.DATA_URL,
sourceType: PLUGIN.PictureSourceType.CAMERA,
allowEdit: false,
encodingType: IMAGE_TYPE_ID,
targetWidth: Constant.camera.image.size.width,
targetHeight: Constant.camera.image.size.height,
saveToPhotoAlbum: true,
}
// Public Scope
// ==============================
$scope.takePhoto = function () {
if ( PLUGIN_READY && GET_IMAGE ) {
SPINNER( true, GLOBAL_CONFIG.secondaryColor ? true : null );
return GET_IMAGE( DEVICE_OPTIONS )
.then( function( image ){
image = {
type: IMAGE_TYPE,
typeId: IMAGE_TYPE_ID,
src: "data:image/"
+ _.toLower( IMAGE_TYPE )
+ ";base64,"
+ image,
context: 'camera'
}
return CACHE_IMAGE( image );
})
.then( function ( image ) {
$scope.imageStyleCamera = { 'background-image':'url('
+ image.src
+ ')','background-repeat':'no-repeat','background-size':'cover' }
$scope.imageCamera = true;
SPINNER( false );
$scope.$emit( 'camera:image:captured', image )
NOTIFY_USER( 'Photo captured','default' );
return image;
})
.catch( function ( err ) {
SPINNER( false );
console.log( 'Error: ', err );
return err;
})
}
}
$scope.removeImageCamera = function () {
REMOVE_CACHED_IMAGE();
$scope.imageStyleCamera = null;
$scope.imageCamera = false;
}
// Event Listeners
// ==============================
$scope.$on( 'camera:image:remove', function () {
$scope.removeImageCamera();
})
}
}, false )
}
return ({
scope: {
config: '=',
data: '='
},
restrict: 'A',
replace: true,
templateUrl: 'app/components/camera/tpl/camera.cameraDevice.html',
link: link,
});
}
directive.$inject = injectables;
module.directive( 'cameraDevice', directive );
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment