Index: chrome/browser/resources/media_internals/util.js |
diff --git a/chrome/browser/resources/media_internals/util.js b/chrome/browser/resources/media_internals/util.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..261fbd2a1138d044143f71e4e798ad993884eb62 |
--- /dev/null |
+++ b/chrome/browser/resources/media_internals/util.js |
@@ -0,0 +1,106 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+cr.define('media', function() { |
+ 'use strict'; |
+ |
+ /** |
+ * The width and height of a bar drawn on a file canvas in pixels. |
+ */ |
+ var BAR_WIDTH = 500; |
+ var BAR_HEIGHT = 15; |
+ |
+ // Maximum display length of a url in characters. Lengthy urls will be clipped |
+ // and postfixed with an ellipsis. |
+ var MAX_URL_LENGTH = 50; |
+ |
+ /** |
+ * Draws a 1px white horizontal line across |context|. |
+ */ |
+ var drawLine = function(context, top) { |
+ context.moveTo(0, top); |
+ context.lineTo(BAR_WIDTH, top); |
+ context.strokeStyle = '#fff'; |
+ context.stroke(); |
+ }; |
+ |
+ /** |
+ * Creates an HTMLElement of type |type| with textContent |content|. |
+ * @param {string} type The type of element to create. |
+ * @param {string} content The content to place in the element. |
+ * @return {HTMLElement} A newly initialized element. |
+ */ |
+ var makeElement = function(type, content) { |
+ var element = document.createElement(type); |
+ element.textContent = content; |
+ return element; |
+ }; |
+ |
+ /** |
+ * Truncates a URL to MAX_URL_LENGTH. If the URL was too long, the last |
arv (Not doing code reviews)
2011/09/22 19:26:04
Use CSS instead.
overflow: hidden;
text-overflow:
scherkus (not reviewing)
2011/09/23 06:52:41
Done (I hope!)
|
+ * three characters of the returned string will be '...', and its length will |
+ * be MAX_URL_LENGTH. |
+ * @param {string} url The url to truncate. |
+ * @return {string} A truncated url. |
+ */ |
+ var clipURL = function(url) { |
+ if (url && url.length > MAX_URL_LENGTH) |
+ return url.slice(0, MAX_URL_LENGTH - 3) + '...'; |
+ return url; |
+ }; |
+ |
+ /** |
+ * Creates a new <li> containing a <details> with a <summary> and sets |
+ * properties to reference them. |
+ * @return {Object} The new <li>. |
+ */ |
+ var createDetailsLi = function() { |
+ var li = document.createElement('li'); |
+ li.details = document.createElement('details'); |
+ li.summary = document.createElement('summary'); |
+ li.appendChild(li.details); |
+ li.details.appendChild(li.summary); |
+ return li |
+ }; |
+ |
+ /** |
+ * Prints each key-value pair in a dictionary to a row in a table. |
+ * @param {Object} dict The dictionary to print. |
+ * @param {HTMLElement} table The <table> element to print to. |
+ */ |
+ var printDictionaryToTable = function(dict, table) { |
+ table.textContent = ''; |
+ for (var key in dict) { |
+ var tr = document.createElement('tr'); |
+ tr.appendChild(makeElement('td', key + ':')); |
+ tr.appendChild(makeElement('td', dict[key])); |
+ table.appendChild(tr); |
+ } |
+ return table; |
+ }; |
+ |
+ /** |
+ * Rounds a number, leaving the specified number of digits after the decimal, |
+ * defaulting to 1. |
+ * @param {number} n The number to be rounded. |
+ * @param {int} digits The number of digits to leave after the decimal. |
+ * @return {number} |n| rounded to |digits| decimal places. |
+ */ |
+ var round = function(n, digits) { |
+ digits = digits || 1; |
+ var factor = Math.pow(10, digits); |
+ return Math.round(n * factor) / factor; |
+ }; |
+ |
+ return { |
+ BAR_WIDTH: BAR_WIDTH, |
+ BAR_HEIGHT: BAR_HEIGHT, |
+ drawLine: drawLine, |
+ makeElement: makeElement, |
+ clipURL: clipURL, |
+ createDetailsLi: createDetailsLi, |
+ printDictionaryToTable: printDictionaryToTable, |
+ round: round, |
+ }; |
+}); |