From 35a9a9bdd42a1c6d9b42a9d0aaca41929250161c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 9 Jan 2020 19:21:30 +0100 Subject: [PATCH] upload progress & ajax error fallback --- config.ini | 2 +- index.php | 5 ++++- static/scripts/app.js | 33 +++++++++++++++++++++++++++++++++ static/styles/design.css | 20 +++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/config.ini b/config.ini index e8fc2f5..ab8df8b 100644 --- a/config.ini +++ b/config.ini @@ -44,6 +44,6 @@ logs_path = data/logs/ [system] system_name = blog -version = 1.21 +version = 1.22 debug = false logs = false \ No newline at end of file diff --git a/index.php b/index.php index 7e7d3d1..76339fe 100644 --- a/index.php +++ b/index.php @@ -173,7 +173,10 @@ if(!empty($scripts)){ -
+
+ +
+
diff --git a/static/scripts/app.js b/static/scripts/app.js index 263aba2..19c42d2 100644 --- a/static/scripts/app.js +++ b/static/scripts/app.js @@ -399,6 +399,11 @@ $.fn.error_msg = function(msg){ }, 5000); }; +// Attach the event handler to the document +$(document).ajaxError(function(){ + $("body").error_msg("Ajax request failed."); +}); + // Apply events on post editing $.fn.apply_edit = function(data){ // Parse link @@ -409,10 +414,16 @@ $.fn.apply_edit = function(data){ var add_content_loading = function(){ modal.find(".e_loading").css("display", "block"); + modal.find(".e_loading .e_meter > span").width(0); + }; + + var content_loading_progress = function(progress){ + modal.find(".e_loading .e_meter > span").width((progress * 100) + "%"); }; var remove_content = function(){ modal.find(".e_loading").hide(); + modal.find(".e_loading .e_meter > span").width(0); modal.find(".content").empty().hide(); modal.find(".i_content_type").val(""); @@ -472,6 +483,10 @@ $.fn.apply_edit = function(data){ return ; add_content(data.content_type, data.content); + }, + error: function() { + $("body").error_msg("Error when communicating with the server."); + remove_content(); } }); }); @@ -489,6 +504,20 @@ $.fn.apply_edit = function(data){ add_content_loading(); $.post({ + xhr: function(){ + var xhr = new window.XMLHttpRequest(); + + // Upload progress + xhr.upload.addEventListener("progress", function(evt){ + if (evt.lengthComputable) { + var percentComplete = evt.loaded / evt.total; + //Do something with upload progress + content_loading_progress(percentComplete); + } + }, false); + + return xhr; + }, dataType: "json", url: "ajax.php?action=upload_image", cache: false, @@ -503,6 +532,10 @@ $.fn.apply_edit = function(data){ } add_content("image", data); + }, + error: function() { + $("body").error_msg("Error when communicating with the server."); + remove_content(); } }); } diff --git a/static/styles/design.css b/static/styles/design.css index 745d7d4..cb2239e 100644 --- a/static/styles/design.css +++ b/static/styles/design.css @@ -671,7 +671,25 @@ body { .e_loading { display: none; text-align: center; - padding-bottom: 15px; +} + +.e_loading .e_meter { + height: 5px; + position: relative; + margin-top: 15px; + box-shadow: inset 0 -1px 1px rgba(255,255,255,0.3); +} + +.e_loading .e_meter > span { + display: block; + width: 0; + height: 100%; + background-color: #4267b2; + box-shadow: + inset 0 2px 9px rgba(255,255,255,0.3), + inset 0 -2px 6px rgba(0,0,0,0.4); + position: relative; + overflow: hidden; } .e_text {