perf-ui.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;(function(window) {
  2. 'use strict';
  3. /** The base path of the lodash builds. */
  4. var basePath = '../';
  5. /** The lodash build to load. */
  6. var build = (build = /build=([^&]+)/.exec(location.search)) && decodeURIComponent(build[1]);
  7. /** The other library to load. */
  8. var other = (other = /other=([^&]+)/.exec(location.search)) && decodeURIComponent(other[1]);
  9. /** The `ui` object. */
  10. var ui = {};
  11. /*--------------------------------------------------------------------------*/
  12. // Initialize controls.
  13. addEventListener('load', function() {
  14. function eventHandler(event) {
  15. var buildIndex = buildList.selectedIndex,
  16. otherIndex = otherList.selectedIndex,
  17. search = location.search.replace(/^\?|&?(?:build|other)=[^&]*&?/g, '');
  18. if (event.stopPropagation) {
  19. event.stopPropagation();
  20. } else {
  21. event.cancelBubble = true;
  22. }
  23. location.href =
  24. location.href.split('?')[0] + '?' +
  25. (search ? search + '&' : '') +
  26. 'build=' + (buildIndex < 0 ? build : buildList[buildIndex].value) + '&' +
  27. 'other=' + (otherIndex < 0 ? other : otherList[otherIndex].value);
  28. }
  29. var span1 = document.createElement('span');
  30. span1.style.cssText = 'float:right';
  31. span1.innerHTML =
  32. '<label for="perf-build">Build: </label>' +
  33. '<select id="perf-build">' +
  34. '<option value="lodash">lodash</option>' +
  35. '</select>';
  36. var span2 = document.createElement('span');
  37. span2.style.cssText = 'float:right';
  38. span2.innerHTML =
  39. '<label for="perf-other">Other Library: </label>' +
  40. '<select id="perf-other">' +
  41. '<option value="underscore-dev">Underscore (development)</option>' +
  42. '<option value="underscore">Underscore (production)</option>' +
  43. '<option value="lodash">lodash</option>' +
  44. '</select>';
  45. var buildList = span1.lastChild,
  46. otherList = span2.lastChild,
  47. toolbar = document.getElementById('perf-toolbar');
  48. toolbar.appendChild(span2);
  49. toolbar.appendChild(span1);
  50. buildList.selectedIndex = (function() {
  51. switch (build) {
  52. case 'lodash':
  53. case null: return 0;
  54. }
  55. return -1;
  56. }());
  57. otherList.selectedIndex = (function() {
  58. switch (other) {
  59. case 'underscore-dev': return 0;
  60. case 'lodash': return 2;
  61. case 'underscore':
  62. case null: return 1;
  63. }
  64. return -1;
  65. }());
  66. buildList.addEventListener('change', eventHandler);
  67. otherList.addEventListener('change', eventHandler);
  68. });
  69. // The lodash build file path.
  70. ui.buildPath = (function() {
  71. var result;
  72. switch (build) {
  73. case null: build = 'lodash';
  74. case 'lodash': result = 'dist/lodash.min.js'; break;
  75. default: return build;
  76. }
  77. return basePath + result;
  78. }());
  79. // The other library file path.
  80. ui.otherPath = (function() {
  81. var result;
  82. switch (other) {
  83. case 'lodash': result = 'dist/lodash.min.js'; break;
  84. case 'underscore-dev': result = 'vendor/underscore/underscore.js'; break;
  85. case null: other = 'underscore';
  86. case 'underscore': result = 'vendor/underscore/underscore-min.js'; break;
  87. default: return other;
  88. }
  89. return basePath + result;
  90. }());
  91. ui.urlParams = { 'build': build, 'other': other };
  92. window.ui = ui;
  93. }(this));