aboutsummaryrefslogtreecommitdiffstats
path: root/apidoc/main.js
blob: bdcf58cbf59840ed9661db292835c7976c682348 (plain) (blame)
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
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.custom.min",
    pathToRegexp: "./vendor/path-to-regexp/index",
    prismjs: "./vendor/prism",
    semver: "./vendor/semver.min",
    utilsSampleRequest: "./utils/send_sample_request",
    webfontloader: "./vendor/webfontloader",
    list: "./vendor/list.min",
    apiData: "./api_data",
    apiProject: "./api_project"
  },
  shim: {
    bootstrap: {
      deps: ["jquery"]
    },
    diffMatchPatch: {
      exports: "diff_match_patch"
    },
    handlebars: {
      exports: "Handlebars"
    },
    handlebarsExtended: {
      deps: ["jquery", "handlebars"],
      exports: "Handlebars"
    },
    prismjs: {
      exports: "Prism"
    }
  },
  urlArgs: "v=" + new Date().getTime(),
  waitSeconds: 150
});

require([
  "jquery",
  "lodash",
  "locales",
  "handlebarsExtended",
  "apiProject",
  "apiData",
  "prismjs",
  "utilsSampleRequest",
  "semver",
  "webfontloader",
  "bootstrap",
  "pathToRegexp",
  "list"
], function(
  $,
  _,
  locale,
  Handlebars,
  apiProject,
  apiData,
  Prism,
  sampleRequest,
  semver,
  WebFont
) {
  // Load google web fonts.
  WebFont.load({
    active: function() {
      // Only init after fonts are loaded.
      init(
        $,
        _,
        locale,
        Handlebars,
        apiProject,
        apiData,
        Prism,
        sampleRequest,
        semver
      );
    },
    inactive: function() {
      // Run init, even if loading fonts fails
      init(
        $,
        _,
        locale,
        Handlebars,
        apiProject,
        apiData,
        Prism,
        sampleRequest,
        semver
      );
    },
    google: {
      families: ["Source Code Pro", "Source Sans Pro:n4,n6,n7"]
    }
  });
});

