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

/y/js/AdminSanPham.js

https://gitlab.com/vantb9x/vbv-doancuoiky
JavaScript | 552 lines | 447 code | 95 blank | 10 comment | 32 complexity | c29fa38abafcc38e001120d0540f6d19 MD5 | raw file
  1. var auth = firebase.auth();
  2. var storageRef = firebase.storage().ref();
  3. var file1, metadata1, file2, metadata2, file3, metadata3;
  4. //SignIn Anonymously to firebase storage
  5. auth.signInAnonymously().then(function (user) {
  6. console.log('Anonymous Sign In Success', user);
  7. }).catch(function (error) {
  8. console.error('Anonymous Sign In Error', error);
  9. });
  10. app.controller('AdminTemplateSanPhamController',
  11. ['$scope', '$mdSidenav', '$mdDialog', '$mdMedia', '$mdBottomSheet', '$firebaseArray', '$routeParams', '$mdToast', '$filter',
  12. function($scope, $mdSidenav, $mdDialog, $mdMedia, $mdBottomSheet, $firebaseArray, $routeParams, $mdToast, $filter)
  13. { //show list bottom
  14. var refSanPham = new Firebase("https://bacphu.firebaseio.com/SanPham/");
  15. $scope.ListSanPham = $firebaseArray(refSanPham);
  16. $scope.openBottemThemSanPham = function() {
  17. $mdBottomSheet.show({
  18. templateUrl: 'template/AdminBottomSheetAddSanPham.html',
  19. controller: 'AdminTemplateSanPhamController',
  20. scope: $scope,
  21. preserveScope: true
  22. });
  23. }
  24. $scope.fileNameChanged1 = function (evt) {
  25. file1 = evt.files[0];
  26. metadata1 = {
  27. 'contentType': file1.type
  28. };
  29. var reader = new FileReader();
  30. reader.onload = function(e)
  31. {
  32. $scope.$apply(function()
  33. {
  34. $scope.step1 = (e.target.result);
  35. });
  36. }
  37. reader.readAsDataURL(file1);
  38. }
  39. $scope.fileNameChanged2 = function (evt) {
  40. file2 = evt.files[0];
  41. metadata2 = {
  42. 'contentType': file2.type
  43. };
  44. var reader = new FileReader();
  45. reader.onload = function(e)
  46. {
  47. $scope.$apply(function()
  48. {
  49. $scope.step2 = (e.target.result);
  50. });
  51. }
  52. reader.readAsDataURL(file2);
  53. }
  54. $scope.fileNameChanged3 = function (evt) {
  55. file3 = evt.files[0];
  56. metadata3 = {
  57. 'contentType': file3.type
  58. };
  59. var reader = new FileReader();
  60. reader.onload = function(e)
  61. {
  62. $scope.$apply(function()
  63. {
  64. $scope.step3 = (e.target.result);
  65. });
  66. }
  67. reader.readAsDataURL(file3);
  68. }
  69. $scope.AdminThemSanPham = function()
  70. {
  71. $scope.ListSanPham.$loaded().then(function(ListSanPham)
  72. {
  73. // //Upload Image to Firebase storage
  74. var uploadTask1 = storageRef.child('images/' + file1.name).put(file1, metadata1);
  75. var uploadTask2 = storageRef.child('images/' + file2.name).put(file2, metadata2);
  76. var uploadTask3 = storageRef.child('images/' + file3.name).put(file3, metadata3);
  77. var url1, url2, url3;
  78. uploadTask1.on('state_changed', null, function(error)
  79. {
  80. console.error("upload failed 1", error);
  81. }, function()
  82. {
  83. url1 = uploadTask1.snapshot.metadata.downloadURLs[0];
  84. uploadTask2.on('state_changed', null, function(error)
  85. {
  86. console.error("upload failed 2", error);
  87. }, function()
  88. {
  89. url2 = uploadTask2.snapshot.metadata.downloadURLs[0];
  90. uploadTask3.on('state_changed', null, function(error)
  91. {
  92. console.error("upload failed 3", error);
  93. }, function()
  94. {
  95. url3 = uploadTask3.snapshot.metadata.downloadURLs[0];
  96. //them san pham
  97. var lengthListSanPham = ListSanPham.length;
  98. var newID;
  99. if (lengthListSanPham === 0)
  100. {
  101. newID = 0;
  102. }
  103. else
  104. {
  105. newID = parseInt(ListSanPham[lengthListSanPham - 1].ID) + 1;
  106. }
  107. var loaiSanPham = $scope.LoaiSanPham;
  108. var tenSanPham = $scope.TenSanPham;
  109. var giaSanPham = $scope.GiaSanPham;
  110. var soLuongSanPham = parseInt($scope.SoLuongSanPham);
  111. var moTaSanPham = $scope.MoTaSanPham;
  112. var maSanPham = $scope.MaSanPham;
  113. var newSanPham = refSanPham.push();
  114. newSanPham.set(
  115. {
  116. ID: newID,
  117. LoaiSanPham: loaiSanPham,
  118. TenSanPham: tenSanPham,
  119. SoLuongSanPham: soLuongSanPham,
  120. GiaSanPham: giaSanPham,
  121. MoTaSanPham: moTaSanPham,
  122. MaSanPham: maSanPham,
  123. ImagePhu1: url1,
  124. ImagePhu2: url2,
  125. ImagePhu3: url3,
  126. SoLanThem: 1,
  127. SoLuongBan: 0
  128. },function(error)
  129. {
  130. if (error)
  131. {
  132. $mdToast.show(
  133. $mdToast.simple().textContent('Thêm sản phẩm bị lỗi')
  134. .position('top right')
  135. .hideDelay(5000));
  136. }
  137. else
  138. {
  139. $mdToast.show(
  140. $mdToast.simple().textContent('Đã thêm 1 sản phẩm mới')
  141. .position('top right')
  142. .hideDelay(5000));
  143. }
  144. });
  145. //them vao chi tiet lan them san pham
  146. var refChiTietLanThemSanPham = new Firebase("https://bacphu.firebaseio.com/ChiTietLanThemSanPham");
  147. var newLanThemSanPham = refChiTietLanThemSanPham.push();
  148. // $scope.ListLanThemSanPham = $firebaseArray(refChiTietLanThemSanPham);
  149. var currentDate = new Date()
  150. var day = currentDate.getDate();
  151. var month = currentDate.getMonth() + 1;
  152. var year = currentDate.getFullYear();
  153. var curDate = day + "/" + month + "/" + year;
  154. newLanThemSanPham.set(
  155. {
  156. IDSanPham: newID,
  157. IDLanThem: 1,
  158. SoLuongLanThem: soLuongSanPham,
  159. NgayThem: curDate
  160. });
  161. $scope.LoaiSanPham = "";
  162. $scope.TenSanPham = "";
  163. $scope.GiaSanPham = "";
  164. $scope.SoLuongSanPham = "";
  165. $scope.MoTaSanPham = "";
  166. $scope.MaSanPham = "";
  167. $scope.step1 = "";
  168. $scope.step2 = "";
  169. $scope.step3 = "";
  170. // //url
  171. // //het them san pham
  172. $mdBottomSheet.hide();
  173. });
  174. });
  175. });
  176. //an thanh bottom them san pham
  177. });
  178. }
  179. $scope.status = ' ';
  180. $scope.customFullscreen = $mdMedia('xs') || $mdMedia('sm');
  181. $scope.TimIDSanPham = function(item)
  182. {
  183. var index = KiemTraPhanTuTonTaiTheoID($scope.ListSanPham, item);
  184. $scope.CurrentSanPhamEdit = $scope.ListSanPham[index];
  185. }
  186. $scope.showEditSanPhamDialog = function(ev)
  187. {
  188. var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && $scope.customFullscreen;
  189. $mdDialog.show(
  190. {
  191. clickOutsideToClose: true,
  192. controller: DialogEditSanPhamController,
  193. templateUrl: 'template/AdminTemplateChinhSuaSanPham.html',
  194. scope: $scope,
  195. preserveScope: true,
  196. parent: angular.element(document.body),
  197. targetEvent: ev,
  198. fullscreen: useFullScreen
  199. })
  200. .then(function(answer)
  201. {
  202. if (answer === "Cancel")
  203. {
  204. $mdDialog.hide();
  205. }
  206. }, function()
  207. {
  208. $scope.status = 'You cancelled the dialog.';
  209. });
  210. $scope.$watch(function()
  211. {
  212. return $mdMedia('xs') || $mdMedia('sm');
  213. }, function(wantsFullScreen)
  214. {
  215. $scope.customFullscreen = (wantsFullScreen === true);
  216. });
  217. };
  218. $scope.xoaSanPham = function(ev, item)
  219. {
  220. alert(item.TenSanPham);
  221. var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && $scope.customFullscreen;
  222. $mdDialog.show(
  223. {
  224. clickOutsideToClose: true,
  225. controller: DialogEditSanPhamController,
  226. templateUrl: 'template/AdminTemplateDialogXoaSanPham.html',
  227. parent: angular.element(document.body),
  228. targetEvent: ev,
  229. scope: $scope,
  230. preserveScope: true,
  231. fullscreen: useFullScreen
  232. })
  233. .then(function(answer)
  234. {
  235. if (answer === "OK")
  236. {
  237. //alert($scope.ListSanPham.length);
  238. // $scope.ListSanPham.$remove(item);
  239. var refKeyDelete = item.$id;
  240. var refDelete = new Firebase("https://bacphu.firebaseio.com/SanPham/"+ refKeyDelete);
  241. refDelete.remove();
  242. $mdToast.show(
  243. $mdToast.simple().textContent('Xóa sản phẩm thành công')
  244. .position('top right')
  245. .hideDelay(5000)
  246. );
  247. }
  248. }, function()
  249. {
  250. $scope.status = 'You cancelled the dialog.';
  251. });
  252. $scope.$watch(function()
  253. {
  254. return $mdMedia('xs') || $mdMedia('sm');
  255. }, function(wantsFullScreen)
  256. {
  257. $scope.customFullscreen = (wantsFullScreen === true);
  258. });
  259. }
  260. $scope.showThemLanNhapSanPham = function(ev)
  261. {
  262. var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && $scope.customFullscreen;
  263. $mdDialog.show(
  264. {
  265. clickOutsideToClose: true,
  266. controller: DialogEditSanPhamController,
  267. templateUrl: 'template/AdminTemplateThemLanNhapSanPham.html',
  268. scope: $scope,
  269. preserveScope: true,
  270. parent: angular.element(document.body),
  271. targetEvent: ev,
  272. fullscreen: useFullScreen
  273. })
  274. .then(function(answer)
  275. {
  276. if (answer === "Cancel")
  277. {
  278. $mdDialog.hide();
  279. }
  280. }, function()
  281. {
  282. $scope.status = 'You cancelled the dialog.';
  283. });
  284. $scope.$watch(function()
  285. {
  286. return $mdMedia('xs') || $mdMedia('sm');
  287. }, function(wantsFullScreen)
  288. {
  289. $scope.customFullscreen = (wantsFullScreen === true);
  290. });
  291. };
  292. $scope.XemChiTietLanNhap = function(ev)
  293. {
  294. var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && $scope.customFullscreen;
  295. $mdDialog.show(
  296. {
  297. clickOutsideToClose: true,
  298. controller: DialogEditSanPhamController,
  299. templateUrl: 'template/AdminXemChiTietLanNhapSanPham.html',
  300. scope: $scope,
  301. preserveScope: true,
  302. parent: angular.element(document.body),
  303. targetEvent: ev,
  304. fullscreen: useFullScreen
  305. })
  306. .then(function(answer)
  307. {
  308. if (answer === "Cancel")
  309. {
  310. $mdDialog.hide();
  311. }
  312. }, function()
  313. {
  314. $scope.status = 'You cancelled the dialog.';
  315. });
  316. $scope.$watch(function()
  317. {
  318. return $mdMedia('xs') || $mdMedia('sm');
  319. }, function(wantsFullScreen)
  320. {
  321. $scope.customFullscreen = (wantsFullScreen === true);
  322. });
  323. };
  324. $scope.flagTheoTen = false;
  325. $scope.SapXepSanPhamTheoTen = function()
  326. {
  327. if ($scope.flagTheoTen === true)
  328. {
  329. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'TenSanPham', true);
  330. $scope.flagTheoTen = false;
  331. }
  332. else
  333. {
  334. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'TenSanPham', false);
  335. $scope.flagTheoTen = true;
  336. }
  337. }
  338. $scope.flagTheoGia = false;
  339. $scope.SapXepSanPhamTheoGia = function()
  340. {
  341. if ($scope.flagTheoGia === true)
  342. {
  343. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'GiaSanPham', true);
  344. $scope.flagTheoGia = false;
  345. }
  346. else
  347. {
  348. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'GiaSanPham', false);
  349. $scope.flagTheoGia = true;
  350. }
  351. }
  352. $scope.flagTheoTongHang = false;
  353. $scope.SapXepSanPhamTheoTongHang = function()
  354. {
  355. if ($scope.flagTheoTongHang === true)
  356. {
  357. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'SoLuongSanPham', true);
  358. $scope.flagTheoTongHang = false;
  359. }
  360. else
  361. {
  362. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'SoLuongSanPham', false);
  363. $scope.flagTheoTongHang = true;
  364. }
  365. }
  366. $scope.flagTheoDaBan = false;
  367. $scope.SapXepSanPhamTheoDaBan = function()
  368. {
  369. if ($scope.flagTheoDaBan === true)
  370. {
  371. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'SoLuongBan', true);
  372. $scope.flagTheoDaBan = false;
  373. }
  374. else
  375. {
  376. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'SoLuongBan', false);
  377. $scope.flagTheoDaBan = true;
  378. }
  379. }
  380. $scope.flagTheoMaSanPham = false;
  381. $scope.SapXepSanPhamTheoMaSanPham = function()
  382. {
  383. if ($scope.flagTheoMaSanPham === true)
  384. {
  385. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'MaSanPham', true);
  386. $scope.flagTheoMaSanPham = false;
  387. }
  388. else
  389. {
  390. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'MaSanPham', false);
  391. $scope.flagTheoMaSanPham = true;
  392. }
  393. }
  394. $scope.flagTheoID = false;
  395. $scope.SapXepSanPhamTheoID = function()
  396. {
  397. if ($scope.flagTheoID === true)
  398. {
  399. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'ID', true);
  400. $scope.flagTheoID = false;
  401. }
  402. else
  403. {
  404. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'ID', false);
  405. $scope.flagTheoID = true;
  406. }
  407. }
  408. $scope.flagTheoLoaiSanPham = false;
  409. $scope.SapXepSanPhamTheoLoaiSanPham = function()
  410. {
  411. if ($scope.flagTheoLoaiSanPham === true)
  412. {
  413. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'LoaiSanPham', true);
  414. $scope.flagTheoLoaiSanPham = false;
  415. }
  416. else
  417. {
  418. $scope.ListSanPham = $filter('orderBy')($scope.ListSanPham, 'LoaiSanPham', false);
  419. $scope.flagTheoLoaiSanPham = true;
  420. }
  421. }
  422. }]);
  423. function DialogEditSanPhamController($scope, $mdDialog) {
  424. $scope.hide = function() {
  425. $mdDialog.hide();
  426. };
  427. $scope.cancel = function() {
  428. $mdDialog.cancel();
  429. };
  430. $scope.answer = function(answer) {
  431. $mdDialog.hide(answer);
  432. };
  433. }
  434. function readURL(input) {
  435. if (input.files && input.files[0]) {
  436. var reader = new FileReader();
  437. reader.onload = function (e) {
  438. $('#blah').attr('src', e.target.result);
  439. }
  440. reader.readAsDataURL(input.files[0]);
  441. }
  442. }
  443. $("#imgInp").change(function(){
  444. readURL(this);
  445. });
  446. function KiemTraPhanTuTonTaiTheoID(array, item)
  447. {
  448. var length = array.length;
  449. for (var i = 0; i < length; i++)
  450. {
  451. if (array[i].ID === item.ID)
  452. {
  453. return i;
  454. }
  455. }
  456. return -1;
  457. }