397 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			397 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| $(function () {
 | |
|     InitAttachmentGridData();
 | |
| 
 | |
|     $('#dialog_adddocument').dialog(function () {
 | |
|         showmaskbg(false);
 | |
|     });
 | |
|     $(window).resize(function () {
 | |
|         $("#documentlist").css("height", $(window).height() - $("#documentlist").offset().top - 4);
 | |
|         grid_attachmentdt && grid_attachmentdt.resize();
 | |
|     }).resize();
 | |
| });
 | |
| 
 | |
| function showAttachments(data) {
 | |
|     var rows = [];
 | |
|     for (var i = 0; i < data.length; i++) {
 | |
|         var r = data[i];
 | |
|         for (var j in r) {
 | |
|             if (j === "VisibleOnWorkOrder")
 | |
|                 r[j] = { DisplayValue: r["VisibleOnWorkOrder"] ? GetTextByKey("P_UTILITY_YES", "Yes") : GetTextByKey("P_UTILITY_NO", "No"), Value: r[j] };
 | |
|             else if (j === "VisibleOnMap")
 | |
|                 r[j] = { DisplayValue: r["VisibleOnMap"] ? GetTextByKey("P_UTILITY_YES", "Yes") : GetTextByKey("P_UTILITY_NO", "No"), Value: r[j] };
 | |
|             else if (j === "VisibleOnMobile")
 | |
|                 r[j] = { DisplayValue: r["VisibleOnMobile"] ? GetTextByKey("P_UTILITY_YES", "Yes") : GetTextByKey("P_UTILITY_NO", "No"), Value: r[j] };
 | |
|             else if (j === "AddedOnLocal")
 | |
|                 r[j] = { DisplayValue: r["AddedOnLocalStr"], Value: r[j] };
 | |
|         }
 | |
|         rows.push(r);
 | |
|     }
 | |
| 
 | |
|     grid_attachmentdt.setData(rows);
 | |
| }
 | |
| 
 | |
| var grid_attachmentdt;
 | |
