Skip to content
tenprint.html 29.2 KiB
Newer Older
<!DOCTYPE html>
<html>
    <head>
        {% for src in js %}
            <script type="text/javascript" src="{{ src }}"></script>
        {% endfor %}
        {% for src in css %}
            <link type="text/css" rel="stylesheet" href="{{ src }}">
        {% endfor %}
        
        <script type="text/javascript" src="{{ url_for( 'files.send_app_files', subpath = 'functions.js' ) }}"></script>
        <link type="text/css" rel="stylesheet" href="{{ url_for( 'files.send_app_files', subpath = 'app.css' ) }}">
        
        <style type="text/css">
            .icnml_button {
                margin-top: 50px;
            }
            .icnml_button > div > a {
                width: 60%;
                margin-bottom: 10px;
            }
        </style>
        
        <script type="text/javascript">
            baseurl = "{{ baseurl }}";
            submission_id = "{{ submission_id }}";
            file_id = "{{ file[ 'uuid' ] }}";
            
            var zones_array = new Array();
            {% for zone in zones %}
                zones_array.push( {{ zone[ 'fpc' ] }} );
            {% endfor %}
            
            var zonesData = {};
            {% for zone in zones %}
                zonesData[ "{{ zone[ 'fpc' ] }}" ] = {
                    "fpc": {{ zone[ 'fpc' ] }},
                    "x": {{ zone[ 'x' ] }},
                    "y": {{ zone[ 'y' ] }},
                    "width": {{ zone[ 'width' ] }},
                    "height": {{ zone[ 'height' ] }}
                };
            {% endfor %}
            
            var update_quality_db = function()
            {
                var quality = $( "#file_{{ file[ 'uuid' ] }}_quality > select > option:checked" ).val();
                
                $.ajax( {
                    url: "{{ url_for( 'submission.submission_tenprint_set_quality', submission_id = submission_id, tenprint_id = file[ 'uuid' ] ) }}",
                    dataType: "json",
                    method: "POST",
                    data: {
                        quality: quality
                    },
                    success: function( data )
                    {
                        if( data.error )
                            toastr.error( "Error while saving the quality value" );
                    },
                    error: function( data )
                    {
                        toastr.error( "Network error" );
                    }
                } );
            }
            
            var update_coordinates_informations = function()
            {
                svg_height = $( window ).height() - 100;
                svg_width = svg_height * {{ svg_hw_factor }};
                
                card_img_info = {
                    "right": svg_width * ( {{ card_info[ 'width_cm' ] }} * {{ img_info[ 'resolution' ] }} / 2.54 ) / ( {{ img_info[ 'width' ] }} ),
                    "bottom": svg_height * ( {{ card_info[ 'height_cm' ] }} * {{ img_info[ 'resolution' ] }} / 2.54 ) / ( {{ img_info[ 'height' ] }} ),
                }
                dpc = card_img_info[ "right" ] / {{ card_info[ 'width_cm' ] }};
            }
            
            var start_segmentation = function()
            {
                $( "#segmentation_button_span_text" ).text( "Segmenting..." );
                $( "#segmentation_button" ).append(
                    $( "<span />" )
                        .attr( "id", "segmentation_button_span_spinner" )
                        .attr( "class", "ld ld-ring ld-cycle" )
                        .css( "position", "absolute" )
                        .css( "right", "20px" )
                )
                
                $( "#segmentation_button" ).off( "click" );
                
                $.ajax( {
                    url: "{{ url_for( 'image.image_tenprint_segmentation', tenprint_id = file[ 'uuid' ] ) }}",
                    dataType: "json",
                    success: function( data )
                    {
                        $( "#segmentation_button_span_text" ).text( "Segementation updated" );
                        $( "#segmentation_button_span_spinner" ).remove();
                    },
                    error: function( data )
                    {
                        $( "#segmentation_button_span_text" ).text( "Error while segmenting..." );
                        $( "#segmentation_button_span_spinner" ).remove();
                    }
                } );
            }
            
            var updateSVG = function()
            {
                $( "#template_svg_div" )
                    .css( "width", svg_width )
                    .css( "height", svg_height );
                
                $( ".icnml_central" )
                    .css( "width", svg_width * 2 )
                    .css( "height", svg_height );
                
                $( "#zoomed_image" ).attr( "width", svg_width );
                $( "#zoomed_image" ).attr( "height", svg_height );
                
                $( "#tp_polygones" ).empty();
                
                for( var i = 0; i < zones_array.length; i++ )
                {
                    var fpc = zones_array[ i ];
                    var zone = zonesData[ fpc ];
                    var x = zone[ "x" ] * svg_width;
                    var y = zone[ "y" ] * svg_height;
                    var height = zone[ "height" ] * svg_height;
                    var width = zone[ "width" ] * svg_width;
                    $( "#tp_polygones" )
                        .append( segment_svg( fpc, x, y, height, width ) );
                }
            }
            
            var delete_tenprint = function()
            {
                $( "<div />" )
                    .attr( "id", "delete_confirmation" )
                    .text( "Do you really want to delete this tenprint card?" )
                    .dialog( {
                        title: "Confirm tenprint deletion",
                        modal: true,
                        buttons: {
                            "OK": function()
                            {
                                $.ajax( {
                                    url: "{{ url_for( 'submission.submission_tenprint_delete', submission_id = submission_id, tenprint_id = file[ 'uuid' ] ) }}",
                                    dataType: "json",
                                    success: function( data )
                                    {
                                        $( "#delete_confirmation" ).remove();
                                        window.location = "{{ url_for( 'submission.submission_tenprint_list', submission_id = submission_id ) }}";
                                    },
                                    error: function( data )
                                    {
                                        $( "#delete_confirmation" ).remove();
                                    }
                                } );
                            },
                            "Cancel": function()
                            {
                                $( this ).dialog( "close" );
                            },
                        },
                        close: function()
                        {
                            $( this ).remove();
                        }
                    } );
            }
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
            
            var update_zone_db = function( fpc, x, y, height, width )
            {
                var img = $( "#template_svg > image" );
                
                $.ajax( {
                    url: "{{ url_for( 'submission.submission_segment_setcoordinates', submission_id = submission_id, tenprint_id = file[ 'uuid' ] ) }}",
                    dataType: "json",
                    method: "POST",
                    data: {
                        fpc: fpc,
                        x: x,
                        y: y,
                        w: width,
                        h: height,
                        img_height: img.height(),
                        img_width: img.width()
                    },
                    success: function( data )
                    {
                    },
                    error: function( data )
                    {
                        toastr.error( "Segment data not saved", "Network error" );
                    }
                } );
            }
            
            var delete_zone_db = function()
            {
                $.ajax( {
                    url: "{{ url_for( 'submission.submission_segment_deletecoordinates', submission_id = submission_id, tenprint_id = file[ 'uuid' ] ) }}",
                    dataType: "json",
                    success: function( data )
                    {
                        current_fpc = 1;
                        $( "#tp_polygones > g" ).remove();
                    },
                    error: function( data )
                    {
                        toastr.error( "Segment data not deleted", "Network error" );
                    }
                } );
            }
            
            var start_click_pos = null;
            var current_action = null;
            var tmp_rect = null;
            var move_text = null;
            var svg_mouse_on = false;
            var current_fpc = zones_array.length + 1;
            
            var svg_mouseenter = function( event )
            {
                svg_mouse_on = true;
            }
            var svg_mouseleave = function( event )
            {
                svg_mouse_on = false;
                
                move_text = null;
                $( "#move_text" )
                    .attr( "x", 0 )
                    .attr( "y", 0 )
                    .text( "" )
            }
            
            var start_action = function( event )
            {
                event.preventDefault();
                
                start_click_pos = {
                    x: event.offsetX,
                    y: event.offsetY
                }
                
                switch( event.target.nodeName )
                {
                    case "image":
                        start_add_segment( event );
                        break;
                    
                    case "polygon":
                    case "rect":
                        if( event.button == 0 )
                        {
                            if( dist_to_br_corner( event ) < 30 )
                                start_resize_segment( event );
                            else
                                start_move_segment( event );
                        
                        } else {
                            $( event.target ).parent().remove();
                        }
                            
                        break;
                    
                    default:
                        console.log( "no action" );
                        break;
                }
                
                return false;
            }
            var end_action = function( event )
            {
                event.preventDefault();
                
                start_click_pos = null;
                
                switch( current_action )
                {
                    case "add_segment":
                        end_add_segment( event );
                        break;
                    
                    case "move_segment":
                        end_move_segment( event );
                        break;
                    
                    case "resize_segment":
                        end_resize_segment( event );
                        break;
                    
                    default:
                        console.log( "no action" );
                        break;
                }
                
                return false;
            }
            var move_action = function( event )
            {
                switch( current_action )
                {
                    case "move_segment":
                        move_move_segment( event );
                        break;
                    
                    case "add_segment":
                        move_add_segment( event );
                        break;
                    
                    case "resize_segment":
                        move_resize_segment( event );
                        break;
                    
                    case null:
                        move_hover_rect( event );
                        break;
                    
                    default:
                        break;
                }
            }
            
            var click_and_drag_value = {
                startx: 0,
                starty: 0,
                endx: 0,
                endy: 0
            };
            var start_add_segment = function( event )
            {
                current_action = "add_segment";
                
                click_and_drag_value = {
                    startx: 0,
                    starty: 0,
                    endx: 0,
                    endy: 0
                };
                
                click_and_drag_value.startx = event.offsetX;
                click_and_drag_value.starty = event.offsetY;
                
                $( SVG( "rect" ) )
                    .attr( "id", "tmp_rect" )
                    .attr( "x", click_and_drag_value.startx )
                    .attr( "y", click_and_drag_value.starty )
                    .attr( "height", 0 )
                    .attr( "width", 0 )
                    .attr( "fill", "rgb( 255, 0, 0 )" )
                    .attr( "fill-opacity", "0.1" )
                    .attr( "stroke", "red" )
                    .attr( "stroke-width", "1" )
                    .appendTo( $( "#tp_polygones" ) );
            }
            var move_add_segment = function( evnet )
            {
                diff_x = event.offsetX - start_click_pos.x;
                diff_y = event.offsetY - start_click_pos.y;
                
                var a = Math.min( event.offsetX, start_click_pos.x );
                var b = Math.min( event.offsetY, start_click_pos.y );
                
                $( "#tmp_rect" )
                    .attr( "x", a )
                    .attr( "y", b )
                    .attr( "width", Math.abs( diff_x ) )
                    .attr( "height", Math.abs( diff_y ) )
            }
            var end_add_segment = function( event )
            {
                click_and_drag_value.endx = event.offsetX;
                click_and_drag_value.endy = event.offsetY;
                
                var x = Math.min( click_and_drag_value.startx, click_and_drag_value.endx );
                var y = Math.min( click_and_drag_value.starty, click_and_drag_value.endy );
                var width = click_and_drag_value.endx - click_and_drag_value.startx;
                var height = click_and_drag_value.endy - click_and_drag_value.starty;
                
                height = Math.abs( height );
                width = Math.abs( width );
                
                $( "#tmp_rect" ).remove();
                
                if( height > 10 && width > 10 )
                {
                    $( "#tp_polygones" )
                        .append( segment_svg( current_fpc, x, y, height, width ) );
                    
                    update_zone_db( current_fpc, x, y, height, width );
                    
                    current_fpc += 1;
                }
                
                click_and_drag_value = {
                    startx: 0,
                    starty: 0,
                    endx: 0,
                    endy: 0
                };
                
                current_action = null;
            }
            
            var move_target = null;
            var move_target_initial_pos = {};
            var start_move_segment = function( event )
            {
                current_action = "move_segment";
                
                move_target = event.target;
                move_target_initial_pos = {
                    x: parseFloat( $( move_target ).attr( "x" ) ),
                    y: parseFloat( $( move_target ).attr( "y" ) )
                }
                
                click_and_drag_value.startx = event.offsetX;
                click_and_drag_value.starty = event.offsetY;
            }
            var move_move_segment = function( event )
            {
                diff_x = event.offsetX - start_click_pos.x;
                diff_y = event.offsetY - start_click_pos.y;
                
                $( move_target ).attr( "x", move_target_initial_pos.x + diff_x );
                $( move_target ).attr( "y", move_target_initial_pos.y + diff_y );
                
                $( move_target ).parent().children( "text" ).attr( "x", move_target_initial_pos.x + diff_x );
                $( move_target ).parent().children( "text" ).attr( "y", move_target_initial_pos.y + diff_y );
            }
            var end_move_segment = function( event )
            {
                var x = $( move_target ).attr( "x" );
                var y = $( move_target ).attr( "y" );
                var w = $( move_target ).attr( "width" );
                var h = $( move_target ).attr( "height" );
                x = parseFloat( x );
                y = parseFloat( y );
                w = parseFloat( w );
                h = parseFloat( h );
                
                var target_id = $( move_target ).attr( "id" );
                target_id = target_id.replace( "segment_fpc_", "" );
                
                update_zone_db( target_id, x, y, h, w );
                
                click_and_drag_value = {
                    startx: 0,
                    starty: 0,
                    endx: 0,
                    endy: 0
                };
                move_target = null;
                move_target_initial_pos = {};
                
                current_action = null;
            }
            
            var move_hover_rect = function( event )
            {
                if( event.target.nodeName === "rect" )
                {
                    if( ( dist_to_br_corner( event ) ) <= 30 )
                    {
                        $( event.target ).css( "cursor", "nwse-resize" );
                    } else {
                        $( event.target ).css( "cursor", "move" );
                    }
                }
            }
            
            var resize_target = null;
            var resize_target_initial_size = {};
            var start_resize_segment = function( event )
            {
                current_action = "resize_segment";
                
                resize_target = event.target;
                resize_target_initial_size = {
                    width: parseFloat( $( resize_target ).attr( "width" ) ),
                    height: parseFloat( $( resize_target ).attr( "height" ) )
                }
            }
            var move_resize_segment = function( event )
            {
                diff_x = event.offsetX - start_click_pos.x;
                diff_y = event.offsetY - start_click_pos.y;
                
                $( resize_target ).attr( "width", resize_target_initial_size.width + diff_x );
                $( resize_target ).attr( "height", resize_target_initial_size.height + diff_y );
            }
            var end_resize_segment = function( event )
            {
                var x = $( resize_target ).attr( "x" );
                var y = $( resize_target ).attr( "y" );
                var w = $( resize_target ).attr( "width" );
                var h = $( resize_target ).attr( "height" );
                x = parseFloat( x );
                y = parseFloat( y );
                w = parseFloat( w );
                h = parseFloat( h );
                
                var target_id = $( resize_target ).attr( "id" );
                target_id = target_id.replace( "segment_fpc_", "" );
                
                update_zone_db( target_id, x, y, h, w );
                
                click_and_drag_value = {
                    startx: 0,
                    starty: 0,
                    endx: 0,
                    endy: 0
                };
                resize_target = null;
                resize_target_initial_size = null;
                current_action = null;
            }
            
            var dist_to_br_corner = function( event )
            {
                var targetx = $( event.target ).attr( "x" );
                var targety = $( event.target ).attr( "y" );
                var targetwidth = $( event.target ).attr( "width" );
                var targetheight = $( event.target ).attr( "height" );
                
                targetx = parseFloat( targetx );
                targety = parseFloat( targety );
                targetwidth = parseFloat( targetwidth );
                targetheight = parseFloat( targetheight );
                
                var dx = ( targetx + targetwidth ) - event.offsetX;
                var dy = ( targety + targetheight ) - event.offsetY;
                
                return Math.sqrt( dx * dx + dy * dy );
            }
            
            var segment_svg = function( fpc, x, y, height, width )
            {
                var svggroup = SVG( "g" );
                
                $( SVG( "rect" ) )
                    .attr( "id", "segment_fpc_" + fpc )
                    .attr( "x", x )
                    .attr( "y", y )
                    .attr( "height", height )
                    .attr( "width", width )
                    .attr( "fill", "rgb( 255, 0, 0 )" )
                    .attr( "fill-opacity", "0.1" )
                    .attr( "stroke", "red" )
                    .attr( "stroke-width", "1" )
                    .appendTo( svggroup );
                
                $( SVG( "text" ) )
                    .attr( "x", x )
                    .attr( "y", y - 5 )
                    .attr( "fill", "rgb( 255, 0, 0 )" )
                    .text( "finger " + fpc )
                    .appendTo( svggroup );
                
                return svggroup;
            }
        </script>
    </head>
    <body class="icnml_main_layout">
        {% include "header.html" %}
        {% include navigation %}
        
        <div class="icnml_content">
            <div class="icnml_tp_oneperpage">
                <form id="icnml_file_update_{{ file[ 'uuid' ] }}">
                    <div class="ui-widget-header ui-corner-top icnml_box_top">File '<span id="header_filename">{{ file[ 'uuid' ] }}</span>'</div>
                    <div class="ui-widget-content ui-corner-bottom icnml_box_content">
                        <div class="icnml_tp_cardbox">
                            <div id="template_svg_div">
                                <svg id="template_svg" width="100%" height="100%">
                                    <image x="0" y="0" width="100%" height="100%" xlink:href="{{ url_for( 'image.image_file_serve', file_id = file[ 'uuid' ] ) }}"></image>
                                    <g id="tp_polygones"></g>
                                    <text id="move_text"></text>
                                </svg>
                            </div>
                        
                            <div>
                                <div class="icnml_box_fields">
                                    <div>Format</div>
                                    <div>{{ file[ 'format' ] }}</div>
                                    
                                    <div>Resolution</div>
                                    <div>{{ file[ 'resolution' ] }} dpi</div>
                                    
                                    <div>Width</div>
                                    <div>{{ file[ 'width' ] }} px</div>
                                    
                                    <div>Height</div>
                                    <div>{{ file[ 'height' ] }} px</div>
                                    
                                    <div>Size</div>
                                    <div>{{ file[ 'size' ] }} Mo</div>
                                    
                                    <div>Upload time</div>
                                    <div id="file_{{ file[ 'uuid' ] }}_uploadtime">-</div>
                                    
                                    <div>Quality</div>
                                    <div id="file_{{ file[ 'uuid' ] }}_quality">
                                        <select name="tenprint_quality_select">
                                            <option value="0" id="tenprint_quality_none_option" selected>None</option>
                                            {% for quality_type in quality_type %}
                                                <option value="{{ quality_type[ 'id' ] }}" id="tenprint_quality_{{ quality_type[ 'id' ] }}_option">
                                                    {{ quality_type[ 'name' ] }}
                                                </option>
                                            {% endfor %}
                                        </select>
                                    </div>
                                </div>
                                <div class="icnml_button">
                                    <div id="delete_segments_information_button_div">
                                        <a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="delete_segments_information_button" role="button" aria-disabled="false">
                                            <span class="ui-button-text" id="delete_segments_information_button_text">Delete segments informations</span>
                                        </a>
                                    </div>
                                    <div id="segmentation_button_div">
                                        <a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="segmentation_button" role="button" aria-disabled="false">
                                            <span class="ui-button-text" id="segmentation_button_span_text">Update segmentation</span>
                                        </a>
                                    </div>
                                    <div id="go_to_segmentation_button_div">
                                        <a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="go_to_segmentation_button" href="{{ url_for( 'submission.admin_submission_tenprint_segments_list', submission_id = submission_id, tenprint_id = file[ 'uuid' ] ) }}">
                                            <span class="ui-button-text" id="go_to_segmentation_button_span_text">Go to segments list</span>
                                        </a>
                                    </div>
                                    <div id="delete_button_div" style="margin-top: 30px;">
                                        <a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="delete_button">
                                            <span class="ui-button-text" id="delete_button_span_text">Delete tenprint card</span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        window.addEventListener( "resize", function( event )
        {
            update_coordinates_informations();
            updateSVG();
        } );
        
        update_coordinates_informations();
        updateSVG();
        
        $( "#delete_button" ).on( "click", delete_tenprint );
        $( "#delete_segments_information_button" ).on( "click", delete_zone_db );
        
        $( "#file_{{ file[ 'uuid' ] }}_quality > select" ).on( "change", update_quality_db );
        
        $( "#tenprint_quality_{{ file[ 'quality' ] }}_option" ).prop( "selected", true );
        
        $( "#file_{{ file[ 'uuid' ] }}_uploadtime" )
            .text( moment.utc( "{{ file[ 'creation_time' ] }}" ).local().format( "MMMM Do YYYY, HH:mm:ss" ) );
        
        $( "#segmentation_button" ).on( "click", start_segmentation );
        
        $( "#template_svg" ).on( "mousedown", start_action );
        $( "#template_svg" ).on( "mousemove", move_action );
        $( "#template_svg" ).on( "mouseup", end_action );
        $( "#template_svg" ).on( "contextmenu", function( event )
        {
            event.preventDefault();
            return false;
        } );
        $( "#template_svg" ).on( "mouseenter", svg_mouseenter );
        $( "#template_svg" ).on( "mouseleave", svg_mouseleave );
        
        $( "#icnml_navigation_updatedonor" )
            .addClass( "activated" );
        
        $( "#navloc" ).append(
            $( "<a />" )
                .attr( "href", "{{ url_for( 'submission.admin_submission_list' ) }}" )
                .text( "Submissions" )
        )
        .append(
            $( "<span />" ).text( ">" )
        )
        .append(
            $( "<a />" )
                .attr( "href", "{{ url_for( 'submission.admin_tenprint_list', submission_id = submission_id ) }}" )
                .text( "Tenprints" )
        )
        .append(
            $( "<span />" ).text( ">" )
        )
        .append(
            $( "<span />" ).text( "{{ file[ 'uuid' ] }}" )
        );
    </script>
    
</html>