Skip to content

Commit 4f4d21e

Browse files
committed
merges issue67 to master #67
2 parents bff0bba + 8e44455 commit 4f4d21e

15 files changed

Lines changed: 538 additions & 479 deletions

app/assets/javascripts/coding.js

Lines changed: 199 additions & 294 deletions
Large diffs are not rendered by default.

app/assets/javascripts/display.js

Lines changed: 29 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -41,73 +41,34 @@ $(function () {
4141

4242
// load the highlighted areas for each image
4343
function loadImagesHighlightedAreas () {
44-
45-
// iterates over all the highlighted areas
46-
var high_areas = $("#high_areas div")
47-
for (var i = high_areas.length -1; i >= 0; i--) {
48-
var c_image_id = $(high_areas[i]).attr("id").substr(5)
49-
50-
var img_name = $($(high_areas[i]).children()[2]).attr("value")
51-
var c_image = $('img[name='+img_name+']')
52-
var high_area1 = $("div[image_name="+img_name+"]").find("div#high_area"+1)
53-
var high_area2 = $("div[image_name="+img_name+"]").find("div#high_area"+2)
54-
55-
56-
var dispalyed_img_size = 670
57-
var ratio = (dispalyed_img_size/c_image.width())
58-
59-
// higlighted area 1
60-
61-
var curr_high_area_code = $("#image"+c_image_id+"_ha1"+"_code_id").attr("value")
62-
63-
// in case of "nothing to code here"
64-
if (curr_high_area_code == "-1") {
65-
high_area1.css("background-color", "#eee")
66-
}else{
67-
high_area1.css("background-color", $("#code_"+curr_high_area_code).css("background-color"))
68-
}
69-
70-
var _top = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_y1").attr("value")) / ratio
71-
72-
_top = (_top) + c_image.position().top
73-
74-
high_area1.css("top", Math.ceil(_top) + "px" )
75-
76-
var _left = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_x1").attr("value")) / ratio
77-
78-
_left = (_left) + c_image.position().left
79-
high_area1.css("left", Math.ceil(_left) + "px")
80-
81-
var _width = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_width").attr("value")) / ratio
82-
83-
high_area1.css("width", (_width) + "px")
84-
85-
var _height = parseFloat($("#image"+c_image_id+"_ha"+"1"+"_height").attr("value")) / ratio
86-
high_area1.css("height", (_height) + "px")
87-
88-
89-
// higlighted area 2
90-
91-
curr_high_area_code = $("#image"+c_image_id+"_ha2"+"_code_id").attr("value")
92-
93-
high_area2.css("background-color", $("#code_"+curr_high_area_code).css("background-color"))
94-
95-
_top = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_y1").attr("value")) / ratio
96-
_top = _top + c_image.position().top
97-
high_area2.css("top", Math.ceil(_top) + "px" )
98-
99-
_left = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_x1").attr("value")) / ratio
100-
_left = _left + c_image.position().left
101-
high_area2.css("left", Math.ceil(_left) + "px")
102-
103-
_width = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_width").attr("value")) / ratio
104-
high_area2.css("width", _width + "px")
105-
106-
_height = parseFloat($("#image"+c_image_id+"_ha"+"2"+"_height").attr("value")) / ratio
107-
high_area2.css("height", _height + "px")
108-
109-
};
110-
44+
45+
// Iterates over all images
46+
$("#high_images .ha_group").each(function () {
47+
var img_id = $(this).attr('id').substr(9);
48+
ha_list = getHighlightedAreas(img_id);
49+
var img = $('img[name='+img_id+']');
50+
// Iterate over all highlighted areas for a single image
51+
var i, ha;
52+
for (i = 0; i < ha_list.length; i++) {
53+
// Get model and view
54+
ha = ha_list[i];
55+
ha_div = $('#ha_' + ha.id);
56+
// Calculate scaling
57+
var dispalyed_img_size = 670;
58+
var ratio = (dispalyed_img_size/img.width());
59+
// Calculate geometry
60+
var _top = ha.y1/ratio + img.position().top;
61+
var _left = ha.x1/ratio + img.position().left;
62+
var _width = ha.width / ratio;
63+
var _height = ha.height / ratio;
64+
// Update css
65+
ha_div.css("top", Math.ceil(_top) + "px" );
66+
ha_div.css("left", Math.ceil(_left) + "px");
67+
ha_div.css("width", _width + "px");
68+
ha_div.css("height", _height + "px");
69+
ha_div.css("background-color", $("#code_"+ha.code_id).css("background-color"));
70+
}
71+
});
11172
}
11273

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

146-
})
107+
})
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Interface for highlighted areas
2+
// addHighlightedArea() - Add a single highlighted area to the current page
3+
// saveHighightedArea() - Save a highlighted area
4+
// deleteHighlightedAreas() - Delete all highlighted areas for the current page
5+
// getHighlightedArea() - Get data for a single highlighted area
6+
// getHighlightedAreas() - Get a list of highlighted areas
7+
8+
function addHighlightedArea (img_id, code_id, selection) {
9+
// Get div containing highlighted area info for the specified image
10+
var ha_group = $("#ha_group_" + img_id);
11+
var count = ha_group.children().length;
12+
var cssid = img_id + '_' + (count+1);
13+
// Create hidden fields to contain data
14+
var ha_elt = $('<div>').attr('id', cssid).appendTo(ha_group);
15+
var tag = '<input type="hidden"/>';
16+
$(tag).attr('name', 'ha_name[]').val(cssid).appendTo(ha_elt);
17+
$(tag).attr('name', 'img_id_'+cssid).val(img_id).appendTo(ha_elt);
18+
$(tag).attr('name', 'id_'+cssid).val(0).appendTo(ha_elt);
19+
$(tag).attr('name', 'code_id_'+cssid).val(code_id).appendTo(ha_elt);
20+
$(tag).attr('name', 'x1_'+cssid).val(selection.x1).appendTo(ha_elt);
21+
$(tag).attr('name', 'y1_'+cssid).val(selection.y1).appendTo(ha_elt);
22+
$(tag).attr('name', 'x2_'+cssid).val(selection.x2).appendTo(ha_elt);
23+
$(tag).attr('name', 'y2_'+cssid).val(selection.y2).appendTo(ha_elt);
24+
$(tag).attr('name', 'width_'+cssid).val(selection.width).appendTo(ha_elt);
25+
$(tag).attr('name', 'height_'+cssid).val(selection.height).appendTo(ha_elt);
26+
$(tag).attr('name', 'deleted_'+cssid).appendTo(ha_elt);
27+
setModified();
28+
clearNothingToCode(img_id);
29+
return getHighlightedArea(cssid);
30+
}
31+
32+
function saveHighlightedArea (ha) {
33+
// Get element containing hidden fields and update their values
34+
cssid = ha.cssid;
35+
var ha_elt = $('#' + cssid);
36+
$("[name='code_id_"+cssid+"']").val(ha.code_id);
37+
$("[name='x1_"+cssid+"']").val(ha.x1);
38+
$("[name='x2_"+cssid+"']").val(ha.x2);
39+
$("[name='y1_"+cssid+"']").val(ha.y1);
40+
$("[name='y2_"+cssid+"']").val(ha.y2);
41+
$("[name='width_"+cssid+"']").val(ha.width);
42+
$("[name='height_"+cssid+"']").val(ha.height);
43+
$("[name='deleted_"+cssid+"']").val(ha.deleted);
44+
setModified();
45+
}
46+
47+
function deleteHighlightedAreas (img_id) {
48+
ha_list = getHighlightedAreas(img_id);
49+
var i;
50+
for (i = 0; i < ha_list.length; i++) {
51+
ha_list[i].deleted = '1';
52+
saveHighlightedArea(ha_list[i]);
53+
}
54+
clearNothingToCode(img_id);
55+
setModified();
56+
}
57+
58+
function getHighlightedArea (cssid) {
59+
ha = {}
60+
ha.cssid = cssid;
61+
ha.id = $("[name='id_"+cssid+"']").val();
62+
ha.code_id = $("[name='code_id_"+cssid+"']").val();
63+
ha.x1 = $("[name='x1_"+cssid+"']").val();
64+
ha.x2 = $("[name='x2_"+cssid+"']").val();
65+
ha.y1 = $("[name='y1_"+cssid+"']").val();
66+
ha.y2 = $("[name='y2_"+cssid+"']").val();
67+
ha.width = $("[name='width_"+cssid+"']").val();
68+
ha.height = $("[name='height_"+cssid+"']").val();
69+
ha.deleted = $("[name='deleted_"+cssid+"']").val();
70+
return ha;
71+
}
72+
73+
function getHighlightedAreas (img_id) {
74+
// Get div containing highlighted area info for the specified image.
75+
// Then load the highlighted area for each child element.
76+
var ha_list = [];
77+
$("#ha_group_" + img_id).children().each(function () {
78+
ha_list.push(getHighlightedArea($(this).attr('id')));
79+
});
80+
return ha_list;
81+
}