function init(
  $,
  _,
  locale,
  Handlebars,
  apiProject,
  apiData,
  Prism,
  sampleRequest,
  semver
) {
  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());

  //
  // Default host url used if no sampleUrl is present in config
  //
  var baseURL = window.location.origin;

  //
  // 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);

  if (apiProject.template.aloneDisplay == null)
    apiProject.template.aloneDisplay = false;

  // 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(semver.compare);
  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,
            url: entry.url
          });
        } else {
          nav.push({
            title: entry.title,
            group: group,
            hidden: true,
            name: entry.name,
            type: entry.type,
            version: entry.version,
            url: entry.url
          });
        }
        oldName = entry.name;
      }
    });
  });

  /**
   * Add navigation items by analyzing the HTML content and searching for h1 and h2 tags
   * @param nav Object the navigation array
   * @param content string the compiled HTML content
   * @param index where to insert items
   * @return boolean true if any good-looking (i.e. with a group identifier) <h1> tag was found
   */
  function add_nav(nav, content, index) {
    var found_level1 = false;
    if (!content) {
      return found_level1;
    }
    var topics = content.match(/<h(1|2).*?>(.+?)<\/h(1|2)>/gi);
    if (topics) {
      topics.forEach(function(entry) {
        var level = entry.substring(2, 3);
        var title = entry.replace(/<.+?>/g, ""); // Remove all HTML tags for the title
        var entry_tags = entry.match(/id="api-([^\-]+)(?:-(.+))?"/); // Find the group and name in the id property
        var group = entry_tags ? entry_tags[1] : null;
        var name = entry_tags ? entry_tags[2] : null;
        if (level == 1 && title && group) {
          nav.splice(index, 0, {
            group: group,
            isHeader: true,
            title: title,
            isFixed: true
          });
          index++;
          found_level1 = true;
        }
        if (level == 2 && title && group && name) {
          nav.splice(index, 0, {
            group: group,
            name: name,
            isHeader: false,
            title: title,
            isFixed: false,
            version: "1.0"
          });
          index++;
        }
      });
    }
    return found_level1;
  }

  // Mainmenu Header entry
  if (apiProject.header) {
    var found_level1 = add_nav(nav, apiProject.header.content, 0); // Add level 1 and 2 titles
    if (!found_level1) {
      // If no Level 1 tags were found, make a title
      nav.unshift({
        group: "_",
        isHeader: true,
        title:
          apiProject.header.title == null
            ? locale.__("General")
            : apiProject.header.title,
        isFixed: true
      });
    }
  }

  // Mainmenu Footer entry
  if (apiProject.footer) {
    var last_nav_index = nav.length;
    var found_level1 = add_nav(nav, apiProject.footer.content, nav.length); // Add level 1 and 2 titles
    if (!found_level1 && apiProject.footer.title != null) {
      // If no Level 1 tags were found, make a title
      nav.splice(last_nav_index, 0, {
        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].hasOwnProperty(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]
          };
        }

        if (apiProject.sampleUrl == false) {
          fields.article.sampleRequest = [
            {
              url: baseURL + fields.article.url
            }
          ];
        }

        // add prefix URL for endpoint unless it's already absolute
        if (apiProject.url) {
          if (fields.article.url.substr(0, 4).toLowerCase() !== "http") {
            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,
          aloneDisplay: apiProject.template.aloneDisplay
        });
        oldName = entry.name;
      }
    });

    // render Section with Articles
    var fields = {
      group: groupEntry,
      title: title,
      description: description,
      articles: articles,
      aloneDisplay: apiProject.template.aloneDisplay
    };
    content += templateSections(fields);
  });
  $("#sections").append(content);

  // Bootstrap Scrollspy
  $(this).scrollspy({ target: "#scrollingNav" });

  // 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");
    });

  /**
   * 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) {
      result =
        result ||
        _.some(fields[name], function(item) {
          return item.type;
        });
    });
    return result;
  }

  /**
   * On Template changes, recall plugins.
   */
  function initDynamic() {
    // Bootstrap popover
    $('button[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 header-content-type switch
    $(".sample-header-content-type-switch").change(function() {
      var paramName = "." + $(this).attr("name") + "-fields";
      var bodyName = "." + $(this).attr("name") + "-body";
      var selectName = "select[name=" + $(this).attr("name") + "]";
      if ($(this).val() == "body-json") {
        $(selectName).val("undefined");
        $(this).val("body-json");
        $(paramName).removeClass("hide");
        $(this)
          .parent()
          .nextAll(paramName)
          .first()
          .addClass("hide");
        $(bodyName).addClass("hide");
        $(this)
          .parent()
          .nextAll(bodyName)
          .first()
          .removeClass("hide");
      } else if ($(this).val() == "body-form-data") {
        $(selectName).val("undefined");
        $(this).val("body-form-data");
        $(bodyName).addClass("hide");
        $(paramName).removeClass("hide");
      } else {
        $(this)
          .parent()
          .nextAll(paramName)
          .first()
          .removeClass("hide");
        $(this)
          .parent()
          .nextAll(bodyName)
          .first()
          .addClass("hide");
      }
      $(this)
        .prev(".sample-request-switch")
        .prop("checked", true);
    });

    // sample request switch
    $(".sample-request-switch").click(function(e) {
      var paramName = "." + $(this).attr("name") + "-fields";
      var bodyName = "." + $(this).attr("name") + "-body";
      var select = $(this)
        .next("." + $(this).attr("name") + "-select")
        .val();
      if ($(this).prop("checked")) {
        if (select == "body-json") {
          $(this)
            .parent()
            .nextAll(bodyName)
            .first()
            .removeClass("hide");
        } else {
          $(this)
            .parent()
            .nextAll(paramName)
            .first()
            .removeClass("hide");
        }
      } else {
        if (select == "body-json") {
          $(this)
            .parent()
            .nextAll(bodyName)
            .first()
            .addClass("hide");
        } else {
          $(this)
            .parent()
            .nextAll(paramName)
            .first()
            .addClass("hide");
        }
      }
    });

    if (apiProject.template.aloneDisplay) {
      //show group
      $(".show-group").click(function() {
        var apiGroup = "." + $(this).attr("data-group") + "-group";
        var apiGroupArticle = "." + $(this).attr("data-group") + "-article";
        $(".show-api-group").addClass("hide");
        $(apiGroup).removeClass("hide");
        $(".show-api-article").addClass("hide");
        $(apiGroupArticle).removeClass("hide");
      });

      //show api
      $(".show-api").click(function() {
        var apiName = "." + $(this).attr("data-name") + "-article";
        var apiGroup = "." + $(this).attr("data-group") + "-group";
        $(".show-api-group").addClass("hide");
        $(apiGroup).removeClass("hide");
        $(".show-api-article").addClass("hide");
        $(apiName).removeClass("hide");
      });
    }

    // call scrollspy refresh method
    $(window).scrollspy("refresh");

    // init modules
    sampleRequest.initDynamic();
    Prism.highlightAll();
  }
  initDynamic();

  if (apiProject.template.aloneDisplay) {
    var hashVal = window.location.hash;
    if (hashVal != null && hashVal.length !== 0) {
      $("." + hashVal.slice(1) + "-init").click();
    }
  }

  //
  // HTML-Template specific jQuery-Functions
  //
  // Change Main Version
  function setMainVersion(selectedVersion) {
    if (typeof selectedVersion === "undefined") {
      selectedVersion = $("#version strong").html();
    } else {
      $("#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 (semver.lte(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"
          );
        }
      }
    });

    // show 1st equal or lower Version of each entry
    $("article[data-version]").each(function(index) {
      var group = $(this).data("group");
      $("section#api-" + group).removeClass("hide");
      if ($("section#api-" + group + " article:visible").length === 0) {
        $("section#api-" + group).addClass("hide");
      } else {
        $("section#api-" + group).removeClass("hide");
      }
    });

    initDynamic();
    return;
  }
  setMainVersion();

  $("#versions li.version a").on("click", function(e) {
    e.preventDefault();

    setMainVersion($(this).html());
  });

  // 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("[\\?&amp;]" + name + "=([^&amp;#]*)").exec(
      window.location.href
    );
    return results && results[1] ? results[1] : null;
  };

  if ($.urlParam("compare")) {
    // URL Paramter ?compare=1 is set
    $("#compareAllWithPredecessor").trigger("click");
  }

  // Quick jump on page load to hash position.
  // Should happen after setting the main version
  // and after triggering the click on the compare button,
  // as these actions modify the content
  // and would make it jump to the wrong position or not jump at all.
  if (window.location.hash) {
    var id = decodeURI(window.location.hash);
    if ($(id).length > 0)
      $("html,body").animate({ scrollTop: parseInt($(id).offset().top) }, 0);
  }

  /**
   * Initialize search
   */
  var options = {
    valueNames: ["nav-list-item", "nav-list-url-item"]
  };
  var endpointsList = new List("scrollingNav", options);

  /**
   * Set initial focus to search input
   */
  $("#scrollingNav .sidenav-search input.search").focus();

  /**
   * Detect ESC key to reset search
   */
  $(document).keyup(function(e) {
    if (e.keyCode === 27) $("span.search-reset").click();
  });

  /**
   * Search reset
   */
  $("span.search-reset").on("click", function() {
    $("#scrollingNav .sidenav-search input.search")
      .val("")
      .focus();
    endpointsList.search();
  });

  /**
   * 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();
  }

  /**
   * Sort the fields.
   */
  function sortFields(fields_object) {
    $.each(fields_object, function(key, fields) {
      // Find only object fields
      var objects = fields.filter(function(item) {
        return item.type === "Object";
      });

      // Check if has any object
      if (objects.length === 0) {
        return;
      }

      // Iterate over all objects
      for (var object of objects) {
        // Retrieve the index
        var index = fields.indexOf(object);

        // Find all child fields for this object
        var objectFields = fields.filter(function(item) {
          return item.field.indexOf(object.field + ".") > -1;
        });

        // Get the child index
        var firstIndex = fields.indexOf(objectFields[0]);

        // Put the object it before the first child index
        fields.splice(index, 1);
        fields.splice(firstIndex, 0, object);

        // Startup the last index with the object index
        var lastIndex = firstIndex;

        // Iterate over all children
        for (var child of objectFields) {
          lastIndex++;

          // Retrieve the index
          var childIndex = fields.indexOf(child);

          // Put it after the object declaration
          fields.splice(childIndex, 1);
          fields.splice(lastIndex, 0, child);
        }
      }

      // Retrieve the first object field index
      var firstObjectIndex = fields.indexOf(objects[0]);

      // Find all non-object fields that doesn't contain dot notation
      var nonObjects = fields.filter(function(item) {
        return item.field.indexOf(".") === -1 && item.type !== "Object";
      });

      // Iterate over all non-objects
      for (var nonObject of nonObjects) {
        // Put it before the first object field
        var index = fields.indexOf(nonObject);
        fields.splice(index, 1);
        fields.splice(firstObjectIndex, 0, nonObject);
      }
    });
  }

  /**
   * 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) {
      sortFields(entry.header.fields);
      fields._hasTypeInHeaderFields = _hasTypeInFields(entry.header.fields);
    }

    if (entry.parameter && entry.parameter.fields) {
      sortFields(entry.parameter.fields);
      fields._hasTypeInParameterFields = _hasTypeInFields(
        entry.parameter.fields
      );
    }

    if (entry.error && entry.error.fields) {
      sortFields(entry.error.fields);
      fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
    }

    if (entry.success && entry.success.fields) {
      sortFields(entry.success.fields);
      fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
    }

    if (entry.info && entry.info.fields) {
      sortFields(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 needs to be reassigned (should actually work with on ... automatically)
    $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;
  }

  /**
   * 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[0]; // reference keep for sorting
          if (key == name || parts[1] == 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;
  }
  Prism.highlightAll();
}