main.js
23.2 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
require.config({
paths: {
bootstrap: './vendor/bootstrap.min',
diffMatchPatch: './vendor/diff_match_patch.min',
handlebars: './vendor/handlebars.min',
handlebarsExtended: './utils/handlebars_helper',
jquery: './vendor/jquery.min',
locales: './locales/locale',
lodash: './vendor/lodash.min',
pathToRegexp: './vendor/path-to-regexp/index',
prettify: './vendor/prettify/prettify',
utilsSampleRequest: './utils/send_sample_request',
},
shim: {
bootstrap: {
deps: ['jquery']
},
diffMatchPatch: {
exports: 'diff_match_patch'
},
handlebars: {
exports: 'Handlebars'
},
handlebarsExtended: {
deps: ['jquery', 'handlebars'],
exports: 'Handlebars'
},
prettify: {
exports: 'prettyPrint'
}
},
urlArgs: 'v=' + (new Date()).getTime(),
waitSeconds: 15
});
require([
'jquery',
'lodash',
'locales',
'handlebarsExtended',
'./api_project.js',
'./api_data.js',
'prettify',
'utilsSampleRequest',
'bootstrap',
'pathToRegexp'
], function($, _, locale, Handlebars, apiProject, apiData, prettyPrint, sampleRequest) {
// load google web fonts
loadGoogleFontCss();
var api = apiData.api;
//
// Templates
//
var templateHeader = Handlebars.compile( $('#template-header').html() );
var templateFooter = Handlebars.compile( $('#template-footer').html() );
var templateArticle = Handlebars.compile( $('#template-article').html() );
var templateCompareArticle = Handlebars.compile( $('#template-compare-article').html() );
var templateGenerator = Handlebars.compile( $('#template-generator').html() );
var templateProject = Handlebars.compile( $('#template-project').html() );
var templateSections = Handlebars.compile( $('#template-sections').html() );
var templateSidenav = Handlebars.compile( $('#template-sidenav').html() );
//
// apiProject defaults
//
if ( ! apiProject.template)
apiProject.template = {};
if (apiProject.template.withCompare == null)
apiProject.template.withCompare = true;
if (apiProject.template.withGenerator == null)
apiProject.template.withGenerator = true;
if (apiProject.template.forceLanguage)
locale.setLanguage(apiProject.template.forceLanguage);
// Setup jQuery Ajax
$.ajaxSetup(apiProject.template.jQueryAjaxSetup);
//
// Data transform
//
// grouped by group
var apiByGroup = _.groupBy(api, function(entry) {
return entry.group;
});
// grouped by group and name
var apiByGroupAndName = {};
$.each(apiByGroup, function(index, entries) {
apiByGroupAndName[index] = _.groupBy(entries, function(entry) {
return entry.name;
});
});
//
// sort api within a group by title ASC and custom order
//
var newList = [];
var umlauts = { 'รค': 'ae', 'รผ': 'ue', 'รถ': 'oe', 'ร': 'ss' }; // TODO: remove in version 1.0
$.each (apiByGroupAndName, function(index, groupEntries) {
// get titles from the first entry of group[].name[] (name has versioning)
var titles = [];
$.each (groupEntries, function(titleName, entries) {
var title = entries[0].title;
if(title !== undefined) {
title.toLowerCase().replace(/[รครถรผร]/g, function($0) { return umlauts[$0]; });
titles.push(title + '#~#' + titleName); // '#~#' keep reference to titleName after sorting
}
});
// sort by name ASC
titles.sort();
// custom order
if (apiProject.order)
titles = sortByOrder(titles, apiProject.order, '#~#');
// add single elements to the new list
titles.forEach(function(name) {
var values = name.split('#~#');
var key = values[1];
groupEntries[key].forEach(function(entry) {
newList.push(entry);
});
});
});
// api overwrite with ordered list
api = newList;
//
// Group- and Versionlists
//
var apiGroups = {};
var apiGroupTitles = {};
var apiVersions = {};
apiVersions[apiProject.version] = 1;
$.each(api, function(index, entry) {
apiGroups[entry.group] = 1;
apiGroupTitles[entry.group] = entry.groupTitle || entry.group;
apiVersions[entry.version] = 1;
});
// sort groups
apiGroups = Object.keys(apiGroups);
apiGroups.sort();
// custom order
if (apiProject.order)
apiGroups = sortByOrder(apiGroups, apiProject.order);
// sort versions DESC
apiVersions = Object.keys(apiVersions);
apiVersions.sort();
apiVersions.reverse();
//
// create Navigationlist
//
var nav = [];
apiGroups.forEach(function(group) {
// Mainmenu entry
nav.push({
group: group,
isHeader: true,
title: apiGroupTitles[group]
});
// Submenu
var oldName = '';
api.forEach(function(entry) {
if (entry.group === group) {
if (oldName !== entry.name) {
nav.push({
title: entry.title,
group: group,
name: entry.name,
type: entry.type,
version: entry.version
});
} else {
nav.push({
title: entry.title,
group: group,
hidden: true,
name: entry.name,
type: entry.type,
version: entry.version
});
}
oldName = entry.name;
}
});
});
// Mainmenu Header entry
if (apiProject.header) {
nav.unshift({
group: '_',
isHeader: true,
title: (apiProject.header.title == null) ? locale.__('General') : apiProject.header.title,
isFixed: true
});
}
// Mainmenu Footer entry
if (apiProject.footer && apiProject.footer.title != null) {
nav.push({
group: '_footer',
isHeader: true,
title: apiProject.footer.title,
isFixed: true
});
}
// render pagetitle
var title = apiProject.title ? apiProject.title : 'apiDoc: ' + apiProject.name + ' - ' + apiProject.version;
$(document).attr('title', title);
// remove loader
$('#loader').remove();
// render sidenav
var fields = {
nav: nav
};
$('#sidenav').append( templateSidenav(fields) );
// render Generator
$('#generator').append( templateGenerator(apiProject) );
// render Project
_.extend(apiProject, { versions: apiVersions});
$('#project').append( templateProject(apiProject) );
// render apiDoc, header/footer documentation
if (apiProject.header)
$('#header').append( templateHeader(apiProject.header) );
if (apiProject.footer)
$('#footer').append( templateFooter(apiProject.footer) );
//
// Render Sections and Articles
//
var articleVersions = {};
var content = '';
apiGroups.forEach(function(groupEntry) {
var articles = [];
var oldName = '';
var fields = {};
var title = groupEntry;
var description = '';
articleVersions[groupEntry] = {};
// render all articles of a group
api.forEach(function(entry) {
if(groupEntry === entry.group) {
if (oldName !== entry.name) {
// determine versions
api.forEach(function(versionEntry) {
if (groupEntry === versionEntry.group && entry.name === versionEntry.name) {
if ( ! articleVersions[entry.group][entry.name])
articleVersions[entry.group][entry.name] = [];
articleVersions[entry.group][entry.name].push(versionEntry.version);
}
});
fields = {
article: entry,
versions: articleVersions[entry.group][entry.name]
};
} else {
fields = {
article: entry,
hidden: true,
versions: articleVersions[entry.group][entry.name]
};
}
// add prefix URL for endpoint
if (apiProject.url)
fields.article.url = apiProject.url + fields.article.url;
addArticleSettings(fields, entry);
if (entry.groupTitle)
title = entry.groupTitle;
// TODO: make groupDescription compareable with older versions (not important for the moment)
if (entry.groupDescription)
description = entry.groupDescription;
articles.push({
article: templateArticle(fields),
group: entry.group,
name: entry.name
});
oldName = entry.name;
}
});
// render Section with Articles
var fields = {
group: groupEntry,
title: title,
description: description,
articles: articles
};
content += templateSections(fields);
});
$('#sections').append( content );
// Bootstrap Scrollspy
var $scrollSpy = $(this).scrollspy({ target: '#scrollingNav', offset: 18 });
$('[data-spy="scroll"]').each(function () {
$scrollSpy('refresh');
});
// Content-Scroll on Navigation click.
$('.sidenav').find('a').on('click', function(e) {
e.preventDefault();
var id = $(this).attr('href');
if ($(id).length > 0)
$('html,body').animate({ scrollTop: parseInt($(id).offset().top) }, 400);
window.location.hash = $(this).attr('href');
});
// Quickjump on Pageload to hash position.
if(window.location.hash) {
var id = window.location.hash;
if ($(id).length > 0)
$('html,body').animate({ scrollTop: parseInt($(id).offset().top) }, 0);
}
/**
* Check if Parameter (sub) List has a type Field.
* Example: @apiSuccess varname1 No type.
* @apiSuccess {String} varname2 With type.
*
* @param {Object} fields
*/
function _hasTypeInFields(fields) {
var result = false;
$.each(fields, function(name) {
if (_.any(fields[name], function(item) { return item.type; }) )
result = true;
});
return result;
}
/**
* On Template changes, recall plugins.
*/
function initDynamic() {
// bootstrap popover
$('a[data-toggle=popover]')
.popover()
.click(function(e) {
e.preventDefault();
})
;
var version = $('#version strong').html();
$('#sidenav li').removeClass('is-new');
if (apiProject.template.withCompare) {
$('#sidenav li[data-version=\'' + version + '\']').each(function(){
var group = $(this).data('group');
var name = $(this).data('name');
var length = $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\']').length;
var index = $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\']').index($(this));
if (length === 1 || index === (length - 1))
$(this).addClass('is-new');
});
}
// tabs
$('.nav-tabs-examples a').click(function (e) {
e.preventDefault();
$(this).tab('show');
});
$('.nav-tabs-examples').find('a:first').tab('show');
// sample request switch
$('.sample-request-switch').click(function (e) {
var name = '.' + $(this).attr('name') + '-fields';
$(name).addClass('hide');
$(this).parent().next(name).removeClass('hide');
});
// init modules
sampleRequest.initDynamic();
}
initDynamic();
// Pre- / Code-Format
prettyPrint();
//
// HTML-Template specific jQuery-Functions
//
// Change Main Version
$('#versions li.version a').on('click', function(e) {
e.preventDefault();
var selectedVersion = $(this).html();
$('#version strong').html(selectedVersion);
// hide all
$('article').addClass('hide');
$('#sidenav li:not(.nav-fixed)').addClass('hide');
// show 1st equal or lower Version of each entry
$('article[data-version]').each(function(index) {
var group = $(this).data('group');
var name = $(this).data('name');
var version = $(this).data('version');
if (version <= selectedVersion) {
if($('article[data-group=\'' + group + '\'][data-name=\'' + name + '\']:visible').length === 0) {
// enable Article
$('article[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + version + '\']').removeClass('hide');
// enable Navigation
$('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + version + '\']').removeClass('hide');
$('#sidenav li.nav-header[data-group=\'' + group + '\']').removeClass('hide');
}
}
});
initDynamic();
return;
});
// compare all article with their predecessor
$('#compareAllWithPredecessor').on('click', changeAllVersionCompareTo);
// change version of an article
$('article .versions li.version a').on('click', changeVersionCompareTo);
// compare url-parameter
$.urlParam = function(name) {
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
return (results && results[1]) ? results[1] : null;
};
if ($.urlParam('compare')) {
// URL Paramter ?compare=1 is set
$('#compareAllWithPredecessor').trigger('click');
if (window.location.hash) {
var id = window.location.hash;
$('html,body').animate({ scrollTop: parseInt($(id).offset().top) - 18 }, 0);
}
}
/**
* Change version of an article to compare it to an other version.
*/
function changeVersionCompareTo(e) {
e.preventDefault();
var $root = $(this).parents('article');
var selectedVersion = $(this).html();
var $button = $root.find('.version');
var currentVersion = $button.find('strong').html();
$button.find('strong').html(selectedVersion);
var group = $root.data('group');
var name = $root.data('name');
var version = $root.data('version');
var compareVersion = $root.data('compare-version');
if (compareVersion === selectedVersion)
return;
if ( ! compareVersion && version == selectedVersion)
return;
if (compareVersion && articleVersions[group][name][0] === selectedVersion || version === selectedVersion) {
// the version of the entry is set to the highest version (reset)
resetArticle(group, name, version);
} else {
var $compareToArticle = $('article[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + selectedVersion + '\']');
var sourceEntry = {};
var compareEntry = {};
$.each(apiByGroupAndName[group][name], function(index, entry) {
if (entry.version === version)
sourceEntry = entry;
if (entry.version === selectedVersion)
compareEntry = entry;
});
var fields = {
article: sourceEntry,
compare: compareEntry,
versions: articleVersions[group][name]
};
// add unique id
// TODO: replace all group-name-version in template with id.
fields.article.id = fields.article.group + '-' + fields.article.name + '-' + fields.article.version;
fields.article.id = fields.article.id.replace(/\./g, '_');
fields.compare.id = fields.compare.group + '-' + fields.compare.name + '-' + fields.compare.version;
fields.compare.id = fields.compare.id.replace(/\./g, '_');
var entry = sourceEntry;
if (entry.parameter && entry.parameter.fields)
fields._hasTypeInParameterFields = _hasTypeInFields(entry.parameter.fields);
if (entry.error && entry.error.fields)
fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
if (entry.success && entry.success.fields)
fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
if (entry.info && entry.info.fields)
fields._hasTypeInInfoFields = _hasTypeInFields(entry.info.fields);
var entry = compareEntry;
if (fields._hasTypeInParameterFields !== true && entry.parameter && entry.parameter.fields)
fields._hasTypeInParameterFields = _hasTypeInFields(entry.parameter.fields);
if (fields._hasTypeInErrorFields !== true && entry.error && entry.error.fields)
fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
if (fields._hasTypeInSuccessFields !== true && entry.success && entry.success.fields)
fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
if (fields._hasTypeInInfoFields !== true && entry.info && entry.info.fields)
fields._hasTypeInInfoFields = _hasTypeInFields(entry.info.fields);
var content = templateCompareArticle(fields);
$root.after(content);
var $content = $root.next();
// Event on.click re-assign
$content.find('.versions li.version a').on('click', changeVersionCompareTo);
// select navigation
$('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + currentVersion + '\']').addClass('has-modifications');
$root.remove();
// TODO: on change main version or select the highest version re-render
}
initDynamic();
}
/**
* Compare all currently selected Versions with their predecessor.
*/
function changeAllVersionCompareTo(e) {
e.preventDefault();
$('article:visible .versions').each(function(){
var $root = $(this).parents('article');
var currentVersion = $root.data('version');
var $foundElement = null;
$(this).find('li.version a').each(function() {
var selectVersion = $(this).html();
if (selectVersion < currentVersion && ! $foundElement)
$foundElement = $(this);
});
if($foundElement)
$foundElement.trigger('click');
});
initDynamic();
}
/**
* Add article settings.
*/
function addArticleSettings(fields, entry) {
// add unique id
// TODO: replace all group-name-version in template with id.
fields.id = fields.article.group + '-' + fields.article.name + '-' + fields.article.version;
fields.id = fields.id.replace(/\./g, '_');
if (entry.header && entry.header.fields)
fields._hasTypeInHeaderFields = _hasTypeInFields(entry.header.fields);
if (entry.parameter && entry.parameter.fields)
fields._hasTypeInParameterFields = _hasTypeInFields(entry.parameter.fields);
if (entry.error && entry.error.fields)
fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
if (entry.success && entry.success.fields)
fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
if (entry.info && entry.info.fields)
fields._hasTypeInInfoFields = _hasTypeInFields(entry.info.fields);
// add template settings
fields.template = apiProject.template;
}
/**
* Render Article.
*/
function renderArticle(group, name, version) {
var entry = {};
$.each(apiByGroupAndName[group][name], function(index, currentEntry) {
if (currentEntry.version === version)
entry = currentEntry;
});
var fields = {
article: entry,
versions: articleVersions[group][name]
};
addArticleSettings(fields, entry);
return templateArticle(fields);
}
/**
* Render original Article and remove the current visible Article.
*/
function resetArticle(group, name, version) {
var $root = $('article[data-group=\'' + group + '\'][data-name=\'' + name + '\']:visible');
var content = renderArticle(group, name, version);
$root.after(content);
var $content = $root.next();
// Event on.click muss neu zugewiesen werden (sollte eigentlich mit on automatisch funktionieren... sollte)
$content.find('.versions li.version a').on('click', changeVersionCompareTo);
$('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + version + '\']').removeClass('has-modifications');
$root.remove();
return;
}
/**
* Load google fonts.
*/
function loadGoogleFontCss() {
var host = document.location.hostname.toLowerCase();
var protocol = document.location.protocol.toLowerCase();
var googleCss = '//fonts.googleapis.com/css?family=Source+Code+Pro|Source+Sans+Pro:400,600,700';
if (host == 'localhost' || !host.length || protocol === 'file:')
googleCss = 'http:' + googleCss;
$('<link/>', {
rel: 'stylesheet',
type: 'text/css',
href: googleCss
}).appendTo('head');
}
/**
* Return ordered entries by custom order and append not defined entries to the end.
* @param {String[]} elements
* @param {String[]} order
* @param {String} splitBy
* @return {String[]} Custom ordered list.
*/
function sortByOrder(elements, order, splitBy) {
var results = [];
order.forEach (function(name) {
if (splitBy)
elements.forEach (function(element) {
var parts = element.split(splitBy);
var key = parts[1]; // reference keep for sorting
if (key == name)
results.push(element);
});
else
elements.forEach (function(key) {
if (key == name)
results.push(name);
});
});
// Append all other entries that ar not defined in order
elements.forEach(function(element) {
if (results.indexOf(element) === -1)
results.push(element);
});
return results;
}
});