From d38f29d207f03a3231c453010b2632507b22e8c2 Mon Sep 17 00:00:00 2001 From: zmagod Date: Mon, 22 Aug 2016 11:29:19 +0200 Subject: [PATCH] set ajax loading of experiments images [fixes SCI-349] --- app/assets/javascripts/projects/show.js | 53 +++++++++++-------- app/controllers/experiments_controller.rb | 16 ++---- app/helpers/projects_helper.rb | 7 --- app/views/projects/show.html.erb | 4 +- app/views/projects/show/_experiment.html.erb | 7 +-- .../projects/show/_workflow_img.html.erb | 3 ++ config/routes.rb | 2 +- 7 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 app/views/projects/show/_workflow_img.html.erb diff --git a/app/assets/javascripts/projects/show.js b/app/assets/javascripts/projects/show.js index 8be597218..ef963b50f 100644 --- a/app/assets/javascripts/projects/show.js +++ b/app/assets/javascripts/projects/show.js @@ -1,48 +1,59 @@ (function(){ var count = 0; - function initProjectExperiment(){ - var url = $("[data-lupdated-url]").data("lupdated-url"); - var updated = "[data-id=" + $("[data-lupdated-id]").data('lupdated-id') +"]"; - var el = $(updated).find("img"); - var timestamp = el.data("timestamp"); - var img_url = $(updated).find(".workflowimg-container").data('updated-img'); - - animateSpinner($(updated).find(".workflowimg-container"), true); - checkUpdatedImg(el, img_url, url, timestamp, updated); - animateSpinner($(updated).find(".workflowimg-container"), false); + function init(){ + $("[data-id]").each(function(){ + var that = $(this); + that.find(".workflowimg-container").hide(); + initProjectExperiment(that); + }); } + function initProjectExperiment(element){ + var container = element.find(".workflowimg-container"); + var url = container.data("check-img"); + var timestamp = container.data("timestamp"); + var img_url = container.data('updated-img'); - function checkUpdatedImg(el, img_url, url, timestamp, updated){ - if (count !== 100){ + animateSpinner(container, true); + checkUpdatedImg(img_url, url, timestamp, container); + } + + // checks if the experiment image is updated + function checkUpdatedImg(img_url, url, timestamp, container){ + if (count < 30 && timestamp){ $.ajax({ url: url, type: "GET", data: { "timestamp": timestamp }, dataType: "json", success: function (data) { - getNewWorkforwImg(el, img_url, updated); - animateSpinner($(updated).find(".workflowimg-container"), false); + getNewWorkforwImg(container, img_url); + container.show(); + animateSpinner(container, false); }, error: function (ev) { if (ev.status == 404) { - setTimeout(checkUpdatedImg(el, img_url, url, timestamp, updated), 200); - } - count++; + setTimeout(checkUpdatedImg(img_url, url, timestamp, container), 500); + } else { + animateSpinner(container, false); + } + count++; } }); + } else { + animateSpinner(container, false); } } - function getNewWorkforwImg(el, url, updated){ + // fetch the new experiment image + function getNewWorkforwImg(el, url){ $.ajax({ url: url, type: "GET", dataType: "json", success: function (data) { - el.html(data.workflowimg); - animateSpinner($(updated).find(".workflowimg-container"), false); + el.append(data.workflowimg); }, error: function (ev) { // TODO @@ -50,5 +61,5 @@ }); } // init - initProjectExperiment(); + init(); })(); diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 6a1880205..8f98df299 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -220,7 +220,7 @@ class ExperimentsController < ApplicationController def updated_img respond_to do |format| format.json do - if @experiment.workflowimg_updated_at.to_i != + if @experiment.workflowimg_updated_at.to_i >= params[:timestamp].to_time.to_i render json: {}, status: 200 else @@ -230,19 +230,13 @@ class ExperimentsController < ApplicationController end end - def get_workflow_img + def fetch_workflow_img respond_to do |format| format.json do render json: { - workflowimg: (link_to image_tag(experiment - .workflowimg - .expiring_url(30), - class: 'img-responsive center-block', - data: { - timestamp: experiment - .workflowimg_updated_at - }), - canvas_experiment_path(experiment)).to_s + workflowimg: render_to_string( + partial: 'projects/show/workflow_img.html.erb' + ) } end end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 9f51bfadb..5edf289da 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -11,11 +11,4 @@ module ProjectsHelper end conns.to_s[1..-2] end - - def last_experiment_updated(project) - project - .active_experiments(updated_at: :desc) - .pluck(:id) - .first.to_s - end end diff --git a/app/views/projects/show.html.erb b/app/views/projects/show.html.erb index 7569d26f3..06581cdb3 100644 --- a/app/views/projects/show.html.erb +++ b/app/views/projects/show.html.erb @@ -23,9 +23,7 @@ -
+
<% @project.active_experiments.each_with_index do |experiment, index| %> <%= render partial: 'projects/show/experiment', locals: { experiment: experiment } %> diff --git a/app/views/projects/show/_experiment.html.erb b/app/views/projects/show/_experiment.html.erb index fd0f7d81f..55203ef1b 100644 --- a/app/views/projects/show/_experiment.html.erb +++ b/app/views/projects/show/_experiment.html.erb @@ -23,11 +23,12 @@
<% if experiment.workflowimg? %>
- <%= link_to image_tag(experiment.workflowimg.expiring_url(30), + <%#= link_to image_tag(experiment.workflowimg.expiring_url(30), class: 'img-responsive center-block', - data: { timestamp: experiment.workflowimg_updated_at }), canvas_experiment_path(experiment) %>
<% end %> diff --git a/app/views/projects/show/_workflow_img.html.erb b/app/views/projects/show/_workflow_img.html.erb new file mode 100644 index 000000000..095ba1b48 --- /dev/null +++ b/app/views/projects/show/_workflow_img.html.erb @@ -0,0 +1,3 @@ +<%= link_to image_tag( @experiment.workflowimg.expiring_url(30), + class: 'img-responsive center-block'), + canvas_experiment_path(@experiment) %> diff --git a/config/routes.rb b/config/routes.rb index 31defb10b..7d53e1c61 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -129,7 +129,7 @@ Rails.application.routes.draw do post 'move' # move experiment get 'samples' # Samples for single project get 'updated_img' # Checks if the workflow image is updated - get 'get_workflow_img' # Get udated workflow img + get 'fetch_workflow_img' # Get udated workflow img # Renders sample datatable for single project (ajax action) post 'samples_index' post :delete_samples,