Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'base/js/namespace',
6
'jqueryui',
7
'base/js/utils',
8
], function(IPython, $, utils) {
9
"use strict";
10
11
var Pager = function (pager_selector, options) {
12
/**
13
* Constructor
14
*
15
* Parameters:
16
* pager_selector: string
17
* options: dictionary
18
* Dictionary of keyword arguments.
19
* events: $(Events) instance
20
*/
21
this.events = options.events;
22
this.pager_element = $(pager_selector);
23
this.pager_button_area = $('#pager-button-area');
24
this._default_end_space = 100;
25
this.pager_element.resizable({handles: 'n', resize: $.proxy(this._resize, this)});
26
this.expanded = false;
27
this.create_button_area();
28
this.bind_events();
29
};
30
31
Pager.prototype.create_button_area = function(){
32
var that = this;
33
this.pager_button_area.append(
34
$('<a>').attr('role', "button")
35
.attr('title',"Open the pager in an external window")
36
.addClass('ui-button')
37
.click(function(){that.detach();})
38
.append(
39
$('<span>').addClass("ui-icon ui-icon-extlink")
40
)
41
);
42
this.pager_button_area.append(
43
$('<a>').attr('role', "button")
44
.attr('title',"Close the pager")
45
.addClass('ui-button')
46
.click(function(){that.collapse();})
47
.append(
48
$('<span>').addClass("ui-icon ui-icon-close")
49
)
50
);
51
};
52
53
54
Pager.prototype.bind_events = function () {
55
var that = this;
56
57
this.pager_element.bind('collapse_pager', function (event, extrap) {
58
// Animate hiding of the pager.
59
var time = (extrap && extrap.duration) ? extrap.duration : 'fast';
60
that.pager_element.animate({
61
height: 'toggle'
62
}, {
63
duration: time,
64
done: function() {
65
$('.end_space').css('height', that._default_end_space);
66
}
67
});
68
});
69
70
this.pager_element.bind('expand_pager', function (event, extrap) {
71
// Clear the pager's height attr if it's set. This allows the
72
// pager to size itself according to its contents.
73
that.pager_element.height('initial');
74
75
// Animate the showing of the pager
76
var time = (extrap && extrap.duration) ? extrap.duration : 'fast';
77
that.pager_element.show(time, function() {
78
// Explicitly set pager height once the pager has shown itself.
79
// This allows the pager-contents div to use percentage sizing.
80
that.pager_element.height(that.pager_element.height());
81
that._resize();
82
});
83
});
84
85
this.events.on('open_with_text.Pager', function (event, payload) {
86
// FIXME: support other mime types
87
if (payload.data['text/plain'] && payload.data['text/plain'] !== "") {
88
that.clear();
89
that.expand();
90
that.append_text(payload.data['text/plain']);
91
}
92
});
93
};
94
95
96
Pager.prototype.collapse = function (extrap) {
97
if (this.expanded === true) {
98
this.expanded = false;
99
this.pager_element.trigger('collapse_pager', extrap);
100
}
101
};
102
103
104
Pager.prototype.expand = function (extrap) {
105
if (this.expanded !== true) {
106
this.expanded = true;
107
this.pager_element.trigger('expand_pager', extrap);
108
}
109
};
110
111
112
Pager.prototype.toggle = function () {
113
if (this.expanded === true) {
114
this.collapse();
115
} else {
116
this.expand();
117
}
118
};
119
120
121
Pager.prototype.clear = function (text) {
122
this.pager_element.find(".container").empty();
123
};
124
125
Pager.prototype.detach = function(){
126
var w = window.open("","_blank");
127
$(w.document.head)
128
.append(
129
$('<link>')
130
.attr('rel',"stylesheet")
131
.attr('href',"/static/css/notebook.css")
132
.attr('type',"text/css")
133
)
134
.append(
135
$('<title>').text("IPython Pager")
136
);
137
var pager_body = $(w.document.body);
138
pager_body.css('overflow','scroll');
139
140
pager_body.append(this.pager_element.clone().children());
141
w.document.close();
142
this.collapse();
143
};
144
145
Pager.prototype.append_text = function (text) {
146
/**
147
* The only user content injected with this HTML call is escaped by
148
* the fixConsole() method.
149
*/
150
this.pager_element.find(".container").append($('<pre/>').html(utils.fixCarriageReturn(utils.fixConsole(text))));
151
};
152
153
154
Pager.prototype._resize = function() {
155
/**
156
* Update document based on pager size.
157
*/
158
159
// Make sure the padding at the end of the notebook is large
160
// enough that the user can scroll to the bottom of the
161
// notebook.
162
$('.end_space').css('height', Math.max(this.pager_element.height(), this._default_end_space));
163
};
164
165
// Backwards compatability.
166
IPython.Pager = Pager;
167
168
return {'Pager': Pager};
169
});
170
171