app/controllers/coding_controller.rb

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ def process_images
66
# set the @thread variable with the request thread
77
@thread = Threadx.find_by_thread_name params[:thread_name]
88
@image_counter = @thread.images.length
9-
@highlighted_areas = []
10-
11-
# add only the hightlighted area related to existing images
12-
@thread.highlighted_areas.each do |ha|
13-
@highlighted_areas << ha if @thread.images.include? ha.image
14-
end
15-
9+
@highlighted_areas = @thread.highlighted_areas
1610

1711
=begin
1812
@@ -38,11 +32,36 @@ def process_images
3832
# process the submitted highlighted area from the coding view, and redirect to the display
3933
def process_highlighted_areas
4034
@thread = Threadx.find_by_thread_name params[:thread_name]
41-
# sort the images
42-
@images = @thread.images.sort do |img1, img2|
43-
img1.publication_date <=> img2.publication_date
35+
@images = @thread.images
36+
37+
# Look for images with nothing to code
38+
params[:image_name].each do |image_name|
39+
image = Image.find_by_image_name(image_name)
40+
@thread.coded_pages.for_user(current_user).for_image(image).delete_all
41+
if params["nothing_to_code_#{image_name}"] == '1'
42+
@thread.coded_pages.create(:user_id => current_user.id, :image_id => image.id)
43+
end
44+
end
45+
# Go through each submitted highlighted area
46+
params[:ha_name].each do |ha_name|
47+
image = Image.find_by_image_name(params["img_id_#{ha_name}"])
48+
if params["id_#{ha_name}"].to_i == 0
49+
# This is a new area
50+
code = Code.find params["code_id_#{ha_name}"]
51+
ha = code.highlighted_areas.create(image_id:image.id, code_id:code.id)
52+
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)
53+
else
54+
# Updating an existing area
55+
ha = @thread.highlighted_areas.find(params["id_#{ha_name}"])
56+
if params["deleted_#{ha_name}"] == '1'
57+
ha.areas[0].destroy
58+
ha.destroy
59+
else
60+
ha.update_attribute('code_id', params["code_id_#{ha_name}"].to_i)
61+
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)
62+
end
63+
end
4464
end
45-
4665
@image_counter = @thread.images.length
4766

