|
21 | 21 | */ |
22 | 22 |
|
23 | 23 | /*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */ |
24 | | -/*global define, $ */ |
| 24 | +/*global define, $, Image */ |
25 | 25 |
|
26 | 26 | define(function (require, exports) { |
27 | 27 | "use strict"; |
28 | 28 |
|
| 29 | + // Fn to allow an event to fire after all images are loaded |
| 30 | + $.fn.imagesLoaded = function () { |
| 31 | + // get all the images (excluding those with no src attribute) |
| 32 | + var $imgs = this.find('img[src!=""]'); |
| 33 | + // if there's no images, just return an already resolved promise |
| 34 | + if (!$imgs.length) { |
| 35 | + return $.Deferred().resolve().promise(); |
| 36 | + } |
| 37 | + |
| 38 | + // for each image, add a deferred object to the array which resolves |
| 39 | + // when the image is loaded (or if loading fails) |
| 40 | + var dfds = []; |
| 41 | + $imgs.each(function () { |
| 42 | + var dfd = $.Deferred(); |
| 43 | + dfds.push(dfd); |
| 44 | + var img = new Image(); |
| 45 | + img.onload = function () { |
| 46 | + dfd.resolve(); |
| 47 | + }; |
| 48 | + img.onerror = function () { |
| 49 | + dfd.resolve(); |
| 50 | + }; |
| 51 | + img.src = this.src; |
| 52 | + |
| 53 | + }); |
| 54 | + |
| 55 | + // return a master promise object which will resolve when all the deferred objects have resolved |
| 56 | + // IE - when all the images are loaded |
| 57 | + return $.when.apply($, dfds); |
| 58 | + }; |
| 59 | + |
29 | 60 | var utils = require("lib/utils"); |
30 | 61 |
|
31 | 62 | // Array to keep all important configuration |
@@ -254,34 +285,34 @@ define(function (require, exports) { |
254 | 285 | $iframe[0].contentWindow.stopMath(); |
255 | 286 | } |
256 | 287 |
|
257 | | - $(body).find('#body-text').html(result.html); |
| 288 | + $(body).find('#body-text').html(result.html).imagesLoaded().then(function () { |
| 289 | + if (result.stem && inputData.renderMath) { |
| 290 | + $iframe[0].contentWindow.startMath(); |
| 291 | + } |
258 | 292 |
|
259 | | - if (result.stem && inputData.renderMath) { |
260 | | - $iframe[0].contentWindow.startMath(); |
261 | | - } |
| 293 | + // Highlight source code |
| 294 | + if (result.sourceHighlighter === 'highlightjs') { |
| 295 | + $('pre code', body).each(function (i, block) { |
| 296 | + $iframe[0].contentWindow.hljs.highlightBlock(block); |
| 297 | + }); |
| 298 | + } else if (result.sourceHighlighter === 'prettify') { |
| 299 | + $iframe[0].contentWindow.prettyPrint(); |
| 300 | + } |
262 | 301 |
|
263 | | - // Highlight source code |
264 | | - if (result.sourceHighlighter === 'highlightjs') { |
265 | | - $('pre code', body).each(function (i, block) { |
266 | | - $iframe[0].contentWindow.hljs.highlightBlock(block); |
| 302 | + // make page anchors work and add titles to links |
| 303 | + $iframe.contents().find('a[href]').each(function (index, link) { |
| 304 | + var href = link.getAttribute("href"); |
| 305 | + if (href.match(/^#/)) { |
| 306 | + $(link).attr('onclick', 'goto("' + href.substring(1) + '");return false;'); |
| 307 | + } |
| 308 | + var title = link.getAttribute('title'); |
| 309 | + if (!title) { |
| 310 | + link.setAttribute("title", href); |
| 311 | + } |
267 | 312 | }); |
268 | | - } else if (result.sourceHighlighter === 'prettify') { |
269 | | - $iframe[0].contentWindow.prettyPrint(); |
270 | | - } |
271 | 313 |
|
272 | | - // make page anchors work and add titles to links |
273 | | - $iframe.contents().find('a[href]').each(function (index, link) { |
274 | | - var href = link.getAttribute("href"); |
275 | | - if (href.match(/^#/)) { |
276 | | - $(link).attr('onclick', 'goto("' + href.substring(1) + '");return false;'); |
277 | | - } |
278 | | - var title = link.getAttribute('title'); |
279 | | - if (!title) { |
280 | | - link.setAttribute("title", href); |
281 | | - } |
| 314 | + onCompleteCallback(); |
282 | 315 | }); |
283 | | - |
284 | | - onCompleteCallback(); |
285 | 316 | } |
286 | 317 |
|
287 | 318 | if (isNewDocument || needsFullUpdate(inputData, result)) { |
|
0 commit comments