'; } else { var html = '
'; } $("#message").empty(); $("#message").append(html); $("#message").fadeIn(1000); setTimeout(function () { $("#message").fadeOut(1000); }, 60000); } function sortOrder() { //alert(""); var order = []; //loop trought each li... var iOrder = 1; $('#sortable li').each(function (e) { // TODO: remove the inogre Li var assetId = $(this).find('#assetId').val(); if (iOrder == 1) { $(this).css("border-top", "skyblue solid 10px"); } else { $(this).css("border-top", "1px solid #c5c5c5"); } // Ignore the deleted one. if ($('#imageStatus_' + assetId).val() == 'Ignore') return; if ($('#imageStatus_' + assetId).val() == 'Remove') return; //add each li position to the array... // the +1 is for make it start from 1 instead of 0 if ($('#Order_' + assetId).val() != iOrder) { window.assetDatas.push({ id: assetId }); } $(this).find('.imageorder').val(iOrder++); //$(this).find("data-id").val(iOrder++); }); } function addNewImage() { //need to be refreshed $("#sortable").sortable("refresh"); } var fadeTime = 300; $(function () { $("#sortable").sortable(); $("#sortable").disableSelection(); $("#sortable").sortable({ update: function (event, ui) { sortOrder(); } }); $(document).bind('drop dragover', function (e) { e.preventDefault(); }); window.imageDatas = []; window.assetDatas = []; //initPreview(); sortOrder(); initFileUpload(); $("#btn-save").click(function (e) { e.preventDefault(); e.stopPropagation(); var breakOut = false; if (window.assetDatas.length == 0 && window.imageDatas.length == 0) { confirmationSuccess("Nothing need to be saved!", true); return false; } // Remove the duplicate asset and new Ignored asset var uniqueAssetDatas = []; $.each(window.assetDatas, function (i, el) { if ($.inArray(el.id, uniqueAssetDatas) === -1) { if ($('#imageStatus_' + el.id).val() != 'Ignore') uniqueAssetDatas.push(el.id); } }); // Remove the duplicate image var uniqueimageDatas = []; $.each(window.imageDatas, function (i, el) { if ($.inArray(el.id, uniqueimageDatas.id) == -1) uniqueimageDatas.push(el) }); if (uniqueimageDatas.length == 0 && uniqueAssetDatas.length == 0) { confirmationSuccess("Nothing need to be saved!", true); window.imageDatas = []; window.assetDatas = []; return false; } // Check if any new added (onlY ADDED) asset has images. // Check if new added in window.assetDatas has image in window.imageDatas. // If YES, Remove the data in assetDatas, So the new Added will only post from window.imageDatas. $.each(uniqueAssetDatas, function (i, el) { var assetId = el; if ($('#imageStatus_' + assetId).val() != 'Add') return; var found = false; for (var i = 0; i < uniqueimageDatas.length; i++) { if (uniqueimageDatas[i].id == assetId) { found = true; uniqueAssetDatas.splice(i, 1); // uniqueAssetDatas.remove(el); break; } } if (!found) { confirmationSuccess("No image uploaded. Please add image before saving!", true); breakOut = true; return false; } return; }); if (breakOut) return; // Combine the assetdata to imageData if they is image changes in image data and also asset data change. So we don't need to post twice. $.each(uniqueimageDatas, function (i, el) { var assetId = el.id; var found = false; for (var i = 0; i < uniqueAssetDatas.length; i++) { if (uniqueAssetDatas[i] == assetId) { found = true; uniqueAssetDatas.splice(i, 1); break; } } return; }); // image with the data uniqueimageDatas.forEach(function (d) { var assetId = d.id; d.data.formData = { //Passing data projectId: $('#projectId').val(), assetId: assetId, displayDate: $('#DisplayDate_' + assetId).val(), source: $('#Source_' + assetId).val(), caption: $('#Caption_' + assetId).val(), order: $('#Order_' + assetId).val(), status: $('#imageStatus_' + assetId).val(), }, d.data.submit(); }) // Data change only without images uniqueAssetDatas.forEach(function (d) { var assetId = d; $.ajax( { url: "/Asset/uploadAssetImage", type: 'POST', dataType: 'json', data: { //Passing data projectId: $('#projectId').val(), assetId: assetId, displayDate:$('#DisplayDate_' + assetId).val(), source: $('#Source_' + assetId).val(), caption:$('#Caption_' + assetId).val(), order: $('#Order_' + assetId).val(), status: $('#imageStatus_' + assetId).val(), }, success: function (data) { var result = data.result; if (result) { var lastmodified = data.lastmodified; confirmationSuccess("Save successfully!", true); // Update the Last modified date filed in detail page. $('#lastmodified').text(lastmodified); return; } if (!result) { confirmationSuccess("Save Failed, Please try again!", true); breakOut = true; return false; } }, failure: function (result) { } }); }) if (breakOut) return; window.imageDatas = []; window.assetDatas = []; }); $("#btn-add").click(function (e) { e.preventDefault(); e.stopPropagation(); // this is a fake asset id used for drag and drop. var uploadFileId = getRandomInt(9999)+""; reloadAssetAndImage(0, uploadFileId, false,'Add','','','',''); }); function reloadAssetAndImage(oldassetId, newassetId, imageFromServer,status,displayDate,source,caption,order) { // add new uploadFileId = newassetId; var newAdd = ""; newAdd = '
' + '' + '
'; if(imageFromServer) newAdd += ''; newAdd += '
' + '
' + '
Order:
' + '
' + '
Display Date:
' + '
' + '
Caption:
' + '
' + '
Source:
' + '
' + '
'; $(newAdd).fadeOut(fadeTime, function () { if(oldassetId == 0) $('#sortable').append(newAdd); else { if (oldassetId != 0) { $('#sortable li ').each(function () { if ($(this).find('#assetId').val() == oldassetId) $(this).replaceWith($(newAdd)); }) } } $(newAdd).fadeIn(fadeTime); }); sortOrder(); initEachFileUpload(uploadFileId) } function initFileUpload() { var dropzoneIds = []; $('#sortable li #assetId').each(function () { dropzoneIds.push($(this).val()); }) for (var d = 0; d < dropzoneIds.length; d++) { var currentId = dropzoneIds[d]; initEachFileUpload(currentId); } } // Every time the file upload added or initilazied function initEachFileUpload(currentId) { // check if image droped var fileuploadid = "#btnInfoImageUpload_" + currentId; $(fileuploadid).fileupload( { dropZone: $(fileuploadid), cache: false, url: "/Asset/uploadAssetImage", type: 'POST', dataType: 'json', contentType: false, processData: false, autoUpload: false, formData: { projectId: $('#projectId').val(), //assetId: $(imageupload).parent().parent().find('#assetId').val(), //displayDate: $(imageupload).parent().parent().find('#DisplayDate').val(), //source: $(imageupload).parent().parent().find('#Source').val(), //caption: $(imageupload).parent().parent().find('#Caption').val(), //order: $(imageupload).parent().parent().find('#Order').val(), }, //data: $('#uploadItemImageForm').serialize(), add: function (e, data) { e.stopPropagation(); e.preventDefault(); console.log(e.originalEvent.delegatedEvent.target); var targetId = e.originalEvent.delegatedEvent.target.id; var assetId = $('#' + targetId).parent().parent().find('#assetId').val(); //Remove the previous added one. var uniqueimageDatas = []; for (var i in window.imageDatas) { if (window.imageDatas[i].id == assetId) { window.imageDatas.splice(i, 1); break; } } //$.each(window.imageDatas, function (i, el) { // if (el.id == assetId) // window.imageDatas.array.splice(i, 1); //}); window.imageDatas.push({ id: assetId, data: data }); // Preview var prviewDiv = "#previewImage_" + assetId; var file = data.files[0]; var reader = new FileReader(); reader.addEventListener("load", function () { $(prviewDiv).fadeOut(fadeTime, function () { // convert image file to base64 string var bin = reader.result; var newFile = document.createElement('div'); $(newFile).addClass("image-dragdrop_title"); newFile.innerHTML = 'Image is ready : ' + file.name ; $(prviewDiv).empty().append(newFile); var img = document.createElement("img"); img.file = file; img.src = bin; $(prviewDiv).append(img); $(prviewDiv).find("img").addClass("image-dragdrop"); $(prviewDiv).fadeIn(fadeTime); }); }, false); if (file) { reader.readAsDataURL(file); } }, progress: function (e, data) { }, success: function (data) { var result = data.result; if (result) { // Update the asset id from database for New Added. var targetId = data.oldassetId; //var previousassetId = $('#' + targetId).parent().parent().find('#assetId').val(); var newassetid = data.newassetId; var lastmodified = data.lastmodified; // var iOrder = 1; var displayDate = $('#DisplayDate_' + targetId).val(); var source = $('#Source_' + targetId).val(); var caption = $('#Caption_' + targetId).val(); var order = $('#Order_' + targetId).val(); reloadAssetAndImage(targetId, newassetid, true, 'Exist',displayDate,source,caption,order); confirmationSuccess("Save successfully!", true); // Update the Last modified date filed in detail page. $('#lastmodified').text(lastmodified); return; } if (!result) { confirmationSuccess("Save Failed, Please try again!", true); return; } }, error: function (xhr, status) { } }); // Check if image deleted var deleteImage = "#btnDeleteImage_" + currentId; $(deleteImage).click(function (e) { if (window.confirm("Are you sure you want to remove this image? If yes, you still need to save to make it effective.")) { var imageStatus = "#imageStatus_" + currentId; // Hide and mark it is needed to be removed. $(deleteImage).parent().hide(); if ($(imageStatus).val() == "Exist") { $(imageStatus).val("Remove"); } if ($(imageStatus).val() == "Add") { $(imageStatus).val("Ignore"); } assetDatas.push({ id: currentId }); sortOrder(); } }); // Check if value changed var order = "#Order_" + currentId; $(order).on('change keydown paste input', function () { assetDatas.push({ id: currentId }); }); var displayDate = "#DisplayDate_" + currentId; $(displayDate).on('change keydown paste input', function () { assetDatas.push({ id: currentId }); }); var caption = "#Caption_" + currentId; $(caption).on('change keydown paste input', function () { assetDatas.push({ id: currentId }); }); var source = "#Source_" + currentId; $(source).on('change keydown paste input', function () { assetDatas.push({ id: currentId }); }); // $(displayDate).datepicker( { changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'M yy', //onClose: function (dateText, inst) { // if ($(window.event.srcElement).hasClass('ui-datepicker-close')) { // var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); // var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); // $(this).datepicker('setDate', new Date(year, month, 1)); // setDetailsDirty("true");; // } // //$(displayDate).valid(); //}, //beforeShow: function (input, inst) { // if ($(input).attr('readonly')) { // return false; // } // if (!$('#ui-datepicker-div').hasClass('')) { // $('#ui-datepicker-div').addClass(""); // } //} } ); } function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } });