helpers.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. (function (drupalSettings) {
  2. Drupal.webprofiler.helpers = (function () {
  3. "use strict";
  4. var escapeRx = function escapeRegExp(string) {
  5. return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
  6. },
  7. repl = function replaceAll(string, find, replace) {
  8. if (typeof string != 'string') {
  9. return '';
  10. }
  11. return string.replace(new RegExp(escapeRx(find), 'g'), replace);
  12. },
  13. shortLink = function (clazz) {
  14. if (!clazz) {
  15. return null;
  16. }
  17. clazz = repl(clazz, '/', '\\');
  18. var parts = clazz.split("\\"), result = [], size = (parts.length - 1);
  19. _.each(parts, function (item, key) {
  20. if (key < size) {
  21. result.push(item.substring(0, 1));
  22. } else {
  23. result.push(item);
  24. }
  25. });
  26. return result.join("\\");
  27. },
  28. abbr = function (clazz) {
  29. if (!clazz) {
  30. return null;
  31. }
  32. return '<abbr title="' + clazz + '">' + shortLink(clazz) + '</abbr>';
  33. },
  34. ideLink = function (file, line) {
  35. if (!file) {
  36. return null;
  37. }
  38. line = line || 0;
  39. file = file.replace(drupalSettings.webprofiler.ide_link_remote, drupalSettings.webprofiler.ide_link_local);
  40. return drupalSettings.webprofiler.ide_link.replace("@file", file).replace("@line", line);
  41. },
  42. classLink = function (data) {
  43. var link = ideLink(data['file'], data['line']), clazz = abbr(data['class']), method = data['method'], output = '';
  44. output = clazz;
  45. if (method) {
  46. output += '::' + method;
  47. }
  48. if (link) {
  49. output = '<a href="' + link + '">' + output + '</a>';
  50. }
  51. return output;
  52. },
  53. printTime = function (data, unit) {
  54. unit = unit || 'ms';
  55. data = Math.round((data + 0.00001) * 100) / 100;
  56. return data + ' ' + unit;
  57. },
  58. frm = function (obj, level) {
  59. level = level || 0;
  60. var str = '<ul class="list--unstyled list--level-' + level + ' list--flat">', prop;
  61. if (typeof obj != 'object') {
  62. return obj;
  63. }
  64. for (prop in obj) {
  65. if (isInt(prop)) {
  66. str += '<li>' + frm(obj[prop], level + 1) + '</li>';
  67. } else {
  68. str += '<li><span class="list-item--bold">' + prop + '</span>: ' + frm(obj[prop], level + 1) + '</li>';
  69. }
  70. }
  71. return str + '</ul>';
  72. },
  73. isInt = function (value) {
  74. var x;
  75. return isNaN(value) ? !1 : (x = parseFloat(value), (0 | x) === x);
  76. };
  77. return {
  78. frm: frm,
  79. ideLink: ideLink,
  80. shortLink: shortLink,
  81. classLink: classLink,
  82. printTime: printTime
  83. }
  84. })();
  85. }(drupalSettings));