PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/www/js/product/controllers-product2.js

https://gitlab.com/bcchoi/farming
JavaScript | 451 lines | 347 code | 58 blank | 46 comment | 41 complexity | 8f63907ef51f84b3c09772e29fc3be93 MD5 | raw file
  1. angular.module('starter.controllers-product', [])
  2. .controller('ProductCtrl2', function(
  3. $scope, $state, $stateParams,
  4. $ionicSlideBoxDelegate, $location, $anchorScroll,
  5. Products2, ProductComments, ProductRatings,
  6. Profile, Utils, Auth, Cart, Wallet, Categories2) {
  7. // global variables
  8. $scope.AuthData = Auth.AuthData;
  9. $scope.CartList = Cart.CachedList;
  10. $scope.map = {
  11. origin: {
  12. lat: 37.39002,
  13. lon: 127.555046
  14. },
  15. location: '37.39002,127.555046',
  16. zoom: 14,
  17. distance: 0,
  18. markers: [],
  19. };
  20. // communicates with the DOM
  21. $scope.status = {
  22. loading: true,
  23. productId: $stateParams.productId,
  24. productSizeCSS: 'product2 small',
  25. activeTab: false,
  26. loadingComments: false,
  27. loadingRatings: false,
  28. loadingPurchaseBoolean: false,
  29. purchaseBoolean: null, // records whether user has bought this product
  30. distance:false,
  31. };
  32. $scope.$on('$ionicView.enter', function(e) {
  33. // global variables
  34. $scope.AuthData = Auth.AuthData;
  35. $scope.CartList = Cart.CachedList;
  36. $scope.status['productId'] = $stateParams.productId;
  37. loadProduct();
  38. loadIndexValues();
  39. loadWallet();
  40. initAttributeSelection();
  41. });
  42. // ---------------------------------------------------------------------------
  43. // Product Loading
  44. // init
  45. $scope.ProductImages = {};
  46. $scope.ProductMeta = {};
  47. $scope.ProductIndexValues = {};
  48. $scope.ProductComments = {};
  49. $scope.ProductCommentsProfiles = {};
  50. $scope.ProductRatings = {};
  51. $scope.ProductRatingsProfiles = {};
  52. $scope.FormData = {};
  53. function loadPoints() {
  54. $scope.map.markers.push({
  55. name: $scope.ProductMeta.title,
  56. lat: $scope.map.origin.lat,
  57. lon: $scope.map.origin.lon,
  58. });
  59. }
  60. function getBusinessLink(productId) {
  61. return '<br> <a href="#/app/product2/' + productId + '">More details</a>';
  62. }
  63. function loadDistance() {
  64. var lat = undefined;
  65. var lon = undefined;
  66. angular.forEach($scope.ProductMeta.attributes, function(value, type){
  67. if(type != null && type === '위치') {
  68. angular.forEach($scope.ProductMeta.attributes[type], function(value, type){
  69. if(type != null){
  70. if(lon == undefined){
  71. lon = value;
  72. }
  73. lat = value; // last index
  74. }
  75. })
  76. }
  77. })
  78. if(lat && lon){
  79. $scope.map.location = lat+','+lon;
  80. $scope.map.origin.lat = lat;
  81. $scope.map.origin.lon = lon;
  82. }
  83. console.log($scope.map.origin);
  84. Utils.getDistanceToOrigin($scope.map.location).then(function(distance) {
  85. $scope.map.distance = distance;
  86. $scope.status['distance'] = true;
  87. });
  88. }
  89. // load the product
  90. function loadProduct() {
  91. $scope.status['loading'] = true;
  92. Products2.getProductMeta($scope.status.productId).then(
  93. function(ProductMeta){
  94. if(ProductMeta == null) {$scope.status['loading'] = null;}
  95. else {
  96. $scope.status['loading'] = false;
  97. $scope.ProductMeta = ProductMeta;
  98. // @dependencies
  99. prepareMeta();
  100. loadAuthor();
  101. loadImages();
  102. loadDistance();
  103. loadPoints();
  104. }
  105. },
  106. function(error){
  107. if(error == null) {$scope.status['loading'] = null;} else {$scope.status['loading'] = false;}
  108. console.log(error);
  109. }
  110. )
  111. };
  112. // --> @d fn load images
  113. function loadImages() {
  114. $scope.status['loadingImages'] = true;
  115. Products2.getProductScreenshots($scope.status.productId).then(
  116. function(ProductImages){
  117. if(ProductImages != null) {
  118. $scope.ProductImages = ProductImages;
  119. }
  120. $scope.status['loadingImages'] = false;
  121. $ionicSlideBoxDelegate.update();
  122. },
  123. function(error){
  124. $scope.status['loadingImages'] = false;
  125. console.log(error);
  126. }
  127. )
  128. };
  129. // --> @d fn load index values
  130. function loadIndexValues() {
  131. $scope.status['loadingIndex'] = true;
  132. Products2.getIndexValues($scope.status.productId).then(
  133. function(ProductIndexValues){
  134. $scope.ProductIndexValues = ProductIndexValues;
  135. $scope.status['loadingIndex'] = false;
  136. },
  137. function(error){
  138. $scope.status['loadingIndex'] = false;
  139. console.log(error);
  140. }
  141. )
  142. };
  143. // --> @d fn helper
  144. function prepareMeta() {
  145. $scope.ProductMeta['tags'] = Utils.formatTagsString($scope.ProductMeta.tagsString);
  146. };
  147. // --> @d fn author
  148. function loadAuthor() {
  149. $scope.status['loadingAuthor'] = true;
  150. Profile.get($scope.ProductMeta.userId).then(
  151. function(AuthorData){
  152. $scope.AuthorData = AuthorData;
  153. $scope.status['loadingAuthor'] = false;
  154. },
  155. function(error){
  156. $scope.status['loadingAuthor'] = false;
  157. console.log(error);
  158. })
  159. };
  160. // ---------------------------------------------------------------------------
  161. // Attributes
  162. function initAttributeSelection() {
  163. if(Cart.CachedMeta.hasOwnProperty($scope.status.productId)) {
  164. if(Cart.CachedMeta[$scope.status.productId].hasOwnProperty('selection')) {
  165. $scope.selection = Cart.CachedMeta[$scope.status.productId]['selection'];
  166. } else {$scope.selection = {};}
  167. } else {$scope.selection = {};};
  168. console.log('attributes select', $scope.selection)
  169. };
  170. $scope.attributeChanged = function() {
  171. Cart.attributeChanged($scope.status.productId, $scope.selection);
  172. };
  173. // ---------------------------------------------------------------------------
  174. // Interaction
  175. $scope.activateTab = function(tabName) {
  176. $scope.status['activeTab'] = tabName;
  177. };
  178. // ---------------------------------------------------------------------------
  179. // Comments
  180. // fn load
  181. $scope.loadComments = function() {
  182. $scope.status['loadingComments'] = true;
  183. ProductComments.load($scope.status.productId).then(
  184. function(loadedProductComments) {
  185. if(ProductComments != null) {$scope.ProductComments = Utils.formatComments(loadedProductComments);}
  186. $scope.status['loadingComments'] = false;
  187. // @dependencies
  188. loadCommentsProfiles();
  189. //scrollToSubmitEnd(); not to be worked
  190. },
  191. function(error){
  192. console.log(error);
  193. $scope.status['loadingComments'] = false;
  194. }
  195. )
  196. };
  197. /**
  198. * Other helpers and buttons
  199. */
  200. function scrollToSubmitEnd() {
  201. $location.hash('submit0');
  202. $anchorScroll.yOffset = 100;
  203. $anchorScroll();
  204. };
  205. // <--
  206. $scope.getCommentProfile = function(userId) {
  207. return $scope.ProductCommentsProfiles[userId]
  208. };
  209. // --> @d fn comments profiles
  210. function loadCommentsProfiles() {
  211. $scope.ProductCommentsProfiles = {};
  212. ProductComments.loadProfiles($scope.ProductComments).then(
  213. function(CommentsProfiles){
  214. $scope.ProductCommentsProfiles = CommentsProfiles;
  215. },
  216. function(error){
  217. console.log(error);
  218. }
  219. );
  220. };
  221. // fn add
  222. $scope.addComment = function() {
  223. if ($scope.AuthData.hasOwnProperty('uid') && $scope.FormData.commentValue) {
  224. var commentObj = {
  225. timestamp: Firebase.ServerValue.TIMESTAMP,
  226. message: $scope.FormData.commentValue,
  227. };
  228. ProductComments.post($scope.status.productId, $scope.AuthData.uid, commentObj).then(
  229. function(success){
  230. $scope.FormData['commentValue'] = "";
  231. // --> refresh
  232. $scope.loadComments();
  233. loadIndexValues();
  234. },
  235. function(error){
  236. console.log(error);
  237. }
  238. )
  239. }
  240. };
  241. // ---------------------------------------------------------------------------
  242. // Ratings
  243. $scope.selectRatingStar = function(value) {
  244. $scope.FormData['ratingValue'] = value;
  245. console.log($scope.FormData['ratingValue'])
  246. };
  247. // fn rating
  248. $scope.loadRatings = function() {
  249. $scope.status['loadingRatings'] = true;
  250. ProductRatings.load($scope.status.productId).then(
  251. function(loadedProductRatings) {
  252. if(ProductRatings != null) {$scope.ProductRatings = loadedProductRatings}
  253. $scope.status['loadingRatings'] = false;
  254. // @dependencies
  255. loadRatingsProfiles();
  256. },
  257. function(error){
  258. console.log(error);
  259. $scope.status['loadingRatings'] = false;
  260. }
  261. )
  262. };
  263. // --> @d fn ratings profiles
  264. function loadRatingsProfiles() {
  265. $scope.ProductRatingsProfiles = {};
  266. ProductRatings.loadProfiles($scope.ProductRatings).then(
  267. function(RatingsProfiles){
  268. $scope.ProductRatingsProfiles = RatingsProfiles;
  269. },
  270. function(error){
  271. console.log(error);
  272. }
  273. );
  274. };
  275. // <--
  276. $scope.getRatingProfile = function(userId) {
  277. return $scope.ProductRatingsProfiles[userId]
  278. };
  279. // fn add
  280. $scope.addRating = function() {
  281. if ($scope.AuthData.hasOwnProperty('uid') && $scope.FormData.ratingMessage && $scope.FormData.ratingValue) {
  282. var ratingObj = {
  283. timestamp: Firebase.ServerValue.TIMESTAMP,
  284. message: $scope.FormData.ratingMessage,
  285. value: $scope.FormData.ratingValue
  286. };
  287. ProductRatings.post($scope.status.productId, $scope.AuthData.uid, ratingObj).then(
  288. function(success){
  289. $scope.FormData['ratingMessage'] = "";
  290. $scope.FormData['ratingValue'] = null;
  291. // --> refresh
  292. $scope.loadRatings();
  293. loadIndexValues();
  294. },
  295. function(error){
  296. console.log(error);
  297. }
  298. )
  299. } else {
  300. Utils.showMessage('Please fill in all fields', 1500);
  301. }
  302. };
  303. // fn purchase
  304. $scope.loadPurchase = function() {
  305. if($scope.AuthData.hasOwnProperty('uid') && $scope.status.hasOwnProperty('productId')) {
  306. $scope.status['loadingPurchaseBoolean'] = true;
  307. ProductRatings.loadPurchase($scope.status.productId, $scope.AuthData.uid).then(
  308. function(purchaseBoolean){
  309. $scope.status['purchaseBoolean'] = purchaseBoolean; //xtest
  310. $scope.status['loadingPurchaseBoolean'] = false;
  311. },
  312. function(error){
  313. console.log(error);
  314. $scope.status['loadingPurchaseBoolean'] = false;
  315. }
  316. )
  317. }
  318. };
  319. // ---------------------------------------------------------------------------
  320. // Formattings
  321. $scope.changeImageSize = function() {
  322. if($scope.status.productSizeCSS == "product2 small") {
  323. $scope.status['productSizeCSS'] = "product2";
  324. } else {
  325. $scope.status['productSizeCSS'] = "product2 small";
  326. }
  327. };
  328. $scope.getCurrentPrice = function(currentPrice, discountPerc) {
  329. return Utils.getCurrentPrice(currentPrice, discountPerc);
  330. };
  331. $scope.getOldPrice = function(currentPrice, discountPerc) {
  332. return Utils.getOldPrice(currentPrice, discountPerc);
  333. };
  334. $scope.formatTimestamp = function(timestamp) {
  335. return Utils.formatTimestamp(timestamp);
  336. };
  337. // ---------------------------------------------------------------------------
  338. // Wallet (my saved items)
  339. $scope.WalletList = Wallet.CachedList;
  340. function loadWallet() {
  341. Wallet.load($scope.AuthData).then(
  342. function(success){
  343. $scope.WalletList = Wallet.CachedList;
  344. });
  345. };
  346. $scope.saveItem = function(productId) {
  347. Wallet.buttonPressed($scope.AuthData, productId).then(
  348. function(success){
  349. $scope.WalletList = Wallet.CachedList;
  350. })
  351. };
  352. $scope.walletPressedCSS = function(productId) {
  353. if($scope.WalletList.hasOwnProperty(productId)){
  354. if($scope.WalletList[productId]) {
  355. return 'button-pressed'
  356. }
  357. }
  358. };
  359. // ---------------------------------------------------------------------------
  360. // Cart
  361. // ** custom addToCart()
  362. $scope.addToCart = function(productId) {
  363. $scope.ProductMetaAdj = {
  364. value: $scope.ProductMeta,
  365. key: productId,
  366. index: $scope.ProductIndexValues
  367. };
  368. var ProductImagesAdj = null;
  369. if($scope.ProductImages.hasOwnProperty('screenshot1')){ProductImagesAdj= $scope.ProductImages['screenshot1']};
  370. Cart.buttonPressed(productId, $scope.ProductMetaAdj, ProductImagesAdj, $scope.selection)
  371. };
  372. $scope.cartPressedCSS = function(productId) {
  373. if($scope.CartList.hasOwnProperty(productId)){
  374. if($scope.CartList[productId]) {
  375. return 'button-pressed'
  376. }
  377. }
  378. };
  379. // ---------------------------------------------------------------------------
  380. // Other
  381. $scope.goTo = function(nextState) {
  382. $state.go(nextState)
  383. };
  384. $scope.goToTag = function(tag) {
  385. $state.go('app.search', {q: tag})
  386. };
  387. $scope.loadAuthorProducts = function(uid) {
  388. $state.go('app.search2', {q: uid, searchType: 'author'})
  389. };
  390. $scope.goToCategory = function(categoryId) {
  391. $state.go('app.browse-category2', {categoryId: categoryId})
  392. };
  393. })