1
+ /*
2
+ * ALA Embedded Comment Utilities
3
+ * https://github.com/alistapart/comment-embed
4
+ *
5
+ * Copyright (c) 2013 A List Apart
6
+ * Licensed under the MIT license.
7
+ */
8
+
9
+
10
+ ( function ( w , undefined ) {
11
+ // Enable JS strict mode
12
+ "use strict" ;
13
+
14
+ var doc = w . document ,
15
+ ala = function ( el ) {
16
+ var elType = typeof ( el ) ,
17
+ ret = [ ] ;
18
+
19
+ if ( el ) {
20
+ if ( elType === "function" ) {
21
+ // Passing a function means it’s a `doc.ready` shortcut.
22
+ return ala . ready ( el ) ;
23
+ }
24
+
25
+ if ( elType === "object" ) {
26
+ // If it’s an object, pass it on through.
27
+ ret = ret . concat ( el ) ;
28
+ }
29
+ // If it’s a string, it’s a selector.
30
+ if ( elType === "string" ) {
31
+ for ( var i = 0 , sel = doc . querySelectorAll ( el ) , il = sel . length ; i < il ; i ++ ) {
32
+ ret [ i ] = sel [ i ] ;
33
+ }
34
+ }
35
+ }
36
+ else {
37
+ // No element? Return the wrapped document.
38
+ ret . push ( doc ) ;
39
+ }
40
+ return ala . extend ( ret , ala . fn ) ;
41
+ } ;
42
+
43
+ ala . extend = function ( first , second ) {
44
+ for ( var i in second ) {
45
+ if ( second . hasOwnProperty ( i ) ) {
46
+ first [ i ] = second [ i ] ;
47
+ }
48
+ }
49
+ return first ;
50
+ } ;
51
+
52
+ ala . ready = function ( fn ) {
53
+ var ready = false ,
54
+ set = [ ] ,
55
+ go = function ( ) {
56
+ if ( ! ready ) {
57
+ while ( set . length ) {
58
+ set . shift ( ) . call ( doc ) ;
59
+ }
60
+ ready = true ;
61
+ }
62
+ } ;
63
+
64
+ // Quick IE8 shiv
65
+ if ( ! w . addEventListener ) {
66
+ w . addEventListener = function ( evt , cb ) {
67
+ return w . attachEvent ( "on" + evt , cb ) ;
68
+ } ;
69
+ }
70
+
71
+ // DOM ready
72
+ w . addEventListener ( "DOMContentLoaded" , go , false ) ;
73
+ w . addEventListener ( "readystatechange" , go , false ) ;
74
+ w . addEventListener ( "load" , go , false ) ;
75
+ // If DOM is already ready at exec time:
76
+ if ( doc . readyState === "complete" ) {
77
+ go ( ) ;
78
+ }
79
+ if ( ready && fn ) {
80
+ fn . call ( doc ) ;
81
+ }
82
+ else if ( fn ) {
83
+ set . push ( fn ) ;
84
+ }
85
+ else {
86
+ go ( ) ;
87
+ }
88
+ return [ doc ] ;
89
+ } ;
90
+
91
+ ala . fn = { } ;
92
+
93
+ ala . fn . each = function ( fn ) {
94
+ for ( var i = 0 , l = this . length ; i < l ; i ++ ) {
95
+ fn . call ( this [ i ] , i ) ;
96
+ }
97
+ return this ;
98
+ } ;
99
+
100
+ ala . fn . data = function ( key , val ) {
101
+ if ( key === undefined ) {
102
+ return this [ 0 ] . alaData ;
103
+ } else {
104
+ if ( val === undefined ) {
105
+ return this [ 0 ] . getAttribute ( "data-" + key ) || this [ 0 ] . alaData && this [ 0 ] . alaData [ key ] ;
106
+ } else {
107
+ return this . each ( function ( ) {
108
+ if ( ! this . alaData ) {
109
+ this . alaData = { } ;
110
+ }
111
+ this . alaData [ key ] = val ;
112
+ } ) ;
113
+ }
114
+ }
115
+ } ;
116
+
117
+ ala . fn . bind = function ( evt , callback ) {
118
+ var evts = evt . split ( " " ) ,
119
+ el = this ;
120
+
121
+ function newCB ( e ) {
122
+ return callback . apply ( el , [ e ] . concat ( e . _args ) ) ;
123
+ }
124
+
125
+ return this . each ( function ( ) {
126
+ for ( var i = 0 , il = evts . length ; i < il ; i ++ ) {
127
+ if ( "addEventListener" in this ) {
128
+ this . addEventListener ( evts [ i ] , newCB , false ) ;
129
+ }
130
+ else if ( this . attachEvent ) {
131
+ this . attachEvent ( "on" + evts [ i ] , newCB ) ;
132
+ }
133
+ }
134
+ } ) ;
135
+ } ;
136
+
137
+ ala . fn . prev = function ( ) {
138
+ var prev = this [ 0 ] . previousSibling ;
139
+
140
+ while ( prev && prev . nodeType !== 1 ) {
141
+ prev = prev . previousSibling ;
142
+ }
143
+ return prev ;
144
+ } ;
145
+
146
+ ala . fn . trigger = function ( evt , args ) {
147
+ var evts = evt . split ( " " ) ;
148
+ return this . each ( function ( ) {
149
+ for ( var i = 0 , il = evts . length ; i < il ; i ++ ) {
150
+ // Real browsers:
151
+ if ( doc . createEvent ) {
152
+ var ev = doc . createEvent ( "Event" ) ;
153
+ ev . initEvent ( evts [ i ] , true , true ) ;
154
+ ev . _args = args ;
155
+ this . dispatchEvent ( ev ) ;
156
+ } else if ( doc . createEventObject ) {
157
+ // IE8:
158
+ var ieev = doc . createEventObject ( ) ;
159
+ ieev . _args = args ;
160
+ ieev . eventType = evt ;
161
+ this . fireEvent ( evt , ieev ) ;
162
+ }
163
+ }
164
+ } ) ;
165
+ } ;
166
+
167
+ ala . fn . throttle = function ( fn , delay ) {
168
+ var timer = null ;
169
+
170
+ return function ( ) {
171
+ var context = this , args = arguments ;
172
+ clearTimeout ( timer ) ;
173
+ timer = setTimeout ( function ( ) {
174
+ fn . apply ( context , args ) ;
175
+ } , delay ) ;
176
+ } ;
177
+ } ;
178
+
179
+ var xmlHttp = ( function ( ) {
180
+ var xmlhttpmethod = false ;
181
+ try {
182
+ xmlhttpmethod = new XMLHttpRequest ( ) ;
183
+ }
184
+ catch ( e ) {
185
+ xmlhttpmethod = new ActiveXObject ( "Microsoft.XMLHTTP" ) ;
186
+ }
187
+ return function ( ) {
188
+ return xmlhttpmethod ;
189
+ } ;
190
+ } ( ) ) ;
191
+
192
+ ala . ajax = function ( url , options ) {
193
+ var req = xmlHttp ( ) ,
194
+ settings = ala . ajax . settings ;
195
+
196
+ if ( options ) {
197
+ ala . extend ( settings , options ) ;
198
+ }
199
+ if ( ! url ) {
200
+ url = settings . url ;
201
+ }
202
+
203
+ if ( ! req || ! url ) {
204
+ return ;
205
+ }
206
+
207
+ req . open ( settings . method , url + settings . data , settings . async ) ;
208
+
209
+ req . onreadystatechange = function ( ) {
210
+ if ( req . readyState !== 4 || req . status !== 200 && req . status !== 304 ) {
211
+ return settings . error ( req . responseText ) ;
212
+ } else if ( req . readyState === 4 ) {
213
+ settings . success ( req . responseText , req . status , req ) ;
214
+ }
215
+ } ;
216
+ if ( req . readyState === 4 ) {
217
+ return ;
218
+ }
219
+
220
+ req . send ( null ) ;
221
+ } ;
222
+
223
+ ala . ajax . settings = {
224
+ success : function ( ) { } ,
225
+ error : function ( ) { } ,
226
+ method : "GET" ,
227
+ async : true ,
228
+ data : null
229
+ } ;
230
+
231
+ w . ala = ala ;
232
+
233
+ } ( this ) ) ;
0 commit comments