| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 | angular.module('odoo')    /**     * -----------------------------------------------------------------------------     *  Description:    Native SQL util instructions     * -----------------------------------------------------------------------------     */    .factory('sqlFactory', function () {        /**         *         */        var count = function (tableName, success, error) {            var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;            db.executeSql(sql, [], function (result) {                success(result.rows.item(0).total);            }, function (err) {                error(err);            });        };        // Execute native SQL SELECT instruction        var select = function (tableName, success, error) {            var sql = 'SELECT * FROM ' + tableName;            db.executeSql(sql, [], function (result) {                success(result.rows);            }, function (err) {                error(err);            });        };        // Execute native SQL SELECT instruction with a constraint        var selectByConstraint = function (tableName, constraint, success, error) {            var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;            db.executeSql(sql, [], function (result) {                success(result.rows);            }, function (err) {                error(err);            });        };        // Execute native SQL SELECT instruction with count instruction        var count = function (tableName, success, error) {            var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;            db.executeSql(sql, [], function (result) {                success(result.rows.item(0).total);            }, function (err) {                error(err);            });        };        return {            select: select,            selectByConstraint: selectByConstraint,            count: count        }    })    /**     * -----------------------------------------------------------------------------     *  Description:    Get user configuration from local database     * -----------------------------------------------------------------------------     */    .factory('configFactory', function (sqlFactory) {        return function (success, error) {            sqlFactory.select('user', function (users) {                if (users.length == 0) {                    success(0);                } else if (users.length == 1) {                    success(users.item(0));                } else {                    var configs = [];                    for (var i = 0; i < users.length; i++) {                        configs.push(users.item(i))                    }                    success(configs);                }            }, function (err) {                error(err);            });        };    })    /**     * -----------------------------------------------------------------------------     *  Description:    Async loop util v2     * -----------------------------------------------------------------------------     */    .factory('asyncLoopFactory', function () {        return function (iterations, func, callback) {            var index = 0;            var done = false;            var loop = {                next: function () {                    if (done) {                        return;                    }                    if (index < iterations) {                        index++;                        func(loop);                    } else {                        done = true;                        callback();                    }                },                iteration: function () {                    return index - 1;                },                break: function () {                    done = true;                    callback();                }            };            loop.next();            return loop;        }    })    /**    * -----------------------------------------------------------------------------    *  Description:    Native device functions manager    * -----------------------------------------------------------------------------    */    .factory('deviceFactory', function (        $timeout,        $rootScope,        $cordovaToast,        $cordovaCamera,        $cordovaDialogs,        $cordovaContacts,        $cordovaVibration,        $cordovaGeolocation,        $cordovaDeviceMotion,        $cordovaLaunchNavigator    ) {        var vibrateDuration = 50;        /**         *         */        var takePicture = function (success, error) {            var options = {                quality: 75,                destinationType: Camera.DestinationType.DATA_URL,                sourceType: Camera.PictureSourceType.CAMERA,                allowEdit: true,                encodingType: Camera.EncodingType.JPEG,                targetWidth: 300,                targetHeight: 300,                popoverOptions: CameraPopoverOptions,                saveToPhotoAlbum: false,                correctOrientation: true            };            $cordovaCamera.getPicture(options).then(function (imageData) {                success(imageData);            }, function (err) {                error(err);            });        }        /**         *         */        var saveContact = function (contact, success, error) {            if (!contact.mobile && !contact.phone && !contact.email) {                return error('No hay nada que guardar');            }            var info = {                name: {                    givenName: contact.name,                    familyName: '',                    formatted: ''                },                nickname: '',                phoneNumbers: [                    {                        value: contact.phone,                        type: 'phone'                    },                    {                        value: contact.mobile,                        type: 'mobile'                    }                ],                emails: [                    {                        value: contact.email,                        type: 'home'                    }                ],                addresses: [                    {                        type: 'home',                        formatted: '',                        streetAddress: contact.street,                        locality: contact.city,                        region: '',                        postalCode: '',                        country: 'Paraguay'                    }                ],                ims: null,                organizations: null,                birthday: null,                note: null,                photos: null,                categories: null,                urls: null            };            $cordovaContacts.save(info).then(function (result) {                $cordovaVibration.vibrate(vibrateDuration);                success(result);            }, function (err) {                $cordovaVibration.vibrate(vibrateDuration);                error(err);            });        }        /**         *         */        var getCurrentPosition = function (success, error) {            var options = {                timeout: 10000,                enableHighAccuracy: false            }            notify('Obteniendo localización');            $cordovaGeolocation.getCurrentPosition(options).then(function (position) {                notify('Localización obtenida con éxito');                success(position);            }, function (err) {                notify('No se pudo obtener la localización, revise si su GPS está activo', true);                error(err);            });        }        /**         *         */        var navigate = function (destination, success, error) {            if (!destination.partner_latitude || !destination.partner_longitude) {                return error('No hay destino');            }            $cordovaLaunchNavigator.navigate(destination).then(function () {                success('Navigator launched');            }, function (err) {                error(err);            });        }        /**         *         */        var vibrate = function () {            $cordovaVibration.vibrate(vibrateDuration);        }        /**         *         */        var toast = function (message, long) {            long = long || false;            $cordovaToast.show(message, long ? 'long' : 'short', 'bottom');        }        /**         *         */        var confirm = function (message, title, success) {            $cordovaDialogs.confirm(message, title, ['Aceptar', 'Cancelar']).then(function (index) {                success(index);            });        }        /**         *         */        var notify = function (message, long) {            vibrate();            toast(message, long);        }        /**         *         */        var detectShake = function () {            var previousMeasurements = {};            var options = {                frequency: 100,                deviation: 25            }            var watcher = null;            var startWatching = function () {                watcher = $cordovaDeviceMotion.watchAcceleration(options);                watcher.then(null, function (err) {                    console.log(err);                }, function (currentMeasurements) {                    evaluateShake(currentMeasurements);                });            }            var evaluateShake = function (currentMeasurements) {                var measurements = {};                if (previousMeasurements.x) {                    measurements.x = Math.abs(previousMeasurements.x, currentMeasurements.x);                    measurements.y = Math.abs(previousMeasurements.y, currentMeasurements.y);                    measurements.z = Math.abs(previousMeasurements.x, currentMeasurements.z);                }                if (measurements.x + measurements.y + measurements.z > options.deviation) {                    stopWatching();                    previousMeasurements = {};                } else {                    previousMeasurements = currentMeasurements;                }            }            var stopWatching = function () {                watcher.clearWatch();                $timeout(function () {                    startWatching();                }, 1500);                $rootScope.$broadcast('device.shaked');            }            startWatching();        }        return {            takePicture: takePicture,            saveContact: saveContact,            getCurrentPosition: getCurrentPosition,            navigate: navigate,            vibrate: vibrate,            toast: toast,            confirm: confirm,            notify: notify,            detectShake: detectShake        }    });
 |