utils.factory.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. angular.module('odoo')
  2. /**
  3. * -----------------------------------------------------------------------------
  4. * Description: Native SQL util instructions
  5. * -----------------------------------------------------------------------------
  6. */
  7. .factory('sqlFactory', function () {
  8. // Execute native SQL SELECT instruction
  9. var select = function(tableName, success, error) {
  10. var sql = 'SELECT * FROM ' + tableName;
  11. db.executeSql(sql, [], function(result) {
  12. success(result.rows);
  13. }, function(err) {
  14. error(err);
  15. });
  16. };
  17. // Execute native SQL SELECT instruction with a constraint
  18. var selectByConstraint = function(tableName, constraint, success, error) {
  19. var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
  20. db.executeSql(sql, [], function(result) {
  21. success(result.rows);
  22. }, function(err) {
  23. error(err);
  24. });
  25. };
  26. // Execute native SQL SELECT instruction with count instruction
  27. var count = function(tableName, success, error) {
  28. var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
  29. db.executeSql(sql, [], function(result) {
  30. success(result.rows.item(0).total);
  31. }, function(err) {
  32. error(err);
  33. });
  34. };
  35. return {
  36. select: select,
  37. selectByConstraint: selectByConstraint,
  38. count: count
  39. }
  40. })
  41. /**
  42. * -----------------------------------------------------------------------------
  43. * Description: Get user configuration from local database
  44. * -----------------------------------------------------------------------------
  45. */
  46. .factory('config', function (sqlFactory) {
  47. return function (success, error) {
  48. sqlFactory.select('user', function (users) {
  49. if (users.length == 1) {
  50. success(users.item(0));
  51. } else {
  52. var configs = [];
  53. for (var i = 0; i < users.length; i++) {
  54. configs.push(users.item(i))
  55. }
  56. success(configs);
  57. }
  58. }, function (err) {
  59. error(err);
  60. });
  61. };
  62. })
  63. /**
  64. * -----------------------------------------------------------------------------
  65. * Description: Async loop util v2
  66. * -----------------------------------------------------------------------------
  67. */
  68. .factory('asyncLoop', function () {
  69. return function (iterations, func, callback) {
  70. var index = 0;
  71. var done = false;
  72. var loop = {
  73. next: function () {
  74. if (done) {
  75. return;
  76. }
  77. if (index < iterations) {
  78. index++;
  79. func(loop);
  80. } else {
  81. done = true;
  82. callback();
  83. }
  84. },
  85. iteration: function() {
  86. return index - 1;
  87. },
  88. break: function() {
  89. done = true;
  90. callback();
  91. }
  92. };
  93. loop.next();
  94. return loop;
  95. }
  96. })
  97. /**
  98. * -----------------------------------------------------------------------------
  99. * Description: Native camera manager
  100. * -----------------------------------------------------------------------------
  101. */
  102. .factory('camera', function ($cordovaCamera) {
  103. // Take a picture using native camera
  104. var takePicture = function (success, error) {
  105. var options = {
  106. quality: 75,
  107. destinationType: Camera.DestinationType.DATA_URL,
  108. sourceType: Camera.PictureSourceType.CAMERA,
  109. allowEdit: true,
  110. encodingType: Camera.EncodingType.JPEG,
  111. targetWidth: 300,
  112. targetHeight: 300,
  113. popoverOptions: CameraPopoverOptions,
  114. saveToPhotoAlbum: false,
  115. correctOrientation:true
  116. };
  117. $cordovaCamera.getPicture(options).then(function(imageData) {
  118. success(imageData);
  119. }, function(err) {
  120. error(err);
  121. });
  122. };
  123. return {
  124. takePicture: takePicture
  125. }
  126. })
  127. /**
  128. * -----------------------------------------------------------------------------
  129. * Description: Native contacts manager
  130. * -----------------------------------------------------------------------------
  131. */
  132. .factory('contact', function ($cordovaContacts) {
  133. // Save customer information to device contacts
  134. var save = function (customer, success, error) {
  135. if(!customer.mobile && !customer.phone && !customer.email) {
  136. error();
  137. return;
  138. }
  139. var contact = {
  140. name: {
  141. givenName: customer.name,
  142. familyName: '',
  143. formatted: ''
  144. },
  145. nickname: '',
  146. phoneNumbers: [
  147. {
  148. value: customer.phone,
  149. type: 'phone'
  150. },
  151. {
  152. value: customer.mobile,
  153. type: 'mobile'
  154. }
  155. ],
  156. emails: [
  157. {
  158. value: customer.email,
  159. type: 'home'
  160. }
  161. ],
  162. addresses: [
  163. {
  164. type: 'home',
  165. formatted: '',
  166. streetAddress: customer.street,
  167. locality: customer.city,
  168. region: '',
  169. postalCode: '',
  170. country: 'Paraguay'
  171. }
  172. ],
  173. ims: null,
  174. organizations: null,
  175. birthday: null,
  176. note: null,
  177. photos: null,
  178. categories: null,
  179. urls: null
  180. };
  181. $cordovaContacts.save(contact).then(function (result) {
  182. success(result);
  183. }, function (err) {
  184. error(err);
  185. });
  186. };
  187. return {
  188. save: save
  189. }
  190. });