app/scripts/lazyload.coffee

Lazy loading plugin

Loads additional heavy libraries when they are used

requires: jQuery or Zepto DOM library
author: Julien Ramboz
version: 1.0

AMD loader

Try loading as AMD module or fall back to default loading

((plugin) -> if typeof define is "function" and define.amd define "lazyload", ["jslib", "core", "header"], plugin else plugin @$ ).call this, lazyload = ($, _) -> "use strict" CDN_PATH = "//cdnjs.cloudflare.com/ajax/libs" CDN_PATH = "http:" + CDN_PATH if not window.location.protocol.match("http") class LazyLoad @load: () ->

Highlight code.

$codeBlocks = $ "pre code" if $codeBlocks.length and not $("html").hasClass("lt-ie8") lib = ["#{CDN_PATH}/highlight.js/8.0/highlight.min.js"] require lib, () -> $.getStyle "#{CDN_PATH}/highlight.js/8.0/styles/foundation.min.css" $codeBlocks.each () -> hljs.highlightBlock(this) $(document).trigger "highlightjs-ready"

Format maths

$mathsBlocks = $ "[data-format='maths']" if $mathsBlocks.length conf = "TeX-MML-AM_HTMLorMML" lib = ["#{CDN_PATH}/mathjax/2.3/MathJax.js?config=#{conf}"] require lib, () -> $(document).trigger "mathjax-ready"

Format markdown

$markdown = $ "[data-format='markdown']" if $markdown.length lib = ["#{CDN_PATH}/marked/0.3.1/marked.min.js"] require lib, (marked) -> $markdown.each () -> el = this marked(this.innerHTML, (err, content) -> throw err if err el.innerHTML = content) $(document).trigger "marked-ready"

Plugin

The actual plugin class

$ -> _.LazyLoad = LazyLoad LazyLoad.load() $