##// END OF EJS Templates
Make SVGs resizable in the NB
Jonathan Frederic -
Show More
@@ -570,19 +570,45 b' var IPython = (function (IPython) {'
570 };
570 };
571
571
572
572
573 var append_svg = function (svg, md, element) {
573 var append_svg = function (svg_html, md, element) {
574 var type = 'image/svg+xml';
574 var type = 'image/svg+xml';
575 var toinsert = this.create_output_subarea(md, "output_svg", type);
575 var toinsert = this.create_output_subarea(md, "output_svg", type);
576 toinsert.append(svg);
576
577 // Get the svg element from within the HTML.
578 var svg = $('<div />').html(svg_html).find('svg');
579 var svg_area = $('<div />');
580 var width = svg.attr('width');
581 var height = svg.attr('height');
582 svg
583 .width('100%')
584 .height('100%');
585 svg_area
586 .width(width)
587 .height(height);
588
589 // The jQuery resize handlers don't seem to work on the svg element.
590 // When the svg renders completely, measure it's size and set the parent
591 // div to that size. Then set the svg to 100% the size of the parent
592 // div and make the parent div resizable.
593 this._dblclick_to_reset_size(svg_area, true, false);
594
595 svg_area.append(svg);
596 toinsert.append(svg_area);
577 element.append(toinsert);
597 element.append(toinsert);
598
578 return toinsert;
599 return toinsert;
579 };
600 };
580
601
581
602 OutputArea.prototype._dblclick_to_reset_size = function (img, immediately, resize_parent) {
582 OutputArea.prototype._dblclick_to_reset_size = function (img) {
603 // Add a resize handler to an element
583 // wrap image after it's loaded on the page,
604 //
584 // otherwise the measured initial size will be incorrect
605 // img: jQuery element
585 img.on("load", function (){
606 // immediately: bool=False
607 // Wait for the element to load before creating the handle.
608 // resize_parent: bool=True
609 // Should the parent of the element be resized when the element is
610 // reset (by double click).
611 var callback = function (){
586 var h0 = img.height();
612 var h0 = img.height();
587 var w0 = img.width();
613 var w0 = img.width();
588 if (!(h0 && w0)) {
614 if (!(h0 && w0)) {
@@ -595,12 +621,20 b' var IPython = (function (IPython) {'
595 });
621 });
596 img.dblclick(function () {
622 img.dblclick(function () {
597 // resize wrapper & image together for some reason:
623 // resize wrapper & image together for some reason:
598 img.parent().height(h0);
599 img.height(h0);
624 img.height(h0);
600 img.parent().width(w0);
601 img.width(w0);
625 img.width(w0);
626 if (resize_parent === undefined || resize_parent) {
627 img.parent().height(h0);
628 img.parent().width(w0);
629 }
602 });
630 });
603 });
631 };
632
633 if (immediately) {
634 callback();
635 } else {
636 img.on("load", callback);
637 }
604 };
638 };
605
639
606 var set_width_height = function (img, md, mime) {
640 var set_width_height = function (img, md, mime) {
General Comments 0
You need to be logged in to leave comments. Login now