Create tagging interface for specified image Example ptTags 78 name wo

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Create a tagging interface for a specified image.
* Example:
* ptTags = [{"y": 78, "name": "words", "friend_id": 3, "id": 4, "x": 23}]
* var photo_tagger = new PhotoTagger("photo","tag_form",ptTags);
*
*/
var PhotoTagger = Class.create(
{
/**
* Set up tagging events for the specified image.
*
* The PhotoTagger renders all specified tags for the image,
* and shows an implementation-specific form for new tag creation.
* If the implementation-specific form contains fields named "x" and "y",
* these fields are updated with the x and y tag locations.
*
*
* @constructor
* @param {string} image_id Element ID of targeted image.
* @param {string} form_id Element ID of implementation-specific form for server-side tag creation.
* @param {array} tags_arr JSON array of existing tags to display
*/
initialize: function(image_id, form_id, tags_arr) {
$(image_id).style.cursor = "crosshair";
$(image_id).photoTagger = this;
$(image_id).parentNode.appendChild($(form_id)); //needs to be adjacent to img element
$(image_id).photoTagger.pt_form_id = form_id;
this.image_id = image_id;
this.tags = tags_arr;
$$('img#'+image_id).invoke('observe','mouseover', PhotoTagger.onMouseover);
$$('photo-tagger-label').invoke('observe','mouseover', PhotoTagger.onMouseover);
$$('img#'+image_id).invoke('observe', 'click', PhotoTagger.onClick);
$$('img#'+image_id).invoke('observe', 'mouseout', PhotoTagger.onMouseout);
taggerframe = new Element('div',{'id' : 'pt-taggerframe'})
$(image_id).parentNode.appendChild(taggerframe);
this.render_tags();
},
/**
* Hides form and tagging frame
*/
cancelForm : function(){
new Effect.Fade(this.pt_form_id);
new Effect.Fade("pt-taggerframe");
},
show_tags: function(){
for (var i=0; i < this.tags.length; i++) {
tag = this.tags[i];
// /100 for percent conversion
style = "left:"+ $(this.image_id).width * (tag.x / 100) +"px; top:"+ $(this.image_id).height * (tag.y / 100) +"px; display:block;";
$("pt-tag-" + tag.id).setStyle(style);
}
$$('div.pt-tag').each(Element.show);
},
hide_tags: function(){
$$('div.pt-tag').each(Element.hide);
},
/**
* Set a new tag array and render all new tags as elements.
* @param {array} tag_array Array of JSON tag objects
*/
update_tags : function(tag_array){
this.tags = tag_array;
this.render_tags();
},
/**
* Ensure that all tags are rendered to elements.
* @private
*/
render_tags : function(){
for (var i=0; i < this.tags.length; i++) {
tag = this.tags[i];
if( $("pt-tag-"+tag.id) == null ){
tag_elem = new Element('div',{"id": "pt-tag-"+tag.id, "class": "pt-tag", "style":"display:none;"});
tag_elem.update(tag.name);
$(this.image_id).parentNode.appendChild(tag_elem);
}
}
},
tags : []
});
//
// STATIC FUNCTIONS
//
/**
* Called on targeted image: Positions the labels on mouseover
* Shows all labels
*/
PhotoTagger.onMouseover = function(e){
this.photoTagger.show_tags();
};
/**
* Called on targeted image: Hide labels on mouseout
*/
PhotoTagger.onMouseout = function(e){
if(PhotoTagger.onMouseoutCheck(this, e)){
this.photoTagger.hide_tags();
}
};
/**
* Are we really out of the photo or on a label?
*/
PhotoTagger.onMouseoutCheck = function(element, event){
picture_location = PhotoTagger.find_element_pos(element);
mouse_location = PhotoTagger.find_mouse_pos(event);
if( mouse_location[0] < picture_location[0] ){
return true;
}
if( mouse_location[1] < picture_location[1]){
return true;
}
if( mouse_location[1] > picture_location[1]+element.height){
return true;
}
if( mouse_location[0] > picture_location[0]+element.width){
return true;
}
return false;
};
/**
* Called on targeted image:
*/
PhotoTagger.onClick = function(e){
picture_location = PhotoTagger.find_element_pos(this);
mouse_location = PhotoTagger.find_mouse_pos(e);
// X and Y positions of the label to be created
tag_x = mouse_location[0] - picture_location[0];
tag_y = mouse_location[1] - picture_location[1];
// label has to be on picture, min 5 px
if( tag_x < 5) tag_x = 5;
if( tag_y < 5) tag_y = 5;
// position for box representing what you are labeling
abs_x = mouse_location[0] - picture_location[0];
abs_y = mouse_location[1] - picture_location[1];
// box pushed down and right if too far up or left
if( abs_x < 50) abs_x = 50;
if( abs_y < 50) abs_y = 50;
// form location
x_pointer_offset = -40;
y_pointer_offset = 38;
form_x = abs_x + x_pointer_offset;
form_y = abs_y + y_pointer_offset; // Relative to containing element in jQ? (was click_abs_y_loc + y_pointer_offset);
frame_height = frame_width = 80;
frame_x = abs_x - (frame_width/2);
frame_y = abs_y - (frame_height/2);
frame_style = "position:absolute; top:"+ (frame_y - 4) +"px; left:"+ frame_x +"px; border:0.3em solid #cccccc; width:7em; height:6em;z-index:10;color:black;display:block; z-index:10001;";
style = "position: absolute;color:black; top:" + form_y + "px; left:" + form_x + "px; background-color:white; border: 0.3em solid #cccccc; padding:0.5em; z-index:10001;";
$('pt-taggerframe').setStyle(frame_style);
$('pt-taggerframe').show();
$('x').value = parseInt((tag_x/this.width)*100);
$('y').value = parseInt((tag_y/this.height)*100);
$('friend_name').value = "";
$('create-tag-response').update("");
$(this.photoTagger.pt_form_id).setStyle(style);
$(this.photoTagger.pt_form_id).show();
$('friend_name').focus();
};
/**
* Quirksmode helper to get element position
* relattive to the top left corner of the document
*/
PhotoTagger.find_element_pos = function(obj) {
var curleft = 0;
var curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while((obj = obj.offsetParent)); //yes, while it's not undefined
}
// mouse position relative to the document's top left corner
return [curleft,curtop];
};
/**
* Quirksmode helper to get mouse event position
* relative to the top left corner of the document
*/
PhotoTagger.find_mouse_pos = function(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY){
posx = e.pageX;
posy = e.pageY;
}else if (e.clientX || e.clientY){
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
return [posx, posy];
};