Skip to content

Commit

Permalink
merges issue67 to master #67
Browse files Browse the repository at this point in the history
  • Loading branch information
elplatt committed Mar 12, 2013
2 parents bff0bba + 8e44455 commit 4f4d21e
Show file tree
Hide file tree
Showing 15 changed files with 538 additions and 479 deletions.
493 changes: 199 additions & 294 deletions app/assets/javascripts/coding.js

Large diffs are not rendered by default.

97 changes: 29 additions & 68 deletions app/assets/javascripts/display.js
Expand Up @@ -41,73 +41,34 @@ $(function () {

// load the highlighted areas for each image
function loadImagesHighlightedAreas () {

// iterates over all the highlighted areas
var high_areas = $("#high_areas div")
for (var i = high_areas.length -1; i >= 0; i--) {
var c_image_id = $(high_areas[i]).attr("id").substr(5)

var img_name = $($(high_areas[i]).children()[2]).attr("value")
var c_image = $('img[name='+img_name+']')
var high_area1 = $("div[image_name="+img_name+"]").find("div#high_area"+1)
var high_area2 = $("div[image_name="+img_name+"]").find("div#high_area"+2)


var dispalyed_img_size = 670
var ratio = (dispalyed_img_size/c_image.width())

// higlighted area 1

var curr_high_area_code = $("#image"+c_image_id+"_ha1"+"_code_id").attr("value")

// in case of "nothing to code here"
if (curr_high_area_code == "-1") {
high_area1.css("background-color", "#eee")
}else{
high_area1.css("background-color", $("#code_"+curr_high_area_code).css("background-color"))
}

var _top = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_y1").attr("value")) / ratio

_top = (_top) + c_image.position().top

high_area1.css("top", Math.ceil(_top) + "px" )

var _left = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_x1").attr("value")) / ratio

_left = (_left) + c_image.position().left
high_area1.css("left", Math.ceil(_left) + "px")

var _width = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_width").attr("value")) / ratio

high_area1.css("width", (_width) + "px")

var _height = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_height").attr("value")) / ratio
high_area1.css("height", (_height) + "px")


// higlighted area 2

curr_high_area_code = $("#image"+c_image_id+"_ha2"+"_code_id").attr("value")

high_area2.css("background-color", $("#code_"+curr_high_area_code).css("background-color"))

_top = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_y1").attr("value")) / ratio
_top = _top + c_image.position().top
high_area2.css("top", Math.ceil(_top) + "px" )

_left = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_x1").attr("value")) / ratio
_left = _left + c_image.position().left
high_area2.css("left", Math.ceil(_left) + "px")

_width = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_width").attr("value")) / ratio
high_area2.css("width", _width + "px")

_height = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_height").attr("value")) / ratio
high_area2.css("height", _height + "px")

};


// Iterates over all images
$("#high_images .ha_group").each(function () {
var img_id = $(this).attr('id').substr(9);
ha_list = getHighlightedAreas(img_id);
var img = $('img[name='+img_id+']');
// Iterate over all highlighted areas for a single image
var i, ha;
for (i = 0; i < ha_list.length; i++) {
// Get model and view
ha = ha_list[i];
ha_div = $('#ha_' + ha.id);
// Calculate scaling
var dispalyed_img_size = 670;
var ratio = (dispalyed_img_size/img.width());
// Calculate geometry
var _top = ha.y1/ratio + img.position().top;
var _left = ha.x1/ratio + img.position().left;
var _width = ha.width / ratio;
var _height = ha.height / ratio;
// Update css
ha_div.css("top", Math.ceil(_top) + "px" );
ha_div.css("left", Math.ceil(_left) + "px");
ha_div.css("width", _width + "px");
ha_div.css("height", _height + "px");
ha_div.css("background-color", $("#code_"+ha.code_id).css("background-color"));
}
});
}

// calculating the percentage of the loading bar of the images
Expand Down Expand Up @@ -143,4 +104,4 @@ $(function () {
$(this).attr("src","/assets/404.jpg")
})

})
})
81 changes: 81 additions & 0 deletions app/assets/javascripts/highlighted_area.js
@@ -0,0 +1,81 @@
// Interface for highlighted areas
// addHighlightedArea() - Add a single highlighted area to the current page
// saveHighightedArea() - Save a highlighted area
// deleteHighlightedAreas() - Delete all highlighted areas for the current page
// getHighlightedArea() - Get data for a single highlighted area
// getHighlightedAreas() - Get a list of highlighted areas

