controllers.js 20 KB

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