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