function addHighlightedArea (img_id, code_id, selection) {
// Get div containing highlighted area info for the specified image
var ha_group = $("#ha_group_" + img_id);
var count = ha_group.children().length;
var cssid = img_id + '_' + (count+1);
// Create hidden fields to contain data
var ha_elt = $('<div>').attr('id', cssid).appendTo(ha_group);
var tag = '<input type="hidden"/>';
$(tag).attr('name', 'ha_name[]').val(cssid).appendTo(ha_elt);
$(tag).attr('name', 'img_id_'+cssid).val(img_id).appendTo(ha_elt);
$(tag).attr('name', 'id_'+cssid).val(0).appendTo(ha_elt);
$(tag).attr('name', 'code_id_'+cssid).val(code_id).appendTo(ha_elt);
$(tag).attr('name', 'x1_'+cssid).val(selection.x1).appendTo(ha_elt);
$(tag).attr('name', 'y1_'+cssid).val(selection.y1).appendTo(ha_elt);
$(tag).attr('name', 'x2_'+cssid).val(selection.x2).appendTo(ha_elt);
$(tag).attr('name', 'y2_'+cssid).val(selection.y2).appendTo(ha_elt);
$(tag).attr('name', 'width_'+cssid).val(selection.width).appendTo(ha_elt);
$(tag).attr('name', 'height_'+cssid).val(selection.height).appendTo(ha_elt);
$(tag).attr('name', 'deleted_'+cssid).appendTo(ha_elt);
setModified();
clearNothingToCode(img_id);
return getHighlightedArea(cssid);
}

function saveHighlightedArea (ha) {
// Get element containing hidden fields and update their values
cssid = ha.cssid;
var ha_elt = $('#' + cssid);
$("[name='code_id_"+cssid+"']").val(ha.code_id);
$("[name='x1_"+cssid+"']").val(ha.x1);
$("[name='x2_"+cssid+"']").val(ha.x2);
$("[name='y1_"+cssid+"']").val(ha.y1);
$("[name='y2_"+cssid+"']").val(ha.y2);
$("[name='width_"+cssid+"']").val(ha.width);
$("[name='height_"+cssid+"']").val(ha.height);
$("[name='deleted_"+cssid+"']").val(ha.deleted);
setModified();
}

function deleteHighlightedAreas (img_id) {
ha_list = getHighlightedAreas(img_id);
var i;
for (i = 0; i < ha_list.length; i++) {
ha_list[i].deleted = '1';
saveHighlightedArea(ha_list[i]);
}
clearNothingToCode(img_id);
setModified();
}

function getHighlightedArea (cssid) {
ha = {}
ha.cssid = cssid;
ha.id = $("[name='id_"+cssid+"']").val();
ha.code_id = $("[name='code_id_"+cssid+"']").val();
ha.x1 = $("[name='x1_"+cssid+"']").val();
ha.x2 = $("[name='x2_"+cssid+"']").val();
ha.y1 = $("[name='y1_"+cssid+"']").val();
ha.y2 = $("[name='y2_"+cssid+"']").val();
ha.width = $("[name='width_"+cssid+"']").val();
ha.height = $("[name='height_"+cssid+"']").val();
ha.deleted = $("[name='deleted_"+cssid+"']").val();
return ha;
}

function getHighlightedAreas (img_id) {
// Get div containing highlighted area info for the specified image.
// Then load the highlighted area for each child element.
var ha_list = [];
$("#ha_group_" + img_id).children().each(function () {
ha_list.push(getHighlightedArea($(this).attr('id')));
});
return ha_list;
}
47 changes: 30 additions & 17 deletions app/controllers/coding_controller.rb
Expand Up @@ -6,13 +6,7 @@ def process_images
# set the @thread variable with the request thread
@thread = Threadx.find_by_thread_name params[:thread_name]
@image_counter = @thread.images.length
@highlighted_areas = []

# add only the hightlighted area related to existing images
@thread.highlighted_areas.each do |ha|
@highlighted_areas << ha if @thread.images.include? ha.image
end

@highlighted_areas = @thread.highlighted_areas

=begin
Expand All @@ -38,11 +32,36 @@ def process_images
# process the submitted highlighted area from the coding view, and redirect to the display
def process_highlighted_areas
@thread = Threadx.find_by_thread_name params[:thread_name]
# sort the images
@images = @thread.images.sort do |img1, img2|
img1.publication_date <=> img2.publication_date
@images = @thread.images

# Look for images with nothing to code
params[:image_name].each do |image_name|
image = Image.find_by_image_name(image_name)
@thread.coded_pages.for_user(current_user).for_image(image).delete_all
if params["nothing_to_code_#{image_name}"] == '1'
@thread.coded_pages.create(:user_id => current_user.id, :image_id => image.id)
end
end
# Go through each submitted highlighted area
params[:ha_name].each do |ha_name|
image = Image.find_by_image_name(params["img_id_#{ha_name}"])
if params["id_#{ha_name}"].to_i == 0
# This is a new area
code = Code.find params["code_id_#{ha_name}"]
ha = code.highlighted_areas.create(image_id:image.id, code_id:code.id)
area = Area.create(highlighted_area_id: ha.id, x1: params["x1_#{ha_name}"].to_i, y1: params["y1_#{ha_name}"].to_i, x2: params["x2_#{ha_name}"].to_i, y2: params["y2_#{ha_name}"].to_i, width: params["width_#{ha_name}"].to_i, height: params["height_#{ha_name}"].to_i)
else
# Updating an existing area
ha = @thread.highlighted_areas.find(params["id_#{ha_name}"])
if params["deleted_#{ha_name}"] == '1'
ha.areas[0].destroy
ha.destroy
else
ha.update_attribute('code_id', params["code_id_#{ha_name}"].to_i)
ha.areas[0].update_attributes(x1: params["x1_#{ha_name}"].to_i, y1: params["y1_#{ha_name}"].to_i, x2: params["x2_#{ha_name}"].to_i, y2: params["y2_#{ha_name}"].to_i, width: params["width_#{ha_name}"].to_i, height: params["height_#{ha_name}"].to_i)
end
end
end

