Skip to content
This repository was archived by the owner on Feb 19, 2022. It is now read-only.

Commit 9f7b0d7

Browse files
committed
Define global gistAsync function
1 parent 67b0527 commit 9f7b0d7

File tree

3 files changed

+65
-54
lines changed

3 files changed

+65
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log follows the conventions of
77

88
## [Unreleased]
99

10+
- Define global function `gistAsync()`.
1011
- Update to jQuery 3.0.0.
1112
- Update project structure to [makenew/coffeescript-package] v2.2.0.
1213
- Release on npm.

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@ Based on Mark Selby's [async-gists.js].
2222

2323
**Requires jQuery.**
2424

25-
Include `javascripts/main.coffee` however you like,
26-
or load the compiled file `js/gist-async.min.js`.
25+
Include `javascripts/main.coffee` however you like
26+
or load the compiled file `js/gist-async.min.js`,
27+
then call the global function, e.g.,
28+
29+
```js
30+
(function (window, document) {
31+
document.addEventListener('DOMContentLoaded', function () {
32+
window.gistAsync();
33+
})
34+
})(window, document);
35+
```
2736

2837
Example markup:
2938

javascripts/main.coffee

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,64 @@
1313
#
1414
'use strict'
1515

16-
$ = jQuery
17-
$ ->
18-
GIST_HOST = 'https://gist.github.com'
19-
elements = $('div[data-gist]')
20-
gists = {}
21-
code = []
22-
stylesheets = []
16+
this.gistAsync = ->
17+
$ = jQuery
18+
$ ->
19+
GIST_HOST = 'https://gist.github.com'
20+
elements = $('div[data-gist]')
21+
gists = {}
22+
code = []
23+
stylesheets = []
2324

24-
# The asynchronous asset loader function.
25-
loader = (url) ->
26-
link = document.createElement 'link'
27-
link.type = 'text/css'
28-
link.rel = 'stylesheet'
29-
link.href = url
30-
document.getElementsByTagName('head')[0].appendChild link
31-
return
25+
# The asynchronous asset loader function.
26+
loader = (url) ->
27+
link = document.createElement 'link'
28+
link.type = 'text/css'
29+
link.rel = 'stylesheet'
30+
link.href = url
31+
document.getElementsByTagName('head')[0].appendChild link
32+
return
3233

33-
elements.addClass('loading')
34+
elements.addClass('loading')
3435

35-
# Get elements referencing a gist
36-
# and build a gists hash referencing
37-
# the elements that use it.
38-
elements.each (index, element) ->
39-
element = $(element)
40-
gist = element.data 'gist'
41-
gists[gist] ?= targets: []
42-
gists[gist].targets.push element
36+
# Get elements referencing a gist
37+
# and build a gists hash referencing
38+
# the elements that use it.
39+
elements.each (index, element) ->
40+
element = $(element)
41+
gist = element.data 'gist'
42+
gists[gist] ?= targets: []
43+
gists[gist].targets.push element
4344

44-
# Load the gists.
45-
$.each gists, (id, data) ->
46-
$.getJSON "#{GIST_HOST}/#{id}.json?callback=?", (data) ->
47-
gist = gists[id]
48-
gist.data = data
45+
# Load the gists.
46+
$.each gists, (id, data) ->
47+
$.getJSON "#{GIST_HOST}/#{id}.json?callback=?", (data) ->
48+
gist = gists[id]
49+
gist.data = data
4950

50-
# Only insert the stylesheets once.
51-
stylesheet = gist.data.stylesheet
52-
if stylesheets.indexOf(stylesheet) < 0
53-
stylesheets.push stylesheet
54-
loader(stylesheet)
51+
# Only insert the stylesheets once.
52+
stylesheet = gist.data.stylesheet
53+
if stylesheets.indexOf(stylesheet) < 0
54+
stylesheets.push stylesheet
55+
loader(stylesheet)
5556

56-
div = gist.data.div
57-
gist.files = $(div).find('.gist-file')
58-
gist.outer = $(div).first().html('')
57+
div = gist.data.div
58+
gist.files = $(div).find('.gist-file')
59+
gist.outer = $(div).first().html('')
5960

60-
# Iterate elements refering to this gist.
61-
$(gist.targets).each (index, target) ->
62-
file = target.data 'gist-file'
63-
if file
64-
outer = gist.outer.clone()
65-
inner = "<div class=\"gist-file\">" \
66-
+ $(gist.files.get(gist.data.files.indexOf(file))).html() \
67-
+ "</div>"
68-
outer.html inner
69-
else
70-
outer = $(div)
61+
# Iterate elements refering to this gist.
62+
$(gist.targets).each (index, target) ->
63+
file = target.data 'gist-file'
64+
if file
65+
outer = gist.outer.clone()
66+
inner = "<div class=\"gist-file\">" \
67+
+ $(gist.files.get(gist.data.files.indexOf(file))).html() \
68+
+ "</div>"
69+
outer.html inner
70+
else
71+
outer = $(div)
7172

72-
outer.hide()
73-
target.fadeOut 'fast', ->
74-
$(this).replaceWith(outer)
75-
outer.fadeIn()
73+
outer.hide()
74+
target.fadeOut 'fast', ->
75+
$(this).replaceWith(outer)
76+
outer.fadeIn()

0 commit comments

Comments
 (0)