| function InitAttachmentGridData() {
 | |
|     $('#btnEdit').attr("disabled", "disabled");
 | |
| 
 | |
|     grid_attachmentdt = createGridView('#documentlist');
 | |
|     var list_columns = [
 | |
|         { name: 'Name', caption: GetTextByKey("P_MA_NAME", "Name"), valueIndex: 'Name', allowFilter: true, css: { 'width': 120, 'text-align': 'left' } },
 | |
|         { name: 'AddedBy', caption: GetTextByKey("P_MA_UPLOADEDBY", "Uploaded By"), valueIndex: 'AddedBy', allowFilter: true, css: { 'width': 100, 'text-align': 'left' } },
 | |
|         { name: 'VisibleOnWorkOrder', caption: GetTextByKey("P_MA_VISIBLEONWORKORDER", "Visible On Work Order?"), valueIndex: 'VisibleOnWorkOrder', allowFilter: true, css: { 'width': 80, 'text-align': 'left' } },
 | |
|         { name: 'VisibleOnMap', caption: GetTextByKey("P_MA_VISIBLEONMAP", "Visible On Map?"), valueIndex: 'VisibleOnMap', allowFilter: true, css: { 'width': 80, 'text-align': 'left' } },
 | |
|         { name: 'VisibleOnMobile', caption: GetTextByKey("P_MA_VISIBLEONMOBILE", "Visible On Mobile?"), valueIndex: 'VisibleOnMobile', allowFilter: true, css: { 'width': 80, 'text-align': 'left' } },
 | |
|         { name: 'AddedOnLocal', caption: GetTextByKey("P_MA_ADDEDON", "Added On"), valueIndex: 'AddedOnLocal', allowFilter: true, css: { 'width': 80, 'text-align': 'left' } },
 | |
|         { name: 'Description', caption: GetTextByKey("P_MA_DESCRIPTION", "Desctiption"), valueIndex: 'Description', css: { 'width': 200, 'text-align': 'left' } },
 | |
|         { name: 'View', caption: "", css: { 'width': 30, 'text-align': 'center' } },
 | |
|         { name: 'Edit', caption: "", css: { 'width': 30, 'text-align': 'center' } },
 | |
|         { name: 'Delete', caption: "", css: { 'width': 30, 'text-align': 'center' } }
 | |
|     ];
 | |
|     var columns = [];
 | |
|     // head
 | |
|     for (var hd in list_columns) {
 | |
|         var col = {};
 | |
|         col.name = list_columns[hd].name;
 | |
|         col.caption = list_columns[hd].caption;
 | |
|         col.visible = true;
 | |
|         col.sortable = true;
 | |
|         col.width = list_columns[hd].css.width;
 | |
|         col.align = list_columns[hd].css["text-align"]
 | |
|         col.key = list_columns[hd].valueIndex;
 | |
|         col.allowFilter = list_columns[hd].allowFilter;
 | |
| 
 | |
|         if (col.name === "View") {
 | |
|             col.sortable = false;
 | |
|             col.resizable = false;
 | |
|             col.type = GridView.ColumnTypes.Icon;
 | |
|             col.text = 'eye';
 | |
|             col.iconType = 'fa-light';
 | |
|             col.events = {
 | |
|                 onclick: function () {
 | |
|                     openDocumentUrl();
 | |
|                 }
 | |
|             };
 | |
|             col.attrs = { 'title': GetTextByKey("P_CRE_EDIT", 'Edit') };
 | |
|         }
 | |
|         else if (col.name === "Edit") {
 | |
|             col.sortable = false;
 | |
|             col.resizable = false;
 | |
|             col.type = GridView.ColumnTypes.Icon;
 | |
|             col.text = 'edit';
 | |
|             col.iconType = 'fa-light';
 | |
|             col.events = {
 | |
|                 onclick: function () {
 | |
|                     onEditDocument();
 | |
|                 }
 | |
|             };
 | |
|             col.attrs = { 'title': GetTextByKey("P_CRE_EDIT", 'Edit') };
 | |
|         }
 | |
|         else if (col.name === "Delete") {
 | |
|             col.sortable = false;
 | |
|             col.resizable = false;
 | |
|             col.type = GridView.ColumnTypes.Icon;
 | |
|             col.text = 'times';
 | |
|             col.iconType = 'fa-light';
 | |
|             col.events = {
 | |
|                 onclick: function () {
 | |
|                     onDeleteDocument(this);
 | |
|                 }
 | |
|             };
 | |
|             col.attrs = { 'title': GetTextByKey("P_CRE_DELETE", 'Delete') };
 | |
|         }
 | |
|         columns.push(col);
 | |
|     }
 | |
|     grid_attachmentdt.canMultiSelect = false;
 | |
|     grid_attachmentdt.columns = columns;
 | |
|     grid_attachmentdt.init();
 | |
|     grid_attachmentdt.onRowDblClicked = onEditDocument;
 | |
| }
 | |
| 
 | |
| 
 | |
| function setPreviewAttachment() {
 | |
|     filedata = undefined;
 | |
|     $("#tbAssetAttas").empty();
 | |
|     $('#tr_document_file').hide();
 | |
|     $('#browseattfile').hide();
 | |
| }
 | |
| 
 | |
| function getAttachments(machineid) {
 | |
|     if (!machineid) return;
 | |
| 
 | |
|     showLoading();
 | |
| 
 | |
|     devicerequest("GetAttachments", contractorid + String.fromCharCode(170) + machineid, function (data) {
 | |
|         hideLoading();
 | |
|         if (typeof (data) === "string") {
 | |
|             showAlert(data, GetTextByKey("P_MA_ERROR", 'Error'));
 | |
|             return;
 | |
|         }
 | |
|         var attdata = [];
 | |
|         if (data && data.length > 0) {
 | |
|             attdata = data;
 | |
|         }
 | |
|         else
 | |
|             attdata = [];
 | |
| 
 | |
|         showAttachments(attdata);
 | |
|     }, function (err) {
 | |
|         hideLoading();
 | |
|     });
 | |
| }
 | |
| 
 | |
| function onEditDocument() {
 | |
|     var doc = grid_attachmentdt.source[grid_attachmentdt.selectedIndex];
 | |
|     if (!doc) {
 | |
|         return;
 | |
|     }
 | |
|     openAddDocument(doc);
 | |
| }
 | |
| 
 | |
