$(function () { $('#dialog_adddocument').dialog(function () { showmaskbg(false); }); }); function setPreviewAttachment() { filedata = undefined; $("#tbAssetAttas").empty(); $('#tr_document_file').hide(); $('#browseattfile').hide(); } function getAttachments(machineid) { $('#tbody_documents').empty(); 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 = []; sortTableData($('#tbdocuments'), attdata); showAttachments(attdata); }, function (err) { hideLoading(); }); } function onEditDocument() { var doc = $(this).data('document'); if (!doc) doc = $(this).parents("tr").data('document'); openAddDocument(doc); } function openDocumentUrl() { var doc = $(this).parents("tr").data('document'); window.open(doc.Url); } function showAttachments(data) { var trs = []; for (var i = 0; i < data.length; i++) { var doc = data[i]; var tr = $('').data('document', doc).dblclick(onEditDocument); var span = $('').text(doc.Name).attr('title', doc.Name).click(openDocumentUrl); tr.append($('').append(span)); tr.append($('').text(doc.AddedBy).attr('title', doc.AddedBy)); tr.append($('').text(doc.VisibleOnWorkOrder ? "Yes" : "No")); tr.append($('').text(doc.VisibleOnMap ? "Yes" : "No")); tr.append($('').text(doc.VisibleOnMobile ? "Yes" : "No")); tr.append($('').text(doc.AddedOnLocalStr).attr('title', doc.AddedOnLocalStr)); tr.append($('').html(replaceHtmlText(doc.Description)).attr('title', doc.Description)); var spview = $('').click(openDocumentUrl); var spedit = $('').click(onEditDocument); var spdel = $('').click(onDeleteDocument);; tr.append($('').append(spview).append(spedit).append(spdel)); trs.push(tr); } $('#tbody_documents').append(trs); } 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); $('#dialog_visibleonmap').prop('checked', doc.VisibleOnMap); $('#dialog_visibleonmobile').prop('checked', doc.VisibleOnMobile); 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 = $(''); 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 = $('').data('file', file); var tdfile = $(''); var filename = $("").text(file.name).click(function () { //window.open("../filesvc.ashx?attchid=" + this.parentElement.parentElement.id + "&sourceType=assetattachment"); }); var imgDom = $('').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; } 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 alerttitle = GetTextByKey("P_MA_DELETEDOCUMENTATION", "Delete Documentation"); var doc = $(this).parents("tr").data('document'); 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"); }