@ -16,28 +16,92 @@ var IPython = (function (IPython) {
var OutputArea = function ( selector , prompt _area ) {
this . selector = selector ;
this . element = $ ( selector ) ;
this . wrapper = $ ( selector ) ;
this . outputs = [ ] ;
this . collapsed = false ;
this . scrolled = false ;
this . clear _out _timeout = null ;
if ( prompt _area === undefined ) {
this . prompt _area = true ;
} else {
this . prompt _area = prompt _area ;
} ;
this . create _elements ( ) ;
this . style ( ) ;
this . bind _events ( ) ;
} ;
OutputArea . prototype . create _elements = function ( ) {
this . element = $ ( "<div/>" ) ;
this . collapse _button = $ ( "<div/>" ) ;
this . prompt _overlay = $ ( "<div/>" ) ;
this . wrapper . append ( this . prompt _overlay ) ;
this . wrapper . append ( this . element ) ;
this . wrapper . append ( this . collapse _button ) ;
} ;
OutputArea . prototype . style = function ( ) {
this . collapse _button . hide ( ) ;
this . prompt _overlay . hide ( ) ;
this . wrapper . addClass ( 'output_wrapper' ) ;
this . element . addClass ( 'output vbox' ) ;
this . collapse _button . button ( ) ;
this . collapse _button . addClass ( 'output_collapsed vbox' ) ;
this . collapse _button . attr ( 'title' , 'click to expand outout' ) ;
this . collapse _button . html ( '. . .' ) ;
this . prompt _overlay . addClass ( 'out_prompt_overlay prompt' ) ;
this . prompt _overlay . attr ( 'title' , 'click to expand outout; double click to hide output' ) ;
this . collapse ( ) ;
} ;
OutputArea . prototype . _should _scroll = function ( lines ) {
if ( ! lines ) {
lines = 50 ;
}
// line-height from http://stackoverflow.com/questions/1185151
var fontSize = this . element . css ( 'font-size' ) ;
var lineHeight = Math . floor ( parseInt ( fontSize . replace ( 'px' , '' ) ) * 1.5 ) ;
return ( this . element . height ( ) > lines * lineHeight ) ;
} ;
OutputArea . prototype . bind _events = function ( ) {
var that = this ;
this . prompt _overlay . dblclick ( function ( ) { that . toggle _output ( ) ; } ) ;
this . prompt _overlay . click ( function ( ) { that . toggle _scroll ( ) ; } ) ;
this . element . resize ( function ( ) {
// maybe scroll output,
// if it's grown large enough and hasn't already been scrolled.
if ( ! that . scrolled && that . _should _scroll ( ) ) {
that . scroll _area ( ) ;
}
} ) ;
this . collapse _button . click ( function ( ) {
that . expand ( ) ;
} ) ;
this . collapse _button . hover ( function ( ) {
$ ( this ) . addClass ( "ui-state-hover" ) ;
} , function ( ) {
$ ( this ) . removeClass ( "ui-state-hover" ) ;
} ) ;
} ;
OutputArea . prototype . collapse = function ( ) {
if ( ! this . collapsed ) {
this . element . hide ( ) ;
this . prompt _overlay . hide ( ) ;
if ( this . element . html ( ) ) {
this . collapse _button . show ( ) ;
}
this . collapsed = true ;
} ;
} ;
@ -45,7 +109,9 @@ var IPython = (function (IPython) {
OutputArea . prototype . expand = function ( ) {
if ( this . collapsed ) {
this . collapse _button . hide ( ) ;
this . element . show ( ) ;
this . prompt _overlay . show ( ) ;
this . collapsed = false ;
} ;
} ;
@ -60,6 +126,38 @@ var IPython = (function (IPython) {
} ;
OutputArea . prototype . scroll _area = function ( ) {
this . element . addClass ( 'output_scroll' ) ;
this . prompt _overlay . attr ( 'title' , 'click to unscroll output; double click to hide' ) ;
this . scrolled = true ;
} ;
OutputArea . prototype . unscroll _area = function ( ) {
this . element . removeClass ( 'output_scroll' ) ;
this . prompt _overlay . attr ( 'title' , 'click to scroll output; double click to hide' ) ;
this . scrolled = false ;
} ;
OutputArea . prototype . scroll _if _long = function ( lines ) {
if ( this . _should _scroll ( lines ) ) {
// only allow scrolling long-enough output
this . scroll _area ( ) ;
} ;
} ;
OutputArea . prototype . toggle _scroll = function ( ) {
if ( this . scrolled ) {
this . unscroll _area ( ) ;
} else {
// only allow scrolling long-enough output
this . scroll _if _long ( 20 ) ;
} ;
} ;
// typeset with MathJax if MathJax is available
OutputArea . prototype . typeset = function ( ) {
if ( window . MathJax ) {
@ -132,6 +230,8 @@ var IPython = (function (IPython) {
this . append _stream ( json ) ;
} ;
this . outputs . push ( json ) ;
var that = this ;
setTimeout ( function ( ) { that . element . trigger ( 'resize' ) ; } , 100 ) ;
} ;
@ -346,6 +446,7 @@ var IPython = (function (IPython) {
// clear all, no need for logic
output _div . html ( "" ) ;
this . outputs = [ ] ;
this . unscroll _area ( ) ;
return ;
}
// remove html output
@ -360,7 +461,8 @@ var IPython = (function (IPython) {
if ( other ) {
output _div . find ( "div.output_subarea" ) . not ( "div.output_stderr" ) . not ( "div.output_stdout" ) . parent ( ) . remove ( ) ;
}
this . unscroll _area ( ) ;
// remove cleared outputs from JSON list:
for ( var i = this . outputs . length - 1 ; i >= 0 ; i -- ) {
var out = this . outputs [ i ] ;