@image_counter = @thread.images.length

# set the highlighted areas values
Expand Down Expand Up @@ -88,12 +107,6 @@ def display
@thread.highlighted_areas.each do |ha|
@highlighted_areas << ha if @thread.images.include? ha.image
end

# sort highlighted areas by the image name
@highlighted_areas.sort! do |ha1,ha2|
ha1.name.split('_')[0][5..100].to_i <=> ha2.name.split('_')[0][5..100].to_i
end


# This part is used to calculate the highlighted areas percentages vertically
@images_columns = {}
Expand Down
48 changes: 44 additions & 4 deletions app/controllers/threads_controller.rb
Expand Up @@ -53,6 +53,31 @@ def create
# (params["topic_name_1"] != "" ) this condition is to be sure the thread has at least one topic
if @thread.valid? && params[:media] != nil && params["topic_name_1"] != ""

<<<<<<< HEAD
=======
# this array is made to be passed to Scraper.get_issues method, because this method accepts the specific format of newspapers names as the following
# {"es" => ["elpais", "abc"], "de" => ["faz", "bild"], "fr" => ["lemonde", "lacroix"], "it" => ["corriere_della_sera", "ilmessaggero"], "uk" => ["the_times", ],"us" => ["wsj", "newyork_times", "usa_today"]}
# name attribute holds the name of the newspaper {"elpais", "abc", ...}
newspapers_names = {}

# the value of the media will be an array of the media ids, like [23,522,12,4]
media = params[:media]

# formatting the newspapers_names hash as mentioned above
media.each do |m|
_media = Media.find(m)
@thread.media << _media
# for each media country_code(code like {"es", "de", ...}) it appends the newspapers
if newspapers_names[_media.country_code] != nil
newspapers_names[_media.country_code] << _media.name
# but if the country_code array is empty, it will create a new array
else
newspapers_names[_media.country_code] = []
newspapers_names[_media.country_code] << _media.name
end
end

>>>>>>> issue67
# create object for each code (topic) submited
codes = []
number_of_topics = params[:topic_count].to_i
Expand Down Expand Up @@ -104,6 +129,7 @@ def create
# It adds a reference to the scraped images to the thread
@thread.images << images

<<<<<<< HEAD
# It adds the highlighted areas, the thread
# there is a limitation in this version which is; it supports two highlighted areas for each image
# we can in the future add loop to add any number of highlighted areas
Expand All @@ -117,6 +143,8 @@ def create

end

=======
>>>>>>> issue67
# add the codes to thread
@thread.codes << codes

Expand Down Expand Up @@ -189,6 +217,20 @@ def update
end

if true
newspapers_names = {}
@thread.media = []

media.each do |m|
_media = Media.find(m)
@thread.media << _media
if newspapers_names[_media.country_code] != nil
newspapers_names[_media.country_code] << _media.name
else
newspapers_names[_media.country_code] = []
newspapers_names[_media.country_code] << _media.name
end
end

newspapers_images = Scraper.get_issues(@thread.start_date, @thread.end_date, newspapers_names)

newspapers_images.each do |image_name, image_info|
Expand Down Expand Up @@ -283,10 +325,8 @@ def show
# the destroy actions, is for deleting a thread
def destroy
@thread = Threadx.find_by_thread_name params[:id]
@thread.codes.each do |code|
code.highlighted_areas.destroy_all
code.destroy
end
@thread.codes.destroy_all
@thread.highlighted_areas.destroy_all
@thread.destroy
redirect_to "/threads/"
end
Expand Down
14 changes: 14 additions & 0 deletions app/models/coded_page.rb
@@ -0,0 +1,14 @@
class CodedPage < ActiveRecord::Base
belongs_to :threadx
has_one :user
has_one :image

def self.for_user(user)
where(:user_id => user.id)
end

def self.for_image(image)
where(:image_id => image.id)
end

end
13 changes: 12 additions & 1 deletion app/models/highlighted_area.rb
Expand Up @@ -3,5 +3,16 @@ class HighlightedArea < ActiveRecord::Base
belongs_to :code
belongs_to :user
belongs_to :image
belongs_to :threadx

def threadx
code.threadx if not code.nil?
end

def self.by_image(image)
where(:image_id => image.id)
end

def self.by_threadx(threadx)
where(:code_id => threadx.code_ids)
end
end

0 comments on commit 4f4d21e

Please sign in to comment.