From 9cd878cf0f275f6b72ae7c3a42048c33a6bf3d02 Mon Sep 17 00:00:00 2001 From: "Nicholas Bollweg (Nick)" Date: Sat, 15 Nov 2014 09:01:35 -0500 Subject: [PATCH 1/6] moving, checking MathJax typesetting to WidgetView widget rendering should not fail hard if MathJax is not available --- IPython/html/static/widgets/js/widget.js | 11 +++++++++++ IPython/html/static/widgets/js/widget_bool.js | 2 +- IPython/html/static/widgets/js/widget_box.js | 2 +- IPython/html/static/widgets/js/widget_int.js | 6 +++--- IPython/html/static/widgets/js/widget_selection.js | 8 ++++---- IPython/html/static/widgets/js/widget_string.js | 7 +++---- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 7e4239b36..7850d3f6f 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -389,6 +389,17 @@ define(["widgets/js/manager", this.on('displayed', callback, context); } }, + + typeset: function(element){ + // check if MathJax is available, and if so use it to + // typeset some DOM + if(!window.MathJax){ return; } + return MathJax.Hub.Queue([ + "Typeset", + MathJax.Hub, + element + ]); + } }); diff --git a/IPython/html/static/widgets/js/widget_bool.js b/IPython/html/static/widgets/js/widget_bool.js index 6fea2cdc7..1a8e94af4 100644 --- a/IPython/html/static/widgets/js/widget_bool.js +++ b/IPython/html/static/widgets/js/widget_bool.js @@ -63,7 +63,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } diff --git a/IPython/html/static/widgets/js/widget_box.js b/IPython/html/static/widgets/js/widget_box.js index 317bea425..7fc31f7a4 100644 --- a/IPython/html/static/widgets/js/widget_box.js +++ b/IPython/html/static/widgets/js/widget_box.js @@ -320,7 +320,7 @@ define([ this.$title.html(" "); // Preserve title height } else { this.$title.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$title.get(0)]); + this.typeset(this.$title.get(0)); } var button_text = this.model.get('button_text'); diff --git a/IPython/html/static/widgets/js/widget_int.js b/IPython/html/static/widgets/js/widget_int.js index 1c08f26d2..0ef415f3c 100644 --- a/IPython/html/static/widgets/js/widget_int.js +++ b/IPython/html/static/widgets/js/widget_int.js @@ -156,7 +156,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } @@ -324,7 +324,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } @@ -444,7 +444,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } return ProgressView.__super__.update.apply(this); diff --git a/IPython/html/static/widgets/js/widget_selection.js b/IPython/html/static/widgets/js/widget_selection.js index b33e2eb93..d364772f4 100644 --- a/IPython/html/static/widgets/js/widget_selection.js +++ b/IPython/html/static/widgets/js/widget_selection.js @@ -98,7 +98,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } @@ -231,7 +231,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } @@ -346,7 +346,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } @@ -469,7 +469,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } diff --git a/IPython/html/static/widgets/js/widget_string.js b/IPython/html/static/widgets/js/widget_string.js index bd896a95b..c26120abc 100644 --- a/IPython/html/static/widgets/js/widget_string.js +++ b/IPython/html/static/widgets/js/widget_string.js @@ -44,8 +44,7 @@ define([ * changed by another view or by a state update from the back-end. */ this.$el.text(this.model.get('value')); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$el.get(0)]); - + this.typeset(this.$el.get(0)); return LatexView.__super__.update.apply(this); }, }); @@ -117,7 +116,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } @@ -201,7 +200,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - MathJax.Hub.Queue(["Typeset",MathJax.Hub,this.$label.get(0)]); + this.typeset(this.$label.get(0)); this.$label.show(); } } From 00fc31b0a87ac6d08e2a68d41ffdc21f5a2ff1fa Mon Sep 17 00:00:00 2001 From: "Nicholas Bollweg (Nick)" Date: Sat, 15 Nov 2014 10:20:37 -0500 Subject: [PATCH 2/6] also setting text in widget.typset --- IPython/html/static/widgets/js/widget.js | 11 ++++------- IPython/html/static/widgets/js/widget_bool.js | 3 +-- IPython/html/static/widgets/js/widget_box.js | 3 +-- IPython/html/static/widgets/js/widget_int.js | 9 +++------ IPython/html/static/widgets/js/widget_selection.js | 12 +++++------- IPython/html/static/widgets/js/widget_string.js | 9 +++------ 6 files changed, 17 insertions(+), 30 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 7850d3f6f..2e4892e2b 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -390,15 +390,12 @@ define(["widgets/js/manager", } }, - typeset: function(element){ + typeset: function(text, $el){ // check if MathJax is available, and if so use it to - // typeset some DOM + // typeset some jQuery selection + $el.text(text); if(!window.MathJax){ return; } - return MathJax.Hub.Queue([ - "Typeset", - MathJax.Hub, - element - ]); + return MathJax.Hub.Queue(["Typeset", MathJax.Hub, $el.get(0)]); } }); diff --git a/IPython/html/static/widgets/js/widget_bool.js b/IPython/html/static/widgets/js/widget_bool.js index 1a8e94af4..f9986f086 100644 --- a/IPython/html/static/widgets/js/widget_bool.js +++ b/IPython/html/static/widgets/js/widget_bool.js @@ -62,8 +62,7 @@ define([ if (description.trim().length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } diff --git a/IPython/html/static/widgets/js/widget_box.js b/IPython/html/static/widgets/js/widget_box.js index 7fc31f7a4..5da898f59 100644 --- a/IPython/html/static/widgets/js/widget_box.js +++ b/IPython/html/static/widgets/js/widget_box.js @@ -319,8 +319,7 @@ define([ if (description.trim().length === 0) { this.$title.html(" "); // Preserve title height } else { - this.$title.text(description); - this.typeset(this.$title.get(0)); + this.typeset(description, this.$title); } var button_text = this.model.get('button_text'); diff --git a/IPython/html/static/widgets/js/widget_int.js b/IPython/html/static/widgets/js/widget_int.js index 0ef415f3c..7d53d7a8f 100644 --- a/IPython/html/static/widgets/js/widget_int.js +++ b/IPython/html/static/widgets/js/widget_int.js @@ -155,8 +155,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } @@ -323,8 +322,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } @@ -443,8 +441,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } return ProgressView.__super__.update.apply(this); diff --git a/IPython/html/static/widgets/js/widget_selection.js b/IPython/html/static/widgets/js/widget_selection.js index d364772f4..4ad1960e3 100644 --- a/IPython/html/static/widgets/js/widget_selection.js +++ b/IPython/html/static/widgets/js/widget_selection.js @@ -97,8 +97,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } @@ -231,7 +230,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } @@ -345,8 +344,8 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.$label.text(); + this.typeset(description, this.$label); this.$label.show(); } } @@ -468,8 +467,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } diff --git a/IPython/html/static/widgets/js/widget_string.js b/IPython/html/static/widgets/js/widget_string.js index c26120abc..5b85ee354 100644 --- a/IPython/html/static/widgets/js/widget_string.js +++ b/IPython/html/static/widgets/js/widget_string.js @@ -43,8 +43,7 @@ define([ * Called when the model is changed. The model may have been * changed by another view or by a state update from the back-end. */ - this.$el.text(this.model.get('value')); - this.typeset(this.$el.get(0)); + this.typeset(this.model.get('value'), this.$el); return LatexView.__super__.update.apply(this); }, }); @@ -115,8 +114,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } @@ -199,8 +197,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.$label.text(description); - this.typeset(this.$label.get(0)); + this.typeset(description, this.$label); this.$label.show(); } } From 57f05c4a2b3d6b48fc79f6c81f7c7c3d237048f3 Mon Sep 17 00:00:00 2001 From: "Nicholas Bollweg (Nick)" Date: Sat, 15 Nov 2014 10:32:43 -0500 Subject: [PATCH 3/6] reversing order of arguments, as text may already exist --- IPython/html/static/widgets/js/widget.js | 14 +++++++++----- IPython/html/static/widgets/js/widget_bool.js | 2 +- IPython/html/static/widgets/js/widget_box.js | 2 +- IPython/html/static/widgets/js/widget_int.js | 6 +++--- IPython/html/static/widgets/js/widget_selection.js | 8 ++++---- IPython/html/static/widgets/js/widget_string.js | 6 +++--- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 2e4892e2b..23fa3853b 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -390,11 +390,15 @@ define(["widgets/js/manager", } }, - typeset: function(text, $el){ - // check if MathJax is available, and if so use it to - // typeset some jQuery selection - $el.text(text); - if(!window.MathJax){ return; } + typeset: function($el, text){ + // after (optionally) updating a selection's text, check if + // MathJax is available and typeset it + if(arguments.length > 1){ + $el.text(text); + } + if(!window.MathJax){ + return; + } return MathJax.Hub.Queue(["Typeset", MathJax.Hub, $el.get(0)]); } }); diff --git a/IPython/html/static/widgets/js/widget_bool.js b/IPython/html/static/widgets/js/widget_bool.js index f9986f086..9b8411814 100644 --- a/IPython/html/static/widgets/js/widget_bool.js +++ b/IPython/html/static/widgets/js/widget_bool.js @@ -62,7 +62,7 @@ define([ if (description.trim().length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } diff --git a/IPython/html/static/widgets/js/widget_box.js b/IPython/html/static/widgets/js/widget_box.js index 5da898f59..e7850ce20 100644 --- a/IPython/html/static/widgets/js/widget_box.js +++ b/IPython/html/static/widgets/js/widget_box.js @@ -319,7 +319,7 @@ define([ if (description.trim().length === 0) { this.$title.html(" "); // Preserve title height } else { - this.typeset(description, this.$title); + this.typeset(this.$title, description); } var button_text = this.model.get('button_text'); diff --git a/IPython/html/static/widgets/js/widget_int.js b/IPython/html/static/widgets/js/widget_int.js index 7d53d7a8f..4b1070593 100644 --- a/IPython/html/static/widgets/js/widget_int.js +++ b/IPython/html/static/widgets/js/widget_int.js @@ -155,7 +155,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } @@ -322,7 +322,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } @@ -441,7 +441,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } return ProgressView.__super__.update.apply(this); diff --git a/IPython/html/static/widgets/js/widget_selection.js b/IPython/html/static/widgets/js/widget_selection.js index 4ad1960e3..52742ae00 100644 --- a/IPython/html/static/widgets/js/widget_selection.js +++ b/IPython/html/static/widgets/js/widget_selection.js @@ -97,7 +97,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } @@ -230,7 +230,7 @@ define([ this.$label.hide(); } else { this.$label.text(description); - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } @@ -345,7 +345,7 @@ define([ this.$label.hide(); } else { this.$label.text(); - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } @@ -467,7 +467,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } diff --git a/IPython/html/static/widgets/js/widget_string.js b/IPython/html/static/widgets/js/widget_string.js index 5b85ee354..eb1d783fb 100644 --- a/IPython/html/static/widgets/js/widget_string.js +++ b/IPython/html/static/widgets/js/widget_string.js @@ -43,7 +43,7 @@ define([ * Called when the model is changed. The model may have been * changed by another view or by a state update from the back-end. */ - this.typeset(this.model.get('value'), this.$el); + this.typeset(this.$el, this.model.get('value')); return LatexView.__super__.update.apply(this); }, }); @@ -114,7 +114,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } @@ -197,7 +197,7 @@ define([ if (description.length === 0) { this.$label.hide(); } else { - this.typeset(description, this.$label); + this.typeset(this.$label, description); this.$label.show(); } } From b17a40d95072cc4ed6117e086b93102edeefdb32 Mon Sep 17 00:00:00 2001 From: "Nicholas Bollweg (Nick)" Date: Sat, 15 Nov 2014 10:48:16 -0500 Subject: [PATCH 4/6] moving typeset to DOMWidgetView --- IPython/html/static/widgets/js/widget.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 23fa3853b..e5d51c1b7 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -388,18 +388,6 @@ define(["widgets/js/manager", } else { this.on('displayed', callback, context); } - }, - - typeset: function($el, text){ - // after (optionally) updating a selection's text, check if - // MathJax is available and typeset it - if(arguments.length > 1){ - $el.text(text); - } - if(!window.MathJax){ - return; - } - return MathJax.Hub.Queue(["Typeset", MathJax.Hub, $el.get(0)]); } }); @@ -587,6 +575,18 @@ define(["widgets/js/manager", } return elements; }, + + typeset: function($el, text){ + // after (optionally) updating a selection's text, check if + // MathJax is available and typeset it + if(arguments.length > 1){ + $el.text(text); + } + if(!window.MathJax){ + return; + } + return MathJax.Hub.Queue(["Typeset", MathJax.Hub, $el.get(0)]); + }, }); From c95a9aaf805cd87d77a5eb1c23e40dbf2c3374e4 Mon Sep 17 00:00:00 2001 From: "Nicholas Bollweg (Nick)" Date: Sat, 15 Nov 2014 11:14:03 -0500 Subject: [PATCH 5/6] accept node or selection --- IPython/html/static/widgets/js/widget.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index e5d51c1b7..6b966de3e 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -576,16 +576,20 @@ define(["widgets/js/manager", return elements; }, - typeset: function($el, text){ - // after (optionally) updating a selection's text, check if - // MathJax is available and typeset it + typeset: function(element, text){ + // after (optionally) updating a node(list) or jQuery selection's + // text, check if MathJax is available and typeset it + var $el = element.jquery ? element : $(element); + if(arguments.length > 1){ $el.text(text); } if(!window.MathJax){ return; } - return MathJax.Hub.Queue(["Typeset", MathJax.Hub, $el.get(0)]); + return $el.map(function(){ + return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]); + }); }, }); From 83757b6a0e5242d963de955667c3ad25bb95e5c4 Mon Sep 17 00:00:00 2001 From: "Nicholas Bollweg (Nick)" Date: Thu, 4 Dec 2014 21:06:34 -0500 Subject: [PATCH 6/6] moving typeset to utils, usage in cell and outputarea --- IPython/html/static/base/js/utils.js | 28 +++++++++++++++++++ IPython/html/static/notebook/js/cell.js | 5 +--- IPython/html/static/notebook/js/outputarea.js | 4 +-- IPython/html/static/widgets/js/widget.js | 14 +--------- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/IPython/html/static/base/js/utils.js b/IPython/html/static/base/js/utils.js index ce4ed4b6e..10dfdd7ae 100644 --- a/IPython/html/static/base/js/utils.js +++ b/IPython/html/static/base/js/utils.js @@ -766,6 +766,33 @@ define([ }; }; + var typeset = function(element, text) { + /** + * Apply MathJax rendering to an element, and optionally set its text + * + * If MathJax is not available, make no changes. + * + * Returns the output any number of typeset elements, or undefined if + * MathJax was not available. + * + * Parameters + * ---------- + * element: Node, NodeList, or jQuery selection + * text: option string + */ + if(!window.MathJax){ + return; + } + var $el = element.jquery ? element : $(element); + if(arguments.length > 1){ + $el.text(text); + } + return $el.map(function(){ + // MathJax takes a DOM node: $.map makes `this` the context + return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]); + }); + }; + var utils = { regex_split : regex_split, uuid : uuid, @@ -799,6 +826,7 @@ define([ load_class: load_class, resolve_promises_dict: resolve_promises_dict, reject: reject, + typeset: typeset, }; // Backwards compatability. diff --git a/IPython/html/static/notebook/js/cell.js b/IPython/html/static/notebook/js/cell.js index b122c2aa3..9c1daa8d2 100644 --- a/IPython/html/static/notebook/js/cell.js +++ b/IPython/html/static/notebook/js/cell.js @@ -220,10 +220,7 @@ define([ * @method typeset */ Cell.prototype.typeset = function () { - if (window.MathJax) { - var cell_math = this.element.get(0); - MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]); - } + utils.typeset(this.element); }; /** diff --git a/IPython/html/static/notebook/js/outputarea.js b/IPython/html/static/notebook/js/outputarea.js index bb9b21808..4275d0245 100644 --- a/IPython/html/static/notebook/js/outputarea.js +++ b/IPython/html/static/notebook/js/outputarea.js @@ -199,9 +199,7 @@ define([ // typeset with MathJax if MathJax is available OutputArea.prototype.typeset = function () { - if (window.MathJax){ - MathJax.Hub.Queue(["Typeset",MathJax.Hub]); - } + utils.typeset(this.element); }; diff --git a/IPython/html/static/widgets/js/widget.js b/IPython/html/static/widgets/js/widget.js index 6b966de3e..84d8befb8 100644 --- a/IPython/html/static/widgets/js/widget.js +++ b/IPython/html/static/widgets/js/widget.js @@ -577,19 +577,7 @@ define(["widgets/js/manager", }, typeset: function(element, text){ - // after (optionally) updating a node(list) or jQuery selection's - // text, check if MathJax is available and typeset it - var $el = element.jquery ? element : $(element); - - if(arguments.length > 1){ - $el.text(text); - } - if(!window.MathJax){ - return; - } - return $el.map(function(){ - return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]); - }); + utils.typeset.apply(null, arguments); }, });