main.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. require.config({
  2. paths: {
  3. bootstrap: './vendor/bootstrap.min',
  4. diffMatchPatch: './vendor/diff_match_patch.min',
  5. handlebars: './vendor/handlebars.min',
  6. handlebarsExtended: './utils/handlebars_helper',
  7. jquery: './vendor/jquery.min',
  8. locales: './locales/locale',
  9. lodash: './vendor/lodash.custom.min',
  10. pathToRegexp: './vendor/path-to-regexp/index',
  11. prismjs: './vendor/prism',
  12. semver: './vendor/semver.min',
  13. utilsSampleRequest: './utils/send_sample_request',
  14. webfontloader: './vendor/webfontloader',
  15. list: './vendor/list.min',
  16. apiData: './api_data',
  17. apiProject: './api_project',
  18. },
  19. shim: {
  20. bootstrap: {
  21. deps: ['jquery']
  22. },
  23. diffMatchPatch: {
  24. exports: 'diff_match_patch'
  25. },
  26. handlebars: {
  27. exports: 'Handlebars'
  28. },
  29. handlebarsExtended: {
  30. deps: ['jquery', 'handlebars'],
  31. exports: 'Handlebars'
  32. },
  33. prismjs: {
  34. exports: 'Prism'
  35. },
  36. },
  37. urlArgs: 'v=' + (new Date()).getTime(),
  38. waitSeconds: 150
  39. });
  40. require([
  41. 'jquery',
  42. 'lodash',
  43. 'locales',
  44. 'handlebarsExtended',
  45. 'apiProject',
  46. 'apiData',
  47. 'prismjs',
  48. 'utilsSampleRequest',
  49. 'semver',
  50. 'webfontloader',
  51. 'bootstrap',
  52. 'pathToRegexp',
  53. 'list'
  54. ], function($, _, locale, Handlebars, apiProject, apiData, Prism, sampleRequest, semver, WebFont) {
  55. // Load google web fonts.
  56. WebFont.load({
  57. active: function() {
  58. // Only init after fonts are loaded.
  59. init($, _, locale, Handlebars, apiProject, apiData, Prism, sampleRequest, semver);
  60. },
  61. inactive: function() {
  62. // Run init, even if loading fonts fails
  63. init($, _, locale, Handlebars, apiProject, apiData, Prism, sampleRequest, semver);
  64. },
  65. google: {
  66. families: ['Source Code Pro', 'Source Sans Pro:n4,n6,n7']
  67. }
  68. });
  69. });
  70. function init($, _, locale, Handlebars, apiProject, apiData, Prism, sampleRequest, semver) {
  71. var api = apiData.api;
  72. //
  73. // Templates
  74. //
  75. var templateHeader = Handlebars.compile( $('#template-header').html() );
  76. var templateFooter = Handlebars.compile( $('#template-footer').html() );
  77. var templateArticle = Handlebars.compile( $('#template-article').html() );
  78. var templateCompareArticle = Handlebars.compile( $('#template-compare-article').html() );
  79. var templateGenerator = Handlebars.compile( $('#template-generator').html() );
  80. var templateProject = Handlebars.compile( $('#template-project').html() );
  81. var templateSections = Handlebars.compile( $('#template-sections').html() );
  82. var templateSidenav = Handlebars.compile( $('#template-sidenav').html() );
  83. //
  84. // apiProject defaults
  85. //
  86. if ( ! apiProject.template)
  87. apiProject.template = {};
  88. if (apiProject.template.withCompare == null)
  89. apiProject.template.withCompare = true;
  90. if (apiProject.template.withGenerator == null)
  91. apiProject.template.withGenerator = true;
  92. if (apiProject.template.forceLanguage)
  93. locale.setLanguage(apiProject.template.forceLanguage);
  94. if (apiProject.template.aloneDisplay == null)
  95. apiProject.template.aloneDisplay = false;
  96. // Setup jQuery Ajax
  97. $.ajaxSetup(apiProject.template.jQueryAjaxSetup);
  98. //
  99. // Data transform
  100. //
  101. // grouped by group
  102. var apiByGroup = _.groupBy(api, function(entry) {
  103. return entry.group;
  104. });
  105. // grouped by group and name
  106. var apiByGroupAndName = {};
  107. $.each(apiByGroup, function(index, entries) {
  108. apiByGroupAndName[index] = _.groupBy(entries, function(entry) {
  109. return entry.name;
  110. });
  111. });
  112. //
  113. // sort api within a group by title ASC and custom order
  114. //
  115. var newList = [];
  116. var umlauts = { 'ä': 'ae', 'ü': 'ue', 'ö': 'oe', 'ß': 'ss' }; // TODO: remove in version 1.0
  117. $.each (apiByGroupAndName, function(index, groupEntries) {
  118. // get titles from the first entry of group[].name[] (name has versioning)
  119. var titles = [];
  120. $.each (groupEntries, function(titleName, entries) {
  121. var title = entries[0].title;
  122. if(title !== undefined) {
  123. title.toLowerCase().replace(/[äöüß]/g, function($0) { return umlauts[$0]; });
  124. titles.push(title + '#~#' + titleName); // '#~#' keep reference to titleName after sorting
  125. }
  126. });
  127. // sort by name ASC
  128. titles.sort();
  129. // custom order
  130. if (apiProject.order)
  131. titles = sortByOrder(titles, apiProject.order, '#~#');
  132. // add single elements to the new list
  133. titles.forEach(function(name) {
  134. var values = name.split('#~#');
  135. var key = values[1];
  136. groupEntries[key].forEach(function(entry) {
  137. newList.push(entry);
  138. });
  139. });
  140. });
  141. // api overwrite with ordered list
  142. api = newList;
  143. //
  144. // Group- and Versionlists
  145. //
  146. var apiGroups = {};
  147. var apiGroupTitles = {};
  148. var apiVersions = {};
  149. apiVersions[apiProject.version] = 1;
  150. $.each(api, function(index, entry) {
  151. apiGroups[entry.group] = 1;
  152. apiGroupTitles[entry.group] = entry.groupTitle || entry.group;
  153. apiVersions[entry.version] = 1;
  154. });
  155. // sort groups
  156. apiGroups = Object.keys(apiGroups);
  157. apiGroups.sort();
  158. // custom order
  159. if (apiProject.order)
  160. apiGroups = sortByOrder(apiGroups, apiProject.order);
  161. // sort versions DESC
  162. apiVersions = Object.keys(apiVersions);
  163. apiVersions.sort(semver.compare);
  164. apiVersions.reverse();
  165. //
  166. // create Navigationlist
  167. //
  168. var nav = [];
  169. apiGroups.forEach(function(group) {
  170. // Mainmenu entry
  171. nav.push({
  172. group: group,
  173. isHeader: true,
  174. title: apiGroupTitles[group]
  175. });
  176. // Submenu
  177. var oldName = '';
  178. api.forEach(function(entry) {
  179. if (entry.group === group) {
  180. if (oldName !== entry.name) {
  181. nav.push({
  182. title: entry.title,
  183. group: group,
  184. name: entry.name,
  185. type: entry.type,
  186. version: entry.version,
  187. url: entry.url
  188. });
  189. } else {
  190. nav.push({
  191. title: entry.title,
  192. group: group,
  193. hidden: true,
  194. name: entry.name,
  195. type: entry.type,
  196. version: entry.version,
  197. url: entry.url
  198. });
  199. }
  200. oldName = entry.name;
  201. }
  202. });
  203. });
  204. /**
  205. * Add navigation items by analyzing the HTML content and searching for h1 and h2 tags
  206. * @param nav Object the navigation array
  207. * @param content string the compiled HTML content
  208. * @param index where to insert items
  209. * @return boolean true if any good-looking (i.e. with a group identifier) <h1> tag was found
  210. */
  211. function add_nav(nav, content, index) {
  212. var found_level1 = false;
  213. if ( ! content) {
  214. return found_level1;
  215. }
  216. var topics = content.match(/<h(1|2).*?>(.+?)<\/h(1|2)>/gi);
  217. if ( topics ) {
  218. topics.forEach(function(entry) {
  219. var level = entry.substring(2,3);
  220. var title = entry.replace(/<.+?>/g, ''); // Remove all HTML tags for the title
  221. var entry_tags = entry.match(/id="api-([^\-]+)(?:-(.+))?"/); // Find the group and name in the id property
  222. var group = (entry_tags ? entry_tags[1] : null);
  223. var name = (entry_tags ? entry_tags[2] : null);
  224. if (level==1 && title && group) {
  225. nav.splice(index, 0, {
  226. group: group,
  227. isHeader: true,
  228. title: title,
  229. isFixed: true
  230. });
  231. index++;
  232. found_level1 = true;
  233. }
  234. if (level==2 && title && group && name) {
  235. nav.splice(index, 0, {
  236. group: group,
  237. name: name,
  238. isHeader: false,
  239. title: title,
  240. isFixed: false,
  241. version: '1.0'
  242. });
  243. index++;
  244. }
  245. });
  246. }
  247. return found_level1;
  248. }
  249. // Mainmenu Header entry
  250. if (apiProject.header) {
  251. var found_level1 = add_nav(nav, apiProject.header.content, 0); // Add level 1 and 2 titles
  252. if (!found_level1) { // If no Level 1 tags were found, make a title
  253. nav.unshift({
  254. group: '_',
  255. isHeader: true,
  256. title: (apiProject.header.title == null) ? locale.__('General') : apiProject.header.title,
  257. isFixed: true
  258. });
  259. }
  260. }
  261. // Mainmenu Footer entry
  262. if (apiProject.footer) {
  263. var last_nav_index = nav.length;
  264. var found_level1 = add_nav(nav, apiProject.footer.content, nav.length); // Add level 1 and 2 titles
  265. if (!found_level1 && apiProject.footer.title != null) { // If no Level 1 tags were found, make a title
  266. nav.splice(last_nav_index, 0, {
  267. group: '_footer',
  268. isHeader: true,
  269. title: apiProject.footer.title,
  270. isFixed: true
  271. });
  272. }
  273. }
  274. // render pagetitle
  275. var title = apiProject.title ? apiProject.title : 'apiDoc: ' + apiProject.name + ' - ' + apiProject.version;
  276. $(document).attr('title', title);
  277. // remove loader
  278. $('#loader').remove();
  279. // render sidenav
  280. var fields = {
  281. nav: nav
  282. };
  283. $('#sidenav').append( templateSidenav(fields) );
  284. // render Generator
  285. $('#generator').append( templateGenerator(apiProject) );
  286. // render Project
  287. _.extend(apiProject, { versions: apiVersions});
  288. $('#project').append( templateProject(apiProject) );
  289. // render apiDoc, header/footer documentation
  290. if (apiProject.header)
  291. $('#header').append( templateHeader(apiProject.header) );
  292. if (apiProject.footer)
  293. $('#footer').append( templateFooter(apiProject.footer) );
  294. //
  295. // Render Sections and Articles
  296. //
  297. var articleVersions = {};
  298. var content = '';
  299. apiGroups.forEach(function(groupEntry) {
  300. var articles = [];
  301. var oldName = '';
  302. var fields = {};
  303. var title = groupEntry;
  304. var description = '';
  305. articleVersions[groupEntry] = {};
  306. // render all articles of a group
  307. api.forEach(function(entry) {
  308. if(groupEntry === entry.group) {
  309. if (oldName !== entry.name) {
  310. // determine versions
  311. api.forEach(function(versionEntry) {
  312. if (groupEntry === versionEntry.group && entry.name === versionEntry.name) {
  313. if ( ! articleVersions[entry.group].hasOwnProperty(entry.name) ) {
  314. articleVersions[entry.group][entry.name] = [];
  315. }
  316. articleVersions[entry.group][entry.name].push(versionEntry.version);
  317. }
  318. });
  319. fields = {
  320. article: entry,
  321. versions: articleVersions[entry.group][entry.name]
  322. };
  323. } else {
  324. fields = {
  325. article: entry,
  326. hidden: true,
  327. versions: articleVersions[entry.group][entry.name]
  328. };
  329. }
  330. // add prefix URL for endpoint unless it's already absolute
  331. if (apiProject.url) {
  332. if (fields.article.url.substr(0, 4).toLowerCase() !== 'http') {
  333. fields.article.url = apiProject.url + fields.article.url;
  334. }
  335. }
  336. addArticleSettings(fields, entry);
  337. if (entry.groupTitle)
  338. title = entry.groupTitle;
  339. // TODO: make groupDescription compareable with older versions (not important for the moment)
  340. if (entry.groupDescription)
  341. description = entry.groupDescription;
  342. articles.push({
  343. article: templateArticle(fields),
  344. group: entry.group,
  345. name: entry.name,
  346. aloneDisplay: apiProject.template.aloneDisplay
  347. });
  348. oldName = entry.name;
  349. }
  350. });
  351. // render Section with Articles
  352. var fields = {
  353. group: groupEntry,
  354. title: title,
  355. description: description,
  356. articles: articles,
  357. aloneDisplay: apiProject.template.aloneDisplay
  358. };
  359. content += templateSections(fields);
  360. });
  361. $('#sections').append( content );
  362. // Bootstrap Scrollspy
  363. $(this).scrollspy({ target: '#scrollingNav' });
  364. // Content-Scroll on Navigation click.
  365. $('.sidenav').find('a').on('click', function(e) {
  366. e.preventDefault();
  367. var id = $(this).attr('href');
  368. if ($(id).length > 0)
  369. $('html,body').animate({ scrollTop: parseInt($(id).offset().top) }, 400);
  370. window.location.hash = $(this).attr('href');
  371. });
  372. /**
  373. * Check if Parameter (sub) List has a type Field.
  374. * Example: @apiSuccess varname1 No type.
  375. * @apiSuccess {String} varname2 With type.
  376. *
  377. * @param {Object} fields
  378. */
  379. function _hasTypeInFields(fields) {
  380. var result = false;
  381. $.each(fields, function(name) {
  382. result = result || _.some(fields[name], function(item) { return item.type; });
  383. });
  384. return result;
  385. }
  386. /**
  387. * On Template changes, recall plugins.
  388. */
  389. function initDynamic() {
  390. // Bootstrap popover
  391. $('button[data-toggle="popover"]').popover().click(function(e) {
  392. e.preventDefault();
  393. });
  394. var version = $('#version strong').html();
  395. $('#sidenav li').removeClass('is-new');
  396. if (apiProject.template.withCompare) {
  397. $('#sidenav li[data-version=\'' + version + '\']').each(function(){
  398. var group = $(this).data('group');
  399. var name = $(this).data('name');
  400. var length = $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\']').length;
  401. var index = $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\']').index($(this));
  402. if (length === 1 || index === (length - 1))
  403. $(this).addClass('is-new');
  404. });
  405. }
  406. // tabs
  407. $('.nav-tabs-examples a').click(function (e) {
  408. e.preventDefault();
  409. $(this).tab('show');
  410. });
  411. $('.nav-tabs-examples').find('a:first').tab('show');
  412. // sample header-content-type switch
  413. $('.sample-header-content-type-switch').change(function () {
  414. var paramName = '.' + $(this).attr('name') + '-fields';
  415. var bodyName = '.' + $(this).attr('name') + '-body';
  416. var selectName = 'select[name=' + $(this).attr('name') + ']';
  417. if ($(this).val() == 'body-json') {
  418. $(selectName).val('undefined');
  419. $(this).val('body-json');
  420. $(paramName).removeClass('hide');
  421. $(this).parent().nextAll(paramName).first().addClass('hide');
  422. $(bodyName).addClass('hide');
  423. $(this).parent().nextAll(bodyName).first().removeClass('hide');
  424. } else if ($(this).val() == "body-form-data") {
  425. $(selectName).val('undefined');
  426. $(this).val('body-form-data');
  427. $(bodyName).addClass('hide');
  428. $(paramName).removeClass('hide');
  429. } else {
  430. $(this).parent().nextAll(paramName).first().removeClass('hide')
  431. $(this).parent().nextAll(bodyName).first().addClass('hide');
  432. }
  433. $(this).prev('.sample-request-switch').prop('checked', true);
  434. });
  435. // sample request switch
  436. $('.sample-request-switch').click(function (e) {
  437. var paramName = '.' + $(this).attr('name') + '-fields';
  438. var bodyName = '.' + $(this).attr('name') + '-body';
  439. var select = $(this).next('.' + $(this).attr('name') + '-select').val();
  440. if($(this).prop("checked")){
  441. if (select == 'body-json'){
  442. $(this).parent().nextAll(bodyName).first().removeClass('hide');
  443. }else {
  444. $(this).parent().nextAll(paramName).first().removeClass('hide');
  445. }
  446. }else {
  447. if (select == 'body-json'){
  448. $(this).parent().nextAll(bodyName).first().addClass('hide');
  449. }else {
  450. $(this).parent().nextAll(paramName).first().addClass('hide');
  451. }
  452. }
  453. });
  454. if (apiProject.template.aloneDisplay){
  455. //show group
  456. $('.show-group').click(function () {
  457. var apiGroup = '.' + $(this).attr('data-group') + '-group';
  458. var apiGroupArticle = '.' + $(this).attr('data-group') + '-article';
  459. $(".show-api-group").addClass('hide');
  460. $(apiGroup).removeClass('hide');
  461. $(".show-api-article").addClass('hide');
  462. $(apiGroupArticle).removeClass('hide');
  463. });
  464. //show api
  465. $('.show-api').click(function () {
  466. var apiName = '.' + $(this).attr('data-name') + '-article';
  467. var apiGroup = '.' + $(this).attr('data-group') + '-group';
  468. $(".show-api-group").addClass('hide');
  469. $(apiGroup).removeClass('hide');
  470. $(".show-api-article").addClass('hide');
  471. $(apiName).removeClass('hide');
  472. });
  473. }
  474. // call scrollspy refresh method
  475. $(window).scrollspy('refresh');
  476. // init modules
  477. sampleRequest.initDynamic();
  478. Prism.highlightAll()
  479. }
  480. initDynamic();
  481. if (apiProject.template.aloneDisplay) {
  482. var hashVal = window.location.hash;
  483. if (hashVal != null && hashVal.length !== 0) {
  484. $("." + hashVal.slice(1) + "-init").click();
  485. }
  486. }
  487. //
  488. // HTML-Template specific jQuery-Functions
  489. //
  490. // Change Main Version
  491. function setMainVersion(selectedVersion) {
  492. if (typeof(selectedVersion) === 'undefined') {
  493. selectedVersion = $('#version strong').html();
  494. }
  495. else {
  496. $('#version strong').html(selectedVersion);
  497. }
  498. // hide all
  499. $('article').addClass('hide');
  500. $('#sidenav li:not(.nav-fixed)').addClass('hide');
  501. // show 1st equal or lower Version of each entry
  502. $('article[data-version]').each(function(index) {
  503. var group = $(this).data('group');
  504. var name = $(this).data('name');
  505. var version = $(this).data('version');
  506. if (semver.lte(version, selectedVersion)) {
  507. if ($('article[data-group=\'' + group + '\'][data-name=\'' + name + '\']:visible').length === 0) {
  508. // enable Article
  509. $('article[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + version + '\']').removeClass('hide');
  510. // enable Navigation
  511. $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + version + '\']').removeClass('hide');
  512. $('#sidenav li.nav-header[data-group=\'' + group + '\']').removeClass('hide');
  513. }
  514. }
  515. });
  516. // show 1st equal or lower Version of each entry
  517. $('article[data-version]').each(function(index) {
  518. var group = $(this).data('group');
  519. $('section#api-' + group).removeClass('hide');
  520. if ($('section#api-' + group + ' article:visible').length === 0) {
  521. $('section#api-' + group).addClass('hide');
  522. } else {
  523. $('section#api-' + group).removeClass('hide');
  524. }
  525. });
  526. initDynamic();
  527. return;
  528. }
  529. setMainVersion();
  530. $('#versions li.version a').on('click', function(e) {
  531. e.preventDefault();
  532. setMainVersion($(this).html());
  533. });
  534. // compare all article with their predecessor
  535. $('#compareAllWithPredecessor').on('click', changeAllVersionCompareTo);
  536. // change version of an article
  537. $('article .versions li.version a').on('click', changeVersionCompareTo);
  538. // compare url-parameter
  539. $.urlParam = function(name) {
  540. var results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
  541. return (results && results[1]) ? results[1] : null;
  542. };
  543. if ($.urlParam('compare')) {
  544. // URL Paramter ?compare=1 is set
  545. $('#compareAllWithPredecessor').trigger('click');
  546. }
  547. // Quick jump on page load to hash position.
  548. // Should happen after setting the main version
  549. // and after triggering the click on the compare button,
  550. // as these actions modify the content
  551. // and would make it jump to the wrong position or not jump at all.
  552. if (window.location.hash) {
  553. var id = decodeURI(window.location.hash);
  554. if ($(id).length > 0)
  555. $('html,body').animate({ scrollTop: parseInt($(id).offset().top) }, 0);
  556. }
  557. /**
  558. * Initialize search
  559. */
  560. var options = {
  561. valueNames: [ 'nav-list-item','nav-list-url-item']
  562. };
  563. var endpointsList = new List('scrollingNav', options);
  564. /**
  565. * Set initial focus to search input
  566. */
  567. $('#scrollingNav .sidenav-search input.search').focus();
  568. /**
  569. * Detect ESC key to reset search
  570. */
  571. $(document).keyup(function(e) {
  572. if (e.keyCode === 27) $('span.search-reset').click();
  573. });
  574. /**
  575. * Search reset
  576. */
  577. $('span.search-reset').on('click', function() {
  578. $('#scrollingNav .sidenav-search input.search')
  579. .val("")
  580. .focus()
  581. ;
  582. endpointsList.search();
  583. });
  584. /**
  585. * Change version of an article to compare it to an other version.
  586. */
  587. function changeVersionCompareTo(e) {
  588. e.preventDefault();
  589. var $root = $(this).parents('article');
  590. var selectedVersion = $(this).html();
  591. var $button = $root.find('.version');
  592. var currentVersion = $button.find('strong').html();
  593. $button.find('strong').html(selectedVersion);
  594. var group = $root.data('group');
  595. var name = $root.data('name');
  596. var version = $root.data('version');
  597. var compareVersion = $root.data('compare-version');
  598. if (compareVersion === selectedVersion)
  599. return;
  600. if ( ! compareVersion && version == selectedVersion)
  601. return;
  602. if (compareVersion && articleVersions[group][name][0] === selectedVersion || version === selectedVersion) {
  603. // the version of the entry is set to the highest version (reset)
  604. resetArticle(group, name, version);
  605. } else {
  606. var $compareToArticle = $('article[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + selectedVersion + '\']');
  607. var sourceEntry = {};
  608. var compareEntry = {};
  609. $.each(apiByGroupAndName[group][name], function(index, entry) {
  610. if (entry.version === version)
  611. sourceEntry = entry;
  612. if (entry.version === selectedVersion)
  613. compareEntry = entry;
  614. });
  615. var fields = {
  616. article: sourceEntry,
  617. compare: compareEntry,
  618. versions: articleVersions[group][name]
  619. };
  620. // add unique id
  621. // TODO: replace all group-name-version in template with id.
  622. fields.article.id = fields.article.group + '-' + fields.article.name + '-' + fields.article.version;
  623. fields.article.id = fields.article.id.replace(/\./g, '_');
  624. fields.compare.id = fields.compare.group + '-' + fields.compare.name + '-' + fields.compare.version;
  625. fields.compare.id = fields.compare.id.replace(/\./g, '_');
  626. var entry = sourceEntry;
  627. if (entry.parameter && entry.parameter.fields)
  628. fields._hasTypeInParameterFields = _hasTypeInFields(entry.parameter.fields);
  629. if (entry.error && entry.error.fields)
  630. fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
  631. if (entry.success && entry.success.fields)
  632. fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
  633. if (entry.info && entry.info.fields)
  634. fields._hasTypeInInfoFields = _hasTypeInFields(entry.info.fields);
  635. var entry = compareEntry;
  636. if (fields._hasTypeInParameterFields !== true && entry.parameter && entry.parameter.fields)
  637. fields._hasTypeInParameterFields = _hasTypeInFields(entry.parameter.fields);
  638. if (fields._hasTypeInErrorFields !== true && entry.error && entry.error.fields)
  639. fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
  640. if (fields._hasTypeInSuccessFields !== true && entry.success && entry.success.fields)
  641. fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
  642. if (fields._hasTypeInInfoFields !== true && entry.info && entry.info.fields)
  643. fields._hasTypeInInfoFields = _hasTypeInFields(entry.info.fields);
  644. var content = templateCompareArticle(fields);
  645. $root.after(content);
  646. var $content = $root.next();
  647. // Event on.click re-assign
  648. $content.find('.versions li.version a').on('click', changeVersionCompareTo);
  649. // select navigation
  650. $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + currentVersion + '\']').addClass('has-modifications');
  651. $root.remove();
  652. // TODO: on change main version or select the highest version re-render
  653. }
  654. initDynamic();
  655. }
  656. /**
  657. * Compare all currently selected Versions with their predecessor.
  658. */
  659. function changeAllVersionCompareTo(e) {
  660. e.preventDefault();
  661. $('article:visible .versions').each(function(){
  662. var $root = $(this).parents('article');
  663. var currentVersion = $root.data('version');
  664. var $foundElement = null;
  665. $(this).find('li.version a').each(function() {
  666. var selectVersion = $(this).html();
  667. if (selectVersion < currentVersion && ! $foundElement)
  668. $foundElement = $(this);
  669. });
  670. if($foundElement)
  671. $foundElement.trigger('click');
  672. });
  673. initDynamic();
  674. }
  675. /**
  676. * Sort the fields.
  677. */
  678. function sortFields(fields_object) {
  679. $.each(fields_object, function (key, fields) {
  680. var reversed = fields.slice().reverse()
  681. var max_dot_count = Math.max.apply(null, reversed.map(function (item) {
  682. return item.field.split(".").length - 1;
  683. }))
  684. for (var dot_count = 1; dot_count <= max_dot_count; dot_count++) {
  685. reversed.forEach(function (item, index) {
  686. var parts = item.field.split(".");
  687. if (parts.length - 1 == dot_count) {
  688. var fields_names = fields.map(function (item) { return item.field; });
  689. if (parts.slice(1).length >= 1) {
  690. var prefix = parts.slice(0, parts.length - 1).join(".");
  691. var prefix_index = fields_names.indexOf(prefix);
  692. if (prefix_index > -1) {
  693. fields.splice(fields_names.indexOf(item.field), 1);
  694. fields.splice(prefix_index + 1, 0, item);
  695. }
  696. }
  697. }
  698. });
  699. }
  700. });
  701. }
  702. /**
  703. * Add article settings.
  704. */
  705. function addArticleSettings(fields, entry) {
  706. // add unique id
  707. // TODO: replace all group-name-version in template with id.
  708. fields.id = fields.article.group + '-' + fields.article.name + '-' + fields.article.version;
  709. fields.id = fields.id.replace(/\./g, '_');
  710. if (entry.header && entry.header.fields) {
  711. sortFields(entry.header.fields);
  712. fields._hasTypeInHeaderFields = _hasTypeInFields(entry.header.fields);
  713. }
  714. if (entry.parameter && entry.parameter.fields) {
  715. sortFields(entry.parameter.fields);
  716. fields._hasTypeInParameterFields = _hasTypeInFields(entry.parameter.fields);
  717. }
  718. if (entry.error && entry.error.fields) {
  719. sortFields(entry.error.fields);
  720. fields._hasTypeInErrorFields = _hasTypeInFields(entry.error.fields);
  721. }
  722. if (entry.success && entry.success.fields) {
  723. sortFields(entry.success.fields);
  724. fields._hasTypeInSuccessFields = _hasTypeInFields(entry.success.fields);
  725. }
  726. if (entry.info && entry.info.fields) {
  727. sortFields(entry.info.fields);
  728. fields._hasTypeInInfoFields = _hasTypeInFields(entry.info.fields);
  729. }
  730. // add template settings
  731. fields.template = apiProject.template;
  732. }
  733. /**
  734. * Render Article.
  735. */
  736. function renderArticle(group, name, version) {
  737. var entry = {};
  738. $.each(apiByGroupAndName[group][name], function(index, currentEntry) {
  739. if (currentEntry.version === version)
  740. entry = currentEntry;
  741. });
  742. var fields = {
  743. article: entry,
  744. versions: articleVersions[group][name]
  745. };
  746. addArticleSettings(fields, entry);
  747. return templateArticle(fields);
  748. }
  749. /**
  750. * Render original Article and remove the current visible Article.
  751. */
  752. function resetArticle(group, name, version) {
  753. var $root = $('article[data-group=\'' + group + '\'][data-name=\'' + name + '\']:visible');
  754. var content = renderArticle(group, name, version);
  755. $root.after(content);
  756. var $content = $root.next();
  757. // Event on.click needs to be reassigned (should actually work with on ... automatically)
  758. $content.find('.versions li.version a').on('click', changeVersionCompareTo);
  759. $('#sidenav li[data-group=\'' + group + '\'][data-name=\'' + name + '\'][data-version=\'' + version + '\']').removeClass('has-modifications');
  760. $root.remove();
  761. return;
  762. }
  763. /**
  764. * Return ordered entries by custom order and append not defined entries to the end.
  765. * @param {String[]} elements
  766. * @param {String[]} order
  767. * @param {String} splitBy
  768. * @return {String[]} Custom ordered list.
  769. */
  770. function sortByOrder(elements, order, splitBy) {
  771. var results = [];
  772. order.forEach (function(name) {
  773. if (splitBy)
  774. elements.forEach (function(element) {
  775. var parts = element.split(splitBy);
  776. var key = parts[0]; // reference keep for sorting
  777. if (key == name || parts[1] == name)
  778. results.push(element);
  779. });
  780. else
  781. elements.forEach (function(key) {
  782. if (key == name)
  783. results.push(name);
  784. });
  785. });
  786. // Append all other entries that ar not defined in order
  787. elements.forEach(function(element) {
  788. if (results.indexOf(element) === -1)
  789. results.push(element);
  790. });
  791. return results;
  792. }
  793. Prism.highlightAll()
  794. }