todolist.js 22.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
(function () {
    "use strict";
    var app = angular.module("xn.page", []);

    app.controller("TodoListController", ["$scope", "$http", "$location", "$modal", "$filter", "dialogService", "toolsService", "TaskService", "SystemService",
        function ($scope, $http, $location, $modal, $filter, dialogService, toolsService, TaskService, SystemService) {
            console.log($scope.userId)
            $scope.ownerUserId = $scope.userId;
            $scope.ownerUserName = $scope.userName;
            $scope.ownerUser = {
                id: $scope.userId,
                name: $scope.userName
            };
            var now = new Date();
            // 今天本周的第几天
            var nowDayOfWeek = now.getDay();
            // 如果是周日,就变成周七
            nowDayOfWeek = nowDayOfWeek == 0 ? 7 : nowDayOfWeek;
            // 获得本周的停止日期
            var weekEndDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (6 - nowDayOfWeek + 1));
            $scope.taskList = {
                starred: {
                    $$type: "starred",
                    isStar: true,
                    isDone: false,
                    pageSize: 0
                },
                today: {
                    $$type: "today",
                    deadlineDate: new Date().getTime(),
                    isDone: false,
                    pageSize: 0
                },
                week: {
                    $$type: "week",
                    deadlineDate: weekEndDate.getTime(),
                    isDone: false,
                    pageSize: 0
                },
                list: []
            };

            $scope.task = {
                canAdd: false,
                showTodo: false,
                title: "",
                todoList: [],
                doneList: []
            };
            $scope.left_view_edit_move = "left_view_edit_init";

            $scope.attachment={
                businessType:"COLLABORATION_TASK",
                businessCategory:"TASK",
                list:[]
            };

            /**
             * 获取任务清单
             */
            $scope.findTaskListSummary = function (activeItem) {
                $scope.task.doneList = [];
                $scope.task.todoList = [];
                TaskService.apiTaskListSummaryFind({ownerUserId: $scope.ownerUserId}).success(function (data) {
                    if (data.errors === null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        $scope.taskList.list = data.result;
                        if (activeItem) {
                        }
                    }
                });
                TaskService.apiTaskListFilterFind({ownerUserId: $scope.ownerUserId}).success(function (data) {
                    if (data.errors === null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        $scope.taskList.starred.taskCount = data.starredTaskCount;
                        $scope.taskList.starred.overdueCount = data.starredOverdueCount;
                        $scope.taskList.today.taskCount = data.todayTaskCount;
                        $scope.taskList.today.overdueCount = data.todayOverdueCount;
                        $scope.taskList.week.taskCount = data.weekTaskCount;
                        $scope.taskList.week.overdueCount = data.weekOverdueCount;
                    }
                });
            };
            $scope.findTaskListSummary();

            //获取成员
            $scope.getUser = function (method) {
                var vm = {
                    keyword: method.data.keyword,
                    pageSize: method.data.pageSize,
                    pageNumber: method.data.pageNumber
                };


                SystemService.findUser(vm).success(function (data) {
                    if (data.errors === null || data.errors.length > 0) {
                        dialogService.tip(data.errors, null, null);
                    } else {
                        method.deferred.resolve({itemList: data.result, totalCount: data.totalCount});
                    }
                });
            };

            $scope.selectUser = function (method) {
                $scope.ownerUserId = method.id;
                $scope.ownerUserName = method.name;
                $scope.findTaskListSummary();
            };
          /*  $scope.$watch(function () {
                return $scope.ownerUser;
            }, function (newValue) {
                if (newValue=="") {
                    console.log("warch ownerUser");

                }
            },true);*/

            /**
             * 查询任务
             */
            $scope.searchTask = function () {
                $scope.task.canAdd = false;
                var param = {
                    ownerUserId: $scope.ownerUserId,
                    keyword: $scope.keyword,
                    isDone: false,
                    pageSize: 0
                };
                if (!param.keyword) {
                    return;
                }
                $scope.task.title = param.keyword;
                $scope.findTask(param);
            };

            /**
             * 任务清单选择处理
             */
            $scope.selectTaskList = function (item) {
                $scope.taskList.starred.isActive = false;
                $scope.taskList.today.isActive = false;
                $scope.taskList.week.isActive = false;
                for (var i = 0, len = $scope.taskList.list.length; i < len; i++) {
                    var taskList = $scope.taskList.list[i];
                    taskList.isActive = false;
                }
                item.isActive = true;

                console.log(item);

                if (item.id) {
                    $scope.task.canAdd = true;
                    $scope.task.objectType = item.taskObjectType;
                    $scope.task.objectCategory = item.taskObjectCategory;
                    $scope.task.listId = item.id;
                    var param = {
                        listId: item.id,
                        objectType: item.taskObjectType,
                        objectCategory: item.taskObjectCategory,
                        pageSize: 0
                    };
                    $scope.findTask(param);

                    if (item.objectType == "MANUAL") {
                        $scope.task.title = item.objectName;
                        $scope.task.objectType = "MANUAL";
                        $scope.task.objectCategory = "NONE";
                    } else {
                        $scope.task.title = item.objectName + "-" + $filter("taskCategory")(item.taskObjectCategory);
                    }
                } else {
                    $scope.task.canAdd = false;
                    item.ownerUserId = $scope.ownerUserId;
                    TaskService.apiTaskFind(item).success(function (data) {
                        if (data.errors === null || data.errors.length > 0) {
                            dialogService.tip(data.errors);
                        } else {
                            console.log(data);
                            $scope.task.todoList = data.result;
                        }
                    });
                    $scope.task.$$type = item.$$type;
                    if (item.$$type == "starred") {
                        $scope.task.title = "已加星标";
                    } else if (item.$$type == "today") {
                        $scope.task.title = "今天";
                    } else if (item.$$type == "week") {
                        $scope.task.title = "周";
                    }
                }
            };

            $scope.findTask = function (param) {
                $scope.task.doneList = [];
                $scope.task.todoList = [];
                $scope.showTodo = false;
                TaskService.apiTaskFind(param).success(function (data) {
                    if (data.errors === null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        console.log(data);

                        for (var i = 0, len = data.result.length; i < len; i++) {
                            var task = data.result[i];
                            if (task.isDone) {
                                $scope.task.doneList.push(task);
                            } else {
                                $scope.task.todoList.push(task);
                            }
                        }
                    }
                });
            };

            /**
             * 任务选择处理
             */
            $scope.selectTask = function (item) {
                for (var i = 0, len = $scope.task.todoList.length; i < len; i++) {
                    var task = $scope.task.todoList[i];
                    task.isActive = false;
                }
                for (var i = 0, len = $scope.task.doneList.length; i < len; i++) {
                    var task = $scope.task.doneList[i];
                    task.isActive = false;
                }
                item.isActive = true;
            };

            /**
             * 设置星标
             */
            $scope.enableStar = function (item) {
                TaskService.apiTaskStarEnable(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        item.isStar = true;
                        item.rowVersion = Number(item.rowVersion) + 1;
                        console.log(item);
                    }
                });
            };

            /**
             * 取消星标
             */
            $scope.disableStar = function (item, index) {
                TaskService.apiTaskStarDisable(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        item.isStar = false;
                        item.rowVersion = Number(item.rowVersion) + 1;
                        if ($scope.task.$$type == "starred") {
                            $scope.task.todoList.splice(index, 1);
                        }
                    }
                    console.log(item);
                });
            };

            /**
             * 设置任务完成
             */
            $scope.done = function (item, index) {
                TaskService.apiTaskDone(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        $scope.task.todoList.splice(index, 1);
                        TaskService.apiTaskGet(item).success(function (data) {
                            if (data.errors == null || data.errors.length > 0) {
                                dialogService.tip(data.errors);
                            } else {
                                item = data.task;
                                $scope.task.doneList.unshift(item);
                            }
                        });
                    }
                });
            };

            /**
             * 设置任务未完成
             */
            $scope.todo = function (item, index) {
                TaskService.apiTaskTodo(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        item.isDone = false;
                        item.rowVersion = Number(item.rowVersion) + 1;
                        $scope.task.doneList.splice(index, 1);
                        $scope.task.todoList.unshift(item);
                    }
                });
            };

            /**
             * 创建清单
             */
            $scope.createTaskList = function () {
                var modalInstance = $modal.open({
                    templateUrl: "createTaskList.html",
                    controller: CreateTaskList,
                    size: "",
                    resolve: {
                        //items: function () {
                        //    return {
                        //        title: '新增交付物'
                        //    };
                        //}
                    }
                });
                modalInstance.result.then(function (data) {
                    $scope.findTaskListSummary();
                });
            };

            /**
             * 编辑清单
             */
            $scope.editTaskList = function (item) {
                var modalInstance = $modal.open({
                    templateUrl: "editTaskList.html",
                    controller: EditTaskList,
                    size: "",
                    resolve: {
                        items: function () {
                            return {
                                id: item.id
                            };
                        }
                    }
                });
                modalInstance.result.then(function (data) {
                    $scope.findTaskListSummary();
                });
            };

            /**
             * 创建任务
             */
            $scope.createTask = function () {
                if (!$scope.task.objectName) {
                    return;
                }
                var param = {
                    objectType: $scope.task.objectType,
                    objectCategory: $scope.task.objectCategory,
                    objectName: $scope.task.objectName,
                    listId: $scope.task.listId
                };
                TaskService.apiTaskCreate(param).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        $scope.task.objectName = null;
                        var searchParam = {
                            objectType: $scope.task.objectType,
                            objectCategory: $scope.task.objectCategory,
                            listId: $scope.task.listId,
                            pageSize: 0
                        };
                        $scope.findTask(searchParam);
                        dialogService.tip([{message: "任务创建成功"}]);
                    }
                });
            };

            //弹出式日历触发函数
            $scope.openDeadlineDate = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.isOpenDeadlineDate = true;
            };
            $scope.openRemindingTime = function ($event) {
                $event.preventDefault();
                $event.stopPropagation();
                $scope.isOpenRemindingTime = true;
            };

            /**
             * 打开任务
             */
            $scope.openTask = function (item) {
                TaskService.apiTaskGet(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        $scope.task.item = data.task;
                        $scope.task.taskName = $scope.task.item.objectName;
                        $scope.attachment.businessId = $scope.task.item.id;
                        if ($scope.task.item.deadlineDate) {
                            $scope.task.item.deadlineDate = new Date($scope.task.item.deadlineDate*1);
                        }
                        if ($scope.task.item.remindingTime) {
                            $scope.task.item.remindingTime = new Date($scope.task.item.remindingTime*1);
                        }
                        $scope.task.subtaskList = data.subtaskList;
                        if (!$scope.task.subtaskList || $scope.task.subtaskList.length == 0) {
                            $scope.task.subtaskList.push({});
                        }
                    }
                });
                $scope.left_view_edit_move = "left_view_edit_enter";
            };

            $scope.addSubTask = function () {
                $scope.task.subtaskList.push({});
            };
            $scope.removeSubTask = function (index) {
                $scope.task.subtaskList.splice(index, 1);
                if ($scope.task.subtaskList.length == 0) {
                    $scope.task.subtaskList.push({});
                }
            };
            $scope.$watch(function () {
                return $scope.task.subtaskList;
            }, function (newValue) {
                if (newValue) {
                    var last = newValue[newValue.length-1];
                    if (last.name) {
                        newValue.push({});
                    }
                    console.log(last);
                }
            },true);

            /**
             * 编辑任务
             */
            $scope.editTask = function (item) {
                if ($scope.task.item.deadlineDate instanceof Date) {
                    $scope.task.item.deadlineDate = $scope.task.item.deadlineDate.format('yyyy-MM-dd');
                }
                if ($scope.task.item.remindingTime instanceof Date) {
                    $scope.task.item.remindingTime = $scope.task.item.remindingTime.format('yyyy-MM-dd');
                }
                item.subtaskList = [];
                for (var i = 0, len = $scope.task.subtaskList.length; i < len; i++) {
                    var subtask = $scope.task.subtaskList[i];
                    if (subtask.name) {
                        item.subtaskList.push(subtask);
                    }
                }

                TaskService.apiTaskUpdate(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        var searchParam = {
                            objectType: $scope.task.objectType,
                            objectCategory: $scope.task.objectCategory,
                            listId: $scope.task.listId,
                            pageSize: 0
                        };
                        $scope.findTask(searchParam);
                        dialogService.tip([{message: "任务编辑成功"}]);
                    }
                });
                $scope.left_view_edit_move = "left_view_edit_leave";
            };

            /**
             * 删除任务
             */
            $scope.deleteTask = function (item) {
                TaskService.apiTaskDelete(item).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        var searchParam = {
                            objectType: $scope.task.objectType,
                            objectCategory: $scope.task.objectCategory,
                            listId: $scope.task.listId,
                            pageSize: 0
                        };
                        $scope.findTask(searchParam);
                        dialogService.tip([{message: "任务删除成功"}]);
                    }
                });
                $scope.left_view_edit_move = "left_view_edit_leave";
            };

            /**
             * 关闭任务
             */
            $scope.colseTask = function (item) {
                $scope.left_view_edit_move = "left_view_edit_leave";
            };
        }]);
    var CreateTaskList = ["$scope", "$modalInstance", "TaskService", "dialogService"
        , function ($scope, $modalInstance, TaskService, dialogService) {
            $scope.sc = {
                objectType: "MANUAL"
            };
            $scope.doSave = function () {
                TaskService.apiTaskListCreate($scope.sc).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        dialogService.tip([{message: "清单创建成功"}]);
                        $modalInstance.close($scope.sc);
                        $scope.sc = {};
                    }
                });
            };
            $scope.closeAlert = function (index, form) {
                form.splice(index, 1);
            };
            $scope.clean = function () {
                $modalInstance.dismiss('cancel');
            };
        }];
    var EditTaskList = ["$scope", "$modalInstance", "items", "TaskService", "dialogService"
        , function ($scope, $modalInstance, items, TaskService, dialogService) {
            $scope.sc = {
                objectType: "MANUAL"
            };

            TaskService.apiTaskListGet(items).success(function (data) {
                if (data.errors === null || data.errors.length > 0) {
                    dialogService.tip(data.errors);
                } else {
                    $scope.sc = data.taskList;
                }
            });
            $scope.doSave = function () {
                TaskService.apiTaskListUpdate($scope.sc).success(function (data) {
                    if (data.errors == null || data.errors.length > 0) {
                        dialogService.tip(data.errors);
                    } else {
                        dialogService.tip([{message: "清单编辑成功"}]);
                        $modalInstance.close($scope.sc);
                        $scope.sc = {};
                    }
                });
            };
            $scope.doDelete = function () {
                var dialogDefaults = {
                    size: "sm"
                };
                var dialogOptions = {
                    closeButtonText: "取消",
                    actionButtonText: "确定删除",
                    headerText: "继续删除?",
                    bodyText: "确定要删除吗?",
                    callback: function () {
                        TaskService.apiTaskListDelete($scope.sc).success(function (data) {
                            if (data.errors == null || data.errors.length > 0) {
                                dialogService.tip(data.errors);
                            } else {
                                dialogService.tip([{message: "清单删除成功"}]);
                                $modalInstance.close($scope.sc);
                                $scope.sc = {};
                            }
                        });
                    }
                };
                dialogService.confirm(dialogDefaults, dialogOptions);
            };

            $scope.closeAlert = function (index, form) {
                form.splice(index, 1);
            };
            $scope.clean = function () {
                $modalInstance.dismiss('cancel');
            };
        }];
})
();