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