Commit | Line | Data |
76f05423 |
1 | var MARGIN=30; |
2 | var svg_root = null; |
3 | var svg_root_element = null; |
4 | var start_element_height = 0; |
5 | var reltypes = {}; |
5f15640c |
6 | var readingdata = {}; |
89aae3ee |
7 | var text_direction = 'LR'; |
76f05423 |
8 | |
a471cb0f |
9 | jQuery.removeFromArray = function(value, arr) { |
10 | return jQuery.grep(arr, function(elem, index) { |
11 | return elem !== value; |
12 | }); |
13 | }; |
14 | |
a84ca4de |
15 | function arrayUnique(array) { |
16 | var a = array.concat(); |
17 | for(var i=0; i<a.length; ++i) { |
18 | for(var j=i+1; j<a.length; ++j) { |
19 | if(a[i] === a[j]) |
20 | a.splice(j--, 1); |
21 | } |
22 | } |
23 | return a; |
24 | }; |
25 | |
5f15640c |
26 | function getTextURL( which ) { |
3f9d7ae5 |
27 | return basepath + textid + '/' + which; |
5f15640c |
28 | } |
29 | |
30 | function getReadingURL( reading_id ) { |
3f9d7ae5 |
31 | return basepath + textid + '/reading/' + reading_id; |
b28e606e |
32 | } |
33 | |
45ee3b96 |
34 | // Make an XML ID into a valid selector |
35 | function jq(myid) { |
36 | return '#' + myid.replace(/(:|\.)/g,'\\$1'); |
37 | } |
38 | |
065c7cf2 |
39 | // Actions for opening the reading panel |
40 | function node_dblclick_listener( evt ) { |
41 | // Open the reading dialogue for the given node. |
42 | // First get the reading info |
43 | var reading_id = $(this).attr('id'); |
44 | var reading_info = readingdata[reading_id]; |
45 | // and then populate the dialog box with it. |
46 | // Set the easy properties first |
47 | $('#reading-form').dialog( 'option', 'title', 'Reading information for "' + reading_info['text'] + '"' ); |
48 | $('#reading_id').val( reading_id ); |
798fa939 |
49 | toggle_checkbox( $('#reading_is_nonsense'), reading_info['is_nonsense'] ); |
50 | toggle_checkbox( $('#reading_grammar_invalid'), reading_info['grammar_invalid'] ); |
065c7cf2 |
51 | // Use .text as a backup for .normal_form |
52 | var normal_form = reading_info['normal_form']; |
53 | if( !normal_form ) { |
54 | normal_form = reading_info['text']; |
55 | } |
56 | var nfboxsize = 10; |
57 | if( normal_form.length > 9 ) { |
58 | nfboxsize = normal_form.length + 1; |
59 | } |
60 | $('#reading_normal_form').attr( 'size', nfboxsize ) |
61 | $('#reading_normal_form').val( normal_form ); |
62 | // Now do the morphological properties. |
0dcdd5ec |
63 | morphology_form( reading_info['lexemes'] ); |
065c7cf2 |
64 | // and then open the dialog. |
0dcdd5ec |
65 | $('#reading-form').dialog("open"); |
66 | } |
67 | |
798fa939 |
68 | function toggle_checkbox( box, value ) { |
69 | if( value == null ) { |
70 | value = false; |
71 | } |
72 | box.attr('checked', value ); |
73 | } |
74 | |
0dcdd5ec |
75 | function morphology_form ( lexlist ) { |
487674b9 |
76 | if( lexlist.length ) { |
77 | $('#morph_outer').show(); |
78 | $('#morphology').empty(); |
79 | $.each( lexlist, function( idx, lex ) { |
80 | var morphoptions = []; |
81 | if( 'wordform_matchlist' in lex ) { |
82 | $.each( lex['wordform_matchlist'], function( tdx, tag ) { |
83 | var tagstr = stringify_wordform( tag ); |
84 | morphoptions.push( tagstr ); |
85 | }); |
86 | } |
87 | var formtag = 'morphology_' + idx; |
88 | var formstr = ''; |
89 | if( 'form' in lex ) { |
90 | formstr = stringify_wordform( lex['form'] ); |
91 | } |
92 | var form_morph_elements = morph_elements( |
93 | formtag, lex['string'], formstr, morphoptions ); |
94 | $.each( form_morph_elements, function( idx, el ) { |
95 | $('#morphology').append( el ); |
08f8443a |
96 | }); |
065c7cf2 |
97 | }); |
487674b9 |
98 | } else { |
99 | $('#morph_outer').hide(); |
100 | } |
065c7cf2 |
101 | } |
102 | |
103 | function stringify_wordform ( tag ) { |
08f8443a |
104 | if( tag ) { |
105 | var elements = tag.split(' // '); |
106 | return elements[1] + ' // ' + elements[2]; |
107 | } |
108 | return '' |
065c7cf2 |
109 | } |
110 | |
d1132306 |
111 | function morph_elements ( formtag, formtxt, currform, morphoptions ) { |
112 | var clicktag = '(Click to select)'; |
113 | if ( !currform ) { |
114 | currform = clicktag; |
115 | } |
065c7cf2 |
116 | var formlabel = $('<label/>').attr( 'id', 'label_' + formtag ).attr( |
d1132306 |
117 | 'for', 'reading_' + formtag ).text( formtxt + ': ' ); |
065c7cf2 |
118 | var forminput = $('<input/>').attr( 'id', 'reading_' + formtag ).attr( |
0dcdd5ec |
119 | 'name', 'reading_' + formtag ).attr( 'size', '50' ).attr( |
120 | 'class', 'reading_morphology' ).val( currform ); |
d1132306 |
121 | forminput.autocomplete({ source: morphoptions, minLength: 0 }); |
122 | forminput.focus( function() { |
123 | if( $(this).val() == clicktag ) { |
124 | $(this).val(''); |
125 | } |
126 | $(this).autocomplete('search', '') |
127 | }); |
065c7cf2 |
128 | var morphel = [ formlabel, forminput, $('<br/>') ]; |
129 | return morphel; |
130 | } |
131 | |
4c41c02c |
132 | function color_inactive ( el ) { |
133 | var reading_id = $(el).parent().attr('id'); |
134 | var reading_info = readingdata[reading_id]; |
135 | // If the reading info has any non-disambiguated lexemes, color it yellow; |
136 | // otherwise color it green. |
137 | $(el).attr( {stroke:'green', fill:'#b3f36d'} ); |
bb3230b1 |
138 | if( reading_info ) { |
139 | $.each( reading_info['lexemes'], function ( idx, lex ) { |
140 | if( !lex['is_disambiguated'] || lex['is_disambiguated'] == 0 ) { |
141 | $(el).attr( {stroke:'orange', fill:'#fee233'} ); |
142 | } |
143 | }); |
144 | } |
4c41c02c |
145 | } |
146 | |
997ebe92 |
147 | function relemmatize () { |
148 | // Send the reading for a new lemmatization and reopen the form. |
a0a66634 |
149 | $('#relemmatize_pending').show(); |
997ebe92 |
150 | var reading_id = $('#reading_id').val() |
151 | ncpath = getReadingURL( reading_id ); |
152 | form_values = { |
153 | 'normal_form': $('#reading_normal_form').val(), |
154 | 'relemmatize': 1 }; |
155 | var jqjson = $.post( ncpath, form_values, function( data ) { |
156 | // Update the form with the return |
157 | if( 'id' in data ) { |
158 | // We got back a good answer. Stash it |
159 | readingdata[reading_id] = data; |
160 | // and regenerate the morphology form. |
161 | morphology_form( data['lexemes'] ); |
162 | } else { |
163 | alert("Could not relemmatize as requested: " + data['error']); |
164 | } |
a0a66634 |
165 | $('#relemmatize_pending').hide(); |
997ebe92 |
166 | }); |
167 | } |
168 | |
065c7cf2 |
169 | // Initialize the SVG once it exists |
b28e606e |
170 | function svgEnlargementLoaded() { |
fc018906 |
171 | //Give some visual evidence that we are working |
172 | $('#loading_overlay').show(); |
173 | lo_height = $("#enlargement_container").outerHeight(); |
174 | lo_width = $("#enlargement_container").outerWidth(); |
175 | $("#loading_overlay").height( lo_height ); |
176 | $("#loading_overlay").width( lo_width ); |
177 | $("#loading_overlay").offset( $("#enlargement_container").offset() ); |
178 | $("#loading_message").offset( |
179 | { 'top': lo_height / 2 - $("#loading_message").height() / 2, |
180 | 'left': lo_width / 2 - $("#loading_message").width() / 2 }); |
30d0ba1e |
181 | if( editable ) { |
182 | // Show the update toggle button. |
183 | $('#update_workspace_button').data('locked', false); |
184 | $('#update_workspace_button').css('background-position', '0px 44px'); |
185 | } |
065c7cf2 |
186 | $('#svgenlargement ellipse').parent().dblclick( node_dblclick_listener ); |
9529f69c |
187 | var graph_svg = $('#svgenlargement svg'); |
188 | var svg_g = $('#svgenlargement svg g')[0]; |
76f05423 |
189 | if (!svg_g) return; |
9529f69c |
190 | svg_root = graph_svg.svg().svg('get').root(); |
76f05423 |
191 | |
192 | // Find the real root and ignore any text nodes |
193 | for (i = 0; i < svg_root.childNodes.length; ++i) { |
194 | if (svg_root.childNodes[i].nodeName != '#text') { |
195 | svg_root_element = svg_root.childNodes[i]; |
196 | break; |
197 | } |
198 | } |
199 | |
30d0ba1e |
200 | //Set viewbox width and height to width and height of $('#svgenlargement svg'). |
201 | //This is essential to make sure zooming and panning works properly. |
9529f69c |
202 | svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' ); |
203 | svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' ); |
204 | //Now set scale and translate so svg height is about 150px and vertically centered in viewbox. |
205 | //This is just to create a nice starting enlargement. |
206 | var initial_svg_height = 250; |
207 | var scale = initial_svg_height/graph_svg.attr( 'height' ); |
208 | var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale); |
209 | var transform = svg_g.getAttribute('transform'); |
210 | var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] ); |
211 | translate += additional_translate; |
212 | var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')'; |
213 | svg_g.setAttribute('transform', transform); |
214 | //used to calculate min and max zoom level: |
4c41c02c |
215 | start_element_height = $('#__START__').children('ellipse')[0].getBBox().height; |
e538eccb |
216 | //some use of call backs to ensure succesive execution |
217 | add_relations( function() { |
218 | var rdgpath = getTextURL( 'readings' ); |
219 | $.getJSON( rdgpath, function( data ) { |
220 | readingdata = data; |
221 | $('#svgenlargement ellipse').each( function( i, el ) { color_inactive( el ) }); |
9231663d |
222 | $('#loading_overlay').hide(); |
e538eccb |
223 | }); |
e538eccb |
224 | }); |
c1add777 |
225 | |
226 | //initialize marquee |
227 | marquee = new Marquee(); |
89aae3ee |
228 | |
229 | if (text_direction == 'RL') { |
230 | scrollToEnd(); |
231 | } |
6afcd813 |
232 | } |
233 | |
fc018906 |
234 | function add_relations( callback_fn ) { |
56e3972e |
235 | // Add the relationship types to the keymap list |
56e3972e |
236 | $.each( relationship_types, function(index, typedef) { |
671c04b1 |
237 | li_elm = $('<li class="key">').css( "border-color", |
238 | relation_manager.relation_colors[index] ).text(typedef.name); |
239 | li_elm.append( $('<div>').attr('class', 'key_tip_container').append( |
240 | $('<div>').attr('class', 'key_tip').text(typedef.description) ) ); |
cfefd283 |
241 | $('#keymaplist').append( li_elm ); |
56e3972e |
242 | }); |
243 | // Now fetch the relationships themselves and add them to the graph |
244 | var rel_types = $.map( relationship_types, function(t) { return t.name }); |
245 | // Save this list of names to the outer element data so that the relationship |
246 | // factory can access it |
247 | $('#keymap').data('relations', rel_types); |
5f15640c |
248 | var textrelpath = getTextURL( 'relationships' ); |
56e3972e |
249 | $.getJSON( textrelpath, function(data) { |
250 | $.each(data, function( index, rel_info ) { |
251 | var type_index = $.inArray(rel_info.type, rel_types); |
51ca18ee |
252 | var source_found = get_ellipse( rel_info.source_id ); |
253 | var target_found = get_ellipse( rel_info.target_id ); |
cfa6ff76 |
254 | var emphasis = rel_info.is_significant; |
56e3972e |
255 | if( type_index != -1 && source_found.size() && target_found.size() ) { |
48a371e5 |
256 | var relation = relation_manager.create( rel_info.source_id, rel_info.target_id, type_index, emphasis ); |
51ca18ee |
257 | // Save the relationship data too. |
258 | $.each( rel_info, function( k, v ) { |
259 | relation.data( k, v ); |
260 | }); |
30d0ba1e |
261 | if( editable ) { |
51ca18ee |
262 | var node_obj = get_node_obj(rel_info.source_id); |
e538eccb |
263 | node_obj.set_selectable( false ); |
30d0ba1e |
264 | node_obj.ellipse.data( 'node_obj', null ); |
51ca18ee |
265 | node_obj = get_node_obj(rel_info.target_id); |
e538eccb |
266 | node_obj.set_selectable( false ); |
30d0ba1e |
267 | node_obj.ellipse.data( 'node_obj', null ); |
268 | } |
56e3972e |
269 | } |
270 | }); |
271 | callback_fn.call(); |
272 | }); |
b28e606e |
273 | } |
274 | |
275 | function get_ellipse( node_id ) { |
45ee3b96 |
276 | return $( jq( node_id ) + ' ellipse'); |
b28e606e |
277 | } |
278 | |
279 | function get_node_obj( node_id ) { |
9529f69c |
280 | var node_ellipse = get_ellipse( node_id ); |
281 | if( node_ellipse.data( 'node_obj' ) == null ) { |
282 | node_ellipse.data( 'node_obj', new node_obj(node_ellipse) ); |
283 | }; |
284 | return node_ellipse.data( 'node_obj' ); |
b28e606e |
285 | } |
286 | |
b28e606e |
287 | function node_obj(ellipse) { |
288 | this.ellipse = ellipse; |
289 | var self = this; |
290 | |
291 | this.x = 0; |
292 | this.y = 0; |
293 | this.dx = 0; |
294 | this.dy = 0; |
295 | this.node_elements = node_elements_for(self.ellipse); |
22a70299 |
296 | |
b28e606e |
297 | this.get_id = function() { |
45ee3b96 |
298 | return $(self.ellipse).parent().attr('id') |
b28e606e |
299 | } |
300 | |
e538eccb |
301 | this.set_selectable = function( clickable ) { |
302 | if( clickable && editable ) { |
303 | $(self.ellipse).attr( {stroke:'black', fill:'#fff'} ); |
304 | $(self.ellipse).parent().hover( this.enter_node, this.leave_node ); |
305 | $(self.ellipse).parent().mousedown( function(evt) { evt.stopPropagation() } ); |
306 | $(self.ellipse).parent().click( function(evt) { |
307 | evt.stopPropagation(); |
308 | if( $('ellipse[fill="#9999ff"]').size() > 0 ) { |
309 | $('ellipse[fill="#9999ff"]').each( function() { |
310 | $(this).data( 'node_obj' ).set_draggable( false ); |
311 | } ); |
312 | } |
313 | self.set_draggable( true ) |
314 | }); |
315 | } else { |
316 | $(self.ellipse).attr( {stroke:'black', fill:'#fff'} ); |
317 | self.ellipse.siblings('text').attr('class', ''); |
318 | $(self.ellipse).parent().unbind(); |
319 | $('body').unbind('mousemove'); |
320 | $('body').unbind('mouseup'); |
321 | } |
322 | } |
323 | |
b28e606e |
324 | this.set_draggable = function( draggable ) { |
30d0ba1e |
325 | if( draggable && editable ) { |
e538eccb |
326 | $(self.ellipse).attr( {stroke:'black', fill:'#9999ff'} ); |
05485bfd |
327 | $(self.ellipse).parent().mousedown( this.mousedown_listener ); |
e538eccb |
328 | $(self.ellipse).parent().unbind( 'mouseenter' ).unbind( 'mouseleave' ); |
05485bfd |
329 | self.ellipse.siblings('text').attr('class', 'noselect draggable'); |
b28e606e |
330 | } else { |
e538eccb |
331 | $(self.ellipse).attr( {stroke:'black', fill:'#fff'} ); |
05485bfd |
332 | self.ellipse.siblings('text').attr('class', ''); |
e538eccb |
333 | $(self.ellipse).parent().unbind( 'mousedown '); |
334 | $(self.ellipse).parent().mousedown( function(evt) { evt.stopPropagation() } ); |
335 | $(self.ellipse).parent().hover( this.enter_node, this.leave_node ); |
b28e606e |
336 | } |
337 | } |
338 | |
339 | this.mousedown_listener = function(evt) { |
340 | evt.stopPropagation(); |
341 | self.x = evt.clientX; |
342 | self.y = evt.clientY; |
343 | $('body').mousemove( self.mousemove_listener ); |
344 | $('body').mouseup( self.mouseup_listener ); |
05485bfd |
345 | $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave') |
e538eccb |
346 | self.ellipse.attr( 'fill', '#6b6bb2' ); |
b28e606e |
347 | first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" ); |
348 | if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) }; |
349 | } |
350 | |
351 | this.mousemove_listener = function(evt) { |
9529f69c |
352 | self.dx = (evt.clientX - self.x) / mouse_scale; |
353 | self.dy = (evt.clientY - self.y) / mouse_scale; |
b28e606e |
354 | self.move_elements(); |
05485bfd |
355 | evt.returnValue = false; |
356 | evt.preventDefault(); |
357 | return false; |
b28e606e |
358 | } |
359 | |
360 | this.mouseup_listener = function(evt) { |
361 | if( $('ellipse[fill="#ffccff"]').size() > 0 ) { |
45ee3b96 |
362 | var source_node_id = $(self.ellipse).parent().attr('id'); |
05485bfd |
363 | var source_node_text = self.ellipse.siblings('text').text(); |
45ee3b96 |
364 | var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id'); |
05485bfd |
365 | var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text(); |
b28e606e |
366 | $('#source_node_id').val( source_node_id ); |
05485bfd |
367 | $('#source_node_text').val( source_node_text ); |
51ca18ee |
368 | $('.rel_rdg_a').text( "'" + source_node_text + "'" ); |
b28e606e |
369 | $('#target_node_id').val( target_node_id ); |
05485bfd |
370 | $('#target_node_text').val( target_node_text ); |
51ca18ee |
371 | $('.rel_rdg_b').text( "'" + target_node_text + "'" ); |
b28e606e |
372 | $('#dialog-form').dialog( 'open' ); |
373 | }; |
374 | $('body').unbind('mousemove'); |
375 | $('body').unbind('mouseup'); |
e538eccb |
376 | self.ellipse.attr( 'fill', '#9999ff' ); |
9529f69c |
377 | self.reset_elements(); |
b28e606e |
378 | } |
f2fb96fc |
379 | |
b28e606e |
380 | this.cpos = function() { |
381 | return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') }; |
382 | } |
383 | |
384 | this.get_g = function() { |
385 | return self.ellipse.parent('g'); |
386 | } |
387 | |
388 | this.enter_node = function(evt) { |
389 | self.ellipse.attr( 'fill', '#ffccff' ); |
390 | } |
391 | |
392 | this.leave_node = function(evt) { |
393 | self.ellipse.attr( 'fill', '#fff' ); |
394 | } |
395 | |
396 | this.greyout_edges = function() { |
397 | $.each( self.node_elements, function(index, value) { |
398 | value.grey_out('.edge'); |
399 | }); |
400 | } |
401 | |
402 | this.ungreyout_edges = function() { |
403 | $.each( self.node_elements, function(index, value) { |
404 | value.un_grey_out('.edge'); |
405 | }); |
406 | } |
407 | |
6c8d1880 |
408 | this.reposition = function( dx, dy ) { |
409 | $.each( self.node_elements, function(index, value) { |
410 | value.reposition( dx, dy ); |
411 | } ); |
412 | } |
413 | |
b28e606e |
414 | this.move_elements = function() { |
415 | $.each( self.node_elements, function(index, value) { |
6c8d1880 |
416 | value.move( self.dx, self.dy ); |
22a70299 |
417 | } ); |
b28e606e |
418 | } |
419 | |
420 | this.reset_elements = function() { |
421 | $.each( self.node_elements, function(index, value) { |
6c8d1880 |
422 | value.reset(); |
22a70299 |
423 | } ); |
b28e606e |
424 | } |
425 | |
426 | this.update_elements = function() { |
427 | self.node_elements = node_elements_for(self.ellipse); |
428 | } |
429 | |
a84ca4de |
430 | this.get_witnesses = function() { |
431 | return readingdata[self.get_id()].witnesses |
432 | } |
6c8d1880 |
433 | |
e538eccb |
434 | self.set_selectable( true ); |
b28e606e |
435 | } |
436 | |
437 | function svgshape( shape_element ) { |
438 | this.shape = shape_element; |
6c8d1880 |
439 | this.reposx = 0; |
440 | this.reposy = 0; |
441 | this.repositioned = this.shape.parent().data( 'repositioned' ); |
442 | if( this.repositioned != null ) { |
443 | this.reposx = this.repositioned[0]; |
444 | this.reposy = this.repositioned[1]; |
445 | } |
446 | this.reposition = function (dx, dy) { |
447 | this.move( dx, dy ); |
448 | this.reposx = this.reposx + dx; |
449 | this.reposy = this.reposy + dy; |
450 | this.shape.parent().data( 'repositioned', [this.reposx,this.reposy] ); |
451 | } |
b28e606e |
452 | this.move = function(dx,dy) { |
6c8d1880 |
453 | this.shape.attr( "transform", "translate( " + (this.reposx + dx) + " " + (this.reposy + dy) + " )" ); |
b28e606e |
454 | } |
455 | this.reset = function() { |
6c8d1880 |
456 | this.shape.attr( "transform", "translate( " + this.reposx + " " + this.reposy + " )" ); |
b28e606e |
457 | } |
458 | this.grey_out = function(filter) { |
459 | if( this.shape.parent(filter).size() != 0 ) { |
460 | this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'}); |
461 | } |
462 | } |
463 | this.un_grey_out = function(filter) { |
464 | if( this.shape.parent(filter).size() != 0 ) { |
465 | this.shape.attr({'stroke':'#000000', 'fill':'#000000'}); |
466 | } |
467 | } |
468 | } |
469 | |
470 | function svgpath( path_element, svg_element ) { |
471 | this.svg_element = svg_element; |
472 | this.path = path_element; |
473 | this.x = this.path.x; |
474 | this.y = this.path.y; |
6c8d1880 |
475 | |
476 | this.reposition = function (dx, dy) { |
477 | this.x = this.x + dx; |
478 | this.y = this.y + dy; |
479 | this.path.x = this.x; |
480 | this.path.y = this.y; |
481 | } |
482 | |
b28e606e |
483 | this.move = function(dx,dy) { |
484 | this.path.x = this.x + dx; |
485 | this.path.y = this.y + dy; |
486 | } |
6c8d1880 |
487 | |
b28e606e |
488 | this.reset = function() { |
489 | this.path.x = this.x; |
490 | this.path.y = this.y; |
491 | } |
6c8d1880 |
492 | |
b28e606e |
493 | this.grey_out = function(filter) { |
494 | if( this.svg_element.parent(filter).size() != 0 ) { |
495 | this.svg_element.attr('stroke', '#e5e5e5'); |
496 | this.svg_element.siblings('text').attr('fill', '#e5e5e5'); |
05485bfd |
497 | this.svg_element.siblings('text').attr('class', 'noselect'); |
b28e606e |
498 | } |
499 | } |
500 | this.un_grey_out = function(filter) { |
501 | if( this.svg_element.parent(filter).size() != 0 ) { |
502 | this.svg_element.attr('stroke', '#000000'); |
503 | this.svg_element.siblings('text').attr('fill', '#000000'); |
05485bfd |
504 | this.svg_element.siblings('text').attr('class', ''); |
b28e606e |
505 | } |
506 | } |
507 | } |
508 | |
509 | function node_elements_for( ellipse ) { |
510 | node_elements = get_edge_elements_for( ellipse ); |
511 | node_elements.push( new svgshape( ellipse.siblings('text') ) ); |
512 | node_elements.push( new svgshape( ellipse ) ); |
513 | return node_elements; |
514 | } |
515 | |
516 | function get_edge_elements_for( ellipse ) { |
517 | edge_elements = new Array(); |
45ee3b96 |
518 | node_id = ellipse.parent().attr('id'); |
b28e606e |
519 | edge_in_pattern = new RegExp( node_id + '$' ); |
22a70299 |
520 | edge_out_pattern = new RegExp( '^' + node_id + '-' ); |
b28e606e |
521 | $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) { |
522 | title = $(this).text(); |
523 | if( edge_in_pattern.test(title) ) { |
524 | polygon = $(this).siblings('polygon'); |
525 | if( polygon.size() > 0 ) { |
526 | edge_elements.push( new svgshape( polygon ) ); |
527 | } |
528 | path_segments = $(this).siblings('path')[0].pathSegList; |
529 | edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) ); |
530 | } |
531 | if( edge_out_pattern.test(title) ) { |
532 | path_segments = $(this).siblings('path')[0].pathSegList; |
533 | edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) ); |
534 | } |
535 | }); |
536 | return edge_elements; |
537 | } |
538 | |
539 | function relation_factory() { |
540 | var self = this; |
541 | this.color_memo = null; |
542 | //TODO: colors hard coded for now |
543 | this.temp_color = '#FFA14F'; |
544 | this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ]; |
545 | |
546 | this.create_temporary = function( source_node_id, target_node_id ) { |
45ee3b96 |
547 | var relation_id = get_relation_id( source_node_id, target_node_id ); |
548 | var relation = $( jq( relation_id ) ); |
9529f69c |
549 | if( relation.size() == 0 ) { |
b28e606e |
550 | draw_relation( source_node_id, target_node_id, self.temp_color ); |
551 | } else { |
552 | self.color_memo = relation.children('path').attr( 'stroke' ); |
553 | relation.children('path').attr( 'stroke', self.temp_color ); |
554 | } |
555 | } |
556 | this.remove_temporary = function() { |
557 | var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]'); |
558 | if( self.color_memo != null ) { |
559 | path_element.attr( 'stroke', self.color_memo ); |
560 | self.color_memo = null; |
561 | } else { |
9529f69c |
562 | var temporary = path_element.parent('g').remove(); |
563 | temporary.empty(); |
564 | temporary = null; |
b28e606e |
565 | } |
566 | } |
48a371e5 |
567 | this.create = function( source_node_id, target_node_id, color_index, emphasis ) { |
b28e606e |
568 | //TODO: Protect from (color_)index out of bound.. |
569 | var relation_color = self.relation_colors[ color_index ]; |
48a371e5 |
570 | var relation = draw_relation( source_node_id, target_node_id, relation_color, emphasis ); |
9529f69c |
571 | get_node_obj( source_node_id ).update_elements(); |
572 | get_node_obj( target_node_id ).update_elements(); |
573 | return relation; |
b28e606e |
574 | } |
9529f69c |
575 | this.toggle_active = function( relation_id ) { |
45ee3b96 |
576 | var relation = $( jq( relation_id ) ); |
9529f69c |
577 | var relation_path = relation.children('path'); |
578 | if( !relation.data( 'active' ) ) { |
579 | relation_path.css( {'cursor':'pointer'} ); |
580 | relation_path.mouseenter( function(event) { |
581 | outerTimer = setTimeout( function() { |
582 | timer = setTimeout( function() { |
45ee3b96 |
583 | var related_nodes = get_related_nodes( relation_id ); |
584 | var source_node_id = related_nodes[0]; |
585 | var target_node_id = related_nodes[1]; |
9529f69c |
586 | $('#delete_source_node_id').val( source_node_id ); |
587 | $('#delete_target_node_id').val( target_node_id ); |
588 | self.showinfo(relation); |
589 | }, 500 ) |
590 | }, 1000 ); |
591 | }); |
592 | relation_path.mouseleave( function(event) { |
593 | clearTimeout(outerTimer); |
594 | if( timer != null ) { clearTimeout(timer); } |
595 | }); |
596 | relation.data( 'active', true ); |
597 | } else { |
598 | relation_path.unbind( 'mouseenter' ); |
599 | relation_path.unbind( 'mouseleave' ); |
600 | relation_path.css( {'cursor':'inherit'} ); |
601 | relation.data( 'active', false ); |
602 | } |
603 | } |
604 | this.showinfo = function(relation) { |
088a14af |
605 | $('#delete_relation_type').text( relation.data('type') ); |
606 | $('#delete_relation_scope').text( relation.data('scope') ); |
51ca18ee |
607 | $('#delete_relation_attributes').empty(); |
e4bdf660 |
608 | var significance = ' is not '; |
609 | if( relation.data( 'is_significant' ) === 'yes') { |
610 | significance = ' is '; |
611 | } else if ( relation.data( 'is_significant' ) === 'maybe' ) { |
612 | significance = ' might be '; |
613 | } |
614 | $('#delete_relation_attributes').append( |
615 | "This relationship" + significance + "stemmatically significant<br/>"); |
51ca18ee |
616 | if( relation.data( 'a_derivable_from_b' ) ) { |
617 | $('#delete_relation_attributes').append( |
618 | "'" + relation.data('source_text') + "' derivable from '" + relation.data('target_text') + "'<br/>"); |
619 | } |
620 | if( relation.data( 'b_derivable_from_a' ) ) { |
621 | $('#delete_relation_attributes').append( |
622 | "'" + relation.data('target_text') + "' derivable from '" + relation.data('source_text') + "'<br/>"); |
623 | } |
624 | if( relation.data( 'non_independent' ) ) { |
625 | $('#delete_relation_attributes').append( |
626 | "Variance unlikely to arise coincidentally<br/>"); |
627 | } |
69a19c91 |
628 | if( relation.data( 'note' ) ) { |
088a14af |
629 | $('#delete_relation_note').text('note: ' + relation.data( 'note' ) ); |
69a19c91 |
630 | } |
9529f69c |
631 | var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' '); |
632 | var xs = parseFloat( points[0].split(',')[0] ); |
633 | var xe = parseFloat( points[1].split(',')[0] ); |
634 | var ys = parseFloat( points[0].split(',')[1] ); |
635 | var ye = parseFloat( points[3].split(',')[1] ); |
636 | var p = svg_root.createSVGPoint(); |
637 | p.x = xs + ((xe-xs)*1.1); |
638 | p.y = ye - ((ye-ys)/2); |
76f05423 |
639 | var ctm = svg_root_element.getScreenCTM(); |
9529f69c |
640 | var nx = p.matrixTransform(ctm).x; |
641 | var ny = p.matrixTransform(ctm).y; |
642 | var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']"); |
643 | $('#delete-form').dialog( 'open' ); |
644 | dialog_aria.offset({ left: nx, top: ny }); |
645 | } |
646 | this.remove = function( relation_id ) { |
30d0ba1e |
647 | if( !editable ) { |
648 | return; |
649 | } |
45ee3b96 |
650 | var relation = $( jq( relation_id ) ); |
9529f69c |
651 | relation.remove(); |
b28e606e |
652 | } |
653 | } |
654 | |
45ee3b96 |
655 | // Utility function to create/return the ID of a relation link between |
656 | // a source and target. |
657 | function get_relation_id( source_id, target_id ) { |
658 | var idlist = [ source_id, target_id ]; |
659 | idlist.sort(); |
660 | return 'relation-' + idlist[0] + '-...-' + idlist[1]; |
661 | } |
662 | |
663 | function get_related_nodes( relation_id ) { |
664 | var srctotarg = relation_id.substr( 9 ); |
665 | return srctotarg.split('-...-'); |
666 | } |
667 | |
48a371e5 |
668 | function draw_relation( source_id, target_id, relation_color, emphasis ) { |
9529f69c |
669 | var source_ellipse = get_ellipse( source_id ); |
670 | var target_ellipse = get_ellipse( target_id ); |
45ee3b96 |
671 | var relation_id = get_relation_id( source_id, target_id ); |
9529f69c |
672 | var svg = $('#svgenlargement').children('svg').svg().svg('get'); |
673 | var path = svg.createPath(); |
674 | var sx = parseInt( source_ellipse.attr('cx') ); |
675 | var rx = parseInt( source_ellipse.attr('rx') ); |
676 | var sy = parseInt( source_ellipse.attr('cy') ); |
677 | var ex = parseInt( target_ellipse.attr('cx') ); |
678 | var ey = parseInt( target_ellipse.attr('cy') ); |
9231663d |
679 | var relation = svg.group( $("#svgenlargement svg g"), { 'class':'relation', 'id':relation_id } ); |
9529f69c |
680 | svg.title( relation, source_id + '->' + target_id ); |
cfa6ff76 |
681 | var stroke_width = emphasis === "yes" ? 6 : emphasis === "maybe" ? 4 : 2; |
48a371e5 |
682 | svg.path( relation, path.move( sx, sy ).curveC( sx + (2*rx), sy, ex + (2*rx), ey, ex, ey ), {fill: 'none', stroke: relation_color, strokeWidth: stroke_width }); |
b28e606e |
683 | var relation_element = $('#svgenlargement .relation').filter( ':last' ); |
684 | relation_element.insertBefore( $('#svgenlargement g g').filter(':first') ); |
9529f69c |
685 | return relation_element; |
b28e606e |
686 | } |
687 | |
5fe117bc |
688 | function detach_node( readings ) { |
217f5e64 |
689 | // separate out the deleted relationships, discard for now |
b6f3476b |
690 | if( 'DELETED' in readings ) { |
691 | // Remove each of the deleted relationship links. |
692 | $.each( readings['DELETED'], function( idx, pair ) { |
693 | var relation_id = get_relation_id( pair[0], pair[1] ); |
694 | var relation = $( jq( relation_id ) ); |
695 | if( relation.size() == 0 ) { |
696 | relation_id = get_relation_id( pair[1], pair[0] ); |
697 | } |
698 | relation_manager.remove( relation_id ); |
699 | }); |
700 | delete readings['DELETED']; |
701 | } |
a471cb0f |
702 | // add new node(s) |
703 | $.extend( readingdata, readings ); |
704 | // remove from existing readings the witnesses for the new nodes/readings |
705 | $.each( readings, function( node_id, reading ) { |
706 | $.each( reading.witnesses, function( index, witness ) { |
707 | var witnesses = readingdata[ reading.orig_rdg ].witnesses; |
708 | readingdata[ reading.orig_rdg ].witnesses = $.removeFromArray( witness, witnesses ); |
709 | } ); |
710 | } ); |
711 | |
a471cb0f |
712 | detached_edges = []; |
8cd2e785 |
713 | |
714 | // here we detach witnesses from the existing edges accoring to what's being relayed by readings |
a471cb0f |
715 | $.each( readings, function( node_id, reading ) { |
716 | var edges = edges_of( get_ellipse( reading.orig_rdg ) ); |
717 | incoming_remaining = []; |
718 | outgoing_remaining = []; |
719 | $.each( reading.witnesses, function( index, witness ) { |
720 | incoming_remaining.push( witness ); |
721 | outgoing_remaining.push( witness ); |
722 | } ); |
723 | $.each( edges, function( index, edge ) { |
724 | detached_edge = edge.detach_witnesses( reading.witnesses ); |
725 | if( detached_edge != null ) { |
726 | detached_edges.push( detached_edge ); |
727 | $.each( detached_edge.witnesses, function( index, witness ) { |
728 | if( detached_edge.is_incoming == true ) { |
729 | incoming_remaining = $.removeFromArray( witness, incoming_remaining ); |
730 | } else { |
731 | outgoing_remaining = $.removeFromArray( witness, outgoing_remaining ); |
732 | } |
733 | } ); |
734 | } |
735 | } ); |
8cd2e785 |
736 | |
60c66cd2 |
737 | // After detaching we still need to check if for *all* readings |
8cd2e785 |
738 | // an edge was detached. It may be that a witness was not |
739 | // explicitly named on an edge but was part of a 'majority' edge |
740 | // in which case we need to duplicate and name that edge after those |
741 | // remaining witnesses. |
a471cb0f |
742 | if( outgoing_remaining.length > 0 ) { |
a471cb0f |
743 | $.each( edges, function( index, edge ) { |
a471cb0f |
744 | if( edge.get_label() == 'majority' && !edge.is_incoming ) { |
745 | detached_edges.push( edge.clone_for( outgoing_remaining ) ); |
746 | } |
747 | } ); |
748 | } |
749 | if( incoming_remaining.length > 0 ) { |
a471cb0f |
750 | $.each( edges, function( index, edge ) { |
751 | if( edge.get_label() == 'majority' && edge.is_incoming ) { |
dc8a13bd |
752 | detached_edges.push( edge.clone_for( incoming_remaining ) ); |
a471cb0f |
753 | } |
754 | } ); |
755 | } |
60c66cd2 |
756 | |
22a70299 |
757 | // Finally multiple selected nodes may share edges |
758 | var copy_array = []; |
759 | $.each( detached_edges, function( index, edge ) { |
760 | var do_copy = true; |
761 | $.each( copy_array, function( index, copy_edge ) { |
762 | if( copy_edge.g_elem.attr( 'id' ) == edge.g_elem.attr( 'id' ) ) { do_copy = false } |
763 | } ); |
764 | if( do_copy == true ) { |
765 | copy_array.push( edge ); |
766 | } |
767 | } ); |
768 | detached_edges = copy_array; |
769 | |
60c66cd2 |
770 | // Lots of unabstracted knowledge down here :/ |
771 | // Clone original node/reading, rename/id it.. |
772 | duplicate_node = get_ellipse( reading.orig_rdg ).parent().clone(); |
773 | duplicate_node.attr( 'id', node_id ); |
774 | duplicate_node.children( 'title' ).text( node_id ); |
6c8d1880 |
775 | |
776 | // This needs somehow to move to node or even to shapes! #repositioned |
777 | duplicate_node_data = get_ellipse( reading.orig_rdg ).parent().data( 'repositioned' ); |
22a70299 |
778 | if( duplicate_node_data != null ) { |
6c8d1880 |
779 | duplicate_node.children( 'ellipse' ).parent().data( 'repositioned', duplicate_node_data ); |
22a70299 |
780 | } |
60c66cd2 |
781 | |
782 | // Add the node and all new edges into the graph |
931ed236 |
783 | var graph_root = $('#svgenlargement svg g.graph'); |
60c66cd2 |
784 | graph_root.append( duplicate_node ); |
785 | $.each( detached_edges, function( index, edge ) { |
dc8a13bd |
786 | id_suffix = node_id.slice( node_id.indexOf( '_' ) ); |
787 | edge.g_elem.attr( 'id', ( edge.g_elem.attr( 'id' ) + id_suffix ) ); |
60c66cd2 |
788 | edge_title = edge.g_elem.children( 'title' ).text(); |
22a70299 |
789 | edge_weight = 0.8 + ( 0.2 * edge.witnesses.length ); |
60c66cd2 |
790 | edge_title = edge_title.replace( reading.orig_rdg, node_id ); |
791 | edge.g_elem.children( 'title' ).text( edge_title ); |
22a70299 |
792 | edge.g_elem.children( 'path').attr( 'stroke-width', edge_weight ); |
60c66cd2 |
793 | // Reg unabstracted knowledge: isn't it more elegant to make |
794 | // it edge.append_to( graph_root )? |
795 | graph_root.append( edge.g_elem ); |
796 | } ); |
22a70299 |
797 | |
798 | // Make the detached node a real node_obj |
799 | var ellipse_elem = get_ellipse( node_id ); |
800 | var new_node = new node_obj( ellipse_elem ); |
801 | ellipse_elem.data( 'node_obj', new_node ); |
802 | |
60c66cd2 |
803 | // Move the node somewhat up for 'dramatic effect' :-p |
9231663d |
804 | new_node.reposition( 0, -70 ); |
60c66cd2 |
805 | |
a471cb0f |
806 | } ); |
60c66cd2 |
807 | |
9231663d |
808 | } |
a471cb0f |
809 | |
9231663d |
810 | function merge_nodes( source_node_id, target_node_id, consequences ) { |
811 | if( consequences.status != null && consequences.status == 'ok' ) { |
812 | merge_node( source_node_id, target_node_id ); |
813 | if( consequences.checkalign != null ) { |
814 | $.each( consequences.checkalign, function( index, node_ids ) { |
0c4354f6 |
815 | var temp_relation = draw_relation( node_ids[0], node_ids[1], "#89a02c" ); |
9231663d |
816 | var sy = parseInt( temp_relation.children('path').attr('d').split('C')[0].split(',')[1] ); |
817 | var ey = parseInt( temp_relation.children('path').attr('d').split(' ')[2].split(',')[1] ); |
818 | var yC = ey + (( sy - ey )/2); |
819 | // TODO: compute xC to be always the same distance to the amplitude of the curve |
820 | var xC = parseInt( temp_relation.children('path').attr('d').split(' ')[1].split(',')[0] ); |
821 | var svg = $('#svgenlargement').children('svg').svg('get'); |
822 | parent_g = svg.group( $('#svgenlargement svg g') ); |
823 | var ids_text = node_ids[0] + '-' + node_ids[1]; |
824 | var merge_id = 'merge-' + ids_text; |
5a80f535 |
825 | var yes = svg.image( parent_g, xC, (yC-8), 16, 16, merge_button_yes, { id: merge_id } ); |
826 | var no = svg.image( parent_g, (xC+20), (yC-8), 16, 16, merge_button_no, { id: 'no' + merge_id } ); |
827 | $(yes).hover( function(){ $(this).addClass( 'draggable' ) }, function(){ $(this).removeClass( 'draggable' ) } ); |
828 | $(no).hover( function(){ $(this).addClass( 'draggable' ) }, function(){ $(this).removeClass( 'draggable' ) } ); |
829 | $(yes).click( function( evt ){ |
9231663d |
830 | merge_node( node_ids[0], node_ids[1] ); |
831 | temp_relation.remove(); |
5a80f535 |
832 | $(evt.target).parent().remove(); |
759f1868 |
833 | //notify backend |
834 | var ncpath = getTextURL( 'merge' ); |
aaf27b2e |
835 | var form_values = "source_id=" + node_ids[0] + "&target_id=" + node_ids[1] + "&single=true"; |
759f1868 |
836 | $.post( ncpath, form_values ); |
9231663d |
837 | } ); |
5a80f535 |
838 | $(no).click( function( evt ) { |
9231663d |
839 | temp_relation.remove(); |
5a80f535 |
840 | $(evt.target).parent().remove(); |
9231663d |
841 | } ); |
842 | } ); |
843 | } |
844 | } |
845 | } |
846 | |
847 | function merge_node( source_node_id, target_node_id ) { |
848 | $.each( edges_of( get_ellipse( source_node_id ) ), function( index, edge ) { |
849 | if( edge.is_incoming == true ) { |
850 | edge.attach_endpoint( target_node_id ); |
851 | } else { |
852 | edge.attach_startpoint( target_node_id ); |
853 | } |
854 | } ); |
855 | $( jq( source_node_id ) ).remove(); |
a471cb0f |
856 | } |
857 | |
a51e34c5 |
858 | function compress_nodes(readings) { |
859 | //add text of other readings to 1st reading |
a51e34c5 |
860 | |
2d34519e |
861 | var first = get_ellipse(readings[0]); |
862 | var first_title = first.parent().find('text')[0]; |
863 | |
864 | for (var i = 1; i < readings.length; i++) { |
865 | var cur = get_ellipse(readings[i]); |
a51e34c5 |
866 | var cur_title = cur.parent().find('text')[0]; |
867 | |
868 | first_title.textContent += " " + cur_title.textContent; |
869 | }; |
870 | |
871 | //delete all others |
872 | for (var i = 1; i < readings.length; i++) { |
873 | var node = get_ellipse(readings[i]); |
874 | var rid = readings[i-1] + '->' + readings[i]; |
875 | |
876 | //[].slice.call(s.getElementsByTagName('title')).find(function(elem){return elem.textContent=='r64.2->r66.2'}).parentNode.remove() |
877 | |
2a65f5c9 |
878 | var titles = svg_root.getElementsByTagName('title'); |
879 | var titlesArray = [].slice.call(titles); |
a51e34c5 |
880 | |
2a65f5c9 |
881 | if (titlesArray.length > 0) { |
882 | var title = titlesArray.find(function(elem){ |
883 | return elem.textContent === rid; |
884 | }); |
885 | |
886 | if (title && title.parentNode) { |
887 | title.parentNode.remove(); |
888 | } |
889 | } |
a51e34c5 |
890 | |
2a65f5c9 |
891 | var x = parseInt(first[0].getAttribute('cx'), 10); |
892 | |
893 | first[0].setAttribute('rx', 4.5 * first_title.textContent.length); |
894 | |
895 | if (text_direction !== "BI") { |
896 | first[0].setAttribute('cx', x + first_title.textContent.length + 20); |
897 | first_title.setAttribute('x', first[0].getAttribute('cx')); |
898 | } |
a51e34c5 |
899 | |
2a65f5c9 |
900 | merge_node(readings[i], readings[0]); |
901 | //node.parent().remove(); |
a51e34c5 |
902 | } |
903 | } |
904 | |
c1add777 |
905 | function Marquee() { |
906 | |
907 | var self = this; |
908 | |
f6516f22 |
909 | this.x = 0; |
910 | this.y = 0; |
911 | this.dx = 0; |
912 | this.dy = 0; |
c1add777 |
913 | this.enlargementOffset = $('#svgenlargement').offset(); |
914 | this.svg_rect = $('#svgenlargement svg').svg('get'); |
915 | |
916 | this.show = function( event ) { |
f6516f22 |
917 | self.x = event.clientX; |
918 | self.y = event.clientY; |
c1add777 |
919 | p = svg_root.createSVGPoint(); |
920 | p.x = event.clientX - self.enlargementOffset.left; |
921 | p.y = event.clientY - self.enlargementOffset.top; |
c1add777 |
922 | self.svg_rect.rect( p.x, p.y, 0, 0, { fill: 'black', 'fill-opacity': '0.1', stroke: 'black', 'stroke-dasharray': '4,2', strokeWidth: '0.02em', id: 'marquee' } ); |
923 | }; |
924 | |
925 | this.expand = function( event ) { |
f6516f22 |
926 | self.dx = (event.clientX - self.x); |
927 | self.dy = (event.clientY - self.y); |
c1add777 |
928 | var rect = $('#marquee'); |
f6516f22 |
929 | if( rect.length != 0 ) { |
930 | var rect_w = Math.abs( self.dx ); |
931 | var rect_h = Math.abs( self.dy ); |
932 | var rect_x = self.x - self.enlargementOffset.left; |
933 | var rect_y = self.y - self.enlargementOffset.top; |
934 | if( self.dx < 0 ) { rect_x = rect_x - rect_w } |
935 | if( self.dy < 0 ) { rect_y = rect_y - rect_h } |
936 | rect.attr("x", rect_x).attr("y", rect_y).attr("width", rect_w).attr("height", rect_h); |
c1add777 |
937 | } |
938 | }; |
939 | |
a84ca4de |
940 | this.select = function() { |
c1add777 |
941 | var rect = $('#marquee'); |
942 | if( rect.length != 0 ) { |
e538eccb |
943 | //unselect any possible selected first |
22a70299 |
944 | //TODO: unless SHIFT? |
e538eccb |
945 | if( $('ellipse[fill="#9999ff"]').size() > 0 ) { |
946 | $('ellipse[fill="#9999ff"]').each( function() { |
947 | $(this).data( 'node_obj' ).set_draggable( false ); |
948 | } ); |
949 | } |
950 | //compute dimension of marquee |
c1add777 |
951 | var left = $('#marquee').offset().left; |
952 | var top = $('#marquee').offset().top; |
953 | var right = left + parseInt( $('#marquee').attr( 'width' ) ); |
954 | var bottom = top + parseInt( $('#marquee').attr( 'height' ) ); |
955 | var tf = svg_root_element.getScreenCTM().inverse(); |
956 | var p = svg_root.createSVGPoint(); |
957 | p.x=left; |
958 | p.y=top; |
959 | var cx_min = p.matrixTransform(tf).x; |
960 | var cy_min = p.matrixTransform(tf).y; |
961 | p.x=right; |
962 | p.y=bottom; |
963 | var cx_max = p.matrixTransform(tf).x; |
964 | var cy_max = p.matrixTransform(tf).y; |
e538eccb |
965 | //select any node with its center inside the marquee |
5fe117bc |
966 | var readings = []; |
e538eccb |
967 | //also merge witness sets from nodes |
a84ca4de |
968 | var witnesses = []; |
c1add777 |
969 | $('#svgenlargement ellipse').each( function( index ) { |
970 | var cx = parseInt( $(this).attr('cx') ); |
971 | var cy = parseInt( $(this).attr('cy') ); |
6c8d1880 |
972 | |
973 | // This needs somehow to move to node or even to shapes! #repositioned |
974 | // We should ask something more aling the lines of: nodes.each { |item| node.selected? } |
975 | var org_translate = $(this).parent().data( 'repositioned' ); |
22a70299 |
976 | if( org_translate != null ) { |
977 | cx = cx + org_translate[0]; |
978 | cy = cy + org_translate[1]; |
979 | } |
6c8d1880 |
980 | |
c1add777 |
981 | if( cx > cx_min && cx < cx_max) { |
982 | if( cy > cy_min && cy < cy_max) { |
983 | // we actually heve no real 'selected' state for nodes, except coloring |
e538eccb |
984 | $(this).attr( 'fill', '#9999ff' ); |
5fe117bc |
985 | // Take note of the selected reading(s) and applicable witness(es) |
986 | // so we can populate the multipleselect-form |
987 | readings.push( $(this).parent().attr('id') ); |
a84ca4de |
988 | var this_witnesses = $(this).data( 'node_obj' ).get_witnesses(); |
989 | witnesses = arrayUnique( witnesses.concat( this_witnesses ) ); |
c1add777 |
990 | } |
991 | } |
992 | }); |
e538eccb |
993 | if( $('ellipse[fill="#9999ff"]').size() > 0 ) { |
5fe117bc |
994 | //add intersection of witnesses sets to the multi select form and open it |
995 | $('#detach_collated_form').empty(); |
a51e34c5 |
996 | |
5fe117bc |
997 | $.each( readings, function( index, value ) { |
998 | $('#detach_collated_form').append( $('<input>').attr( |
999 | "type", "hidden").attr("name", "readings[]").attr( |
1000 | "value", value ) ); |
a51e34c5 |
1001 | }); |
1002 | $.each( witnesses, function( index, value ) { |
5fe117bc |
1003 | $('#detach_collated_form').append( |
1004 | '<input type="checkbox" name="witnesses[]" value="' + value |
1005 | + '">' + value + '<br>' ); |
a84ca4de |
1006 | }); |
5fe117bc |
1007 | $('#multiple_selected_readings').attr('value', readings.join(',') ); |
a51e34c5 |
1008 | |
1009 | if ($('#action-merge')[0].checked) { |
1010 | $('#detach_collated_form').hide(); |
1011 | $('#multipleselect-form-text').hide(); |
1012 | |
1013 | $('#detach_btn').hide(); |
1014 | $('#merge_btn').show(); |
1015 | } else { |
1016 | $('#detach_collated_form').show(); |
1017 | $('#multipleselect-form-text').show(); |
1018 | |
1019 | $('#detach_btn').show(); |
1020 | $('#merge_btn').hide(); |
1021 | } |
1022 | |
1023 | $('#action-detach').change(function() { |
1024 | if ($('#action-detach')[0].checked) { |
1025 | $('#detach_collated_form').show(); |
2d34519e |
1026 | $('#multipleselect-form-text').show(); |
a51e34c5 |
1027 | |
1028 | $('#detach_btn').show(); |
1029 | $('#merge_btn').hide(); |
1030 | } |
1031 | }); |
1032 | |
1033 | $('#action-merge').change(function() { |
1034 | if ($('#action-merge')[0].checked) { |
1035 | $('#detach_collated_form').hide(); |
2d34519e |
1036 | $('#multipleselect-form-text').hide(); |
a51e34c5 |
1037 | |
1038 | $('#detach_btn').hide(); |
1039 | $('#merge_btn').show(); |
1040 | } |
1041 | }); |
1042 | |
a84ca4de |
1043 | $('#multipleselect-form').dialog( 'open' ); |
1044 | } |
c1add777 |
1045 | self.svg_rect.remove( $('#marquee') ); |
1046 | } |
1047 | }; |
1048 | |
f6516f22 |
1049 | this.unselect = function() { |
e538eccb |
1050 | $('ellipse[fill="#9999ff"]').attr( 'fill', '#fff' ); |
f6516f22 |
1051 | } |
1052 | |
c1add777 |
1053 | } |
1054 | |
b001c73d |
1055 | function readings_equivalent( source, target ) { |
1056 | var sourcetext = readingdata[source].text; |
1057 | var targettext = readingdata[target].text; |
1058 | if( sourcetext === targettext ) { |
1059 | return true; |
1060 | } |
1061 | // Lowercase and strip punctuation from both and compare again |
f069bb5c |
1062 | var nonwc = XRegExp('[^\\p{L}\\s]|_'); |
1063 | var stlc = XRegExp.replace( sourcetext.toLocaleLowerCase(), nonwc, "", 'all' ); |
1064 | var ttlc = XRegExp.replace( targettext.toLocaleLowerCase(), nonwc, "", 'all' ); |
b001c73d |
1065 | if( stlc === ttlc ) { |
1066 | return true; |
1067 | } |
1068 | return false; |
1069 | } |
1070 | |
89aae3ee |
1071 | function scrollToEnd() { |
1072 | var stateTf = svg_root_element.getCTM().inverse(); |
1073 | |
1074 | var vbdim = svg_root.viewBox.baseVal; |
1075 | var width = Math.floor(svg_root_element.getBoundingClientRect().width) - vbdim.width; |
1076 | |
1077 | var p = svg_root.createSVGPoint(); |
1078 | p.x = width; |
1079 | p.y = 0; |
1080 | p = p.matrixTransform(stateTf); |
1081 | |
1082 | var matrix = stateTf.inverse().translate(-p.x, -100); |
1083 | var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")"; |
1084 | svg_root_element.setAttribute("transform", s); |
1085 | } |
9529f69c |
1086 | |
b28e606e |
1087 | $(document).ready(function () { |
9529f69c |
1088 | |
1089 | timer = null; |
b28e606e |
1090 | relation_manager = new relation_factory(); |
1091 | |
c1add777 |
1092 | $('#update_workspace_button').data('locked', false); |
b001c73d |
1093 | |
1094 | // Set up the mouse events on the SVG enlargement |
9529f69c |
1095 | $('#enlargement').mousedown(function (event) { |
b28e606e |
1096 | $(this) |
9529f69c |
1097 | .data('down', true) |
1098 | .data('x', event.clientX) |
1099 | .data('y', event.clientY) |
1100 | .data('scrollLeft', this.scrollLeft) |
c1add777 |
1101 | stateTf = svg_root_element.getCTM().inverse(); |
1102 | var p = svg_root.createSVGPoint(); |
1103 | p.x = event.clientX; |
1104 | p.y = event.clientY; |
1105 | stateOrigin = p.matrixTransform(stateTf); |
1106 | |
1107 | // Activate marquee if in interaction mode |
1108 | if( $('#update_workspace_button').data('locked') == true ) { marquee.show( event ) }; |
1109 | |
1110 | event.returnValue = false; |
1111 | event.preventDefault(); |
1112 | return false; |
b28e606e |
1113 | }).mouseup(function (event) { |
a84ca4de |
1114 | marquee.select(); |
c1add777 |
1115 | $(this).data('down', false); |
b28e606e |
1116 | }).mousemove(function (event) { |
9529f69c |
1117 | if( timer != null ) { clearTimeout(timer); } |
1118 | if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) { |
1119 | var p = svg_root.createSVGPoint(); |
1120 | p.x = event.clientX; |
1121 | p.y = event.clientY; |
1122 | p = p.matrixTransform(stateTf); |
1123 | var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y); |
1124 | var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")"; |
76f05423 |
1125 | svg_root_element.setAttribute("transform", s); |
b28e606e |
1126 | } |
c1add777 |
1127 | marquee.expand( event ); |
76f05423 |
1128 | event.returnValue = false; |
1129 | event.preventDefault(); |
b28e606e |
1130 | }).mousewheel(function (event, delta) { |
9529f69c |
1131 | event.returnValue = false; |
1132 | event.preventDefault(); |
1133 | if ( $('#update_workspace_button').data('locked') == false ) { |
76f05423 |
1134 | if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta; |
1135 | if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail; |
9529f69c |
1136 | if( delta < -9 ) { delta = -9 }; |
1137 | var z = 1 + delta/10; |
76f05423 |
1138 | z = delta > 0 ? 1 : -1; |
1139 | var g = svg_root_element; |
1140 | if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) { |
9529f69c |
1141 | var root = svg_root; |
1142 | var p = root.createSVGPoint(); |
76f05423 |
1143 | p.x = event.originalEvent.clientX; |
1144 | p.y = event.originalEvent.clientY; |
9529f69c |
1145 | p = p.matrixTransform(g.getCTM().inverse()); |
76f05423 |
1146 | var scaleLevel = 1+(z/20); |
1147 | var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y); |
9529f69c |
1148 | var matrix = g.getCTM().multiply(k); |
1149 | var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")"; |
1150 | g.setAttribute("transform", s); |
1151 | } |
1152 | } |
b28e606e |
1153 | }).css({ |
1154 | 'overflow' : 'hidden', |
1155 | 'cursor' : '-moz-grab' |
1156 | }); |
1157 | |
c1add777 |
1158 | |
b001c73d |
1159 | // Set up the relationship creation dialog. This also functions as the reading |
1160 | // merge dialog where appropriate. |
b001c73d |
1161 | |
30d0ba1e |
1162 | if( editable ) { |
9231663d |
1163 | $( '#dialog-form' ).dialog( { |
30d0ba1e |
1164 | autoOpen: false, |
e4bdf660 |
1165 | height: 350, |
cfa6ff76 |
1166 | width: 340, |
30d0ba1e |
1167 | modal: true, |
1168 | buttons: { |
9231663d |
1169 | 'Merge readings': function( evt ) { |
932277e1 |
1170 | var mybuttons = $(evt.target).closest('button').parent().find('button'); |
1171 | mybuttons.button( 'disable' ); |
9231663d |
1172 | $( '#status' ).empty(); |
1173 | form_values = $( '#collapse_node_form' ).serialize(); |
b001c73d |
1174 | ncpath = getTextURL( 'merge' ); |
9231663d |
1175 | var jqjson = $.post( ncpath, form_values, function( data ) { |
1176 | merge_nodes( $( '#source_node_id' ).val(), $( '#target_node_id' ).val(), data ); |
932277e1 |
1177 | mybuttons.button( 'enable' ); |
9231663d |
1178 | $( '#dialog-form' ).dialog( 'close' ); |
1179 | } ); |
b001c73d |
1180 | }, |
1181 | OK: function( evt ) { |
932277e1 |
1182 | var mybuttons = $(evt.target).closest('button').parent().find('button'); |
1183 | mybuttons.button( 'disable' ); |
9231663d |
1184 | $( '#status' ).empty(); |
1185 | form_values = $( '#collapse_node_form' ).serialize(); |
30d0ba1e |
1186 | ncpath = getTextURL( 'relationships' ); |
9231663d |
1187 | var jqjson = $.post( ncpath, form_values, function( data ) { |
1188 | $.each( data, function( item, source_target ) { |
30d0ba1e |
1189 | var source_found = get_ellipse( source_target[0] ); |
1190 | var target_found = get_ellipse( source_target[1] ); |
9231663d |
1191 | var relation_found = $.inArray( source_target[2], $( '#keymap' ).data( 'relations' ) ); |
30d0ba1e |
1192 | if( source_found.size() && target_found.size() && relation_found > -1 ) { |
cfa6ff76 |
1193 | var emphasis = $('#is_significant option:selected').attr('value'); |
48a371e5 |
1194 | var relation = relation_manager.create( source_target[0], source_target[1], relation_found, emphasis ); |
45ee3b96 |
1195 | relation_manager.toggle_active( relation.attr('id') ); |
51ca18ee |
1196 | $.each( $('#collapse_node_form').serializeArray(), function( i, k ) { |
1197 | relation.data( k.name, k.value ); |
1198 | }); |
7b54e481 |
1199 | } |
932277e1 |
1200 | mybuttons.button( 'enable' ); |
30d0ba1e |
1201 | }); |
9231663d |
1202 | $( '#dialog-form' ).dialog( 'close' ); |
30d0ba1e |
1203 | }, 'json' ); |
1204 | }, |
1205 | Cancel: function() { |
9231663d |
1206 | $( this ).dialog( 'close' ); |
30d0ba1e |
1207 | } |
1208 | }, |
1209 | create: function(event, ui) { |
1210 | $(this).data( 'relation_drawn', false ); |
a166dca8 |
1211 | $('#rel_type').data( 'changed_after_open', false ); |
56e3972e |
1212 | $.each( relationship_types, function(index, typedef) { |
1213 | $('#rel_type').append( $('<option />').attr( "value", typedef.name ).text(typedef.name) ); |
1214 | }); |
1215 | $.each( relationship_scopes, function(index, value) { |
1216 | $('#scope').append( $('<option />').attr( "value", value ).text(value) ); |
a166dca8 |
1217 | }); |
e4bdf660 |
1218 | $.each( ternary_values, function( index, value ) { |
1219 | $('#is_significant').append( $('<option />').attr( "value", value ).text(value) ); |
1220 | }); |
1221 | // Handler to reset fields to default, the first time the relationship |
1222 | // is changed after opening the form. |
a166dca8 |
1223 | $('#rel_type').change( function () { |
1224 | if( !$(this).data( 'changed_after_open' ) ) { |
1225 | $('#note').val(''); |
e4bdf660 |
1226 | $(this).find(':checked').removeAttr('checked'); |
a166dca8 |
1227 | } |
1228 | $(this).data( 'changed_after_open', true ); |
1229 | }); |
30d0ba1e |
1230 | }, |
1231 | open: function() { |
b001c73d |
1232 | relation_manager.create_temporary( |
1233 | $('#source_node_id').val(), $('#target_node_id').val() ); |
1234 | var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ) |
1235 | if( readings_equivalent( $('#source_node_id').val(), |
1236 | $('#target_node_id').val() ) ) { |
1237 | buttonset.find( "button:contains('Merge readings')" ).show(); |
1238 | } else { |
1239 | buttonset.find( "button:contains('Merge readings')" ).hide(); |
1240 | } |
30d0ba1e |
1241 | $(".ui-widget-overlay").css("background", "none"); |
1242 | $("#dialog_overlay").show(); |
1243 | $("#dialog_overlay").height( $("#enlargement_container").height() ); |
1244 | $("#dialog_overlay").width( $("#enlargement_container").innerWidth() ); |
1245 | $("#dialog_overlay").offset( $("#enlargement_container").offset() ); |
a166dca8 |
1246 | $('#rel_type').data( 'changed_after_open', false ); |
30d0ba1e |
1247 | }, |
1248 | close: function() { |
1249 | relation_manager.remove_temporary(); |
1250 | $( '#status' ).empty(); |
1251 | $("#dialog_overlay").hide(); |
1252 | } |
1253 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
e4bdf660 |
1254 | if( ( ajaxSettings.url == getTextURL('relationships') |
1255 | || ajaxSettings.url == getTextURL('merge') ) |
21e6ebc7 |
1256 | && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) { |
1257 | var error; |
1258 | if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) { |
1259 | error = 'You are not authorized to modify this tradition. (Try logging in again?)'; |
1260 | } else { |
1261 | try { |
1262 | var errobj = jQuery.parseJSON( jqXHR.responseText ); |
1263 | error = errobj.error + '</br>The relationship cannot be made.</p>'; |
1264 | } catch(e) { |
1265 | error = jqXHR.responseText; |
1266 | } |
1267 | } |
1268 | $('#status').append( '<p class="error">Error: ' + error ); |
1269 | } |
1270 | $(event.target).parent().find('.ui-button').button("enable"); |
30d0ba1e |
1271 | } ); |
1272 | } |
b28e606e |
1273 | |
b001c73d |
1274 | // Set up the relationship info display and deletion dialog. |
9529f69c |
1275 | $( "#delete-form" ).dialog({ |
1276 | autoOpen: false, |
69a19c91 |
1277 | height: 135, |
d5b3c010 |
1278 | width: 300, |
9529f69c |
1279 | modal: false, |
b001c73d |
1280 | buttons: { |
1281 | OK: function() { $( this ).dialog( "close" ); }, |
1282 | "Delete all": function () { delete_relation( true ); }, |
1283 | Delete: function() { delete_relation( false ); } |
1284 | }, |
9529f69c |
1285 | create: function(event, ui) { |
30d0ba1e |
1286 | // TODO What is this logic doing? |
a84ca4de |
1287 | // This scales the buttons in the dialog and makes it look proper |
1288 | // Not sure how essential it is, does anything break if it's not here? |
9529f69c |
1289 | var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' ); |
b001c73d |
1290 | buttonset.find( "button:contains('OK')" ).css( 'float', 'right' ); |
a84ca4de |
1291 | // A: This makes sure that the pop up delete relation dialogue for a hovered over |
1292 | // relation auto closes if the user doesn't engage (mouseover) with it. |
9529f69c |
1293 | var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']"); |
1294 | dialog_aria.mouseenter( function() { |
1295 | if( mouseWait != null ) { clearTimeout(mouseWait) }; |
1296 | }) |
1297 | dialog_aria.mouseleave( function() { |
1298 | mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 ); |
1299 | }) |
1300 | }, |
1301 | open: function() { |
b001c73d |
1302 | // Show the appropriate buttons... |
1303 | var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ) |
1304 | // If the user can't edit, show only the OK button |
088a14af |
1305 | if( !editable ) { |
b001c73d |
1306 | buttonset.find( "button:contains('Delete')" ).hide(); |
1307 | // If the relationship scope is local, show only OK and Delete |
088a14af |
1308 | } else if( $('#delete_relation_scope').text() === 'local' ) { |
1309 | $( this ).dialog( "option", "width", 160 ); |
b001c73d |
1310 | buttonset.find( "button:contains('Delete')" ).show(); |
1311 | buttonset.find( "button:contains('Delete all')" ).hide(); |
1312 | // Otherwise, show all three |
088a14af |
1313 | } else { |
1314 | $( this ).dialog( "option", "width", 200 ); |
b001c73d |
1315 | buttonset.find( "button:contains('Delete')" ).show(); |
088a14af |
1316 | } |
9529f69c |
1317 | mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 ); |
1318 | }, |
088a14af |
1319 | close: function() {} |
9529f69c |
1320 | }); |
1321 | |
a84ca4de |
1322 | $( "#multipleselect-form" ).dialog({ |
1323 | autoOpen: false, |
1324 | height: 150, |
1325 | width: 250, |
1326 | modal: true, |
5fe117bc |
1327 | buttons: { |
22a70299 |
1328 | Cancel: function() { $( this ).dialog( "close" ); }, |
a51e34c5 |
1329 | Detach: function ( evt ) { |
1330 | evt.target.id = 'detach_btn'; |
1331 | |
22a70299 |
1332 | var self = $(this); |
a51e34c5 |
1333 | var mybuttons = $(evt.target).closest('button').parent().find('button'); |
1334 | mybuttons.button( 'disable' ); |
22a70299 |
1335 | var form_values = $('#detach_collated_form').serialize(); |
1336 | ncpath = getTextURL( 'duplicate' ); |
1337 | var jqjson = $.post( ncpath, form_values, function(data) { |
1338 | detach_node( data ); |
932277e1 |
1339 | mybuttons.button("enable"); |
22a70299 |
1340 | self.dialog( "close" ); |
1341 | } ); |
a51e34c5 |
1342 | }, |
1343 | Merge: function (evt) { |
1344 | evt.target.id = 'merge_btn'; |
1345 | |
1346 | var self = $(this); |
1347 | var mybuttons = $(evt.target).closest('button').parent().find('button'); |
1348 | mybuttons.button('disable'); |
1349 | |
1350 | var ncpath = getTextURL('compress'); |
1351 | var form_values = $('#detach_collated_form').serialize(); |
1352 | |
1353 | var jqjson = $.post(ncpath, form_values, function(data) { |
2d34519e |
1354 | if (data.success) { |
1355 | if (data.nodes) { |
1356 | compress_nodes(data.nodes); |
1357 | } |
1358 | |
1359 | mybuttons.button('enable'); |
1360 | self.dialog('close'); |
1361 | } else if (data.error_msg) { |
1362 | document.getElementById('duplicate-merge-error').innerHTML = data.error_msg; |
1363 | mybuttons.button('enable'); |
a51e34c5 |
1364 | |
2d34519e |
1365 | } |
a51e34c5 |
1366 | }); |
22a70299 |
1367 | } |
7ef4a584 |
1368 | }, |
1369 | create: function(event, ui) { |
a84ca4de |
1370 | var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' ); |
1371 | buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' ); |
1372 | }, |
1373 | open: function() { |
1374 | $( this ).dialog( "option", "width", 200 ); |
e538eccb |
1375 | $(".ui-widget-overlay").css("background", "none"); |
5fe117bc |
1376 | $('#multipleselect-form-status').empty(); |
e538eccb |
1377 | $("#dialog_overlay").show(); |
1378 | $("#dialog_overlay").height( $("#enlargement_container").height() ); |
1379 | $("#dialog_overlay").width( $("#enlargement_container").innerWidth() ); |
1380 | $("#dialog_overlay").offset( $("#enlargement_container").offset() ); |
a51e34c5 |
1381 | |
1382 | var mybuttons = $(this).parent().find('button'); |
1383 | |
1384 | mybuttons[1].id = 'detach_btn'; |
1385 | mybuttons[2].id = 'merge_btn'; |
a84ca4de |
1386 | }, |
e538eccb |
1387 | close: function() { |
1388 | marquee.unselect(); |
1389 | $("#dialog_overlay").hide(); |
1390 | } |
5fe117bc |
1391 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
1392 | if( ajaxSettings.url == getTextURL('duplicate') |
1393 | && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) { |
1394 | var error; |
1395 | if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) { |
1396 | error = 'You are not authorized to modify this tradition. (Try logging in again?)'; |
1397 | } else { |
1398 | try { |
1399 | var errobj = jQuery.parseJSON( jqXHR.responseText ); |
1400 | error = errobj.error + '</br>The relationship cannot be made.</p>'; |
1401 | } catch(e) { |
1402 | error = jqXHR.responseText; |
1403 | } |
1404 | } |
1405 | $('#multipleselect-form-status').append( '<p class="error">Error: ' + error ); |
1406 | } |
1407 | $(event.target).parent().find('.ui-button').button("enable"); |
1408 | }); |
1409 | |
a84ca4de |
1410 | |
088a14af |
1411 | // Helpers for relationship deletion |
1412 | |
1413 | function delete_relation( scopewide ) { |
1414 | form_values = $('#delete_relation_form').serialize(); |
1415 | if( scopewide ) { |
1416 | form_values += "&scopewide=true"; |
1417 | } |
1418 | ncpath = getTextURL( 'relationships' ); |
1419 | var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) { |
1420 | $.each( data, function(item, source_target) { |
1421 | relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) ); |
1422 | }); |
1423 | $( "#delete-form" ).dialog( "close" ); |
1424 | }, dataType: 'json', type: 'DELETE' }); |
1425 | } |
1426 | |
1427 | function toggle_relation_active( node_id ) { |
1428 | $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) { |
1429 | matchid = new RegExp( "^" + node_id ); |
1430 | if( $(this).text().match( matchid ) != null ) { |
1431 | var relation_id = $(this).parent().attr('id'); |
1432 | relation_manager.toggle_active( relation_id ); |
1433 | }; |
1434 | }); |
1435 | } |
1436 | |
487674b9 |
1437 | // function for reading form dialog should go here; |
1438 | // just hide the element for now if we don't have morphology |
1439 | if( can_morphologize ) { |
1440 | $('#reading-form').dialog({ |
1441 | autoOpen: false, |
1442 | // height: 400, |
1443 | width: 450, |
1444 | modal: true, |
1445 | buttons: { |
1446 | Cancel: function() { |
1447 | $( this ).dialog( "close" ); |
1448 | }, |
1449 | Update: function( evt ) { |
1450 | // Disable the button |
932277e1 |
1451 | var mybuttons = $(evt.target).closest('button').parent().find('button'); |
1452 | mybuttons.button( 'disable' ); |
487674b9 |
1453 | $('#reading_status').empty(); |
1454 | var reading_id = $('#reading_id').val() |
1455 | form_values = { |
1456 | 'id' : reading_id, |
1457 | 'is_nonsense': $('#reading_is_nonsense').is(':checked'), |
1458 | 'grammar_invalid': $('#reading_grammar_invalid').is(':checked'), |
1459 | 'normal_form': $('#reading_normal_form').val() }; |
1460 | // Add the morphology values |
1461 | $('.reading_morphology').each( function() { |
1462 | if( $(this).val() != '(Click to select)' ) { |
1463 | var rmid = $(this).attr('id'); |
1464 | rmid = rmid.substring(8); |
1465 | form_values[rmid] = $(this).val(); |
1466 | } |
45ee3b96 |
1467 | }); |
487674b9 |
1468 | // Make the JSON call |
1469 | ncpath = getReadingURL( reading_id ); |
1470 | var reading_element = readingdata[reading_id]; |
1471 | // $(':button :contains("Update")').attr("disabled", true); |
1472 | var jqjson = $.post( ncpath, form_values, function(data) { |
1473 | $.each( data, function(key, value) { |
1474 | reading_element[key] = value; |
1475 | }); |
1476 | if( $('#update_workspace_button').data('locked') == false ) { |
1477 | color_inactive( get_ellipse( reading_id ) ); |
1478 | } |
932277e1 |
1479 | mybuttons.button("enable"); |
487674b9 |
1480 | $( "#reading-form" ).dialog( "close" ); |
1481 | }); |
1482 | // Re-color the node if necessary |
1483 | return false; |
1484 | } |
1485 | }, |
1486 | create: function() { |
30d0ba1e |
1487 | if( !editable ) { |
1488 | // Get rid of the disallowed editing UI bits |
1489 | $( this ).dialog( "option", "buttons", |
1490 | [{ text: "OK", click: function() { $( this ).dialog( "close" ); }}] ); |
1491 | $('#reading_relemmatize').hide(); |
1492 | } |
487674b9 |
1493 | }, |
1494 | open: function() { |
1495 | $(".ui-widget-overlay").css("background", "none"); |
1496 | $("#dialog_overlay").show(); |
1497 | $('#reading_status').empty(); |
1498 | $("#dialog_overlay").height( $("#enlargement_container").height() ); |
1499 | $("#dialog_overlay").width( $("#enlargement_container").innerWidth() ); |
1500 | $("#dialog_overlay").offset( $("#enlargement_container").offset() ); |
1501 | $("#reading-form").parent().find('.ui-button').button("enable"); |
1502 | }, |
1503 | close: function() { |
1504 | $("#dialog_overlay").hide(); |
1505 | } |
1506 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
21e6ebc7 |
1507 | if( ajaxSettings.url.lastIndexOf( getReadingURL('') ) > -1 |
487674b9 |
1508 | && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) { |
21e6ebc7 |
1509 | var error; |
1510 | if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) { |
1511 | error = 'You are not authorized to modify this tradition. (Try logging in again?)'; |
1512 | } else { |
1513 | try { |
1514 | var errobj = jQuery.parseJSON( jqXHR.responseText ); |
1515 | error = errobj.error + '</br>The relationship cannot be made.</p>'; |
1516 | } catch(e) { |
1517 | error = jqXHR.responseText; |
1518 | } |
1519 | } |
1520 | $('#status').append( '<p class="error">Error: ' + error ); |
1521 | } |
1522 | $(event.target).parent().find('.ui-button').button("enable"); |
487674b9 |
1523 | }); |
1524 | } else { |
1525 | $('#reading-form').hide(); |
45ee3b96 |
1526 | } |
4c41c02c |
1527 | |
76f05423 |
1528 | |
b28e606e |
1529 | $('#update_workspace_button').click( function() { |
30d0ba1e |
1530 | if( !editable ) { |
1531 | return; |
1532 | } |
b28e606e |
1533 | var svg_enlargement = $('#svgenlargement').svg().svg('get').root(); |
76f05423 |
1534 | mouse_scale = svg_root_element.getScreenCTM().a; |
9529f69c |
1535 | if( $(this).data('locked') == true ) { |
1536 | $('#svgenlargement ellipse' ).each( function( index ) { |
1537 | if( $(this).data( 'node_obj' ) != null ) { |
1538 | $(this).data( 'node_obj' ).ungreyout_edges(); |
e538eccb |
1539 | $(this).data( 'node_obj' ).set_selectable( false ); |
1540 | color_inactive( $(this) ); |
9529f69c |
1541 | var node_id = $(this).data( 'node_obj' ).get_id(); |
1542 | toggle_relation_active( node_id ); |
1543 | $(this).data( 'node_obj', null ); |
1544 | } |
b28e606e |
1545 | }) |
b28e606e |
1546 | $(this).data('locked', false); |
9529f69c |
1547 | $(this).css('background-position', '0px 44px'); |
b28e606e |
1548 | } else { |
9529f69c |
1549 | var left = $('#enlargement').offset().left; |
1550 | var right = left + $('#enlargement').width(); |
76f05423 |
1551 | var tf = svg_root_element.getScreenCTM().inverse(); |
9529f69c |
1552 | var p = svg_root.createSVGPoint(); |
1553 | p.x=left; |
1554 | p.y=100; |
1555 | var cx_min = p.matrixTransform(tf).x; |
1556 | p.x=right; |
1557 | var cx_max = p.matrixTransform(tf).x; |
1558 | $('#svgenlargement ellipse').each( function( index ) { |
1559 | var cx = parseInt( $(this).attr('cx') ); |
1560 | if( cx > cx_min && cx < cx_max) { |
1561 | if( $(this).data( 'node_obj' ) == null ) { |
1562 | $(this).data( 'node_obj', new node_obj( $(this) ) ); |
1563 | } else { |
e538eccb |
1564 | $(this).data( 'node_obj' ).set_selectable( true ); |
9529f69c |
1565 | } |
1566 | $(this).data( 'node_obj' ).greyout_edges(); |
1567 | var node_id = $(this).data( 'node_obj' ).get_id(); |
1568 | toggle_relation_active( node_id ); |
b28e606e |
1569 | } |
9529f69c |
1570 | }); |
1571 | $(this).css('background-position', '0px 0px'); |
b28e606e |
1572 | $(this).data('locked', true ); |
b28e606e |
1573 | } |
1574 | }); |
30d0ba1e |
1575 | |
1576 | if( !editable ) { |
1577 | // Hide the unused elements |
1578 | $('#dialog-form').hide(); |
1579 | $('#update_workspace_button').hide(); |
1580 | } |
1581 | |
b28e606e |
1582 | |
e847b186 |
1583 | $('.helptag').popupWindow({ |
1584 | height:500, |
1585 | width:800, |
1586 | top:50, |
1587 | left:50, |
1588 | scrollbars:1 |
1589 | }); |
1590 | |
76f05423 |
1591 | expandFillPageClients(); |
1592 | $(window).resize(function() { |
1593 | expandFillPageClients(); |
1594 | }); |
1595 | |
9529f69c |
1596 | }); |
b28e606e |
1597 | |
1598 | |
76f05423 |
1599 | function expandFillPageClients() { |
1600 | $('.fillPage').each(function () { |
1601 | $(this).height($(window).height() - $(this).offset().top - MARGIN); |
1602 | }); |
1603 | } |
1604 | |
89aae3ee |
1605 | function loadSVG(svgData, direction) { |
1606 | text_direction = direction; |
1607 | |
76f05423 |
1608 | var svgElement = $('#svgenlargement'); |
1609 | |
1610 | $(svgElement).svg('destroy'); |
1611 | |
1612 | $(svgElement).svg({ |
1613 | loadURL: svgData, |
1614 | onLoad : svgEnlargementLoaded |
1615 | }); |
1616 | } |
1617 | |
1618 | |
c1add777 |
1619 | |
76f05423 |
1620 | /* OS Gadget stuff |
1621 | |
1622 | function svg_select_callback(topic, data, subscriberData) { |
1623 | svgData = data; |
1624 | loadSVG(svgData); |
1625 | } |
1626 | |
1627 | function loaded() { |
1628 | var prefs = new gadgets.Prefs(); |
1629 | var preferredHeight = parseInt(prefs.getString('height')); |
1630 | if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight); |
1631 | expandFillPageClients(); |
1632 | } |
1633 | |
1634 | if (gadgets.util.hasFeature('pubsub-2')) { |
1635 | gadgets.HubSettings.onConnect = function(hum, suc, err) { |
1636 | subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback); |
1637 | loaded(); |
1638 | }; |
1639 | } |
1640 | else gadgets.util.registerOnLoadHandler(loaded); |
1641 | */ |