Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. angular.module('App').controller('AddNewsController', function (
  2. $rootScope, $scope, $http, $mdToast, $mdDialog, $route,
  3. $timeout, request, $mdpDatePicker, $mdpTimePicker ) {
  4.  
  5. // define variable
  6. var self = $scope, root = $rootScope;
  7. var is_new = ( root.getCurNewsId() == null );
  8. var original = null;
  9. var now = new Date().getTime();
  10. var dir = "/uploads/news/";
  11.  
  12. root.search_enable = false;
  13. root.toolbar_menu = null;
  14. root.pagetitle = (is_new) ? 'Add News' : 'Edit News';
  15. self.button_text = (is_new) ? 'SAVE' : 'UPDATE';
  16. self.submit_loading = false;
  17. self.image = {};
  18. self.gallery = {};
  19. self.gallery.valid = true;
  20. self.images_obj = [];
  21. self.topic_selected = [];
  22. self.news_date = moment(new Date()).format('DD MMM YYYY HH:mm');
  23. self.news_date_milis = now;
  24. self.type_array = ["ARTICLE", "GALLERY", "VIDEO"];
  25. root.closeAndDisableSearch();
  26. var old_img_name = null;
  27.  
  28. request.getAllTopic().then(function (resp) {
  29. self.topic_data = resp.data;
  30. });
  31. /* check edit or add new */
  32. if (is_new) {
  33. self.topic_valid = false;
  34. self.image.valid = false;
  35. original = {
  36. title: null, content: null, image: null, url: null, total_view : 0, total_comment: 0, date:now,
  37. type: 'ARTICLE', draft: 1, featured: 0, created_at: now, last_update: now
  38. };
  39. self.news = angular.copy(original);
  40. } else {
  41. self.image.valid = true;
  42. self.topic_valid = true;
  43. self.original_topic = [];
  44. self.original_tags = [];
  45. console.log(self.news_date);
  46. request.getOneNews(root.getCurNewsId()).then(function (resp) {
  47. original = resp.data;
  48. self.news = angular.copy(original);
  49. old_img_name = angular.copy(self.news.image);
  50. self.news_date = moment(original.date).format('DD MMM YYYY HH:mm');
  51. console.log(self.news_date);
  52. });
  53. request.getAllTopicByNewsId(root.getCurNewsId()).then(function (resp) {
  54. for (var i = 0; i < resp.data.length; i++) {
  55. self.original_topic.push(resp.data[i].id);
  56. }
  57. root.sortArrayOfInt(self.original_topic);
  58. self.topic_selected = angular.copy(self.original_topic);
  59. });
  60. request.getAllNewsGalleryByNewsId(root.getCurNewsId()).then(function(resp){
  61. self.images_obj = angular.copy(resp.data);
  62. });
  63. }
  64.  
  65. /* for selecting topic*/
  66. self.toggleTopic = function (i, list) {
  67. var idx = list.indexOf(i.id);
  68. if (idx > -1) {
  69. list.splice(idx, 1);
  70. } else {
  71. list.push(i.id);
  72. }
  73. root.sortArrayOfInt(list);
  74. self.topic_valid = (self.topic_selected.length > 0);
  75. };
  76. self.isTopicSelected = function (i, list) {
  77. return list.indexOf(i.id) > -1;
  78. };
  79.  
  80. /* method for submit action */
  81. /* [0] news_topic done, [1] primary image done, [2] gallery image done */
  82. self.done_arr = [false, false, false, false];
  83. self.submit = function (v) {
  84. self.submit_loading = true;
  85. self.resp_submit = null;
  86. self.submit_done = false;
  87. self.done_arr = [false, false, false, false];
  88. v.date = self.news_date_milis;
  89.  
  90. if (is_new) { // create
  91. v.image = (self.image.file != null) ? now + root.getExtension(self.image.file) : v.image;
  92. request.insertOneNews(v).then(function (resp) {
  93. self.resp_submit = resp;
  94. if (resp.status == 'success') {
  95. self.prepareNewsTopic(resp.data.id);
  96. request.insertAllNewsTopic(self.news_topic).then(function () {
  97. self.done_arr[0] = true;
  98. }); // insert table relation
  99. request.uploadFileToUrl(self.image.file, dir, v.image, "").then(function () {
  100. self.done_arr[1] = true;
  101. }); // upload primary image
  102. if(v.type == 'GALLERY' && self.gallery.files && self.gallery.files.length > 0){
  103. uploadGalleryImages(resp.data.id, 0);
  104. } else { self.done_arr[2] = true; } // upload gallery image
  105. } else {
  106. self.done_arr[0] = true;
  107. self.submit_done = true;
  108. }
  109. });
  110. } else { // update
  111. v.last_update = now;
  112. v.image = (self.image.file != null) ? now + root.getExtension(self.image.file) : v.image;
  113. request.updateOneNews(v.id, v).then(function (resp) {
  114. self.resp_submit = resp;
  115. if (resp.status == 'success') {
  116. self.prepareNewsTopic(resp.data.id);
  117. request.insertAllNewsTopic(self.news_topic).then(function () {
  118. self.done_arr[0] = true;
  119. }); // insert table relation
  120. if (self.image.file != null) {
  121. request.uploadFileToUrl(self.image.file, dir, v.image, old_img_name).then(function () {
  122. self.done_arr[1] = true;
  123. }); // upload primary image
  124. } else { self.done_arr[1] = true; }
  125. if(v.type == 'GALLERY' && self.gallery.files && self.gallery.files.length > 0){
  126. uploadGalleryImages(v.id, 0);
  127. } else { self.done_arr[2] = true; } // upload gallery image
  128. } else {
  129. self.done_arr[0] = true;
  130. self.submit_done = true;
  131. }
  132. });
  133. }
  134.  
  135. };
  136.  
  137. /* Submit watch onFinish Checker */
  138. self.$watchCollection('done_arr', function (new_val, old_val) {
  139. if (self.submit_done || (new_val[0] && new_val[1] && new_val[2])) {
  140. $timeout(function () { // give delay for good UI
  141. if (self.resp_submit.status == 'success') {
  142. if (self.send_notif) {
  143. self.sendNotification(is_new ? self.resp_submit.data : self.resp_submit.data.news);
  144. }
  145. root.showConfirmDialogSimple('', self.resp_submit.msg, function () {
  146. window.location.href = '#news';
  147. });
  148. } else {
  149. root.showInfoDialogSimple('', self.resp_submit.msg);
  150. }
  151. self.submit_loading = false;
  152. }, 1000);
  153. }
  154. });
  155.  
  156. /* checker when all data ready to submit */
  157. self.isReadySubmit = function () {
  158. if (is_new) {
  159. self.is_clean = angular.equals(original, self.news);
  160. return (!self.is_clean && self.image.valid && self.topic_valid);
  161. } else {
  162. self.is_clean = ( angular.equals(original, self.news)
  163. && angular.equals(self.original_topic, self.topic_selected)
  164. && (self.gallery.files != null && self.gallery.files.length == 0) );
  165. if (self.image.file != null) {
  166. return (self.topic_valid && self.image.valid);
  167. } else {
  168. return (!self.is_clean && self.topic_valid);
  169. }
  170. }
  171. };
  172.  
  173. /* for selecting primary image file */
  174. self.onFileSelect = function (files) {
  175. self.image.valid = false;
  176. self.image.file = files[0];
  177. if (root.constrainFile(files[0])) {
  178. self.image.valid = true;
  179. }
  180. $mdToast.show($mdToast.simple().content("Selected file").position('bottom right'));
  181. };
  182.  
  183.  
  184. /* for selecting primary image file */
  185. self.onGalleryFileSelect = function (files) {
  186. self.gallery.valid = true;
  187. self.gallery.files = [];
  188. for (i = 0; i < files.length; i++) {
  189. if (!root.constrainFile(files[i])) {
  190. self.gallery.valid = false;
  191. break;
  192. }
  193. self.gallery.files.push(files[i]);
  194. }
  195. self.gallery.empty = self.gallery.valid && self.gallery.files.length == 0;
  196. $mdToast.show($mdToast.simple().content("Selected multi file").position('bottom right'));
  197. };
  198.  
  199. /* uploader for optional images, using recursive method*/
  200. var uploadGalleryImages = function(n_id, n){
  201. if(n == 0){
  202. deleteImages();
  203. self.images_obj = [];
  204. }
  205. if(n < self.gallery.files.length){
  206. var nfile = self.gallery.files[n];
  207. var name = now + n + "_" + root.getExtension(nfile);
  208. request.uploadFileToUrl(nfile, dir, name, '').then(function(resp){
  209. if(resp.status == 'success'){
  210. self.images_obj[n] = { news_id:n_id, name:name };
  211. uploadGalleryImages(n_id, (n+1),);
  212. }else{
  213. uploadGalleryImages(n_id, n);
  214. }
  215. });
  216. } else {
  217. if(self.images_obj.length > 0){
  218. request.insertAllNewsGallery(self.images_obj).then(function(resp){ self.done_arr[2] = true; });
  219. } else {
  220. self.done_arr[2] = true;
  221. }
  222. }
  223. };
  224.  
  225.  
  226. var deleteImages = function () {
  227. if(self.images_obj && self.images_obj.length > 0){
  228. var image_names = [];
  229. for(var i = 0; i < self.images_obj.length; i++) {
  230. image_names.push(self.images_obj[i].name);
  231. }
  232. request.deleteFiles(dir, image_names);
  233. }
  234. };
  235.  
  236. /* normalize news_topic object by adding news id */
  237. self.prepareNewsTopic = function (id) {
  238. self.news_topic = [];
  239. for (var i = 0; i < self.topic_selected.length; i++) {
  240. var item = {news_id: id, topic_id: self.topic_selected[i]};
  241. self.news_topic.push(item);
  242. }
  243. };
  244.  
  245. /* for notification */
  246. self.sendNotification = function (obj) {
  247. var title = (is_new) ? root.NEWS_ADD : root.NEWS_UPDATE;
  248. var body = root.getNotificationBody('NEWS', obj, title, obj.title, null);
  249. root.requestPostNotification(body, function (resp) {});
  250. };
  251.  
  252. self.cancel = function () {
  253. window.location.href = '#news';
  254. };
  255. self.isNewEntry = function () {
  256. return is_new;
  257. };
  258. self.draftChanged = function (draft) {
  259. if (draft == 1) self.send_notif = false;
  260. };
  261. self.showDatePicker = function(ev) {
  262. $mdpDatePicker($scope.currentDate, {
  263. targetEvent: ev
  264. }).then(function(date) {
  265. self.showTimePicker(ev, date)
  266. });
  267. };
  268. self.showTimePicker = function(ev, date) {
  269. $mdpTimePicker($scope.currentTime, {
  270. targetEvent: ev,
  271. autoSwitch: true
  272. }).then(function (time) {
  273. var date_result = new Date(
  274. date.getFullYear(), date.getMonth(), date.getDate(),
  275. time.getHours(), time.getMinutes(), 0
  276. );
  277. self.news_date_milis = date_result.getTime();
  278. console.log(date_result);
  279. self.news_date = moment(date_result).format('DD MMM YYYY HH:mm');
  280. });
  281. };
  282.  
  283. /* dialog View Image*/
  284. self.viewImage = function (ev, data) {
  285. $mdDialog.show({
  286. controller: ViewImageDialogController,
  287. parent: angular.element(document.body), targetEvent: ev, clickOutsideToClose: true, file_url: data,
  288. template: '<md-dialog ng-cloak aria-label="viewImage">' +
  289. ' <md-dialog-content style="max-width:800px;max-height:810px;" >' +
  290. ' <img style="margin: auto; max-width: 100%; max-height= 100%;" ng-src="{{file_url}}">' +
  291. ' </md-dialog-content>' +
  292. '</md-dialog>'
  293.  
  294. })
  295. };
  296.  
  297. });
  298.  
  299. function ViewImageDialogController($scope, $mdDialog, $mdToast, file_url) {
  300. $scope.file_url = file_url;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement