pager.js
187 lines
| 6.4 KiB
| application/javascript
|
JavascriptLexer
MinRK
|
r16586 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r4357 | |||
Jonathan Frederic
|
r17196 | define([ | ||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17215 | 'jqueryui', | ||
Jonathan Frederic
|
r17197 | 'base/js/utils', | ||
Jonathan Frederic
|
r17198 | ], function(IPython, $, utils) { | ||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
Brian E. Granger
|
r4357 | |||
jon
|
r17210 | var Pager = function (pager_selector, pager_splitter_selector, options) { | ||
jon
|
r17211 | // Constructor | ||
// | ||||
// Parameters: | ||||
// pager_selector: string | ||||
// pager_splitter_selector: string | ||||
// options: dictionary | ||||
// Dictionary of keyword arguments. | ||||
// events: $(Events) instance | ||||
// layout_manager: LayoutManager instance | ||||
jon
|
r17210 | this.events = options.events; | ||
Brian E. Granger
|
r4357 | this.pager_element = $(pager_selector); | ||
Matthias BUSSONNIER
|
r8265 | this.pager_button_area = $('#pager_button_area'); | ||
Matthias BUSSONNIER
|
r6723 | var that = this; | ||
Brian E. Granger
|
r4372 | this.percentage_height = 0.40; | ||
jon
|
r17210 | options.layout_manager.pager = this; | ||
Matthias BUSSONNIER
|
r6723 | this.pager_splitter_element = $(pager_splitter_selector) | ||
.draggable({ | ||||
containment: 'window', | ||||
axis:'y', | ||||
helper: null , | ||||
Matthias BUSSONNIER
|
r6739 | drag: function(event, ui) { | ||
Matthias BUSSONNIER
|
r6723 | // recalculate the amount of space the pager should take | ||
MinRK
|
r10908 | var pheight = ($(document.body).height()-event.clientY-4); | ||
jon
|
r17210 | var downprct = pheight/options.layout_manager.app_height(); | ||
Matthias BUSSONNIER
|
r6739 | downprct = Math.min(0.9, downprct); | ||
if (downprct < 0.1) { | ||||
Matthias BUSSONNIER
|
r6723 | that.percentage_height = 0.1; | ||
that.collapse({'duration':0}); | ||||
Matthias BUSSONNIER
|
r6739 | } else if (downprct > 0.2) { | ||
Matthias BUSSONNIER
|
r6723 | that.percentage_height = downprct; | ||
that.expand({'duration':0}); | ||||
} | ||||
jon
|
r17210 | options.layout_manager.do_resize(); | ||
Matthias BUSSONNIER
|
r6723 | } | ||
}); | ||||
this.expanded = false; | ||||
Brian E. Granger
|
r4357 | this.style(); | ||
Matthias BUSSONNIER
|
r8265 | this.create_button_area(); | ||
Brian E. Granger
|
r4357 | this.bind_events(); | ||
}; | ||||
Matthias BUSSONNIER
|
r8265 | Pager.prototype.create_button_area = function(){ | ||
var that = this; | ||||
this.pager_button_area.append( | ||||
$('<a>').attr('role', "button") | ||||
Erik Tollerud
|
r10469 | .attr('title',"Open the pager in an external window") | ||
Matthias BUSSONNIER
|
r8265 | .addClass('ui-button') | ||
Jonathan Frederic
|
r17196 | .click(function(){that.detach();}) | ||
Erik Tollerud
|
r10469 | .attr('style','position: absolute; right: 20px;') | ||
Matthias BUSSONNIER
|
r8265 | .append( | ||
Bussonnier Matthias
|
r8449 | $('<span>').addClass("ui-icon ui-icon-extlink") | ||
Matthias BUSSONNIER
|
r8265 | ) | ||
Jonathan Frederic
|
r17196 | ); | ||
Erik Tollerud
|
r10469 | this.pager_button_area.append( | ||
$('<a>').attr('role', "button") | ||||
Erik Tollerud
|
r10474 | .attr('title',"Close the pager") | ||
Erik Tollerud
|
r10469 | .addClass('ui-button') | ||
Jonathan Frederic
|
r17196 | .click(function(){that.collapse();}) | ||
Erik Tollerud
|
r10469 | .attr('style','position: absolute; right: 5px;') | ||
.append( | ||||
$('<span>').addClass("ui-icon ui-icon-close") | ||||
) | ||||
Jonathan Frederic
|
r17196 | ); | ||
Matthias BUSSONNIER
|
r8265 | }; | ||
Brian E. Granger
|
r4357 | Pager.prototype.style = function () { | ||
Brian E. Granger
|
r4363 | this.pager_splitter_element.addClass('border-box-sizing ui-widget ui-state-default'); | ||
MinRK
|
r10914 | this.pager_element.addClass('border-box-sizing'); | ||
this.pager_element.find(".container").addClass('border-box-sizing'); | ||||
Matthias BUSSONNIER
|
r6723 | this.pager_splitter_element.attr('title', 'Click to Show/Hide pager area, drag to Resize'); | ||
Brian E. Granger
|
r4357 | }; | ||
Pager.prototype.bind_events = function () { | ||||
var that = this; | ||||
Brian E. Granger
|
r4361 | |||
Matthias BUSSONNIER
|
r6739 | this.pager_element.bind('collapse_pager', function (event, extrap) { | ||
MinRK
|
r16586 | var time = 'fast'; | ||
if (extrap && extrap.duration) { | ||||
time = extrap.duration; | ||||
} | ||||
Matthias BUSSONNIER
|
r6723 | that.pager_element.hide(time); | ||
Brian E. Granger
|
r4361 | }); | ||
Matthias BUSSONNIER
|
r6739 | this.pager_element.bind('expand_pager', function (event, extrap) { | ||
MinRK
|
r16586 | var time = 'fast'; | ||
if (extrap && extrap.duration) { | ||||
time = extrap.duration; | ||||
} | ||||
Matthias BUSSONNIER
|
r6723 | that.pager_element.show(time); | ||
Brian E. Granger
|
r4357 | }); | ||
Brian E. Granger
|
r4363 | this.pager_splitter_element.hover( | ||
Brian E. Granger
|
r4357 | function () { | ||
Brian E. Granger
|
r4363 | that.pager_splitter_element.addClass('ui-state-hover'); | ||
Brian E. Granger
|
r4357 | }, | ||
function () { | ||||
Brian E. Granger
|
r4363 | that.pager_splitter_element.removeClass('ui-state-hover'); | ||
Brian E. Granger
|
r4357 | } | ||
); | ||||
Brian E. Granger
|
r4361 | |||
Brian E. Granger
|
r4363 | this.pager_splitter_element.click(function () { | ||
Brian E. Granger
|
r4361 | that.toggle(); | ||
}); | ||||
Brian E. Granger
|
r4363 | |||
Jonathan Frederic
|
r17196 | this.events.on('open_with_text.Pager', function (event, payload) { | ||
MinRK
|
r16586 | // FIXME: support other mime types | ||
if (payload.data['text/plain'] && payload.data['text/plain'] !== "") { | ||||
Brian Granger
|
r7168 | that.clear(); | ||
that.expand(); | ||||
MinRK
|
r16586 | that.append_text(payload.data['text/plain']); | ||
} | ||||
Brian Granger
|
r7168 | }); | ||
Brian E. Granger
|
r4357 | }; | ||
Matthias BUSSONNIER
|
r6723 | Pager.prototype.collapse = function (extrap) { | ||
Brian E. Granger
|
r4361 | if (this.expanded === true) { | ||
this.expanded = false; | ||||
Matthias BUSSONNIER
|
r6739 | this.pager_element.add($('div#notebook')).trigger('collapse_pager', extrap); | ||
MinRK
|
r16586 | } | ||
Brian E. Granger
|
r4357 | }; | ||
Matthias BUSSONNIER
|
r6723 | Pager.prototype.expand = function (extrap) { | ||
Brian E. Granger
|
r4361 | if (this.expanded !== true) { | ||
this.expanded = true; | ||||
Matthias BUSSONNIER
|
r6739 | this.pager_element.add($('div#notebook')).trigger('expand_pager', extrap); | ||
MinRK
|
r16586 | } | ||
Brian E. Granger
|
r4361 | }; | ||
Pager.prototype.toggle = function () { | ||||
if (this.expanded === true) { | ||||
this.collapse(); | ||||
} else { | ||||
this.expand(); | ||||
MinRK
|
r16586 | } | ||
Brian E. Granger
|
r4357 | }; | ||
Pager.prototype.clear = function (text) { | ||||
MinRK
|
r10914 | this.pager_element.find(".container").empty(); | ||
Brian E. Granger
|
r4357 | }; | ||
Matthias BUSSONNIER
|
r8265 | Pager.prototype.detach = function(){ | ||
MinRK
|
r11542 | var w = window.open("","_blank"); | ||
Matthias BUSSONNIER
|
r8265 | $(w.document.head) | ||
.append( | ||||
$('<link>') | ||||
.attr('rel',"stylesheet") | ||||
.attr('href',"/static/css/notebook.css") | ||||
.attr('type',"text/css") | ||||
Bussonnier Matthias
|
r8449 | ) | ||
.append( | ||||
$('<title>').text("IPython Pager") | ||||
Matthias BUSSONNIER
|
r8265 | ); | ||
MinRK
|
r11542 | var pager_body = $(w.document.body); | ||
pager_body.css('overflow','scroll'); | ||||
Matthias BUSSONNIER
|
r8265 | |||
MinRK
|
r11542 | pager_body.append(this.pager_element.clone().children()); | ||
Matthias BUSSONNIER
|
r8265 | w.document.close(); | ||
this.collapse(); | ||||
MinRK
|
r16586 | }; | ||
Brian E. Granger
|
r4357 | |||
Pager.prototype.append_text = function (text) { | ||||
Jonathan Frederic
|
r15406 | // The only user content injected with this HTML call is escaped by | ||
Jonathan Frederic
|
r15376 | // the fixConsole() method. | ||
Jonathan Frederic
|
r17198 | this.pager_element.find(".container").append($('<pre/>').html(utils.fixCarriageReturn(utils.fixConsole(text)))); | ||
Matthias BUSSONNIER
|
r6723 | }; | ||
Brian E. Granger
|
r4357 | |||
Jonathan Frederic
|
r17196 | // Backwards compatability. | ||
Brian E. Granger
|
r4357 | IPython.Pager = Pager; | ||
Jonathan Frederic
|
r17201 | return {'Pager': Pager}; | ||
Jonathan Frederic
|
r17196 | }); | ||