4867
# set the highlighted areas values
@@ -88,12 +107,6 @@ def display
88107
@thread.highlighted_areas.each do |ha|
89108
@highlighted_areas << ha if @thread.images.include? ha.image
90109
end
91-
92-
# sort highlighted areas by the image name
93-
@highlighted_areas.sort! do |ha1,ha2|
94-
ha1.name.split('_')[0][5..100].to_i <=> ha2.name.split('_')[0][5..100].to_i
95-
end
96-
97110

98111
# This part is used to calculate the highlighted areas percentages vertically
99112
@images_columns = {}

app/controllers/threads_controller.rb

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ def create
5353
# (params["topic_name_1"] != "" ) this condition is to be sure the thread has at least one topic
5454
if @thread.valid? && params[:media] != nil && params["topic_name_1"] != ""
5555

56+
<<<<<<< HEAD
57+
=======
58+
# 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
59+
# {"es" => ["elpais", "abc"], "de" => ["faz", "bild"], "fr" => ["lemonde", "lacroix"], "it" => ["corriere_della_sera", "ilmessaggero"], "uk" => ["the_times", ],"us" => ["wsj", "newyork_times", "usa_today"]}
60+
# name attribute holds the name of the newspaper {"elpais", "abc", ...}
61+
newspapers_names = {}
62+
63+
# the value of the media will be an array of the media ids, like [23,522,12,4]
64+
media = params[:media]
65+
66+
# formatting the newspapers_names hash as mentioned above
67+
media.each do |m|
68+
_media = Media.find(m)
69+
@thread.media << _media
70+
# for each media country_code(code like {"es", "de", ...}) it appends the newspapers
71+
if newspapers_names[_media.country_code] != nil
72+
newspapers_names[_media.country_code] << _media.name
73+
# but if the country_code array is empty, it will create a new array
74+
else
75+
newspapers_names[_media.country_code] = []
76+
newspapers_names[_media.country_code] << _media.name
77+
end
78+
end
79+
80+
>>>>>>> issue67
5681
# create object for each code (topic) submited
5782
codes = []
5883
number_of_topics = params[:topic_count].to_i
@@ -104,6 +129,7 @@ def create
104129
# It adds a reference to the scraped images to the thread
105130
@thread.images << images
106131

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

