-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
565 lines (464 loc) · 16.6 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// luapower.com (Cosmin Apreutesei, public domain)
// helpers
// -----------------------------------------------------------------------------------------------------------------------
var LOCAL = window.location.protocol == 'file:'
function current_package(packages) {
return PROJECT && packages[PROJECT]
}
function current_module(packages) {
var pkg = current_package(packages)
if (!pkg) return
return pkg.modules[DOCNAME]
}
function ellipsis(s, maxlen) {
return s.substring(0, maxlen-1) + (s.length <= maxlen ? '' : '...')
}
function github_api_(url, success) {
$.ajax({
url: 'https://api.github.com/' + url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=?',
cache: true, // because of github's aggressive throttling policies
dataType: 'jsonp',
jsonpCallback:'ghajpc', // a global function will be created so careful what you name here
success: success,
})
}
function github_api(url, success) {
var try_again
try_again = function(data) {
if (data.meta.status == 304) { // github api is shit
github_api_(url, try_again)
} else {
success(data)
}
}
github_api_(url, try_again)
}
function github_date(date) {
return strftime('%b %d, %Y', new Date(date))
}
function ahref(href, text, attrs) {
return '<a' + (attrs ? ' ' + attrs : '') + (href ? ' href="' + href + '"' : '') + '>' + text + '</a>'
}
function link(link, attrs) {
return ahref(link[1], link[0], attrs)
}
// rendering -----------------------------------------------------------------
function template_object(name) {
return $('#' + name + '_template')
}
function load_partial_(name) {
return template_object(name).html()
}
function render(template_name, data, dst) {
var t = template_object(template_name).html()
var s = Mustache.render(t, data || {}, load_partial_)
if (dst)
$(dst).html(s)
return s
}
// lights
// -----------------------------------------------------------------------------------------------------------------------
function get_lights_state() { return jQuery.cookie('lights') == 'on' }
function set_lights_state(on) { jQuery.cookie('lights', on ? 'on' : 'off') }
function set_lights_button_text(on) {
jQuery('#lights').html('lights ' + (on ? 'off' : 'on'))
}
function set_lights(on) {
if (on !== true && on !== false)
on = get_lights_state()
var css = on ? 'light' : 'dark'
jQuery('#lights_css').attr('href', 'templates/hack_' + css + '.css')
set_lights_state(on)
set_lights_button_text(on)
}
function set_lights_button() {
// there was no button to set when the lights was set so we set it now
set_lights_button_text(get_lights_state())
$('#lights').click(function() {
set_lights(!get_lights_state())
return false
})
}
// package db
// -----------------------------------------------------------------------------------------------------------------------
// package tables
function set_concise_package_table(packages) {
//sorted list of categories and item count
var cats = {}
var n = 0
$.each(packages, function(pkg, t) {
cats[t.category] = {}
n = n + 1
})
var cat_list = Object.keys(cats).sort()
n = n + cat_list.length
//attach packages to their categories
$.each(packages, function(pkg, t) {
cats[t.category][pkg] = t
})
//build the final list of categories and packages
var name_list = []
$.each(cat_list, function(i, cat) {
name_list.push('<td><strong style="font-size: 100%; text-transform: uppercase; font-weight: normal;">' +
cat + '</strong></td>')
$.each(Object.keys(cats[cat]).sort(), function(i, pkg) {
var t = cats[cat][pkg]
name_list.push('<td>' + (t.link && link(t.link) || pkg) + '</td>')
})
})
//distribute name_list over a n-col table in top-to-bottom-then-left-to-right order
var s = '<table width="100%">'
var cols = 5
var rows = Math.ceil(n / cols)
for(var i = 0; i < rows; i++) {
s = s + '<tr>'
for(var j = 0; j < cols; j++)
s = s + name_list[j * rows + i] || '<td></td>'
s = s + '</tr>'
}
$('#package_table').html(s)
}
function set_long_package_table(packages) {
var s = '<table id="package_table_table" width="100%"><thead>' +
'<tr>' +
'<th title="Hold Shift to sort by multiple columns" class="header" style="width: 1%;">Type</th>' +
'<th title="Hold Shift to sort by multiple columns" class="header" style="width: 1%;">Package</th>' +
'<th style="width: 20%;" data-sorter="false" class="header" >What</th>' +
'<th title="Hold Shift to sort by multiple columns" class="header" style="width: 1%;">Tag</th>' +
'<th title="Hold Shift to sort by multiple columns" class="header" style="width: 1%;">Platforms</th>' +
'<th title="Hold Shift to sort by multiple columns" class="header" style="width: 1%;">License</th>' +
'</tr></thead><tbody>'
var platforms = ['mingw32', 'linux32', 'osx64', 'android']
$.each(packages, function(k, t) {
s = s + '<tr>'
s = s + '<td>' + t.type + '</td>'
s = s + '<td>' + link(t.link) + '</td>'
s = s + '<td>' + (t.tagline || '') + '</td>'
s = s + '<td>' + (t.git_tag || '') + '</td>'
s = s + '<td style="min-width: 136px;" >'
//platform icons: display and sorting order
var has_lua = t.type.indexOf('Lua') >= 0
var has_ffi = t.type.indexOf('ffi') >= 0
var imgs1 = ''
if (has_ffi)
imgs1 += '<div title="LuaJIT" class="icon icon-luajit-enabled"></div>'
else if (has_lua)
imgs1 += '<div title="Lua" class="icon icon-lua-enabled"></div>'
else
imgs1 += '<div class="icon"></div>'
var pn = 0 // number of platforms
var ps = '' // platforms sort string
var imgs = ''
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i]
var enabled = t.platforms[platform]
imgs += '<div' + (enabled ? ' title="' + platform + '"' : '') +
' class="icon icon-' + platform + '-' + (enabled ? 'enabled' : 'disabled') + '"></div>'
if (enabled) {
pn += 1
ps += platform + ';'
}
}
// if no explicit platforms, remove the disabled buttons
if (pn == 0) imgs = ''
// fix platforms sorting order
if (pn == 0 && has_lua)
pn = platforms.length + (has_ffi ? 1 : 2)
else if (pn > 0)
ps = (has_lua ? (has_ffi ? 1 : 2) : 0) + ';' + ps
s = s + '<span style="display: none;">' + pn + ';' + ps + '</span>' // sort string for tablesorter
s = s + imgs1 + imgs
s = s + '</td>'
s = s + '<td>' + (t.c_license || 'PD') + '</td>'
s = s + '</tr>'
})
s = s + '</tbody></table>'
$('#package_table').html(s)
$('#package_table_table').tablesorter({
cancelSelection: true,
sortList: [[1,0]], //initial sort by name
})
}
function get_table_type_state() { return $.cookie('table_type') || 'long' }
function set_table_type_state(type) { $.cookie('table_type', type) }
function set_package_table(packages) {
if ($('#package_table').length == 0) return
var table_type = get_table_type_state()
if (table_type == 'concise')
set_concise_package_table(packages)
else if (table_type == 'long')
set_long_package_table(packages)
}
// current package & current module info
function get_package_dependencies(pkg) {
return //disabled
if (pkg.pdep_links.length == 0) return
var t = []
for (var i=0; i < pkg.pdep_links.length; i++)
t.push(link(pkg.pdep_links[i]))
return t.join(', ')
}
function set_package_info(packages) {
if ($('#package_info').length == 0) return
var pkg = current_package(packages)
if (!pkg) return
var t = []
t.push(pkg.git_tag)
t.push(pkg.type)
if(pkg.c_link)
t.push(link(pkg.c_link) + ' (' + pkg.c_license + ')')
var deps = get_package_dependencies(pkg)
if (deps)
t.push('depends: ' + deps)
$('#package_info').html(t.join(' | '))
}
function set_module_info(packages) {
if ($('#module_info').length == 0) return
var mod = current_module(packages)
if (!mod) return
// list module module dependencies
var pdeps = []
for (var i=0; i < mod.pdep_links.length; i++)
pdeps.push(link(mod.pdep_links[i]))
// list module package dependencies
var mdeps = []
for (var i=0; i < mod.mdep_links.length; i++)
mdeps.push(link(mod.mdep_links[i]))
var tt = []
if (mod.source_link) tt.push(link(mod.source_link))
if (mod.test_link) tt.push(link(mod.test_link))
if (mod.demo_link) tt.push(link(mod.demo_link))
if (pdeps.length > 0) tt.push('<span title="package dependencies">depends: ' + pdeps.join(', ') + '</span>')
if (mdeps.length > 0) tt.push('<span title="module dependencies">requires: ' + mdeps.join(', ') + '</span>')
$('#module_info').html(tt.join(' | '))
}
function set_package_tab(packages) {
if (!$('#tab2_container').length) return
var pkg = current_package(packages)
if (!pkg) return
$('#tab2_container').hide()
pkg.module_array = []
for (m in pkg.modules)
pkg.module_array.push(pkg.modules[m])
render('info_tab', pkg, '#tab2_container')
$('#tab1_button').click(function() {
$('#tab2_container').hide()
$('#tab1_container').show()
})
$('#tab2_button').click(function() {
$('#tab1_container').hide()
$('#tab2_container').show()
})
}
function load_package_db() {
if ($('#package_table,#package_info,#module_info').length == 0) {
package_db_loaded()
return
}
$.getJSON('packages.json', function(packages) {
set_package_table(packages)
set_package_info(packages)
set_package_tab(packages)
set_module_info(packages)
load_github_events(packages)
package_db_loaded()
})
}
// toc file
// -----------------------------------------------------------------------------------------------------------------------
function set_toc_tree() {
// find items representing TOC items and style them accordingly.
// also, turn them into links that can expand/collapse the tree leaf.
$('#toc_container li > ul').each(function(i) {
// find this list's parent list item.
var parent_li = $(this).parent('li')
// style the list item as folder.
parent_li.addClass('folder')
// temporarily remove the list from the parent list item,
// wrap the remaining text in an anchor, then reattach it.
var sub_ul = $(this).remove()
if (parent_li.find('a').length == 0)
parent_li.wrapInner('<a/>').find('a').attr('href', 'javascript:void(0)').click(function() {
// Make the anchor toggle the leaf display.
sub_ul.toggle()
})
parent_li.append(sub_ul)
})
// hide all lists except the outermost.
$('#toc_container ul ul').hide()
// expand the list containing the element that links to the current page
// DOCNAME is set in the html template by pandoc
var clink = $('#toc_container li > a[href="' + DOCNAME + '.html"], #toc_container li > a[href="' + DOCNAME + '"]')
if (clink.length > 0) {
clink.parents().show() //expand parents
clink.parent().find('ul').show() //expand children
clink.parent().wrapInner('<span/>').find('span').addClass('toc-selected')
clink.replaceWith(clink.html())
}
}
function load_toc_file() {
if ($('#toc_container').length == 0) {
toc_file_loaded()
return
}
$('#toc_container').load('toc.html', function() {
set_toc_tree()
toc_file_loaded()
})
}
// doc-ready -----------------------------------------------------------------
// make extenral links open in new window
function fix_external_links() {
$("a[href^='http']").not('.btn').each(function() {
if (this.hostname != location.hostname)
$(this).attr('target', '_blank')
})
}
//fix homepage links for file:/// access
function fix_homepage_links() {
if (LOCAL)
$('a[href="/"]').attr('href', 'index.html')
}
// doc-ready dispatcher
var _package_db_loaded = false
var _toc_file_loaded = false
function package_db_loaded() {_package_db_loaded = true; check_ready() }
function toc_file_loaded() { _toc_file_loaded = true; check_ready() }
function check_ready() { if (_package_db_loaded && _toc_file_loaded) doc_ready() }
function doc_ready() {
fix_external_links()
fix_homepage_links()
}
// github commit log
// -----------------------------------------------------------------------------------------------------------------------
function get_commit_log_line(entry) {
var date = github_date(entry.commit.committer.date)
var message = entry.commit.message
var url = 'https://github.com/luapower/' + PROJECT + '/commit/' + entry.sha
if (message == 'unimportant')
return
return ahref(url, ellipsis(message, 26) + ' ' + date,
' title="' + message + '"' +
' class="faint"')
}
function set_commit_log(commits) {
var t = []
for(var i=0, c=0; c < 4 && i < commits.data.length; i++) {
var s = get_commit_log_line(commits.data[i])
if (!s)
continue
t.push(s)
c++
}
$('#package_info').html($('#package_info').html() + '<hr id="package_info_hr">') // TODO: not repeatable!
$('#commit_log').html(t.join('<br>'))
fix_external_links()
}
function load_commit_log() {
if ($('#commit_log').length == 0) return
github_api('repos/luapower/' + PROJECT + '/commits', set_commit_log)
}
// github events
// -----------------------------------------------------------------------------------------------------------------------
function get_repo_link(repo, packages) {
var plink
var project
if (repo.match(/^luapower\//)) { // it's a luapower repo
var project = repo.replace(/^luapower\//, '')
if (project in packages)
plink = link(packages[project].link)
}
// we're not interested in news for non-packages (if we were, we would do the following)
// if(!plink) plink = ahref('https://github.com/' + repo, project || repo)
return plink
}
function add_news_rows(rows, event, packages) {
var maxtext = 50
if (event.type != 'PushEvent' && event.type != 'CreateEvent' && event.type != 'IssuesEvent')
return
var plink = get_repo_link(event.repo.name, packages)
if (!plink) return
var s = '<td style="width: 15%">' + github_date(event.created_at) +
'</td><td style="width: 20%">' + plink + '</td><td style="width: 65%">'
if (event.type == 'PushEvent') {
for (var i = 0; i < event.payload.commits.length; i++) {
var commit = event.payload.commits[i]
var url = 'https://github.com/' + event.repo.name + '/commit/' + commit.sha
if (commit.message != 'unimportant')
rows.push(s + ahref(url, ellipsis(commit.message, maxtext)) + '</td>')
}
} else if (event.type == 'CreateEvent' && event.payload.ref_type == 'tag') {
var url = 'https://github.com/' + event.repo.name + '/tree/' + event.payload.ref
rows.push(s + 'New tag: <b>' + ahref(url, event.payload.ref) + '</b>' + '</td>')
} else if (event.type == 'IssuesEvent') {
var url = 'https://github.com/' + event.repo.name + '/issues/' + event.payload.issue.number
var text = 'issue <b>' + event.payload.action + '</b>: ' +
ahref(url, ellipsis(event.payload.issue.title, maxtext - 15))
rows.push(s + text + '</td>')
}
}
function set_news_table(rows, events, packages) {
var s = '<table width="100%">'
s = s + '<tr>' + rows.join('</tr><tr>') + '</tr>'
s = s + '</table>'
$('#news_table').html(s)
fix_external_links()
}
function load_github_events(packages) {
if ($('#news_table').length == 0) return
var page = 1
var maxpage = 10
var rows = []
var maxrows = 10
var try_next
try_next = function(events) {
for(var i=0; i < events.data.length; i++) {
add_news_rows(rows, events.data[i], packages)
}
if (page < maxpage && rows.length < maxrows) {
page += 1
github_api('orgs/luapower/events?page=' + page, try_next)
} else {
rows = rows.slice(0, maxrows)
set_news_table(rows, events, packages)
}
}
github_api('orgs/luapower/events?page=' + page, try_next)
}
// disqus
// -----------------------------------------------------------------------------------------------------------------------
function load_disqus() {
if (LOCAL) return // don't load offline
if (!disqus_shortname) return //disabled
var disqus_identifier = DOCNAME; //discussion identifier (when a page will be renamed, the comments will be gone!)
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}
// analytics
// -----------------------------------------------------------------------------------------------------------------------
function load_analytics() {
if (LOCAL) return //don't load offline
if (!analytics_id || !analytics_website) return //disabled
(function(i,s,o,g,r,a,m) {
i['GoogleAnalyticsObject']=r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1*new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async=1;
a.src=g;
m.parentNode.insertBefore(a,m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', analytics_id, analytics_website);
ga('send', 'pageview');
}
// main
// -----------------------------------------------------------------------------------------------------------------------
jQuery(set_lights_button)
jQuery(load_package_db)
jQuery(load_toc_file)
jQuery(load_commit_log)
jQuery(load_disqus)
jQuery(load_analytics)