OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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('extensions', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * ExtensionCode is an element which displays code in a styled div, and is |
| 10 * designed to highlight errors. |
| 11 */ |
| 12 function ExtensionCode(div) { |
| 13 div.__proto__ = ExtensionCode.prototype; |
| 14 return div; |
| 15 } |
| 16 |
| 17 ExtensionCode.prototype = { |
| 18 __proto__: HTMLDivElement.prototype, |
| 19 |
| 20 /** |
| 21 * Populate the content area of the code div with the given code. This will |
| 22 * highlight the erroneous section (if any). |
| 23 * @param {Object} code An object with four strings: beforeHighlight, |
| 24 * afterHighlight, highlight, and the message. The 'highlight' strings |
| 25 * represent the three portions of the file's content to display - the |
| 26 * portion which is most relevant and should be emphasized (highlight), |
| 27 * and the parts both before and after this portion. The message is the |
| 28 * error message, which will be the mouseover hint for the highlighted |
| 29 * region. These may be empty. |
| 30 * @param {string} emptyMessage The message to display if the code |
| 31 * object is empty (e.g., 'could not load code'). |
| 32 */ |
| 33 populate: function(code, emptyMessage) { |
| 34 // Clear any remnant content, so we don't have multiple code listed. |
| 35 this.clear(); |
| 36 |
| 37 var sourceDiv = document.createElement('div'); |
| 38 sourceDiv.classList.add('extension-code-source'); |
| 39 this.appendChild(sourceDiv); |
| 40 |
| 41 // If there's no code, then display an appropriate message. |
| 42 if (!code) { |
| 43 var span = document.createElement('span'); |
| 44 span.textContent = emptyMessage; |
| 45 sourceDiv.appendChild(span); |
| 46 return; |
| 47 } |
| 48 |
| 49 var lineCount = 0; |
| 50 var createSpan = function(source, isHighlighted) { |
| 51 lineCount += source.split('\n').length - 1; |
| 52 var span = document.createElement('span'); |
| 53 span.className = isHighlighted ? 'extension-code-highlighted-source' : |
| 54 'extension-code-normal-source'; |
| 55 span.textContent = source; |
| 56 return span; |
| 57 }; |
| 58 |
| 59 if (code.beforeHighlight) |
| 60 sourceDiv.appendChild(createSpan(code.beforeHighlight, false)); |
| 61 |
| 62 if (code.highlight) { |
| 63 var highlightSpan = createSpan(code.highlight, true); |
| 64 highlightSpan.title = code.message; |
| 65 sourceDiv.appendChild(highlightSpan); |
| 66 } |
| 67 |
| 68 if (code.afterHighlight) |
| 69 sourceDiv.appendChild(createSpan(code.afterHighlight, false)); |
| 70 |
| 71 // Make the line numbers. This should be the number of line breaks + 1 |
| 72 // (the last line doesn't break, but should still be numbered). |
| 73 var content = ''; |
| 74 for (var i = 1; i < lineCount + 1; ++i) |
| 75 content += i + '\n'; |
| 76 var span = document.createElement('span'); |
| 77 span.textContent = content; |
| 78 |
| 79 var linesDiv = document.createElement('div'); |
| 80 linesDiv.classList.add('extension-code-line-numbers'); |
| 81 linesDiv.appendChild(span); |
| 82 this.insertBefore(linesDiv, this.firstChild); |
| 83 }, |
| 84 |
| 85 /** |
| 86 * Clears the content of the element. |
| 87 */ |
| 88 clear: function() { |
| 89 while (this.firstChild) |
| 90 this.removeChild(this.firstChild); |
| 91 }, |
| 92 |
| 93 /** |
| 94 * Scrolls to the error, if there is one. This cannot be called when the |
| 95 * div is hidden. |
| 96 */ |
| 97 scrollToError: function() { |
| 98 var errorSpan = this.querySelector('.extension-code-highlighted-source'); |
| 99 if (errorSpan) |
| 100 errorSpan.scrollIntoView(false); // Scroll to bottom of view. |
| 101 } |
| 102 }; |
| 103 |
| 104 // Export |
| 105 return { |
| 106 ExtensionCode: ExtensionCode |
| 107 }; |
| 108 }); |
OLD | NEW |