coverage_html.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. // For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. // Coverage.py HTML report browser code.
  4. /*jslint browser: true, sloppy: true, vars: true, plusplus: true, maxerr: 50, indent: 4 */
  5. /*global coverage: true, document, window, $ */
  6. coverage = {};
  7. // Find all the elements with shortkey_* class, and use them to assign a shortcut key.
  8. coverage.assign_shortkeys = function () {
  9. $("*[class*='shortkey_']").each(function (i, e) {
  10. $.each($(e).attr("class").split(" "), function (i, c) {
  11. if (/^shortkey_/.test(c)) {
  12. $(document).bind('keydown', c.substr(9), function () {
  13. $(e).click();
  14. });
  15. }
  16. });
  17. });
  18. };
  19. // Create the events for the help panel.
  20. coverage.wire_up_help_panel = function () {
  21. $("#keyboard_icon").click(function () {
  22. // Show the help panel, and position it so the keyboard icon in the
  23. // panel is in the same place as the keyboard icon in the header.
  24. $(".help_panel").show();
  25. var koff = $("#keyboard_icon").offset();
  26. var poff = $("#panel_icon").position();
  27. $(".help_panel").offset({
  28. top: koff.top-poff.top,
  29. left: koff.left-poff.left
  30. });
  31. });
  32. $("#panel_icon").click(function () {
  33. $(".help_panel").hide();
  34. });
  35. };
  36. // Create the events for the filter box.
  37. coverage.wire_up_filter = function () {
  38. // Cache elements.
  39. var table = $("table.index");
  40. var table_rows = table.find("tbody tr");
  41. var table_row_names = table_rows.find("td.name a");
  42. var no_rows = $("#no_rows");
  43. // Create a duplicate table footer that we can modify with dynamic summed values.
  44. var table_footer = $("table.index tfoot tr");
  45. var table_dynamic_footer = table_footer.clone();
  46. table_dynamic_footer.attr('class', 'total_dynamic hidden');
  47. table_footer.after(table_dynamic_footer);
  48. // Observe filter keyevents.
  49. $("#filter").on("keyup change", $.debounce(150, function (event) {
  50. var filter_value = $(this).val();
  51. if (filter_value === "") {
  52. // Filter box is empty, remove all filtering.
  53. table_rows.removeClass("hidden");
  54. // Show standard footer, hide dynamic footer.
  55. table_footer.removeClass("hidden");
  56. table_dynamic_footer.addClass("hidden");
  57. // Hide placeholder, show table.
  58. if (no_rows.length > 0) {
  59. no_rows.hide();
  60. }
  61. table.show();
  62. }
  63. else {
  64. // Filter table items by value.
  65. var hidden = 0;
  66. var shown = 0;
  67. // Hide / show elements.
  68. $.each(table_row_names, function () {
  69. var element = $(this).parents("tr");
  70. if ($(this).text().indexOf(filter_value) === -1) {
  71. // hide
  72. element.addClass("hidden");
  73. hidden++;
  74. }
  75. else {
  76. // show
  77. element.removeClass("hidden");
  78. shown++;
  79. }
  80. });
  81. // Show placeholder if no rows will be displayed.
  82. if (no_rows.length > 0) {
  83. if (shown === 0) {
  84. // Show placeholder, hide table.
  85. no_rows.show();
  86. table.hide();
  87. }
  88. else {
  89. // Hide placeholder, show table.
  90. no_rows.hide();
  91. table.show();
  92. }
  93. }
  94. // Manage dynamic header:
  95. if (hidden > 0) {
  96. // Calculate new dynamic sum values based on visible rows.
  97. for (var column = 2; column < 20; column++) {
  98. // Calculate summed value.
  99. var cells = table_rows.find('td:nth-child(' + column + ')');
  100. if (!cells.length) {
  101. // No more columns...!
  102. break;
  103. }
  104. var sum = 0, numer = 0, denom = 0;
  105. $.each(cells.filter(':visible'), function () {
  106. var ratio = $(this).data("ratio");
  107. if (ratio) {
  108. var splitted = ratio.split(" ");
  109. numer += parseInt(splitted[0], 10);
  110. denom += parseInt(splitted[1], 10);
  111. }
  112. else {
  113. sum += parseInt(this.innerHTML, 10);
  114. }
  115. });
  116. // Get footer cell element.
  117. var footer_cell = table_dynamic_footer.find('td:nth-child(' + column + ')');
  118. // Set value into dynamic footer cell element.
  119. if (cells[0].innerHTML.indexOf('%') > -1) {
  120. // Percentage columns use the numerator and denominator,
  121. // and adapt to the number of decimal places.
  122. var match = /\.([0-9]+)/.exec(cells[0].innerHTML);
  123. var places = 0;
  124. if (match) {
  125. places = match[1].length;
  126. }
  127. var pct = numer * 100 / denom;
  128. footer_cell.text(pct.toFixed(places) + '%');
  129. }
  130. else {
  131. footer_cell.text(sum);
  132. }
  133. }
  134. // Hide standard footer, show dynamic footer.
  135. table_footer.addClass("hidden");
  136. table_dynamic_footer.removeClass("hidden");
  137. }
  138. else {
  139. // Show standard footer, hide dynamic footer.
  140. table_footer.removeClass("hidden");
  141. table_dynamic_footer.addClass("hidden");
  142. }
  143. }
  144. }));
  145. // Trigger change event on setup, to force filter on page refresh
  146. // (filter value may still be present).
  147. $("#filter").trigger("change");
  148. };
  149. // Loaded on index.html
  150. coverage.index_ready = function ($) {
  151. // Look for a cookie containing previous sort settings:
  152. var sort_list = [];
  153. var cookie_name = "COVERAGE_INDEX_SORT";
  154. var i;
  155. // This almost makes it worth installing the jQuery cookie plugin:
  156. if (document.cookie.indexOf(cookie_name) > -1) {
  157. var cookies = document.cookie.split(";");
  158. for (i = 0; i < cookies.length; i++) {
  159. var parts = cookies[i].split("=");
  160. if ($.trim(parts[0]) === cookie_name && parts[1]) {
  161. sort_list = eval("[[" + parts[1] + "]]");
  162. break;
  163. }
  164. }
  165. }
  166. // Create a new widget which exists only to save and restore
  167. // the sort order:
  168. $.tablesorter.addWidget({
  169. id: "persistentSort",
  170. // Format is called by the widget before displaying:
  171. format: function (table) {
  172. if (table.config.sortList.length === 0 && sort_list.length > 0) {
  173. // This table hasn't been sorted before - we'll use
  174. // our stored settings:
  175. $(table).trigger('sorton', [sort_list]);
  176. }
  177. else {
  178. // This is not the first load - something has
  179. // already defined sorting so we'll just update
  180. // our stored value to match:
  181. sort_list = table.config.sortList;
  182. }
  183. }
  184. });
  185. // Configure our tablesorter to handle the variable number of
  186. // columns produced depending on report options:
  187. var headers = [];
  188. var col_count = $("table.index > thead > tr > th").length;
  189. headers[0] = { sorter: 'text' };
  190. for (i = 1; i < col_count-1; i++) {
  191. headers[i] = { sorter: 'digit' };
  192. }
  193. headers[col_count-1] = { sorter: 'percent' };
  194. // Enable the table sorter:
  195. $("table.index").tablesorter({
  196. widgets: ['persistentSort'],
  197. headers: headers
  198. });
  199. coverage.assign_shortkeys();
  200. coverage.wire_up_help_panel();
  201. coverage.wire_up_filter();
  202. // Watch for page unload events so we can save the final sort settings:
  203. $(window).unload(function () {
  204. document.cookie = cookie_name + "=" + sort_list.toString() + "; path=/";
  205. });
  206. };
  207. // -- pyfile stuff --
  208. coverage.pyfile_ready = function ($) {
  209. // If we're directed to a particular line number, highlight the line.
  210. var frag = location.hash;
  211. if (frag.length > 2 && frag[1] === 'n') {
  212. $(frag).addClass('highlight');
  213. coverage.set_sel(parseInt(frag.substr(2), 10));
  214. }
  215. else {
  216. coverage.set_sel(0);
  217. }
  218. $(document)
  219. .bind('keydown', 'j', coverage.to_next_chunk_nicely)
  220. .bind('keydown', 'k', coverage.to_prev_chunk_nicely)
  221. .bind('keydown', '0', coverage.to_top)
  222. .bind('keydown', '1', coverage.to_first_chunk)
  223. ;
  224. $(".button_toggle_run").click(function (evt) {coverage.toggle_lines(evt.target, "run");});
  225. $(".button_toggle_exc").click(function (evt) {coverage.toggle_lines(evt.target, "exc");});
  226. $(".button_toggle_mis").click(function (evt) {coverage.toggle_lines(evt.target, "mis");});
  227. $(".button_toggle_par").click(function (evt) {coverage.toggle_lines(evt.target, "par");});
  228. coverage.assign_shortkeys();
  229. coverage.wire_up_help_panel();
  230. coverage.init_scroll_markers();
  231. // Rebuild scroll markers after window high changing
  232. $(window).resize(coverage.resize_scroll_markers);
  233. };
  234. coverage.toggle_lines = function (btn, cls) {
  235. btn = $(btn);
  236. var hide = "hide_"+cls;
  237. if (btn.hasClass(hide)) {
  238. $("#source ."+cls).removeClass(hide);
  239. btn.removeClass(hide);
  240. }
  241. else {
  242. $("#source ."+cls).addClass(hide);
  243. btn.addClass(hide);
  244. }
  245. };
  246. // Return the nth line div.
  247. coverage.line_elt = function (n) {
  248. return $("#t" + n);
  249. };
  250. // Return the nth line number div.
  251. coverage.num_elt = function (n) {
  252. return $("#n" + n);
  253. };
  254. // Return the container of all the code.
  255. coverage.code_container = function () {
  256. return $(".linenos");
  257. };
  258. // Set the selection. b and e are line numbers.
  259. coverage.set_sel = function (b, e) {
  260. // The first line selected.
  261. coverage.sel_begin = b;
  262. // The next line not selected.
  263. coverage.sel_end = (e === undefined) ? b+1 : e;
  264. };
  265. coverage.to_top = function () {
  266. coverage.set_sel(0, 1);
  267. coverage.scroll_window(0);
  268. };
  269. coverage.to_first_chunk = function () {
  270. coverage.set_sel(0, 1);
  271. coverage.to_next_chunk();
  272. };
  273. coverage.is_transparent = function (color) {
  274. // Different browsers return different colors for "none".
  275. return color === "transparent" || color === "rgba(0, 0, 0, 0)";
  276. };
  277. coverage.to_next_chunk = function () {
  278. var c = coverage;
  279. // Find the start of the next colored chunk.
  280. var probe = c.sel_end;
  281. var color, probe_line;
  282. while (true) {
  283. probe_line = c.line_elt(probe);
  284. if (probe_line.length === 0) {
  285. return;
  286. }
  287. color = probe_line.css("background-color");
  288. if (!c.is_transparent(color)) {
  289. break;
  290. }
  291. probe++;
  292. }
  293. // There's a next chunk, `probe` points to it.
  294. var begin = probe;
  295. // Find the end of this chunk.
  296. var next_color = color;
  297. while (next_color === color) {
  298. probe++;
  299. probe_line = c.line_elt(probe);
  300. next_color = probe_line.css("background-color");
  301. }
  302. c.set_sel(begin, probe);
  303. c.show_selection();
  304. };
  305. coverage.to_prev_chunk = function () {
  306. var c = coverage;
  307. // Find the end of the prev colored chunk.
  308. var probe = c.sel_begin-1;
  309. var probe_line = c.line_elt(probe);
  310. if (probe_line.length === 0) {
  311. return;
  312. }
  313. var color = probe_line.css("background-color");
  314. while (probe > 0 && c.is_transparent(color)) {
  315. probe--;
  316. probe_line = c.line_elt(probe);
  317. if (probe_line.length === 0) {
  318. return;
  319. }
  320. color = probe_line.css("background-color");
  321. }
  322. // There's a prev chunk, `probe` points to its last line.
  323. var end = probe+1;
  324. // Find the beginning of this chunk.
  325. var prev_color = color;
  326. while (prev_color === color) {
  327. probe--;
  328. probe_line = c.line_elt(probe);
  329. prev_color = probe_line.css("background-color");
  330. }
  331. c.set_sel(probe+1, end);
  332. c.show_selection();
  333. };
  334. // Return the line number of the line nearest pixel position pos
  335. coverage.line_at_pos = function (pos) {
  336. var l1 = coverage.line_elt(1),
  337. l2 = coverage.line_elt(2),
  338. result;
  339. if (l1.length && l2.length) {
  340. var l1_top = l1.offset().top,
  341. line_height = l2.offset().top - l1_top,
  342. nlines = (pos - l1_top) / line_height;
  343. if (nlines < 1) {
  344. result = 1;
  345. }
  346. else {
  347. result = Math.ceil(nlines);
  348. }
  349. }
  350. else {
  351. result = 1;
  352. }
  353. return result;
  354. };
  355. // Returns 0, 1, or 2: how many of the two ends of the selection are on
  356. // the screen right now?
  357. coverage.selection_ends_on_screen = function () {
  358. if (coverage.sel_begin === 0) {
  359. return 0;
  360. }
  361. var top = coverage.line_elt(coverage.sel_begin);
  362. var next = coverage.line_elt(coverage.sel_end-1);
  363. return (
  364. (top.isOnScreen() ? 1 : 0) +
  365. (next.isOnScreen() ? 1 : 0)
  366. );
  367. };
  368. coverage.to_next_chunk_nicely = function () {
  369. coverage.finish_scrolling();
  370. if (coverage.selection_ends_on_screen() === 0) {
  371. // The selection is entirely off the screen: select the top line on
  372. // the screen.
  373. var win = $(window);
  374. coverage.select_line_or_chunk(coverage.line_at_pos(win.scrollTop()));
  375. }
  376. coverage.to_next_chunk();
  377. };
  378. coverage.to_prev_chunk_nicely = function () {
  379. coverage.finish_scrolling();
  380. if (coverage.selection_ends_on_screen() === 0) {
  381. var win = $(window);
  382. coverage.select_line_or_chunk(coverage.line_at_pos(win.scrollTop() + win.height()));
  383. }
  384. coverage.to_prev_chunk();
  385. };
  386. // Select line number lineno, or if it is in a colored chunk, select the
  387. // entire chunk
  388. coverage.select_line_or_chunk = function (lineno) {
  389. var c = coverage;
  390. var probe_line = c.line_elt(lineno);
  391. if (probe_line.length === 0) {
  392. return;
  393. }
  394. var the_color = probe_line.css("background-color");
  395. if (!c.is_transparent(the_color)) {
  396. // The line is in a highlighted chunk.
  397. // Search backward for the first line.
  398. var probe = lineno;
  399. var color = the_color;
  400. while (probe > 0 && color === the_color) {
  401. probe--;
  402. probe_line = c.line_elt(probe);
  403. if (probe_line.length === 0) {
  404. break;
  405. }
  406. color = probe_line.css("background-color");
  407. }
  408. var begin = probe + 1;
  409. // Search forward for the last line.
  410. probe = lineno;
  411. color = the_color;
  412. while (color === the_color) {
  413. probe++;
  414. probe_line = c.line_elt(probe);
  415. color = probe_line.css("background-color");
  416. }
  417. coverage.set_sel(begin, probe);
  418. }
  419. else {
  420. coverage.set_sel(lineno);
  421. }
  422. };
  423. coverage.show_selection = function () {
  424. var c = coverage;
  425. // Highlight the lines in the chunk
  426. c.code_container().find(".highlight").removeClass("highlight");
  427. for (var probe = c.sel_begin; probe > 0 && probe < c.sel_end; probe++) {
  428. c.num_elt(probe).addClass("highlight");
  429. }
  430. c.scroll_to_selection();
  431. };
  432. coverage.scroll_to_selection = function () {
  433. // Scroll the page if the chunk isn't fully visible.
  434. if (coverage.selection_ends_on_screen() < 2) {
  435. // Need to move the page. The html,body trick makes it scroll in all
  436. // browsers, got it from http://stackoverflow.com/questions/3042651
  437. var top = coverage.line_elt(coverage.sel_begin);
  438. var top_pos = parseInt(top.offset().top, 10);
  439. coverage.scroll_window(top_pos - 30);
  440. }
  441. };
  442. coverage.scroll_window = function (to_pos) {
  443. $("html,body").animate({scrollTop: to_pos}, 200);
  444. };
  445. coverage.finish_scrolling = function () {
  446. $("html,body").stop(true, true);
  447. };
  448. coverage.init_scroll_markers = function () {
  449. var c = coverage;
  450. // Init some variables
  451. c.lines_len = $('td.text p').length;
  452. c.body_h = $('body').height();
  453. c.header_h = $('div#header').height();
  454. c.missed_lines = $('td.text p.mis, td.text p.par');
  455. // Build html
  456. c.resize_scroll_markers();
  457. };
  458. coverage.resize_scroll_markers = function () {
  459. var c = coverage,
  460. min_line_height = 3,
  461. max_line_height = 10,
  462. visible_window_h = $(window).height();
  463. $('#scroll_marker').remove();
  464. // Don't build markers if the window has no scroll bar.
  465. if (c.body_h <= visible_window_h) {
  466. return;
  467. }
  468. $("body").append("<div id='scroll_marker'>&nbsp;</div>");
  469. var scroll_marker = $('#scroll_marker'),
  470. marker_scale = scroll_marker.height() / c.body_h,
  471. line_height = scroll_marker.height() / c.lines_len;
  472. // Line height must be between the extremes.
  473. if (line_height > min_line_height) {
  474. if (line_height > max_line_height) {
  475. line_height = max_line_height;
  476. }
  477. }
  478. else {
  479. line_height = min_line_height;
  480. }
  481. var previous_line = -99,
  482. last_mark,
  483. last_top;
  484. c.missed_lines.each(function () {
  485. var line_top = Math.round($(this).offset().top * marker_scale),
  486. id_name = $(this).attr('id'),
  487. line_number = parseInt(id_name.substring(1, id_name.length));
  488. if (line_number === previous_line + 1) {
  489. // If this solid missed block just make previous mark higher.
  490. last_mark.css({
  491. 'height': line_top + line_height - last_top
  492. });
  493. }
  494. else {
  495. // Add colored line in scroll_marker block.
  496. scroll_marker.append('<div id="m' + line_number + '" class="marker"></div>');
  497. last_mark = $('#m' + line_number);
  498. last_mark.css({
  499. 'height': line_height,
  500. 'top': line_top
  501. });
  502. last_top = line_top;
  503. }
  504. previous_line = line_number;
  505. });
  506. };