controllers.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. starter.controller('ScheduleCtrl', function ($rootScope, $scope, $state, $cordovaCalendar, global, Schedule, Tool) {
  2. $scope.goBack = function () {
  3. global.goBack();
  4. }
  5. global.fetch_user().then(function () {
  6. getscheduledata();
  7. })
  8. $scope.toadd = function () {
  9. $state.go('schedule-create', {
  10. id: -1
  11. });
  12. }
  13. $scope.toman = function () {
  14. $state.go('schedule-man');
  15. }
  16. $scope.todetails = function (details) {
  17. Schedule.scheduleDetails = details;
  18. $state.go('schedule-details', {
  19. id: details.id
  20. });
  21. }
  22. $scope.doRefresh = function () {
  23. global.refresh = true;
  24. getscheduledata();
  25. }
  26. $scope.loadMore = function () {
  27. if ($scope.schedules != undefined && $scope.schedules.next != null) {
  28. Tool.get($scope.schedules.next).then(function (data) {
  29. _.each(data.results, function (item) {
  30. $scope.schedules.results.push(item);
  31. })
  32. $scope.schedules.next = data.next;
  33. $scope.schedules.previous = data.previous;
  34. }).finally(function () {
  35. $scope.$broadcast('scroll.infiniteScrollComplete');
  36. $scope.loading = false;
  37. });
  38. } else {
  39. $scope.$broadcast('scroll.infiniteScrollComplete');
  40. }
  41. }
  42. $scope.moreCanBeLoaded = function () {
  43. return $scope.schedules != undefined && $scope.schedules.next != null ? true : false;
  44. }
  45. function getscheduledata() {
  46. if (global.user.token != "") {
  47. if ($scope.schedules == undefined) {
  48. $scope.loading = true;
  49. }
  50. Schedule.schedule.get({'type': 'receive'}, function (res) {
  51. $scope.schedules = res;
  52. }, function (err) {
  53. alert(JSON.stringify(err));
  54. }).$promise.finally(function () {
  55. $scope.$broadcast('scroll.refreshComplete');
  56. $scope.loading = false;
  57. });
  58. }
  59. }
  60. enter = $scope.$on("$ionicView.enter", function (event, data) {
  61. if (data.fromCache && $rootScope.commons.refresh) {
  62. $rootScope.commons.refresh = false;
  63. getscheduledata();
  64. }
  65. })
  66. $scope.$on("$destroy", function () {
  67. enter = null;
  68. })
  69. // function getscheduledata() {
  70. // if (global.user.token != "") {
  71. // Schedule.getReceive().then(function (data) {
  72. // data_unover = [];
  73. // data_over = [];
  74. // _.each(data, function (item) {
  75. // var isover = false;
  76. // var currentDateTime = new Date();
  77. // if ((item.end_date != null && new Date(Date.parse(item.end_date)) < currentDateTime) ||
  78. // (item.repeat_type == 0 && item.start_date != null && new Date(Date.parse(item.start_date)) < currentDateTime)) {
  79. // isOver = true;
  80. // } else {
  81. // isOver = false;
  82. // }
  83. // if (isOver) {
  84. // //已过期
  85. // data_over.push(item);
  86. // } else {
  87. // //未过期
  88. // data_unover.push(item);
  89. // }
  90. //
  91. // });
  92. //
  93. // data_unover = _.sortBy(data_unover, 'execute_dd'); //升序
  94. // data_over = _.sortBy(data_over, 'execute_dd').reverse(); //降序
  95. // data = [];
  96. // _.each(data_unover, function (item) {
  97. // data.push(item);
  98. // });
  99. // _.each(data_over, function (item) {
  100. // data.push(item);
  101. // });
  102. // $scope.Schedule = data;
  103. // }, function (error) {
  104. // alert(JSON.stringify(error));
  105. // });
  106. // }
  107. // }
  108. })
  109. .controller('SchedulePostedCtrl', function ($rootScope, $scope, $state, global, Schedule, Tool) {
  110. global.fetch_user().then(function () {
  111. getscheduledata();
  112. })
  113. $scope.toadd = function () {
  114. $state.go('schedule-create');
  115. }
  116. $scope.todetails = function (id) {
  117. $state.go('schedule-details', {
  118. id: id
  119. });
  120. }
  121. $scope.doRefresh = function () {
  122. global.refresh = true;
  123. getscheduledata();
  124. }
  125. $scope.loadMore = function () {
  126. if ($scope.schedules != undefined && $scope.schedules.next != null) {
  127. Tool.get($scope.schedules.next).then(function (data) {
  128. _.each(data.results, function (item) {
  129. $scope.schedules.results.push(item);
  130. })
  131. $scope.schedules.next = data.next;
  132. $scope.schedules.previous = data.previous;
  133. }).finally(function () {
  134. $scope.$broadcast('scroll.infiniteScrollComplete');
  135. $scope.loading = false;
  136. });
  137. } else {
  138. $scope.$broadcast('scroll.infiniteScrollComplete');
  139. }
  140. }
  141. $scope.moreCanBeLoaded = function () {
  142. return $scope.schedules != undefined && $scope.schedules.next != null ? true : false;
  143. }
  144. function getscheduledata() {
  145. if (global.user.token != "") {
  146. if ($scope.schedules == undefined) {
  147. $scope.loading = true;
  148. }
  149. Schedule.schedule.get({'type': 'release'}, function (res) {
  150. $scope.schedules = res;
  151. }, function (err) {
  152. alert(JSON.stringify(err));
  153. }).$promise.finally(function () {
  154. $scope.$broadcast('scroll.refreshComplete');
  155. $scope.loading = false;
  156. });
  157. }
  158. }
  159. enter = $scope.$on("$ionicView.enter", function (event, data) {
  160. if (data.fromCache && $rootScope.commons.refresh) {
  161. $rootScope.commons.refresh = false;
  162. getscheduledata();
  163. }
  164. })
  165. $scope.$on("$destroy", function () {
  166. enter = null;
  167. })
  168. })
  169. .controller('ScheduleCreateCtrl', function ($rootScope, $scope, $state, $ionicHistory, $cordovaDatePicker, $cordovaCalendar, global, Schedule, Member, showPopup, Tool) {
  170. var id = $state.params['id'];
  171. var isModified = false;
  172. $scope.repeatfreqWeekDayHt = {
  173. "1": false,
  174. "2": false,
  175. "3": false,
  176. "4": false,
  177. "5": false,
  178. "6": false,
  179. "0": false
  180. }
  181. $scope.repeatfreqMonthDayHt = {
  182. "1": false,
  183. "2": false,
  184. "3": false,
  185. "4": false,
  186. "5": false,
  187. "6": false,
  188. "7": false,
  189. "8": false,
  190. "9": false,
  191. "10": false,
  192. "11": false,
  193. "12": false,
  194. "13": false,
  195. "14": false,
  196. "15": false,
  197. "16": false,
  198. "17": false,
  199. "18": false,
  200. "19": false,
  201. "20": false,
  202. "21": false,
  203. "22": false,
  204. "23": false,
  205. "24": false,
  206. "25": false,
  207. "26": false,
  208. "27": false,
  209. "28": false,
  210. "29": false,
  211. "30": false,
  212. "31": false
  213. }
  214. $scope.data = {
  215. content: null,
  216. start_date: getDateTimeYMDHM(1),
  217. start_date_title: null,
  218. repeat_type: 0,
  219. freq: '',
  220. repeat_freq_title: '不重复',
  221. end_date: null,
  222. repeat_end_date_type: 1,
  223. notification_type: 1,
  224. schedulemembers: []
  225. }
  226. $scope.popup = {
  227. isPopup: false
  228. }
  229. if (id > 0) {
  230. $scope.data = Tool.cloneObj(Schedule.scheduleDetails);
  231. $scope.data.freq = $scope.data.freq.split(',');
  232. if ($scope.data.repeat_type == 2) {
  233. setRepeatByFreq($scope.data.freq, $scope.repeatfreqWeekDayHt);
  234. } else if ($scope.data.repeat_type == 3) {
  235. setRepeatByFreq($scope.data.freq, $scope.repeatfreqMonthDayHt);
  236. }
  237. $scope.data.repeat_freq_title = Schedule.getRepeatFreqTitle($scope.data.repeat_type, $scope.data.freq);
  238. if ($scope.data.end_date == null) {
  239. $scope.data.repeat_end_date_type = 1;
  240. } else {
  241. $scope.data.repeat_end_date_type = 2;
  242. }
  243. var content_old = $scope.data.content;
  244. var date_old = $scope.data.start_date;
  245. }
  246. $scope.data.start_date_title = Schedule.getDateTimeTitle($scope.data.start_date);
  247. $scope.warningHt = Schedule.notificationType;
  248. $scope.repeatHt = Schedule.repeatType;
  249. $scope.weekDayHt = Schedule.weekDay;
  250. $scope.repeatfreqstyleunactive = {
  251. "border": "1px solid #ddd",
  252. "background-color": "#fff"
  253. }
  254. $scope.repeatfreqstyleactive = {
  255. "border": "1px solid #ddd",
  256. "background-color": "red"
  257. }
  258. $scope.goBack = function () {
  259. if (id < 0 && ($scope.data.content != null || $scope.data.schedulemembers.length > 0)) {
  260. showPopup.confirm('是否取消创建?', '是', '否').then(function (res) {
  261. if (res)
  262. global.goBack();
  263. });
  264. } else {
  265. global.goBack();
  266. }
  267. }
  268. $scope.selectmember = function () {
  269. Member.resourcemember = [];
  270. _.each($scope.data.schedulemembers, function (user) {
  271. Member.resourcemember.push(user);
  272. })
  273. Member.titlename = '选择成员'
  274. Member.disabled = false;
  275. $state.go('selectmember');
  276. }
  277. //选择重复 重复类型(0:永不,1:每天 2:每周,3:每月,4:每年)
  278. showPopup.modalTemplate('templates/modal-selectrepeatid.html', 'slide-in-right', $scope).then(function (modal) {
  279. $scope.repeat_id_modal = modal;
  280. });
  281. $scope.showRepeatIdModal = function () {
  282. $scope.repeat_id_modal.show();
  283. $rootScope.commons.modal = $scope.repeat_id_modal;
  284. }
  285. $scope.closeRepeatIdModal = function () {
  286. //0:永不,1:每天 2:每周,3:每月,4:每年
  287. if ($scope.data.repeat_type == "2") {
  288. $scope.data.freq = getFreqByRepeat($scope.repeatfreqWeekDayHt);
  289. } else if ($scope.data.repeat_type == "3") {
  290. $scope.data.freq = getFreqByRepeat($scope.repeatfreqMonthDayHt);
  291. }
  292. $scope.data.repeat_freq_title = Schedule.getRepeatFreqTitle($scope.data.repeat_type, $scope.data.freq);
  293. $rootScope.commons.modal.hide();
  294. }
  295. $scope.changeRepeatId = function (repeat_type) {
  296. //0:永不,1:每天 2:每周,3:每月,4:每年
  297. if (repeat_type == "1") {
  298. $scope.data.freq = 1;
  299. } else if (repeat_type == "2") {
  300. $scope.repeatfreqWeekDayHt[new Date().getDay()] = true;
  301. } else if (repeat_type == "3") {
  302. $scope.repeatfreqMonthDayHt[new Date().getDate()] = true;
  303. }
  304. $scope.data.repeat_type = repeat_type;
  305. }
  306. $scope.changeDayRepeatfreqIdColor = function (freq) {
  307. $scope.data.freq = freq;
  308. }
  309. $scope.changeWeekDayRepeatfreqIdColor = function (freq) {
  310. if (!(_.compact($scope.repeatfreqWeekDayHt).length == 1 && $scope.repeatfreqWeekDayHt[freq]))
  311. $scope.repeatfreqWeekDayHt[freq] = !$scope.repeatfreqWeekDayHt[freq];
  312. }
  313. $scope.changeMonthDayRepeatfreqIdColor = function (freq) {
  314. if (!(_.compact($scope.repeatfreqMonthDayHt).length == 1 && $scope.repeatfreqMonthDayHt[freq]))
  315. $scope.repeatfreqMonthDayHt[freq] = !$scope.repeatfreqMonthDayHt[freq];
  316. }
  317. //选择结束重复(null:一直重复)
  318. showPopup.modalTemplate('templates/modal-selectrepeatenddd.html', 'slide-in-right', $scope).then(function (modal) {
  319. $scope.repeat_end_dd_modal = modal;
  320. });
  321. $scope.showRepeatendddModal = function () {
  322. $scope.repeat_end_dd_modal.show();
  323. $rootScope.commons.modal = $scope.repeat_end_dd_modal;
  324. }
  325. $scope.closeRepeatendddModal = function () {
  326. $rootScope.commons.modal.hide();
  327. }
  328. $scope.changeRepeatendddId = function (repeat_end_date_type) {
  329. $scope.data.repeat_end_date_type = repeat_end_date_type;
  330. $scope.data.end_date = null;
  331. $scope.closeRepeatendddModal();
  332. }
  333. //选择提醒 提醒类型(0:不提醒,1:事件发生时,2:提前5分钟,3:提前15分钟,4:提前30分钟,5:提前1小时,6:提前2小时,7:提前5小时)
  334. showPopup.modalTemplate('templates/modal-selectwarningid.html', 'slide-in-right', $scope).then(function (modal) {
  335. $scope.warning_id_modal = modal;
  336. });
  337. $scope.showWarningIdModal = function () {
  338. $scope.warning_id_modal.show();
  339. $rootScope.commons.modal = $scope.warning_id_modal;
  340. }
  341. $scope.closeWarningIdModal = function () {
  342. $rootScope.commons.modal.hide();
  343. }
  344. $scope.changeWarningId = function (notification_type) {
  345. $scope.data.notification_type = notification_type;
  346. $scope.closeWarningIdModal();
  347. }
  348. $scope.addSchedule = function () {
  349. if (Schedule.checkSchedule($scope.data)) {
  350. showPopup.showLoading(1, '正在提交');
  351. if (id < 0) {
  352. createSchdule();
  353. } else {
  354. // if (isModified) { // todo :edit change is modified
  355. Schedule.deleteEvent(content_old, date_old);
  356. createSchdule();
  357. // }
  358. }
  359. }
  360. }
  361. $scope.chooseDate = function () {
  362. var options = {
  363. mode: 'date',
  364. date: $scope.data.end_date != null ? new Date($scope.data.end_date) : new Date(),
  365. androidTheme: 3
  366. };
  367. $cordovaDatePicker.show(options).then(function (date) {
  368. if (date == undefined) return;
  369. $scope.data.end_date = date;
  370. $scope.data.repeat_end_date_type = 2;
  371. $scope.closeRepeatendddModal();
  372. }, function (cancel) {
  373. $scope.data.end_date = null;
  374. $scope.data.repeat_end_date_type = 1;
  375. });
  376. }
  377. $scope.chooseStartddDate = function () {
  378. var options = {
  379. mode: 'datetime',
  380. date: new Date($scope.data.start_date),
  381. androidTheme: 3
  382. };
  383. $cordovaDatePicker.show(options).then(function (date) {
  384. if (date == undefined) return;
  385. $scope.data.start_date = date;
  386. $scope.data.start_date_title = Schedule.getDateTimeTitle($scope.data.start_date);
  387. });
  388. }
  389. var watch = $scope.$watch('data', function (n, o) {
  390. if (n == o)
  391. return;
  392. isModified = true;
  393. }, true);
  394. var enter = $scope.$on("$ionicView.enter", function () {
  395. selectMember();
  396. });
  397. $scope.$on("$destroy", function () {
  398. if ($rootScope.commons.modal)
  399. $rootScope.commons.modal = null;
  400. $scope.repeat_end_dd_modal.remove();
  401. $scope.warning_id_modal.remove();
  402. $scope.repeat_id_modal.remove();
  403. enter = null;
  404. watch();
  405. })
  406. function createSchdule() {
  407. if ($scope.data.repeat_end_date_type == 1)
  408. $scope.data.end_date = null;
  409. var data = _.pick($scope.data, 'content', 'start_date', 'repeat_type', 'freq', 'end_date', 'notification_type', 'schedulemembers');
  410. if (data.freq.length > 0)
  411. data.freq = data.freq.join(',');
  412. data.schedulemembers = _.map(data.schedulemembers, function (user) {
  413. return user.user_id;
  414. })
  415. if (id < 0) {
  416. Schedule.schedule.save(data, function (data) {
  417. $rootScope.commons.refresh = true;
  418. Schedule.createEventWithOptions($scope.data.content, $scope.data.start_date, $scope.data.end_date, $scope.data.repeat_type, $scope.data.freq, $scope.data.notification_type);
  419. showPopup.hideLoading();
  420. $ionicHistory.goBack();
  421. })
  422. } else {
  423. data.id = id;
  424. Schedule.schedule.update(data, function (data) {
  425. $rootScope.commons.refresh = true;
  426. Schedule.createEventWithOptions($scope.data.content, $scope.data.start_date, $scope.data.end_date, $scope.data.repeat_type, $scope.data.freq, $scope.data.notification_type);
  427. showPopup.hideLoading();
  428. $ionicHistory.goBack();
  429. })
  430. }
  431. }
  432. function getFreqByRepeat(repeat) {
  433. var freq = []
  434. _.each(repeat, function (value, key) {
  435. if (value) freq.push(parseInt(key));
  436. })
  437. return freq;
  438. }
  439. function setRepeatByFreq(freq, repeat) {
  440. _.each(freq, function (f) {
  441. repeat[f] = !repeat[f];
  442. })
  443. }
  444. function selectMember() {
  445. if (Member.selectedemplst.length > 0) {
  446. $scope.data.schedulemembers = [];
  447. console.log(Member.selectedemplst);
  448. _.each(Member.selectedemplst, function (item) {
  449. $scope.data.schedulemembers.push({
  450. user_id: item.user_id,
  451. username: item.username,
  452. deptname: item.depname,
  453. degree: item.degree
  454. })
  455. });
  456. $scope.sel_member_names = Schedule.getMemberTitle($scope.data.schedulemembers);
  457. Member.selectedemplst = [];
  458. }
  459. }
  460. function getDateTimeYMDHM(add_hours) {
  461. var date = new Date()
  462. date.setHours(date.getHours() + add_hours);
  463. date.setMinutes(0);
  464. return date;
  465. }
  466. })
  467. .controller('ScheduleDetailsCtrl', function ($rootScope, $scope, $state, $ionicHistory, $ionicPopover, Schedule, global, showPopup, Tool) {
  468. var id = $state.params['id'];
  469. $scope.popup = {
  470. isPopup: false,
  471. }
  472. var view = $ionicHistory.backView();
  473. global.fetch_user().then(function () {
  474. if (view) {
  475. $scope.details = Schedule.scheduleDetails;
  476. // $scope.execute_dd_title = Schedule.getDateTimeTitle(res.execute_dd);
  477. $scope.scheduleMemberNames = Schedule.getMemberTitle($scope.details.schedulemembers);
  478. $scope.repeat_req_title = Schedule.getRepeatFreqTitle($scope.details.repeat_type, $scope.details.freq);
  479. $scope.isShowRemoveMember = _.find($scope.details.schedulemembers, function (user) {
  480. return user.user_id == global.user.usrid;
  481. });
  482. $scope.isOver = false;
  483. if ($scope.details.end_date != null && new Date() > new Date($scope.details.end_date))
  484. $scope.isOver = true;
  485. } else {
  486. getscheduledetails();
  487. }
  488. });
  489. $ionicPopover.fromTemplateUrl('templates/detailsmenu.html', {
  490. scope: $scope,
  491. }).then(function (popover) {
  492. $scope.popover = popover;
  493. });
  494. $scope.deleteSchedule = function () {
  495. $scope.popover.hide();
  496. $scope.popup.isPopup = true;
  497. showPopup.confirm('是否删除该日程?', '是', '否').then(function (res) {
  498. if (res) {
  499. Schedule.deleteEvent($scope.details.content, $scope.details.start_date);
  500. Schedule.schedule.delete({'id': id}, function () {
  501. $rootScope.commons.refresh = true;
  502. $ionicHistory.goBack();
  503. });
  504. }
  505. });
  506. }
  507. $scope.deleteMember = function () {
  508. $scope.popup.isPopup = true;
  509. showPopup.confirm('退出后,你将不再参与该日程', '是', '否').then(function (res) {
  510. if (res) {
  511. Schedule.deleteEvent($scope.details.content, $scope.details.start_date);
  512. Schedule.schedulemenber.delete({'schedule_id': id}).then(function () {
  513. $rootScope.commons.refresh = true;
  514. $ionicHistory.goBack();
  515. });
  516. }
  517. });
  518. }
  519. $scope.edit = function () {
  520. $scope.popover.hide();
  521. $state.go('schedule-create', {
  522. 'id': $scope.details.id
  523. })
  524. }
  525. $scope.doRefresh = function () {
  526. global.refresh = true;
  527. getscheduledetails();
  528. }
  529. function getscheduledetails() {
  530. if (global.user.token != "") {
  531. if ($scope.details == undefined) {
  532. $scope.loading = true;
  533. }
  534. Schedule.schedule.get({'id': id}, function (res) {
  535. Schedule.scheduleDetails = _.clone(res);
  536. console.log(res);
  537. $scope.details = res;
  538. // $scope.execute_dd_title = Schedule.getDateTimeTitle(res.execute_dd);
  539. $scope.scheduleMemberNames = Schedule.getMemberTitle(res.schedulemembers);
  540. $scope.repeat_req_title = Schedule.getRepeatFreqTitle(res.repeat_type, res.freq);
  541. $scope.isShowRemoveMember = _.find(res.schedulemembers, function (user) {
  542. return user.user_id == global.user.usrid;
  543. });
  544. $scope.isOver = false;
  545. if ($scope.details.end_date != null && new Date() > new Date($scope.details.end_date))
  546. $scope.isOver = true;
  547. // //是否过期
  548. // var currentDateTime = new Date();
  549. // var executeddTime = new Date(Date.parse($scope.data.execute_dd));
  550. // if (($scope.data.end_date != null && new Date(Date.parse($scope.data.end_date)) < currentDateTime) && currentDateTime > executeddTime ||
  551. // ($scope.data.repeat_type == 0 && $scope.data.start_date != null && new Date(Date.parse($scope.data.start_date)) < currentDateTime)) {
  552. // $scope.isOver = true;
  553. // } else {
  554. // $scope.isOver = false;
  555. // }
  556. }, function (err) {
  557. alert(JSON.stringify(err));
  558. }).$promise.finally(function () {
  559. $scope.$broadcast('scroll.refreshComplete');
  560. $scope.loading = false;
  561. })
  562. }
  563. }
  564. enter = $scope.$on("$ionicView.enter", function (event, data) {
  565. if (data.fromCache && $rootScope.commons.refresh) {
  566. if (view == null)
  567. $rootScope.commons.refresh = false;
  568. getscheduledetails();
  569. }
  570. });
  571. $scope.$on("$destroy", function () {
  572. $scope.popover.remove();
  573. enter = null;
  574. });
  575. });