utils.factory.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. angular.module('odoo')
  2. .factory('async', function () {
  3. return {
  4. loop: function (iterations, func, callback) {
  5. var index = 0;
  6. var done = false;
  7. var loop = {
  8. next: function () {
  9. if (done) {
  10. return;
  11. }
  12. if (index < iterations) {
  13. index++;
  14. func(loop);
  15. } else {
  16. done = true;
  17. callback();
  18. }
  19. },
  20. iteration: function() {
  21. return index - 1;
  22. },
  23. break: function() {
  24. done = true;
  25. callback();
  26. }
  27. };
  28. loop.next();
  29. return loop;
  30. }
  31. }
  32. })
  33. .factory('camera', function ($cordovaCamera) {
  34. return {
  35. takePicture: function (success, error) {
  36. var options = {
  37. quality: 75,
  38. destinationType: Camera.DestinationType.DATA_URL,
  39. sourceType: Camera.PictureSourceType.CAMERA,
  40. allowEdit: true,
  41. encodingType: Camera.EncodingType.JPEG,
  42. targetWidth: 300,
  43. targetHeight: 300,
  44. popoverOptions: CameraPopoverOptions,
  45. saveToPhotoAlbum: false,
  46. correctOrientation:true
  47. };
  48. $cordovaCamera.getPicture(options).then(function(imageData) {
  49. success(imageData);
  50. }, function(err) {
  51. error(err);
  52. });
  53. }
  54. }
  55. })
  56. .factory('contact', function ($cordovaContacts) {
  57. return {
  58. save: function (customer, success, error) {
  59. if(!customer.mobile && !customer.phone && !customer.email) {
  60. error();
  61. return;
  62. }
  63. var contact = {
  64. name: {
  65. givenName: customer.name,
  66. familyName: '',
  67. formatted: ''
  68. },
  69. nickname: '',
  70. phoneNumbers: [
  71. {
  72. value: customer.phone,
  73. type: 'phone'
  74. },
  75. {
  76. value: customer.mobile,
  77. type: 'mobile'
  78. }
  79. ],
  80. emails: [
  81. {
  82. value: customer.email,
  83. type: 'home'
  84. }
  85. ],
  86. addresses: [
  87. {
  88. type: 'home',
  89. formatted: '',
  90. streetAddress: customer.street,
  91. locality: customer.city,
  92. region: '',
  93. postalCode: '',
  94. country: 'Paraguay'
  95. }
  96. ],
  97. ims: null,
  98. organizations: null,
  99. birthday: null,
  100. note: null,
  101. photos: null,
  102. categories: null,
  103. urls: null
  104. };
  105. $cordovaContacts.save(contact).then(function (result) {
  106. success(result);
  107. }, function (err) {
  108. error(err);
  109. });
  110. }
  111. }
  112. });