118144
end
119145

146+
=======
147+
>>>>>>> issue67
120148
# add the codes to thread
121149
@thread.codes << codes
122150

@@ -189,6 +217,20 @@ def update
189217
end
190218

191219
if true
220+
newspapers_names = {}
221+
@thread.media = []
222+
223+
media.each do |m|
224+
_media = Media.find(m)
225+
@thread.media << _media
226+
if newspapers_names[_media.country_code] != nil
227+
newspapers_names[_media.country_code] << _media.name
228+
else
229+
newspapers_names[_media.country_code] = []
230+
newspapers_names[_media.country_code] << _media.name
231+
end
232+
end
233+
192234
newspapers_images = Scraper.get_issues(@thread.start_date, @thread.end_date, newspapers_names)
193235

194236
newspapers_images.each do |image_name, image_info|
@@ -283,10 +325,8 @@ def show
283325
# the destroy actions, is for deleting a thread
284326
def destroy
285327
@thread = Threadx.find_by_thread_name params[:id]
286-
@thread.codes.each do |code|
287-
code.highlighted_areas.destroy_all
288-
code.destroy
289-
end
328+
@thread.codes.destroy_all
329+
@thread.highlighted_areas.destroy_all
290330
@thread.destroy
291331
redirect_to "/threads/"
292332
end

app/models/coded_page.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CodedPage < ActiveRecord::Base
2+
belongs_to :threadx
3+
has_one :user
4+
has_one :image
5+
6+
def self.for_user(user)
7+
where(:user_id => user.id)
8+
end
9+
10+
def self.for_image(image)
11+
where(:image_id => image.id)
12+
end
13+
14+
end

app/models/highlighted_area.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,16 @@ class HighlightedArea < ActiveRecord::Base
33
belongs_to :code
44
belongs_to :user
55
belongs_to :image
6-
belongs_to :threadx
6+
7+
def threadx
8+
code.threadx if not code.nil?
9+
end
10+
11+
def self.by_image(image)
12+
where(:image_id => image.id)
13+
end
14+
15+
def self.by_threadx(threadx)
16+
where(:code_id => threadx.code_ids)
17+
end
718
end

0 commit comments

Comments
 (0)