| function openDocumentUrl() {
 | |
|     var doc = grid_attachmentdt.source[grid_attachmentdt.selectedIndex];
 | |
|     if (!doc) {
 | |
|         return;
 | |
|     }
 | |
|     window.open(doc.Url);
 | |
| }
 | |
| 
 | |
| var assetdocumentid;
 | |
| function openAddDocument(doc) {
 | |
|     setPreviewAttachment();
 | |
|     if (doc) {
 | |
|         assetdocumentid = doc.Id;
 | |
|         $('#tr_document_radio').hide();
 | |
|         $('#dialog_adddoc_name').val(doc.Name);
 | |
|         $('#dialog_adddoc_desc').val(doc.Description);
 | |
|         $('#dialog_visibleonwo').prop('checked', doc.VisibleOnWorkOrder.Value);
 | |
|         $('#dialog_visibleonmap').prop('checked', doc.VisibleOnMap.Value);
 | |
|         $('#dialog_visibleonmobile').prop('checked', doc.VisibleOnMobile.Value);
 | |
|         if (doc.FileType.toLowerCase() === "url") {
 | |
|             $('#dialog_rdourl').prop('checked', true);
 | |
|             //$('#dialog_adddoc_url').attr("disabled", true);
 | |
|             $('#dialog_adddoc_url').val(doc.Url);
 | |
|             $('#tr_document_url').show();
 | |
| 
 | |
|         } else {
 | |
|             $('#dialog_rdofile').prop('checked', true);
 | |
|             $('#tr_document_url').hide();
 | |
|         }
 | |
|     }
 | |
|     else {
 | |
|         assetdocumentid = undefined;
 | |
|         $('#dialog_adddoc_url').attr("disabled", false);
 | |
|         $('#tr_document_url').show();
 | |
|         $('#tr_document_radio').show();
 | |
| 
 | |
|         $('#dialog_adddoc_name').val('');
 | |
|         $('#dialog_adddoc_url').val('');
 | |
|         $('#dialog_adddoc_desc').val('');
 | |
|         $('#dialog_rdourl').prop('checked', true);
 | |
|         $('#dialog_visibleonwo').attr("checked", false);
 | |
|         $('#dialog_visibleonmap').attr("checked", false);
 | |
|         $('#dialog_visibleonmobile').attr("checked", false);
 | |
|     }
 | |
|     $('#dialog_adddocument .dialog-title span.title').text(GetTextByKey("P_MA_ADDITIONALDOCUMENTATION", 'Additional Documentation'));
 | |
|     showmaskbg(true);
 | |
|     $('#dialog_adddocument')
 | |
|         .attr('act', 'add')
 | |
|         .css({
 | |
|             'top': (document.documentElement.clientHeight - $('#dialog_adddocument').height()) / 3,
 | |
|             'left': (document.documentElement.clientWidth - $('#dialog_adddocument').width()) / 2
 | |
|         })
 | |
|         .showDialogfixed();
 | |
|     $('#dialog_adddoc_name').focus();
 | |
| }
 | |
| 
 | |
| var filedata;
 | |
| function BrowseAssetDocument() {
 | |
|     var file = $('<input type="file" style="display: none;" multiple="multiple" />');
 | |
|     file.change(function () {
 | |
|         var files = this.files;
 | |
|         var file = files[0];
 | |
|         if (file.size == 0) {
 | |
|             alert(GetTextByKey("P_MA_DOCUMENTTIPS", "Document size is 0kb, uploading failed."));
 | |
|             return false;
 | |
|         }
 | |
|         if (file.size > 50 * 1024 * 1024) {
 | |
|             alert(GetTextByKey("P_MA_DOCUMENTTIPS1", "Document is too large. Maximum file size is 50 MB."));
 | |
|             return false;
 | |
|         }
 | |
|         filedata = file;
 | |
|         if ($('#dialog_adddoc_name').val() === "")
 | |
|             $('#dialog_adddoc_name').val(file.name);
 | |
| 
 | |
|         $('#tbAssetAttas').empty();
 | |
|         var trrecoard = $('<tr></tr>').data('file', file);
 | |
|         var tdfile = $('<td style=""></td>');
 | |
|         var filename = $("<label style='border-bottom: 1px solid;cursor:pointer;word-break: break-all;'></label>").text(file.name).click(function () {
 | |
|             //window.open("../filesvc.ashx?attchid=" + this.parentElement.parentElement.id +  "&sourceType=assetattachment");
 | |
|         });
 | |
|         var imgDom = $('<span class="sbutton icondelete" style="padding:0;margin-left:5px;" ></span>').click(function () {
 | |
|             deletePreviewAssetAttachment(this);
 | |
|         });
 | |
| 
 | |
|         tdfile.append(filename, imgDom);
 | |
|         trrecoard.append(tdfile).appendTo($('#tbAssetAttas'));
 | |
| 
 | |
|     }).click();
 | |
| }
 | |
