Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame^] | 1 | ;(function(window) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | /** The base path of the lodash builds. */ |
| 5 | var basePath = '../'; |
| 6 | |
| 7 | /** The lodash build to load. */ |
| 8 | var build = (build = /build=([^&]+)/.exec(location.search)) && decodeURIComponent(build[1]); |
| 9 | |
| 10 | /** The other library to load. */ |
| 11 | var other = (other = /other=([^&]+)/.exec(location.search)) && decodeURIComponent(other[1]); |
| 12 | |
| 13 | /** The `ui` object. */ |
| 14 | var ui = {}; |
| 15 | |
| 16 | /*--------------------------------------------------------------------------*/ |
| 17 | |
| 18 | /** |
| 19 | * Registers an event listener on an element. |
| 20 | * |
| 21 | * @private |
| 22 | * @param {Element} element The element. |
| 23 | * @param {string} eventName The name of the event. |
| 24 | * @param {Function} handler The event handler. |
| 25 | * @returns {Element} The element. |
| 26 | */ |
| 27 | function addListener(element, eventName, handler) { |
| 28 | if (typeof element.addEventListener != 'undefined') { |
| 29 | element.addEventListener(eventName, handler, false); |
| 30 | } else if (typeof element.attachEvent != 'undefined') { |
| 31 | element.attachEvent('on' + eventName, handler); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /*--------------------------------------------------------------------------*/ |
| 36 | |
| 37 | // Initialize controls. |
| 38 | addListener(window, 'load', function() { |
| 39 | function eventHandler(event) { |
| 40 | var buildIndex = buildList.selectedIndex, |
| 41 | otherIndex = otherList.selectedIndex, |
| 42 | search = location.search.replace(/^\?|&?(?:build|other)=[^&]*&?/g, ''); |
| 43 | |
| 44 | if (event.stopPropagation) { |
| 45 | event.stopPropagation(); |
| 46 | } else { |
| 47 | event.cancelBubble = true; |
| 48 | } |
| 49 | location.href = |
| 50 | location.href.split('?')[0] + '?' + |
| 51 | (search ? search + '&' : '') + |
| 52 | 'build=' + (buildIndex < 0 ? build : buildList[buildIndex].value) + '&' + |
| 53 | 'other=' + (otherIndex < 0 ? other : otherList[otherIndex].value); |
| 54 | } |
| 55 | |
| 56 | var span1 = document.createElement('span'); |
| 57 | span1.style.cssText = 'float:right'; |
| 58 | span1.innerHTML = |
| 59 | '<label for="perf-build">Build: </label>' + |
| 60 | '<select id="perf-build">' + |
| 61 | '<option value="lodash">lodash</option>' + |
| 62 | '</select>'; |
| 63 | |
| 64 | var span2 = document.createElement('span'); |
| 65 | span2.style.cssText = 'float:right'; |
| 66 | span2.innerHTML = |
| 67 | '<label for="perf-other">Other Library: </label>' + |
| 68 | '<select id="perf-other">' + |
| 69 | '<option value="underscore-dev">Underscore (development)</option>' + |
| 70 | '<option value="underscore">Underscore (production)</option>' + |
| 71 | '<option value="lodash">lodash</option>' + |
| 72 | '</select>'; |
| 73 | |
| 74 | var buildList = span1.lastChild, |
| 75 | otherList = span2.lastChild, |
| 76 | toolbar = document.getElementById('perf-toolbar'); |
| 77 | |
| 78 | toolbar.appendChild(span2); |
| 79 | toolbar.appendChild(span1); |
| 80 | |
| 81 | buildList.selectedIndex = (function() { |
| 82 | switch (build) { |
| 83 | case 'lodash': |
| 84 | case null: return 0; |
| 85 | } |
| 86 | return -1; |
| 87 | }()); |
| 88 | |
| 89 | otherList.selectedIndex = (function() { |
| 90 | switch (other) { |
| 91 | case 'underscore-dev': return 0; |
| 92 | case 'lodash': return 2; |
| 93 | case 'underscore': |
| 94 | case null: return 1; |
| 95 | } |
| 96 | return -1; |
| 97 | }()); |
| 98 | |
| 99 | addListener(buildList, 'change', eventHandler); |
| 100 | addListener(otherList, 'change', eventHandler); |
| 101 | }); |
| 102 | |
| 103 | // The lodash build file path. |
| 104 | ui.buildPath = (function() { |
| 105 | var result; |
| 106 | switch (build) { |
| 107 | case null: build = 'lodash'; |
| 108 | case 'lodash': result = 'dist/lodash.min.js'; break; |
| 109 | default: return build; |
| 110 | } |
| 111 | return basePath + result; |
| 112 | }()); |
| 113 | |
| 114 | // The other library file path. |
| 115 | ui.otherPath = (function() { |
| 116 | var result; |
| 117 | switch (other) { |
| 118 | case 'lodash': result = 'dist/lodash.min.js'; break; |
| 119 | case 'underscore-dev': result = 'vendor/underscore/underscore.js'; break; |
| 120 | case null: other = 'underscore'; |
| 121 | case 'underscore': result = 'vendor/underscore/underscore-min.js'; break; |
| 122 | default: return other; |
| 123 | } |
| 124 | return basePath + result; |
| 125 | }()); |
| 126 | |
| 127 | ui.urlParams = { 'build': build, 'other': other }; |
| 128 | |
| 129 | window.ui = ui; |
| 130 | |
| 131 | }(this)); |