BemsWebApplication.Accident = function (params, viewInfo) { "use strict"; var imagefileUrl1 = ko.observable(), imagefileUrl2 = ko.observable(), imagefileUrl3 = ko.observable(), dropZone1 = null, dropZone2 = null, uploadedFileInfo = {}; var isEditModeInPopup = ko.observable(false); var eq = BWA.DataUtil.constructEqualFilter, and = BWA.DataUtil.andFilter, noteq = BWA.DataUtil.constructNotEqualFilter; var accidentTypeDataSource = BemsWebApplication.db.createDataSource('FmsAccidentCodeType', true, false, true), companyDataSource = BemsWebApplication.db.createDataSource('CmCompany', true, true), departmentDataSource = BemsWebApplication.db.createDataSource('CmDepartment', true, true); var accidentTypesForSearch = ko.observableArray(); var accidentTypes = ko.observableArray(), companies = ko.observableArray(), departments = ko.observableArray(); var deferredForSearch = new $.Deferred(); var dataSourceOptions = { select: [ 'SiteId', 'AccidentId', 'Name', 'StartDate', 'EndDate', 'AccidentTypeId', 'DepartmentId', 'AccidentLocation', 'Reason', 'Action', 'Damage', 'Measurement', 'FileId1', 'FileId2', 'FileId3', 'CompanyId', 'CmFile/Name', 'CmFile1/Name', 'CmFile2/Name', 'FmsAccidentCodeType/Name', 'CmDepartment/Name', 'CmCompany/Name', ], expand: [ 'CmDepartment', 'CmCompany', 'FmsAccidentCodeType', 'CmFile', 'CmFile1', 'CmFile2' ], extendOptions: { forceOriginalField: true } }; function setupDropZone(isEditModeInPopup, dataViewModel) { var timer = null; isEditModeInPopup.subscribe(function (value) {//2019-11-12 변경 if (value) { if (timer) { clearInterval(timer); } timer = setInterval(function () { if ($('#accidentDropZone1').length) { if (_.isNull(dropZone1)) { dropZone1 = BWA.DropZone.create({ id: '#accidentDropZone1', categoryId: $Code.FileCategory.ACCIDENT, uploadedFileInfo: uploadedFileInfo, handleRemoveBeforePromise: function () { var keys = BWA.db.extractKeysObject('FmsAccident', dataViewModel); return BWA.db.FmsAccident.update(keys, { FileId1: null }); }, handleSuccess: function (fileInfo) { if (uploadedFileInfo.FileId) { dataViewModel.FileId1(fileInfo.FileId); var keys = BWA.db.extractKeysObject('FmsAccident', dataViewModel); if (keys.AccidentId != null) { return BWA.db.FmsAccident.update(keys, { FileId1: fileInfo.FileId }) .done(function (res) { $('#gridContainer').dxDataGrid('instance').refresh(); }); } } }, handleRemovedFile: function () { dataViewModel.FileId1(null); }, }); } } if ($('#accidentDropZone2').length) { if (_.isNull(dropZone2)) { dropZone2 = BWA.DropZone.create({ id: '#accidentDropZone2', categoryId: $Code.FileCategory.ACCIDENT, uploadedFileInfo: uploadedFileInfo, handleRemoveBeforePromise: function () { var keys = BWA.db.extractKeysObject('FmsAccident', dataViewModel); return BWA.db.FmsAccident.update(keys, { FileId2: null }); }, handleSuccess: function (fileInfo) { if (uploadedFileInfo.FileId) { dataViewModel.FileId2(fileInfo.FileId); var keys = BWA.db.extractKeysObject('FmsAccident', dataViewModel); if (keys.AccidentId != null) { return BWA.db.FmsAccident.update(keys, { FileId2: fileInfo.FileId }) .done(function (res) { $('#gridContainer').dxDataGrid('instance').refresh(); }); } } }, handleRemovedFile: function () { dataViewModel.FileId2(null); }, }); } } }, 100); } else { if (timer) { clearInterval(timer); timer = null; } dropZone1 = null; dropZone2 = null; } }); } var viewModel = BWA.DataGrid.createViewWithDataGrid(params, viewInfo, 'FmsAccident', { dataSourceOptions: dataSourceOptions, columns: [ { dataField: 'AccidentId', caption: $G('number'), width: '10%', alignment: 'center' }, { dataField: 'FmsAccidentCodeType/Name', caption: '사고유형', width: '20%', alignment: 'center' }, { dataField: 'Name', caption: '사고명', width: '80%', alignment: 'center' }, { dataField: 'CmDepartment/Name', caption: '담당부서', width: '30%', alignment: 'center' }, { dataField: 'StartDate', caption: '사고발생일', width: '20%', alignment: 'center', sortOrder: 'desc', dataType: "shortDate", format: "yyyy-MM-dd", customizeText: function (cellInfo) { return cellInfo.valueText; } }, { dataField: 'EndDate', caption: '조치완료일', width: '20%', alignment: 'center', dataType: "shortDate", format: "yyyy-MM-dd", customizeText: function (cellInfo) { return cellInfo.valueText; } }, ], paging: { enabled: true, pageSize: 18 }, pager: { }, "export": { enabled: true, fileName: '사고 목록', }, onExporting: function (e) { e.component.columnOption("AccidentId", "visible", false); }, onExported: function (e) { e.component.columnOption("AccidentId", "visible", true); }, popupWidth: '900px', searchViewItems: [ { id: 'Name' }, { id: 'AccidentTypeId', ignoreValue: 0, defaultValue: 0, dataSource: accidentTypesForSearch }, { id: 'StartDate', type: 'dateRange', isOnlyDate: true }, { id: 'EndDate', type: 'dateRange', isOnlyDate: true } ], beforeInsertingDataViewModel: function (dataModel, dbModelId) { var dfd = $.Deferred(); if (dataModel.StartDate() > dataModel.EndDate()) { return dfd.resolve(false, '사고발생일은 조치완료일 이전으로 선택해야 합니다.'); } else { return dfd.resolve(true);; } }, beforeUpdateDataViewModel: function () { var dataModel = viewModel.dataModel; if (dataModel.StartDate() > dataModel.EndDate()) { utils.toast.show('사고발생일은 조치완료일 이전으로 선택해야 합니다.', 'error'); return 0; } else { return 2; } }, handleAfterSaveInPopup: function (responseKey, response) {//2019-11-12 변경 viewModel.dataModel.AccidentId(responseKey.AccidentId); }, handleSaveButtonInPopup: function (isInsert) {//2019-11-12 변경 if (isInsert === false) {//추가, 변경 여부 if (_.isNull(dropZone1) === false) { if (viewModel.dataModel.FileId1() != null) { //변경 - 그림삭제 var temp1 = { SiteId: BWA.UserInfo.SiteId(), FileId: viewModel.dataModel.FileId1() } BWA.db.CmFile.byKey(temp1).done(function (data) { dropZone1.removeFile(data); dropZone1.processQueue(); }); } else { dropZone1.processQueue();//변경 - 그림 삭제 필요없음 } } } else { if (_.isNull(dropZone1) === false) { //추가 if (viewModel.dataModel.FileId1() == null) { dropZone1.processQueue(); } } } if (isInsert === false) {//추가, 변경 여부 if (_.isNull(dropZone2) === false) { if (viewModel.dataModel.FileId2() != null) { //변경 - 그림삭제 var temp2 = { SiteId: BWA.UserInfo.SiteId(), FileId: viewModel.dataModel.FileId2() } BWA.db.CmFile.byKey(temp2).done(function (data) { dropZone2.removeFile(data); dropZone2.processQueue(); }); } else { dropZone2.processQueue();//변경 - 그림 삭제 필요없음 } } } else { if (_.isNull(dropZone2) === false) { //추가 if (viewModel.dataModel.FileId2() == null) { dropZone2.processQueue(); } } } }, handleBeforeDelete: function () {//2019-11-12 변경 var temp1 = { SiteId: BWA.UserInfo.SiteId(), FileId: viewModel.dataModel.FileId1() } if (temp1.FileId != null) { BWA.db.CmFile.byKey(temp1).done(function (data) { if ($('#accidentDropZone3').length) { if (_.isNull(dropZone1)) { dropZone1 = BWA.DropZone.create({ id: '#accidentDropZone3', categoryId: $Code.FileCategory.ACCIDENT }); dropZone1.removeFile(data); dropZone1 = null; } } }); } var temp2 = { SiteId: BWA.UserInfo.SiteId(), FileId: viewModel.dataModel.FileId2() } if (temp2.FileId != null) { BWA.db.CmFile.byKey(temp2).done(function (data) { if ($('#accidentDropZone4').length) { if (_.isNull(dropZone2)) { dropZone2 = BWA.DropZone.create({ id: '#accidentDropZone4', categoryId: $Code.FileCategory.ACCIDENT }); dropZone2.removeFile(data); dropZone2 = null; } } }); } }, handleAfterLoadingModelByKey: function (data, dataViewModel, isNewInPopup) { if (isNewInPopup() == false) { BWA.DataUtil.setArrayDataSourceFilter(departmentDataSource, 'CompanyId', dataViewModel.CompanyId(), departments); if (_.isNull(dataViewModel.FileId1()) === false) { imagefileUrl1('{0}/CmFile(SiteId={1},FileId={2})/$value'.formati(BWA.db._url, dataViewModel.SiteId(), dataViewModel.FileId1())); } else { imagefileUrl1(undefined); } if (_.isNull(dataViewModel.FileId2()) === false) { imagefileUrl2('{0}/CmFile(SiteId={1},FileId={2})/$value'.formati(BWA.db._url, dataViewModel.SiteId(), dataViewModel.FileId2())); } else { imagefileUrl2(undefined); } if (_.isNull(dataViewModel.FileId3()) === false) { imagefileUrl3('{0}/CmFile(SiteId={1},FileId={2})/$value'.formati(BWA.db._url, dataViewModel.SiteId(), dataViewModel.FileId3())); } else { imagefileUrl3(undefined); } } }, handleDataGridRowClick: function (id, dataGrid, clickRow, popupVisible) { dataGrid.clearSelection(); var data = clickRow.data; var dataModel = viewModel.dataModel; if (data['CompanyId']() != null) dataModel.CompanyName = $KoSet(dataModel.CompanyName, data['CmCompany/Name']()); else dataModel.CompanyName(''); if (data['DepartmentId']() != null) dataModel.DepartmentName = $KoSet(dataModel.DepartmentName, data['CmDepartment/Name']()); else dataModel.DepartmentName(''); dataModel.AccidentTypeName = $KoSet(dataModel.AccidentTypeName, data['FmsAccidentCodeType/Name']()); if (data['FileId1']() != null) dataModel.AttachmentFileName1(data['CmFile/Name']()); else dataModel.AttachmentFileName1(''); if (data['FileId2']() != null) dataModel.AttachmentFileName2(data['CmFile1/Name']()); else dataModel.AttachmentFileName2(''); if (data['FileId3']() != null) dataModel.AttachmentFileName3(data['CmFile2/Name']()); else dataModel.AttachmentFileName3(''); popupVisible(true); }, handleViewShowing: function (dataModel) { $.when( accidentTypeDataSource.load(), companyDataSource.load() ) .done(function (dbAccidents, dbCompanies) { accidentTypes(dbAccidents[0]); companies(dbCompanies[0]); departments([]); accidentTypesForSearch($SearchView.createArrayOfSelectBox('AccidentTypeId', accidentTypes())); deferredForSearch.resolve(); }); }, handleViewShown: function (dataViewModel, isEditModeInPopup) { setupDropZone(isEditModeInPopup, dataViewModel); }, handleCancelInPopup: function () { isEditModeInPopup(false); }, handlePopupButtonEdit: function (dataModel) { isEditModeInPopup(true); }, handlePopupButtonClose: function () { isEditModeInPopup(false); }, handleSaveSuccess: function () { if (_.isNull(dropZone1) === false) { dropZone1.resetUploadedFileInfo(); } if (_.isNull(dropZone2) === false) { dropZone2.resetUploadedFileInfo(); } isEditModeInPopup(false); }, handlePopupShowing: function (isEditMode, isNew, dataViewModel) { imagefileUrl1(undefined); imagefileUrl2(undefined); if (_.isNull(dropZone1) === false) dropZone1.resetUploadedFileInfo(); if (_.isNull(dropZone2) === false) dropZone2.resetUploadedFileInfo(); if (isNew) { // 신규등록인 경우 dropZone1 = null; dropZone2 = null; } }, handlePopupShown: function (isEditMode, isNew, dataViewModel) { var accidentTypes = viewModel.accidentTypes(); isEditModeInPopup(false); if (isNew) { // 신규등록인 경우 isEditModeInPopup(true); if (!_.isEmpty(accidentTypes)) { dataViewModel.AccidentTypeId(accidentTypes[0].AccidentTypeId()); } } } }); viewModel.handleSelectBoxChangeCompanyInPopup = function (e) { BWA.DataUtil.setArrayDataSourceFilter(departmentDataSource, 'CompanyId', e.value, departments); viewModel.dataModel.DepartmentId(null); } viewModel.imagefileUrl1 = imagefileUrl1; viewModel.imagefileUrl2 = imagefileUrl2; viewModel.imagefileUrl3 = imagefileUrl3; viewModel.isEditModeInPopup = isEditModeInPopup; viewModel.accidentTypes = accidentTypes; viewModel.companies = companies; viewModel.departments = departments; viewModel.dataModel.CompanyName = ko.observable(); viewModel.dataModel.DepartmentName = ko.observable(); viewModel.dataModel.AccidentTypeName = ko.observable(); viewModel.dataModel.AttachmentFileName1 = ko.observable(); viewModel.dataModel.AttachmentFileName2 = ko.observable(); viewModel.dataModel.AttachmentFileName3 = ko.observable(); return viewModel; };