| 
 | |
| function deletePreviewAssetAttachment(e) {
 | |
|     filedata = undefined;
 | |
|     var tr = $(e).parent().parent();
 | |
|     $(tr).remove();
 | |
| }
 | |
| 
 | |
| function OnSaveDocument() {
 | |
|     if (!machineid) {
 | |
|         OnSave(0, false, function () {
 | |
|             SaveAssetDocument(filedata);
 | |
|         });
 | |
|         return;
 | |
|     }
 | |
|     else {
 | |
|         if (!assetdocumentid)
 | |
|             SaveAssetDocument(filedata);
 | |
|         else
 | |
|             UpdateAssetDocument();
 | |
|     }
 | |
| }
 | |
| 
 | |
| function SaveAssetDocument(file) {
 | |
|     $('#adddocumentmask').show();
 | |
|     var alerttitle = GetTextByKey("P_MA_ADDITIONALDOCUMENTATION", 'Additional Documentation');
 | |
|     var item = {
 | |
|         'CustomerID': contractorid,
 | |
|         'AssetID': machineid,
 | |
|         'Name': $('#dialog_adddoc_name').val(),
 | |
|         'VisibleOnWorkOrder': $('#dialog_visibleonwo').is(':checked'),
 | |
|         'VisibleOnMap': $('#dialog_visibleonmap').is(':checked'),
 | |
|         'VisibleOnMobile': $('#dialog_visibleonmobile').is(':checked'),
 | |
|         'Description': $('#dialog_adddoc_desc').val()
 | |
|     };
 | |
| 
 | |
|     if (item.Name === "") {
 | |
|         showAlert(GetTextByKey("P_MA_NAMECANNOTBEEMPTY", 'Name cannot be empty.'), alerttitle);
 | |
|         $('#adddocumentmask').hide();
 | |
|         return;
 | |
|     }
 | |
| 
 | |
|     var type = $('input[name="rdoattachmentmode"]:checked').val();
 | |
|     if (type === "0") {
 | |
|         item.FileType = "URL";
 | |
|         item.Url = $('#dialog_adddoc_url').val();
 | |
|         if (item.Url === "") {
 | |
|             showAlert(GetTextByKey("P_MA_URLCANNOTBEEMPTY", 'Url cannot be empty.'), alerttitle);
 | |
|             $('#adddocumentmask').hide();
 | |
|             return;
 | |
|         }
 | |
|     }
 | |
|     else {
 | |
|         if (!file) {
 | |
|             showAlert(GetTextByKey("P_MA_FILECANNOTBEEMPTY", 'File cannot be empty.'), alerttitle);
 | |
|             $('#adddocumentmask').hide();
 | |
|             return;
 | |
|         }
 | |
|         item.FileType = file.name.substring(file.name.lastIndexOf("."));
 | |
|     }
 | |
| 
 | |
|     showloading(true);
 | |
|     var formData = new FormData();
 | |
|     formData.append("iconFile", file);
 | |
|     formData.append("MethodName", "UploadAssetDocument");
 | |
|     formData.append("ClientData", htmlencode(JSON.stringify(item)));
 | |
|     $.ajax({
 | |
|         url: 'ManageMachines.aspx',
 | |
|         type: 'POST',
 | |
|         dataType: 'json',
 | |
|         processData: false,
 | |
|         contentType: false,
 | |
|         data: formData,
 | |
|         async: true,
 | |
|         success: function (data) {
 | |
|             showloading(false);
 | |
|             if (data !== 'OK') {
 | |
|                 showAlert(data, alerttitle);
 | |
|             } else {
 | |
|                 getAttachments(machineid);
 | |
|             }
 | |
|             $('#dialog_adddocument').hideDialog();
 | |
|             $('#adddocumentmask').hide();
 | |
|         },
 | |
|         error: function (err) {
 | |
|             showloading(false);
 | |
|             showAlert(err.statusText, alerttitle);
 | |
|             $('#adddocumentmask').hide();
 | |
|         }
 | |
|     });
 | |
| }
 | |
