main.js 32 KB

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