request reading data on page load
[scpubgit/stemmatology.git] / stemmaweb / root / js / relationship.js
CommitLineData
aff524fc 1var MARGIN=30;
2var svg_root = null;
3var svg_root_element = null;
4var start_element_height = 0;
5var reltypes = {};
a8928d1d 6var readingdata = {};
aff524fc 7
7fd1d97a 8function getTextPath() {
aff524fc 9 var currpath = window.location.pathname;
93daee83 10 // Get rid of trailing slash
7fd1d97a 11 if( currpath.lastIndexOf('/') == currpath.length - 1 ) {
12 currpath = currpath.slice( 0, currpath.length - 1)
13 };
93daee83 14 // Get rid of query parameters
15 if( currpath.lastIndexOf('?') != -1 ) {
16 currpath = currpath.slice( 0, currpath.lastIndexOf('?') );
17 };
7fd1d97a 18 var path_elements = currpath.split('/');
19 var textid = path_elements.pop();
20 var basepath = path_elements.join( '/' );
21 var path_parts = [ basepath, textid ];
22 return path_parts;
581aee24 23}
24
72874569 25function getRelativePath() {
26 var path_parts = getTextPath();
27 return path_parts[0];
28}
29
a8928d1d 30function getTextURL( which ) {
72874569 31 var path_parts = getTextPath();
a8928d1d 32 return path_parts[0] + '/' + path_parts[1] + '/' + which;
33}
34
35function getReadingURL( reading_id ) {
36 var path_parts = getTextPath();
37 return path_parts[0] + '/' + path_parts[1] + '/reading/' + reading_id;
581aee24 38}
39
28f2ef34 40// Make an XML ID into a valid selector
41function jq(myid) {
42 return '#' + myid.replace(/(:|\.)/g,'\\$1');
43}
44
581aee24 45function svgEnlargementLoaded() {
63213207 46 //Give some visual evidence that we are working
47 $('#loading_overlay').show();
48 lo_height = $("#enlargement_container").outerHeight();
49 lo_width = $("#enlargement_container").outerWidth();
50 $("#loading_overlay").height( lo_height );
51 $("#loading_overlay").width( lo_width );
52 $("#loading_overlay").offset( $("#enlargement_container").offset() );
53 $("#loading_message").offset(
54 { 'top': lo_height / 2 - $("#loading_message").height() / 2,
55 'left': lo_width / 2 - $("#loading_message").width() / 2 });
72874569 56 //Set viewbox widht and height to widht and height of $('#svgenlargement svg').
aff524fc 57 $('#update_workspace_button').data('locked', false);
58 $('#update_workspace_button').css('background-position', '0px 44px');
72874569 59 //This is essential to make sure zooming and panning works properly.
60 $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} );
61 var graph_svg = $('#svgenlargement svg');
62 var svg_g = $('#svgenlargement svg g')[0];
aff524fc 63 if (!svg_g) return;
72874569 64 svg_root = graph_svg.svg().svg('get').root();
aff524fc 65
66 // Find the real root and ignore any text nodes
67 for (i = 0; i < svg_root.childNodes.length; ++i) {
68 if (svg_root.childNodes[i].nodeName != '#text') {
69 svg_root_element = svg_root.childNodes[i];
70 break;
71 }
72 }
73
74 // This might be a little application specific and should be pushed to the backend
75 // but... This collapes corrector hands when they agree with the original
76 // e.g., P46-*,P46-C becomes simply P46
77 $(svg_root).find('text').each(function () {
78 var t = $(this).text();
79 var ws = t.split(',');
80 for (i = 0; i < ws.length; ++i) {
81 if (ws[i].indexOf('-*')> -1) {
82 var collapse = false;
83 var main = $.trim(ws[i].substring(0,ws[i].indexOf('-')));
84 for (j = 0; j < ws.length; ++j) {
85 if (i != j && ws[j].indexOf('-')> -1) {
86 var pair = $.trim(ws[j].substring(0,ws[j].indexOf('-')));
87 if (main == pair) {
88 collapse = true;
89 ws[j] = '';
90 }
91 }
92 }
93 if (collapse) ws[i] = main;
94 }
95 }
96 t = '';
97 for (i = 0; i < ws.length; ++i) {
98 if (ws[i].length) {
99 if (t.length>0) t+=', ';
100 t+=ws[i];
101 }
102 }
103 $(this).text(t);
104 });
105
72874569 106 svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
107 svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
108 //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
109 //This is just to create a nice starting enlargement.
110 var initial_svg_height = 250;
111 var scale = initial_svg_height/graph_svg.attr( 'height' );
112 var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
113 var transform = svg_g.getAttribute('transform');
114 var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
115 translate += additional_translate;
116 var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
117 svg_g.setAttribute('transform', transform);
118 //used to calculate min and max zoom level:
28f2ef34 119 start_element_height = $('#__START__ ellipse')[0].getBBox().height;
63213207 120 add_relations( function() { $('#loading_overlay').hide(); });
7fd1d97a 121}
122
63213207 123function add_relations( callback_fn ) {
72874569 124 var basepath = getRelativePath();
a8928d1d 125 var textrelpath = getTextURL( 'relationships' );
72874569 126 $.getJSON( basepath + '/definitions', function(data) {
7fd1d97a 127 var rel_types = data.types.sort();
72874569 128 $.getJSON( textrelpath,
7fd1d97a 129 function(data) {
130 $.each(data, function( index, rel_info ) {
131 var type_index = $.inArray(rel_info.type, rel_types);
93daee83 132 var source_found = get_ellipse( rel_info.source );
133 var target_found = get_ellipse( rel_info.target );
134 if( type_index != -1 && source_found.size() && target_found.size() ) {
72874569 135 var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
136 relation.data( 'type', rel_info.type );
137 relation.data( 'scope', rel_info.scope );
31aaf446 138 relation.data( 'note', rel_info.note );
72874569 139 var node_obj = get_node_obj(rel_info.source);
140 node_obj.set_draggable( false );
141 node_obj.ellipse.data( 'node_obj', null );
142 node_obj = get_node_obj(rel_info.target);
143 node_obj.set_draggable( false );
144 node_obj.ellipse.data( 'node_obj', null );
7fd1d97a 145 }
63213207 146 });
147 callback_fn.call();
72874569 148 });
7fd1d97a 149 });
581aee24 150}
151
152function get_ellipse( node_id ) {
28f2ef34 153 return $( jq( node_id ) + ' ellipse');
581aee24 154}
155
156function get_node_obj( node_id ) {
72874569 157 var node_ellipse = get_ellipse( node_id );
158 if( node_ellipse.data( 'node_obj' ) == null ) {
159 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
160 };
161 return node_ellipse.data( 'node_obj' );
581aee24 162}
163
581aee24 164function node_obj(ellipse) {
165 this.ellipse = ellipse;
166 var self = this;
167
168 this.x = 0;
169 this.y = 0;
170 this.dx = 0;
171 this.dy = 0;
172 this.node_elements = node_elements_for(self.ellipse);
173
174 this.get_id = function() {
28f2ef34 175 return $(self.ellipse).parent().attr('id')
581aee24 176 }
177
178 this.set_draggable = function( draggable ) {
179 if( draggable ) {
0fa07e25 180 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
181 $(self.ellipse).parent().mousedown( this.mousedown_listener );
182 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
183 self.ellipse.siblings('text').attr('class', 'noselect draggable');
581aee24 184 } else {
0fa07e25 185 self.ellipse.siblings('text').attr('class', '');
186 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave').unbind('mousedown');
187 $(self.ellipse).attr( {stroke:'green', fill:'#b3f36d'} );
581aee24 188 }
189 }
190
191 this.mousedown_listener = function(evt) {
192 evt.stopPropagation();
193 self.x = evt.clientX;
194 self.y = evt.clientY;
195 $('body').mousemove( self.mousemove_listener );
196 $('body').mouseup( self.mouseup_listener );
0fa07e25 197 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
581aee24 198 self.ellipse.attr( 'fill', '#ff66ff' );
199 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
200 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
201 }
202
203 this.mousemove_listener = function(evt) {
72874569 204 self.dx = (evt.clientX - self.x) / mouse_scale;
205 self.dy = (evt.clientY - self.y) / mouse_scale;
581aee24 206 self.move_elements();
0fa07e25 207 evt.returnValue = false;
208 evt.preventDefault();
209 return false;
581aee24 210 }
211
212 this.mouseup_listener = function(evt) {
213 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
28f2ef34 214 var source_node_id = $(self.ellipse).parent().attr('id');
0fa07e25 215 var source_node_text = self.ellipse.siblings('text').text();
28f2ef34 216 var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id');
0fa07e25 217 var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
581aee24 218 $('#source_node_id').val( source_node_id );
0fa07e25 219 $('#source_node_text').val( source_node_text );
581aee24 220 $('#target_node_id').val( target_node_id );
0fa07e25 221 $('#target_node_text').val( target_node_text );
581aee24 222 $('#dialog-form').dialog( 'open' );
223 };
224 $('body').unbind('mousemove');
225 $('body').unbind('mouseup');
226 self.ellipse.attr( 'fill', '#fff' );
0fa07e25 227 $(self.ellipse).parent().hover( self.enter_node, self.leave_node );
72874569 228 self.reset_elements();
581aee24 229 }
230
231 this.cpos = function() {
232 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
233 }
234
235 this.get_g = function() {
236 return self.ellipse.parent('g');
237 }
238
239 this.enter_node = function(evt) {
240 self.ellipse.attr( 'fill', '#ffccff' );
241 }
242
243 this.leave_node = function(evt) {
244 self.ellipse.attr( 'fill', '#fff' );
245 }
246
247 this.greyout_edges = function() {
248 $.each( self.node_elements, function(index, value) {
249 value.grey_out('.edge');
250 });
251 }
252
253 this.ungreyout_edges = function() {
254 $.each( self.node_elements, function(index, value) {
255 value.un_grey_out('.edge');
256 });
257 }
258
259 this.move_elements = function() {
260 $.each( self.node_elements, function(index, value) {
261 value.move(self.dx,self.dy);
262 });
263 }
264
265 this.reset_elements = function() {
266 $.each( self.node_elements, function(index, value) {
267 value.reset();
268 });
269 }
270
271 this.update_elements = function() {
272 self.node_elements = node_elements_for(self.ellipse);
273 }
274
275 self.set_draggable( true );
276}
277
278function svgshape( shape_element ) {
279 this.shape = shape_element;
280 this.move = function(dx,dy) {
281 this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
282 }
283 this.reset = function() {
284 this.shape.attr( "transform", "translate( 0, 0 )" );
285 }
286 this.grey_out = function(filter) {
287 if( this.shape.parent(filter).size() != 0 ) {
288 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
289 }
290 }
291 this.un_grey_out = function(filter) {
292 if( this.shape.parent(filter).size() != 0 ) {
293 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
294 }
295 }
296}
297
298function svgpath( path_element, svg_element ) {
299 this.svg_element = svg_element;
300 this.path = path_element;
301 this.x = this.path.x;
302 this.y = this.path.y;
303 this.move = function(dx,dy) {
304 this.path.x = this.x + dx;
305 this.path.y = this.y + dy;
306 }
307 this.reset = function() {
308 this.path.x = this.x;
309 this.path.y = this.y;
310 }
311 this.grey_out = function(filter) {
312 if( this.svg_element.parent(filter).size() != 0 ) {
313 this.svg_element.attr('stroke', '#e5e5e5');
314 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
0fa07e25 315 this.svg_element.siblings('text').attr('class', 'noselect');
581aee24 316 }
317 }
318 this.un_grey_out = function(filter) {
319 if( this.svg_element.parent(filter).size() != 0 ) {
320 this.svg_element.attr('stroke', '#000000');
321 this.svg_element.siblings('text').attr('fill', '#000000');
0fa07e25 322 this.svg_element.siblings('text').attr('class', '');
581aee24 323 }
324 }
325}
326
327function node_elements_for( ellipse ) {
328 node_elements = get_edge_elements_for( ellipse );
329 node_elements.push( new svgshape( ellipse.siblings('text') ) );
330 node_elements.push( new svgshape( ellipse ) );
331 return node_elements;
332}
333
334function get_edge_elements_for( ellipse ) {
335 edge_elements = new Array();
28f2ef34 336 node_id = ellipse.parent().attr('id');
581aee24 337 edge_in_pattern = new RegExp( node_id + '$' );
338 edge_out_pattern = new RegExp( '^' + node_id );
339 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
340 title = $(this).text();
341 if( edge_in_pattern.test(title) ) {
342 polygon = $(this).siblings('polygon');
343 if( polygon.size() > 0 ) {
344 edge_elements.push( new svgshape( polygon ) );
345 }
346 path_segments = $(this).siblings('path')[0].pathSegList;
347 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
348 }
349 if( edge_out_pattern.test(title) ) {
350 path_segments = $(this).siblings('path')[0].pathSegList;
351 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
352 }
353 });
354 return edge_elements;
355}
356
357function relation_factory() {
358 var self = this;
359 this.color_memo = null;
360 //TODO: colors hard coded for now
361 this.temp_color = '#FFA14F';
362 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
363
364 this.create_temporary = function( source_node_id, target_node_id ) {
28f2ef34 365 var relation_id = get_relation_id( source_node_id, target_node_id );
366 var relation = $( jq( relation_id ) );
72874569 367 if( relation.size() == 0 ) {
581aee24 368 draw_relation( source_node_id, target_node_id, self.temp_color );
369 } else {
370 self.color_memo = relation.children('path').attr( 'stroke' );
371 relation.children('path').attr( 'stroke', self.temp_color );
372 }
373 }
374 this.remove_temporary = function() {
375 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
376 if( self.color_memo != null ) {
377 path_element.attr( 'stroke', self.color_memo );
378 self.color_memo = null;
379 } else {
72874569 380 var temporary = path_element.parent('g').remove();
381 temporary.empty();
382 temporary = null;
581aee24 383 }
384 }
385 this.create = function( source_node_id, target_node_id, color_index ) {
386 //TODO: Protect from (color_)index out of bound..
387 var relation_color = self.relation_colors[ color_index ];
72874569 388 var relation = draw_relation( source_node_id, target_node_id, relation_color );
389 get_node_obj( source_node_id ).update_elements();
390 get_node_obj( target_node_id ).update_elements();
391 return relation;
581aee24 392 }
72874569 393 this.toggle_active = function( relation_id ) {
28f2ef34 394 var relation = $( jq( relation_id ) );
72874569 395 var relation_path = relation.children('path');
396 if( !relation.data( 'active' ) ) {
397 relation_path.css( {'cursor':'pointer'} );
398 relation_path.mouseenter( function(event) {
399 outerTimer = setTimeout( function() {
400 timer = setTimeout( function() {
28f2ef34 401 var related_nodes = get_related_nodes( relation_id );
402 var source_node_id = related_nodes[0];
403 var target_node_id = related_nodes[1];
72874569 404 $('#delete_source_node_id').val( source_node_id );
405 $('#delete_target_node_id').val( target_node_id );
406 self.showinfo(relation);
407 }, 500 )
408 }, 1000 );
409 });
410 relation_path.mouseleave( function(event) {
411 clearTimeout(outerTimer);
412 if( timer != null ) { clearTimeout(timer); }
413 });
414 relation.data( 'active', true );
415 } else {
416 relation_path.unbind( 'mouseenter' );
417 relation_path.unbind( 'mouseleave' );
418 relation_path.css( {'cursor':'inherit'} );
419 relation.data( 'active', false );
420 }
421 }
422 this.showinfo = function(relation) {
31aaf446 423 var htmlstr = 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' );
424 if( relation.data( 'note' ) ) {
425 htmlstr = htmlstr + '<br/>note: ' + relation.data( 'note' );
426 }
427 $('#delete-form-text').html( htmlstr );
72874569 428 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
429 var xs = parseFloat( points[0].split(',')[0] );
430 var xe = parseFloat( points[1].split(',')[0] );
431 var ys = parseFloat( points[0].split(',')[1] );
432 var ye = parseFloat( points[3].split(',')[1] );
433 var p = svg_root.createSVGPoint();
434 p.x = xs + ((xe-xs)*1.1);
435 p.y = ye - ((ye-ys)/2);
aff524fc 436 var ctm = svg_root_element.getScreenCTM();
72874569 437 var nx = p.matrixTransform(ctm).x;
438 var ny = p.matrixTransform(ctm).y;
439 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
440 $('#delete-form').dialog( 'open' );
441 dialog_aria.offset({ left: nx, top: ny });
442 }
443 this.remove = function( relation_id ) {
28f2ef34 444 var relation = $( jq( relation_id ) );
72874569 445 relation.remove();
581aee24 446 }
447}
448
28f2ef34 449// Utility function to create/return the ID of a relation link between
450// a source and target.
451function get_relation_id( source_id, target_id ) {
452 var idlist = [ source_id, target_id ];
453 idlist.sort();
454 return 'relation-' + idlist[0] + '-...-' + idlist[1];
455}
456
457function get_related_nodes( relation_id ) {
458 var srctotarg = relation_id.substr( 9 );
459 return srctotarg.split('-...-');
460}
461
581aee24 462function draw_relation( source_id, target_id, relation_color ) {
72874569 463 var source_ellipse = get_ellipse( source_id );
464 var target_ellipse = get_ellipse( target_id );
28f2ef34 465 var relation_id = get_relation_id( source_id, target_id );
72874569 466 var svg = $('#svgenlargement').children('svg').svg().svg('get');
467 var path = svg.createPath();
468 var sx = parseInt( source_ellipse.attr('cx') );
469 var rx = parseInt( source_ellipse.attr('rx') );
470 var sy = parseInt( source_ellipse.attr('cy') );
471 var ex = parseInt( target_ellipse.attr('cx') );
472 var ey = parseInt( target_ellipse.attr('cy') );
28f2ef34 473 var relation = svg.group( $("#svgenlargement svg g"),
474 { 'class':'relation', 'id':relation_id } );
72874569 475 svg.title( relation, source_id + '->' + target_id );
476 svg.path( relation, path.move( sx, sy ).curveC( sx + (2*rx), sy, ex + (2*rx), ey, ex, ey ), {fill: 'none', stroke: relation_color, strokeWidth: 4});
581aee24 477 var relation_element = $('#svgenlargement .relation').filter( ':last' );
478 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
72874569 479 return relation_element;
581aee24 480}
481
72874569 482
581aee24 483$(document).ready(function () {
72874569 484
485 timer = null;
581aee24 486 relation_manager = new relation_factory();
72874569 487 $('#update_workspace_button').data('locked', false);
581aee24 488
72874569 489 $('#enlargement').mousedown(function (event) {
581aee24 490 $(this)
72874569 491 .data('down', true)
492 .data('x', event.clientX)
493 .data('y', event.clientY)
494 .data('scrollLeft', this.scrollLeft)
aff524fc 495 stateTf = svg_root_element.getCTM().inverse();
72874569 496 var p = svg_root.createSVGPoint();
497 p.x = event.clientX;
498 p.y = event.clientY;
499 stateOrigin = p.matrixTransform(stateTf);
aff524fc 500 event.returnValue = false;
501 event.preventDefault();
72874569 502 return false;
581aee24 503 }).mouseup(function (event) {
72874569 504 $(this).data('down', false);
581aee24 505 }).mousemove(function (event) {
72874569 506 if( timer != null ) { clearTimeout(timer); }
507 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
508 var p = svg_root.createSVGPoint();
509 p.x = event.clientX;
510 p.y = event.clientY;
511 p = p.matrixTransform(stateTf);
512 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
513 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
aff524fc 514 svg_root_element.setAttribute("transform", s);
581aee24 515 }
aff524fc 516 event.returnValue = false;
517 event.preventDefault();
581aee24 518 }).mousewheel(function (event, delta) {
72874569 519 event.returnValue = false;
520 event.preventDefault();
521 if ( $('#update_workspace_button').data('locked') == false ) {
aff524fc 522 if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
523 if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
72874569 524 if( delta < -9 ) { delta = -9 };
525 var z = 1 + delta/10;
aff524fc 526 z = delta > 0 ? 1 : -1;
527 var g = svg_root_element;
528 if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) {
72874569 529 var root = svg_root;
530 var p = root.createSVGPoint();
aff524fc 531 p.x = event.originalEvent.clientX;
532 p.y = event.originalEvent.clientY;
72874569 533 p = p.matrixTransform(g.getCTM().inverse());
aff524fc 534 var scaleLevel = 1+(z/20);
535 var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y);
72874569 536 var matrix = g.getCTM().multiply(k);
537 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
538 g.setAttribute("transform", s);
539 }
540 }
581aee24 541 }).css({
542 'overflow' : 'hidden',
543 'cursor' : '-moz-grab'
544 });
545
a8928d1d 546 var rdgpath = getTextURL( 'readings' );
547 $.getJSON( rdgpath, function( data ) {
548 readingdata = data;
549 });
581aee24 550
551 $( "#dialog-form" ).dialog({
552 autoOpen: false,
553 height: 270,
554 width: 290,
555 modal: true,
556 buttons: {
557 "Ok": function() {
558 $('#status').empty();
aff524fc 559 form_values = $('#collapse_node_form').serialize();
a8928d1d 560 ncpath = getTextURL( 'relationships' );
72874569 561 $(':button :contains("Ok")').attr("disabled", true);
581aee24 562 var jqjson = $.post( ncpath, form_values, function(data) {
563 $.each( data, function(item, source_target) {
988c7619 564 var source_found = get_ellipse( source_target[0] );
565 var target_found = get_ellipse( source_target[1] );
566 if( source_found.size() && target_found.size() ) {
28f2ef34 567 var relation = relation_manager.create( source_target[0], source_target[1], $('#rel_type')[0].selectedIndex-1 );
988c7619 568 relation.data( 'type', $('#rel_type :selected').text() );
569 relation.data( 'scope', $('#scope :selected').text() );
570 relation.data( 'note', $('#note').val() );
28f2ef34 571 relation_manager.toggle_active( relation.attr('id') );
988c7619 572 }
581aee24 573 });
581aee24 574 $( "#dialog-form" ).dialog( "close" );
72874569 575 }, 'json' );
581aee24 576 },
577 Cancel: function() {
581aee24 578 $( this ).dialog( "close" );
579 }
580 },
581 create: function(event, ui) {
582 $(this).data( 'relation_drawn', false );
583 //TODO? Err handling?
72874569 584 var basepath = getRelativePath();
585 var jqjson = $.getJSON( basepath + '/definitions', function(data) {
581aee24 586 var types = data.types.sort();
587 $.each( types, function(index, value) {
aff524fc 588 $('#rel_type').append( $('<option />').attr( "value", value ).text(value) );
581aee24 589 $('#keymaplist').append( $('<li>').css( "border-color", relation_manager.relation_colors[index] ).text(value) );
590 });
591 var scopes = data.scopes;
592 $.each( scopes, function(index, value) {
aff524fc 593 $('#scope').append( $('<option />').attr( "value", value ).text(value) );
581aee24 594 });
595 });
596 },
597 open: function() {
598 relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
72874569 599 $(".ui-widget-overlay").css("background", "none");
600 $("#dialog_overlay").show();
601 $("#dialog_overlay").height( $("#enlargement_container").height() );
63213207 602 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
72874569 603 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
581aee24 604 },
605 close: function() {
72874569 606 relation_manager.remove_temporary();
581aee24 607 $( '#status' ).empty();
608 $("#dialog_overlay").hide();
609 }
610 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
72874569 611 if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) {
612 var errobj = jQuery.parseJSON( jqXHR.responseText );
613 $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
581aee24 614 }
615 } );
616
72874569 617 $( "#delete-form" ).dialog({
618 autoOpen: false,
31aaf446 619 height: 135,
72874569 620 width: 160,
621 modal: false,
622 buttons: {
623 Cancel: function() {
624 $( this ).dialog( "close" );
625 },
626 Delete: function() {
a8928d1d 627 form_values = $('#delete_relation_form').serialize();
628 ncpath = getTextURL( 'relationships' );
72874569 629 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
630 $.each( data, function(item, source_target) {
28f2ef34 631 relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) );
72874569 632 });
633 $( "#delete-form" ).dialog( "close" );
634 }, dataType: 'json', type: 'DELETE' });
635 }
636 },
637 create: function(event, ui) {
638 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
639 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
640 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
641 dialog_aria.mouseenter( function() {
642 if( mouseWait != null ) { clearTimeout(mouseWait) };
643 })
644 dialog_aria.mouseleave( function() {
645 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
646 })
647 },
648 open: function() {
649 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
650 },
651 close: function() {
652 }
653 });
654
aff524fc 655 // function for reading form dialog should go here; for now hide the element
28f2ef34 656 $('#reading-form').dialog({
657 autoOpen: false,
658 height: 400,
659 width: 200,
660 modal: true,
661 buttons: {
662 Cancel: function() {
663 $( this ).dialog( "close" );
664 },
665 Update: function() {
666 form_values = $('#reading_data_form').serialize();
667 ncpath = getReadingURL();
668 var reading_element = get_node_obj( $('#reading_data_id').val() );
669 $(':button :contains("Update")').attr("disabled", true);
670 var jqjson = $.post( ncpath, form_values, function(data) {
671 $.each( data, function(key, value) {
672 reading_element.data( key, value );
673 });
674 $( "#reading-form" ).dialog( "close" );
675 });
676 }
677 },
678 create: function() {},
679 open: function() {},
680 close: function() {
681 $("#dialog_overlay").hide();
682 }
683 });
aff524fc 684
581aee24 685 $('#update_workspace_button').click( function() {
686 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
aff524fc 687 mouse_scale = svg_root_element.getScreenCTM().a;
72874569 688 if( $(this).data('locked') == true ) {
689 $('#svgenlargement ellipse' ).each( function( index ) {
690 if( $(this).data( 'node_obj' ) != null ) {
691 $(this).data( 'node_obj' ).ungreyout_edges();
692 $(this).data( 'node_obj' ).set_draggable( false );
693 var node_id = $(this).data( 'node_obj' ).get_id();
694 toggle_relation_active( node_id );
695 $(this).data( 'node_obj', null );
696 }
581aee24 697 })
581aee24 698 $(this).data('locked', false);
72874569 699 $(this).css('background-position', '0px 44px');
581aee24 700 } else {
72874569 701 var left = $('#enlargement').offset().left;
702 var right = left + $('#enlargement').width();
aff524fc 703 var tf = svg_root_element.getScreenCTM().inverse();
72874569 704 var p = svg_root.createSVGPoint();
705 p.x=left;
706 p.y=100;
707 var cx_min = p.matrixTransform(tf).x;
708 p.x=right;
709 var cx_max = p.matrixTransform(tf).x;
710 $('#svgenlargement ellipse').each( function( index ) {
711 var cx = parseInt( $(this).attr('cx') );
712 if( cx > cx_min && cx < cx_max) {
713 if( $(this).data( 'node_obj' ) == null ) {
714 $(this).data( 'node_obj', new node_obj( $(this) ) );
715 } else {
716 $(this).data( 'node_obj' ).set_draggable( true );
717 }
718 $(this).data( 'node_obj' ).greyout_edges();
719 var node_id = $(this).data( 'node_obj' ).get_id();
720 toggle_relation_active( node_id );
581aee24 721 }
72874569 722 });
723 $(this).css('background-position', '0px 0px');
581aee24 724 $(this).data('locked', true );
581aee24 725 }
726 });
727
9ca77245 728 $('.helptag').popupWindow({
729 height:500,
730 width:800,
731 top:50,
732 left:50,
733 scrollbars:1
734 });
735
736
72874569 737 function toggle_relation_active( node_id ) {
738 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
739 matchid = new RegExp( "^" + node_id );
740 if( $(this).text().match( matchid ) != null ) {
28f2ef34 741 var relation_id = $(this).parent().attr('id');
742 relation_manager.toggle_active( relation_id );
72874569 743 };
744 });
581aee24 745 }
581aee24 746
aff524fc 747 expandFillPageClients();
748 $(window).resize(function() {
749 expandFillPageClients();
750 });
751
72874569 752});
581aee24 753
754
aff524fc 755function expandFillPageClients() {
756 $('.fillPage').each(function () {
757 $(this).height($(window).height() - $(this).offset().top - MARGIN);
758 });
759}
760
761function loadSVG(svgData) {
762 var svgElement = $('#svgenlargement');
763
764 $(svgElement).svg('destroy');
765
766 $(svgElement).svg({
767 loadURL: svgData,
768 onLoad : svgEnlargementLoaded
769 });
770}
771
772
773/* OS Gadget stuff
774
775function svg_select_callback(topic, data, subscriberData) {
776 svgData = data;
777 loadSVG(svgData);
778}
779
780function loaded() {
781 var prefs = new gadgets.Prefs();
782 var preferredHeight = parseInt(prefs.getString('height'));
783 if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight);
784 expandFillPageClients();
785}
786
787if (gadgets.util.hasFeature('pubsub-2')) {
788 gadgets.HubSettings.onConnect = function(hum, suc, err) {
789 subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback);
790 loaded();
791 };
792}
793else gadgets.util.registerOnLoadHandler(loaded);
794*/