| 
 | |
| function UpdateAssetDocument() {
 | |
|     $('#adddocumentmask').show();
 | |
|     var item = {
 | |
|         'Id': assetdocumentid,
 | |
|         'CustomerID': contractorid,
 | |
|         'Name': $('#dialog_adddoc_name').val(),
 | |
|         'VisibleOnWorkOrder': $('#dialog_visibleonwo').is(':checked'),
 | |
|         'VisibleOnMap': $('#dialog_visibleonmap').is(':checked'),
 | |
|         'VisibleOnMobile': $('#dialog_visibleonmobile').is(':checked'),
 | |
|         'Description': $('#dialog_adddoc_desc').val()
 | |
|     };
 | |
| 
 | |
|     if (item.Name === "") {
 | |
|         showAlert(GetTextByKey("P_MA_NAMECANNOTBEEMPTY", 'Name cannot be empty.'), alerttitle);
 | |
|         $('#adddocumentmask').hide();
 | |
|         return;
 | |
|     }
 | |
|     var type = $('input[name="rdoattachmentmode"]:checked').val();
 | |
|     if (type === "0") {
 | |
|         item.FileType = "URL";
 | |
|         item.Url = $('#dialog_adddoc_url').val();
 | |
|         if (item.Url === "") {
 | |
|             showAlert(GetTextByKey("P_MA_URLCANNOTBEEMPTY", 'Url cannot be empty.'), alerttitle);
 | |
|             $('#adddocumentmask').hide();
 | |
|             return;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     showloading(true);
 | |
|     var param = JSON.stringify(item);
 | |
|     param = htmlencode(param);
 | |
|     devicerequest("UpdateAssetDocument", param, function (data) {
 | |
|         showloading(false);
 | |
|         if (data !== 'OK') {
 | |
|             showAlert(data, GetTextByKey("P_MA_ADDITIONALDOCUMENTATION", 'Additional Documentation'));
 | |
|         } else {
 | |
|             getAttachments(machineid);
 | |
|         }
 | |
|         $('#dialog_adddocument').hideDialog();
 | |
|         $('#adddocumentmask').hide();
 | |
|     }, function (err) {
 | |
|         showloading(false);
 | |
|         showAlert(err.statusText, GetTextByKey("P_MA_ADDITIONALDOCUMENTATION", 'Additional Documentation'));
 | |
|         $('#adddocumentmask').hide();
 | |
|     });
 | |
| }
 | |
| 
 | |
| function onDeleteDocument() {
 | |
|     var doc = grid_attachmentdt.source[grid_attachmentdt.selectedIndex];
 | |
|     if (!doc) {
 | |
|         return;
 | |
|     }
 | |
|     var alerttitle = GetTextByKey("P_MA_DELETEDOCUMENTATION", "Delete Documentation");
 | |
|     showConfirm(GetTextByKey("P_MA_DELETEDOCUMENTTTIPS", 'Are you sure you want to delete the document?'), alerttitle, function () {
 | |
|         devicerequest("DeleteAttachment", contractorid + String.fromCharCode(170) + doc.Id, function (data) {
 | |
|             if (data !== 'OK')
 | |
|                 showAlert(data, alerttitle);
 | |
|             else
 | |
|                 getAttachments(machineid);
 | |
|         }, function (err) {
 | |
|             showAlert(GetTextByKey("P_MA_FAILEDDELETEDOCUMENT", 'Failed to delete this document.'), alerttitle);
 | |
|         });
 | |
|     });
 | |
| }
 | |
| 
 | |
| function viewAttachment(attid) {
 | |
|     window.open("../filesvc.ashx?attchid=" + attid + "&custid=" + contractorid + "&sourceType=assetattachment");
 | |
| } |