1
1
class Instafeed
2
- constructor : (params ) ->
2
+ constructor : (params , context ) ->
3
3
# default options
4
4
@options =
5
5
target : ' instafeed'
@@ -15,11 +15,30 @@ class Instafeed
15
15
if typeof params is ' object'
16
16
@options [option] = value for option, value of params
17
17
18
+ # save a reference to context, which defaults to curr scope
19
+ # this will be used to cache data from parsing to the real
20
+ # instance the user interacts with (for pagination)
21
+ @context = if context? then context else this
22
+
18
23
# generate a unique key for the instance
19
24
@unique = @ _genKey ()
20
25
26
+ # method to check if there are more results to load
27
+ hasNext : ->
28
+ return typeof @context .nextUrl is ' string' and @context .nextUrl .length > 0
29
+
30
+ # method to display next results using the pagination
31
+ # data from API. Manually passing a url to .run() will
32
+ # bypass the URL creation from options.
33
+ next : ->
34
+ # check for a valid next url first
35
+ return false if not @ hasNext ()
36
+
37
+ # call run with the next results
38
+ return @ run (@context .nextUrl )
39
+
21
40
# MAKE IT GO!
22
- run : ->
41
+ run : ( url ) ->
23
42
# make sure either a client id or access token is set
24
43
if typeof @options .clientId isnt ' string'
25
44
unless typeof @options .accessToken is ' string'
@@ -41,16 +60,17 @@ class Instafeed
41
60
# give the script an id so it can removed later
42
61
script .id = ' instafeed-fetcher'
43
62
44
- # assign the script src using _buildUrl()
45
- script .src = @ _buildUrl ()
63
+ # assign the script src using _buildUrl(), or by
64
+ # using the argument passed to the function
65
+ script .src = url || @ _buildUrl ()
46
66
47
67
# add the new script object to the header
48
68
header = document .getElementsByTagName ' head'
49
69
header[0 ].appendChild script
50
70
51
71
# create a global object to cache the options
52
72
instanceName = " instafeedCache#{ @unique } "
53
- window [instanceName] = new Instafeed @options
73
+ window [instanceName] = new Instafeed @options , this
54
74
window [instanceName].unique = @unique
55
75
56
76
# return true if everything ran
@@ -89,6 +109,13 @@ class Instafeed
89
109
if @options .success ? and typeof @options .success is ' function'
90
110
@options .success .call (this , response)
91
111
112
+ # cache the pagination data, if it exists. Apply the value
113
+ # to the "context" object, which will be a true reference
114
+ # if this instance was created just for parsing
115
+ @context .nextUrl = ' '
116
+ if response .pagination ?
117
+ @context .nextUrl = response .pagination .next_url
118
+
92
119
# before images are inserted into the DOM, check for sorting
93
120
if @options .sortBy isnt ' most-recent'
94
121
# if sort is set to random, don't check for polarity
@@ -121,13 +148,13 @@ class Instafeed
121
148
# to make it easier to test various parts of the class,
122
149
# any DOM manipulation first checks for the DOM to exist
123
150
if document ? and @options .mock is false
124
- # clear the current dom node
125
- document .getElementById (@options .target ).innerHTML = ' '
126
-
127
151
# limit the number of images if needed
128
152
images = response .data
129
153
images = images[0 .. @options .limit ] if images .length > @options .limit
130
154
155
+ # create the document fragment
156
+ fragment = document .createDocumentFragment ()
157
+
131
158
# filter the results
132
159
if @options .filter ? and typeof @options .filter is ' function'
133
160
images = @ _filter (images, @options .filter )
@@ -139,6 +166,9 @@ class Instafeed
139
166
imageString = ' '
140
167
imgUrl = ' '
141
168
169
+ # create a temp dom node that will hold the html
170
+ tmpEl = document .createElement (' div' )
171
+
142
172
# loop through the images
143
173
for image in images
144
174
# use protocol relative image url
@@ -159,12 +189,14 @@ class Instafeed
159
189
# add the image partial to the html string
160
190
htmlString += imageString
161
191
162
- # add the final html to the target DOM node
163
- document .getElementById (@options .target ).innerHTML = htmlString
164
- else
165
- # create a document fragment
166
- fragment = document .createDocumentFragment ()
192
+ # add the final html string to the temp node
193
+ tmpEl .innerHTML = htmlString
167
194
195
+ # loop through the contents of the temp node
196
+ # and append them to the fragment
197
+ for node in [].slice .call (tmpEl .childNodes )
198
+ fragment .appendChild (node)
199
+ else
168
200
# loop through the images
169
201
for image in images
170
202
# create the image using the @options's resolution
@@ -186,8 +218,8 @@ class Instafeed
186
218
# add the image (without link) to the fragment
187
219
fragment .appendChild img
188
220
189
- # Add the fragment to the DOM
190
- document .getElementById (@options .target ).appendChild fragment
221
+ # Add the fragment to the DOM
222
+ document .getElementById (@options .target ).appendChild fragment
191
223
192
224
# remove the injected script tag
193
225
header = document .getElementsByTagName (' head' )[0 ]
0 commit comments