Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 cr.define('media', function() { | |
| 6 'use strict'; | |
| 7 | |
| 8 /** | |
| 9 * The width and height of a bar drawn on a file canvas in pixels. | |
| 10 */ | |
| 11 var BAR_WIDTH = 500; | |
| 12 var BAR_HEIGHT = 15; | |
| 13 | |
| 14 // Maximum display length of a url in characters. Lengthy urls will be clipped | |
| 15 // and postfixed with an ellipsis. | |
| 16 var MAX_URL_LENGTH = 50; | |
| 17 | |
| 18 /** | |
| 19 * Draws a 1px white horizontal line across |context|. | |
| 20 */ | |
| 21 var drawLine = function(context, top) { | |
| 22 context.moveTo(0, top); | |
| 23 context.lineTo(BAR_WIDTH, top); | |
| 24 context.strokeStyle = '#fff'; | |
| 25 context.stroke(); | |
| 26 }; | |
| 27 | |
| 28 /** | |
| 29 * Creates an HTMLElement of type |type| with textContent |content|. | |
| 30 * @param {string} type The type of element to create. | |
| 31 * @param {string} content The content to place in the element. | |
| 32 * @return {HTMLElement} A newly initialized element. | |
| 33 */ | |
| 34 var makeElement = function(type, content) { | |
| 35 var element = document.createElement(type); | |
| 36 element.textContent = content; | |
| 37 return element; | |
| 38 }; | |
| 39 | |
| 40 /** | |
| 41 * 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!)
| |
| 42 * three characters of the returned string will be '...', and its length will | |
| 43 * be MAX_URL_LENGTH. | |
| 44 * @param {string} url The url to truncate. | |
| 45 * @return {string} A truncated url. | |
| 46 */ | |
| 47 var clipURL = function(url) { | |
| 48 if (url && url.length > MAX_URL_LENGTH) | |
| 49 return url.slice(0, MAX_URL_LENGTH - 3) + '...'; | |
| 50 return url; | |
| 51 }; | |
| 52 | |
| 53 /** | |
| 54 * Creates a new <li> containing a <details> with a <summary> and sets | |
| 55 * properties to reference them. | |
| 56 * @return {Object} The new <li>. | |
| 57 */ | |
| 58 var createDetailsLi = function() { | |
| 59 var li = document.createElement('li'); | |
| 60 li.details = document.createElement('details'); | |
| 61 li.summary = document.createElement('summary'); | |
| 62 li.appendChild(li.details); | |
| 63 li.details.appendChild(li.summary); | |
| 64 return li | |
| 65 }; | |
| 66 | |
| 67 /** | |
| 68 * Prints each key-value pair in a dictionary to a row in a table. | |
| 69 * @param {Object} dict The dictionary to print. | |
| 70 * @param {HTMLElement} table The <table> element to print to. | |
| 71 */ | |
| 72 var printDictionaryToTable = function(dict, table) { | |
| 73 table.textContent = ''; | |
| 74 for (var key in dict) { | |
| 75 var tr = document.createElement('tr'); | |
| 76 tr.appendChild(makeElement('td', key + ':')); | |
| 77 tr.appendChild(makeElement('td', dict[key])); | |
| 78 table.appendChild(tr); | |
| 79 } | |
| 80 return table; | |
| 81 }; | |
| 82 | |
| 83 /** | |
| 84 * Rounds a number, leaving the specified number of digits after the decimal, | |
| 85 * defaulting to 1. | |
| 86 * @param {number} n The number to be rounded. | |
| 87 * @param {int} digits The number of digits to leave after the decimal. | |
| 88 * @return {number} |n| rounded to |digits| decimal places. | |
| 89 */ | |
| 90 var round = function(n, digits) { | |
| 91 digits = digits || 1; | |
| 92 var factor = Math.pow(10, digits); | |
| 93 return Math.round(n * factor) / factor; | |
| 94 }; | |
| 95 | |
| 96 return { | |
| 97 BAR_WIDTH: BAR_WIDTH, | |
| 98 BAR_HEIGHT: BAR_HEIGHT, | |
| 99 drawLine: drawLine, | |
| 100 makeElement: makeElement, | |
| 101 clipURL: clipURL, | |
| 102 createDetailsLi: createDetailsLi, | |
| 103 printDictionaryToTable: printDictionaryToTable, | |
| 104 round: round, | |
| 105 }; | |
| 106 }); | |
| OLD | NEW |