5
5
< link rel ="stylesheet " href ="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css " />
6
6
< link rel ="stylesheet " href ="http://node-os.com/css/default.css " />
7
7
< script type ="text/javascript " src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js "> </ script >
8
- < script type ="text/javascript " src ="api.min.js " > </ script >
8
+ < script type ="text/javascript " src ="/ api.min.js " > </ script >
9
9
< style >
10
10
# articles img {max-width : 100% }
11
+ # articles article .title img {width : 100px }
11
12
</ style >
12
13
</ head >
13
14
< body >
35
36
< section ng-app ="NodeOsBlog " id ="articles " class ="section ">
36
37
< div class ="container " ng-controller ="ErrorListCtrl " >
37
38
< article ng-repeat ="(index, error) in errors " class ="row ">
38
- < div class ="col-sm-8 col-sm-offset-2 {{error.class}} " ng-mousedown =" removeError(error) " >
39
+ < div class ="col-sm-8 col-sm-offset-2 {{error.class}} " >
39
40
< header class ="alert alert-danger " role ="alert ">
40
41
< h3 class ="title "> {{error.name}}</ h3 >
42
+ < p ng-mousedown ="removeError(error) " > To remove this message, click here</ p >
41
43
</ header >
42
- < div class ="content "> {{ error.message}} </ div >
44
+ < div class ="content "ng-bind-html =" error.message | to_trusted " > </ div >
43
45
< footer >
44
46
Don't worry, it probably was not your fault... probably....
45
47
< br /> < br />
46
48
Even if it was,
47
49
< a target ="_blank " href ="https://github.com/formula1/NodeOS-Blog/issues " > just submit an issue :)</ a >
48
- < br /> < br />
49
- To remove this message, click it.
50
50
</ footer >
51
51
</ div >
52
52
</ article >
@@ -134,20 +134,22 @@ <h3 class="footer-title">Share</h3>
134
134
} ;
135
135
} ] ) ;
136
136
var errors = [ ] ;
137
- var is_authed = false ;
137
+
138
138
function add403 ( ) {
139
139
var topush = {
140
140
class :"four-zero-three" ,
141
141
name :"You've hit max data"
142
142
} ;
143
- if ( is_authed ) {
144
- topush . message = "Apparently, people have been using our app too much..." ;
143
+ if ( is_authed === 1 ) {
144
+ topush . message = "Apparently, people have been using our app too much..." +
145
+ "<button onclick='login()'>Log in</button>" ;
145
146
} else {
146
147
topush . message = "If you'd like to continue, please " +
147
- "<button onclick='hello(\"github\"). login()'>Log in</button>" ;
148
+ "<button onclick='login()'>Log in</button>" ;
148
149
}
149
150
errors . push ( topush ) ;
150
151
}
152
+
151
153
NodeOsBlog . controller ( 'ErrorListCtrl' , function ( $scope ) {
152
154
$scope . removeError = function ( error ) {
153
155
var l = errors . length ;
@@ -163,89 +165,239 @@ <h3 class="footer-title">Share</h3>
163
165
}
164
166
} ;
165
167
} ) ;
166
- var hello = require ( "hello" ) ;
167
- hello . on ( "auth.login" , function ( auth ) {
168
- hello ( auth . network ) . api ( "/me" ) . then ( function ( r ) {
169
- document . querySelector ( ".four-zero-three .content" ) . innerHTML ( "You've authenticated!" ) ;
168
+ /* */
169
+ function parseMarkdown ( item , next ) {
170
+ uriAsAuthority ( "https://api.github.com/markdown/raw" , function ( uri ) {
171
+ jQuery . ajax ( {
172
+ url :uri ,
173
+ type :'POST' ,
174
+ headers : {
175
+ 'Content-Type' : 'text/plain'
176
+ } ,
177
+ data :item . body
178
+ } ) . done ( function ( data ) {
179
+ item . bodyHTML = data ;
180
+ } ) . fail ( function ( response , type , title , config ) {
181
+ if ( response . status === 403 ) {
182
+ add403 ( ) ;
183
+ } else {
184
+ errors . push ( { name :"Bad markdown call: " + response . status , message : data . message } ) ;
185
+ }
186
+ item . bodyHTML = "<pre>" + item . body + "</pre>" ;
187
+ } ) . always ( function ( ) {
188
+ next ( item ) ;
189
+ } ) ;
170
190
} ) ;
171
- } ) ;
191
+ }
192
+
193
+ /*
194
+ var markdown = require("markdown").markdown;
195
+ function parseMarkdown(item,next){
196
+ console.log("parsing");
197
+
198
+ try{
199
+ var t = jQuery("<div>"+markdown.toHTML(item.body)+"</div>");
200
+ t.find("code").wrap("<pre></pre>");
201
+
202
+ item.bodyHTML = t.html();
203
+ console.log(item.bodyHTML);
204
+ }catch(e){
205
+ console.log(e);
206
+ item.bodyHTML = "<pre>"+item.body+"</pre>";
207
+ }
208
+ setTimeout(next.bind(next,item),1);
209
+ }
210
+ /* */
211
+ function CookieStore ( id ) {
212
+ this . id = id ;
213
+ var cook = document . cookie . split ( ";" ) ;
214
+ var current ;
215
+ var i ;
216
+ for ( i = 0 ; i < cook . length ; i ++ ) {
217
+ if ( cook [ i ] . indexOf ( "=" ) != - 1 ) {
218
+ current = cook [ i ] . split ( "=" ) ;
219
+ if ( current [ 0 ] . replace ( / s / , "" ) == id ) {
220
+ this . cookie = JSON . parse ( decodeURIComponent ( current [ 1 ] ) ) ;
221
+ return ;
222
+ }
223
+ }
224
+ }
225
+ this . cookie = { } ;
226
+ this . save ( ) ;
227
+ }
228
+
229
+ CookieStore . prototype . get = function ( key ) {
230
+ return this . cookie [ key ] ;
231
+ } ;
232
+ CookieStore . prototype . set = function ( key , value ) {
233
+ this . cookie [ key ] = value ;
234
+ this . save ( ) ;
235
+ return this ;
236
+ } ;
237
+ CookieStore . prototype . delete = function ( key ) {
238
+ delete this . cookie [ key ] ;
239
+ this . save ( ) ;
240
+ return this ;
241
+ } ;
242
+
243
+ CookieStore . prototype . save = function ( ) {
244
+ document . cookie = this . id + "=" + encodeURIComponent ( JSON . stringify ( this . cookie ) ) + ";" ;
245
+ } ;
246
+
247
+ //==========Auth Start===================
248
+
249
+
250
+ var auth = new CookieStore ( "auth" ) ;
251
+ var is_authed = - 1 ;
252
+ var auth_queue = [ ] ;
253
+ var url = require ( "url" ) ;
254
+ var querystring = require ( "querystring" ) ;
255
+ var docuri = url . parse ( window . location . href ) ;
256
+ docuri . query = querystring . parse ( docuri . query ) ;
257
+ var config = {
258
+ cid : "dafb27cb88db35267e75" ,
259
+ yqluri : "store://gdDAnJkTXAuVgzAQ8wboA2"
260
+ } ;
261
+ if ( docuri . query && docuri . query . code ) {
262
+ if ( docuri . query . state != auth . get ( "state" ) ) {
263
+ errors . push ( new Error ( "Improper State" ) ) ;
264
+ } else {
265
+ getAccess ( docuri . query . code ) ;
266
+ }
267
+ } else if ( auth . get ( "access_token" ) ) {
268
+ console . log ( auth . get ( "access_token" ) ) ;
269
+ is_authed = 1 ;
270
+ }
271
+
272
+ function login ( ) {
273
+ var state = Date . now ( ) + "_" + Math . random ( ) ;
274
+ auth . set ( "state" , state ) ;
275
+ window . location . href = "https://github.com/login/oauth/authorize" +
276
+ "?client_id=" + config . cid +
277
+ "&state=" + state ;
278
+ }
279
+ function getAccess ( code ) {
280
+ is_authed = 0 ;
281
+ auth . delete ( "state" ) ;
282
+ jQuery . get ( compileYQL ( [
283
+ "env \"" + config . yqluri + "\"" ,
284
+ "select * from github where CODE=\"" + code + "\""
285
+ ] ) ) . done ( function ( results ) {
286
+ console . log ( "auth success" ) ;
287
+ console . log ( arguments ) ;
288
+ auth . set ( "access_token" , results . query . results . OAuth . access_token ) ;
289
+ is_authed = 1 ;
290
+ jQuery ( ".four-zero-three .content" ) . text ( "You've authenticated!" ) ;
291
+ } ) . fail ( function ( e ) {
292
+ console . error ( arguments ) ;
293
+ is_authed = - 1 ;
294
+ errors . push ( {
295
+ class :"four-zero-three" ,
296
+ name :"You've failed authorization" ,
297
+ message : "You can always try again"
298
+ } ) ;
299
+ } ) . always ( function ( ) {
300
+ console . log ( "always" ) ;
301
+ while ( auth_queue . length ) {
302
+ uriAsAuthority . apply ( void ( 0 ) , auth_queue . pop ( ) ) ;
303
+ }
304
+ } ) ;
305
+ }
306
+
307
+ function compileYQL ( query ) {
308
+ if ( Array . isArray ( query ) ) {
309
+ query = query . join ( ";" ) ;
310
+ }
311
+ if ( typeof query != "string" ) {
312
+ throw new Error ( "A yql query must be a string" ) ;
313
+ }
314
+ return "https://query.yahooapis.com/v1/public/yql?q=" +
315
+ encodeURIComponent ( query ) +
316
+ "&diagnostics=true&format=json" ;
317
+ }
318
+
319
+ function uriAsAuthority ( uri , next ) {
320
+ if ( is_authed === - 1 ) return next ( uri ) ;
321
+ if ( is_authed === 0 ) return auth_queue . push ( [ uri , next ] ) ;
322
+ uri = url . parse ( uri ) ;
323
+ uri . query = querystring . parse ( uri . query ) ;
324
+ uri . query . access_token = auth . get ( "access_token" ) ;
325
+ uri . search = "?" + querystring . stringify ( uri . query ) ;
326
+ next ( url . format ( uri ) ) ;
327
+ }
328
+
329
+ function logout ( ) {
330
+ auth . delete ( "access_token" ) ;
331
+ is_authed = - 1 ;
332
+ }
172
333
var hash = document . location . toString ( ) . split ( "#" ) [ 1 ] ;
173
334
NodeOsBlog . controller ( 'BlogSingleCtrl' , function ( $scope , $http ) {
174
335
$scope . uriPath = "/NodeOS/NodeOS/issues/" + hash ;
175
336
$scope . blog = [ ] ;
337
+ $scope . uriAsAuthority = uriAsAuthority ;
338
+ $scope . parseMarkdown = parseMarkdown ;
176
339
177
340
var markdown = require ( "markdown" ) . markdown ;
178
- $scope . parseMarkdown = function ( item , next ) {
179
- try {
180
- item . bodyHTML = markdown . toHTML ( item . body ) ;
181
- } catch ( e ) {
182
- item . bodyHTML = "<pre>" + item . body + "</pre>" ;
183
- }
184
- next ( item ) ;
185
- } ;
186
- $http . get ( 'https://api.github.com/repos' + $scope . uriPath )
187
- . success ( function ( data , status , headers ) {
188
- var l = data . labels . length ;
189
- while ( l -- ) {
190
- if ( data . labels [ l ] . name === "blog" ) {
191
- break ;
341
+ $scope . uriAsAuthority ( 'https://api.github.com/repos' + $scope . uriPath , function ( uri ) {
342
+ $http . get ( uri ) . success ( function ( data , status , headers ) {
343
+ var l = data . labels . length ;
344
+ while ( l -- ) {
345
+ if ( data . labels [ l ] . name === "blog" ) {
346
+ break ;
347
+ }
192
348
}
193
- }
194
- if ( l < 0 ) {
195
- return errors . push ( {
196
- name :"Trying to load a non-blog?" ,
197
- message : "I technically can't stop you since this is clientside." +
198
- " Hopefully my code feels clean enough to hack"
349
+ if ( l < 0 ) {
350
+ return errors . push ( {
351
+ name :"Trying to load a non-blog?" ,
352
+ message : "I technically can't stop you since this is clientside." +
353
+ " Hopefully my code feels clean enough to hack"
354
+ } ) ;
355
+ }
356
+ $scope . parseMarkdown ( data , function ( item ) {
357
+ $scope . blog . push ( item ) ;
358
+ $scope . $apply ( ) ;
199
359
} ) ;
200
- }
201
- $scope . parseMarkdown ( data , function ( item ) {
202
- $scope . blog . push ( item ) ;
360
+ } ) . error ( function ( data , status , headers , config ) {
361
+ if ( status === 403 ) {
362
+ add403 ( ) ;
363
+ } else {
364
+ errors . push ( { name :"Bad issues list request: " + status , message : data . message } ) ;
365
+ }
203
366
} ) ;
204
- } ) . error ( function ( data , status , headers , config ) {
205
- if ( status === 403 ) {
206
- add403 ( ) ;
207
- } else {
208
- errors . push ( { name :"Bad issues list request: " + status , message : data . message } ) ;
209
- }
210
367
} ) ;
211
368
} ) ;
212
369
NodeOsBlog . controller ( 'CommentListCtrl' , function ( $scope , $http ) {
213
370
$scope . uriPath = "/NodeOS/NodeOS/issues/" + hash + "/comments" ;
214
371
$scope . blog = [ ] ;
215
- $scope . parseMarkdown = function ( item , next ) {
216
- $http . post ( "https://api.github.com/markdown" , { text :item . body } )
217
- . success ( function ( data ) {
218
- item . bodyHTML = data ;
219
- next ( item ) ;
220
- } ) . error ( function ( data , status , headers , config ) {
221
- errors . push ( { name :"Bad markdown call: " + status , message : data . message } ) ;
222
- item . bodyHTML = "<pre>" + item . body + "</pre>" ;
223
- next ( item ) ;
224
- } ) ;
225
- } ;
226
372
$scope . last = void ( 0 ) ;
373
+ $scope . uriAsAuthority = uriAsAuthority ;
374
+ $scope . parseMarkdown = parseMarkdown ;
227
375
$scope . loadMore = function ( page ) {
228
376
if ( $scope . last && $scope . last < page ) return ;
229
377
var i = 0 ;
230
378
var l = - 1 ;
231
- $http . get ( 'https://api.github.com/repos' + $scope . uriPath + '?labels=blog&sort=created&page=' + page )
232
- . success ( function ( data , status , headers ) {
233
- if ( ! $scope . last ) $scope . last = headers . link ?headers . link . split ( "=" ) . pop ( ) :1 ;
234
- l = data . length ;
235
- if ( i === l ) return ; //No more
236
- var iterator = function ( item ) {
237
- $scope . blog . push ( item ) ;
238
- i ++ ;
239
- if ( i === l ) return ;
240
- $scope . parseMarkdown ( data [ i ] , iterator ) ;
241
- } ;
242
- $scope . parseMarkdown ( data [ 0 ] , iterator ) ;
243
- } ) . error ( function ( data , status , headers , config ) {
244
- if ( status === 403 ) {
245
- add403 ( ) ;
246
- } else {
247
- errors . push ( { name :"Bad issues list request: " + status , message : data . message } ) ;
248
- }
379
+ $scope . uriAsAuthority (
380
+ 'https://api.github.com/repos' + $scope . uriPath + '?labels=blog&sort=created&page=' + page ,
381
+ function ( uri ) {
382
+ $http . get ( uri ) . success ( function ( data , status , headers ) {
383
+ if ( ! $scope . last ) $scope . last = headers . link ?headers . link . split ( "=" ) . pop ( ) :1 ;
384
+ l = data . length ;
385
+ if ( i === l ) return ; //No more
386
+ var iterator = function ( item ) {
387
+ $scope . blog . push ( item ) ;
388
+ $scope . $apply ( ) ;
389
+ i ++ ;
390
+ if ( i === l ) return ;
391
+ $scope . parseMarkdown ( data [ i ] , iterator ) ;
392
+ } ;
393
+ $scope . parseMarkdown ( data [ 0 ] , iterator ) ;
394
+ } ) . error ( function ( data , status , headers , config ) {
395
+ if ( status === 403 ) {
396
+ add403 ( ) ;
397
+ } else {
398
+ errors . push ( { name :"Bad issues list request: " + status , message : data . message } ) ;
399
+ }
400
+ } ) ;
249
401
} ) ;
250
402
} ;
251
403
$scope . loadMore ( 1 ) ;
0 commit comments