|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */ |
| 25 | +/*global define, $, brackets, window */ |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Manages tickmarks shown along the scrollbar track. |
| 30 | + * NOT yet intended for use by anyone other than the FindReplace module. |
| 31 | + * It is assumed that markers are always clear()ed when switching editors. |
| 32 | + */ |
| 33 | +define(function (require, exports, module) { |
| 34 | + "use strict"; |
| 35 | + |
| 36 | + var Editor = require("editor/Editor"), |
| 37 | + EditorManager = require("editor/EditorManager"), |
| 38 | + Async = require("utils/Async"); |
| 39 | + |
| 40 | + |
| 41 | + /** @const @type {number} Height (and width) or scrollbar up/down arrow button on Win */ |
| 42 | + var WIN_ARROW_HT = 17; |
| 43 | + |
| 44 | + /** @type {?Editor} Editor the markers are currently shown for, or null if not shown */ |
| 45 | + var editor; |
| 46 | + |
| 47 | + /** @type {number} Top of scrollbar track area, relative to top of scrollbar */ |
| 48 | + var trackOffset; |
| 49 | + |
| 50 | + /** @type {number} Height of scrollbar track area */ |
| 51 | + var trackHt; |
| 52 | + |
| 53 | + /** @type {!Array.<{line: number, ch: number}>} Text positions of markers */ |
| 54 | + var marks = []; |
| 55 | + |
| 56 | + |
| 57 | + /** Measure scrollbar track */ |
| 58 | + function _calcScaling() { |
| 59 | + var rootElem = editor.getRootElement(); |
| 60 | + var $sb = $(".CodeMirror-vscrollbar", rootElem); |
| 61 | + |
| 62 | + trackHt = $sb[0].offsetHeight; |
| 63 | + |
| 64 | + if (trackHt > 0) { |
| 65 | + // Scrollbar visible: determine offset of track from top of scrollbar |
| 66 | + if (brackets.platform === "win") { |
| 67 | + trackOffset = WIN_ARROW_HT; // Up arrow pushes down track |
| 68 | + } else { |
| 69 | + trackOffset = 0; // No arrows |
| 70 | + } |
| 71 | + |
| 72 | + } else { |
| 73 | + // No scrollbar: use the height of the entire code content |
| 74 | + trackHt = $(".CodeMirror-sizer", rootElem)[0].offsetHeight; |
| 75 | + trackOffset = 0; |
| 76 | + } |
| 77 | + |
| 78 | + trackHt -= trackOffset * 2; |
| 79 | + } |
| 80 | + |
| 81 | + /** Add one tickmark to the DOM */ |
| 82 | + function _renderMark(pos) { |
| 83 | + var top = Math.round(pos.line / editor.lineCount() * trackHt) + trackOffset; |
| 84 | + top--; // subtract ~1/2 the ht of a tickmark to center it on ideal pos |
| 85 | + |
| 86 | + var $mark = $("<div class='tickmark' style='top:" + top + "px'></div>"); |
| 87 | + |
| 88 | + $(".tickmark-track", editor.getRootElement()).append($mark); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * Clear any markers in the editor's tickmark track, but leave it visible. Safe to call when |
| 94 | + * tickmark track is not visible also. |
| 95 | + */ |
| 96 | + function clear() { |
| 97 | + if (editor) { |
| 98 | + $(".tickmark-track", editor.getRootElement()).empty(); |
| 99 | + marks = []; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** Add or remove the tickmark track from the editor's UI */ |
| 104 | + function setVisible(curEditor, visible) { |
| 105 | + // short-circuit no-ops |
| 106 | + if ((visible && curEditor === editor) || (!visible && !editor)) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + if (visible) { |
| 111 | + console.assert(!editor); |
| 112 | + editor = curEditor; |
| 113 | + |
| 114 | + // Don't support inline editors yet - search inside them is pretty screwy anyway (#2110) |
| 115 | + if (editor.isTextSubset()) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + var rootElem = editor.getRootElement(); |
| 120 | + var $sb = $(".CodeMirror-vscrollbar", rootElem); |
| 121 | + var $overlay = $("<div class='tickmark-track'></div>"); |
| 122 | + $sb.parent().append($overlay); |
| 123 | + |
| 124 | + _calcScaling(); |
| 125 | + |
| 126 | + // Update tickmarks during window resize (whenever resizing has paused/stopped for > 1/3 sec) |
| 127 | + $(window).on("resize.ScrollTrackMarkers", Async.whenIdle(300, function () { |
| 128 | + if (marks.length) { |
| 129 | + _calcScaling(); |
| 130 | + $(".tickmark-track", editor.getRootElement()).empty(); |
| 131 | + marks.forEach(function (pos) { |
| 132 | + _renderMark(pos); |
| 133 | + }); |
| 134 | + } |
| 135 | + })); |
| 136 | + |
| 137 | + } else { |
| 138 | + console.assert(editor === curEditor); |
| 139 | + $(".tickmark-track", curEditor.getRootElement()).remove(); |
| 140 | + editor = null; |
| 141 | + marks = []; |
| 142 | + $(window).off("resize.ScrollTrackMarkers"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** Add a tickmark to the editor's tickmark track, if it's visible */ |
| 147 | + function addTickmark(curEditor, pos) { |
| 148 | + console.assert(editor === curEditor); |
| 149 | + |
| 150 | + marks.push(pos); |
| 151 | + _renderMark(pos); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + exports.clear = clear; |
| 156 | + exports.setVisible = setVisible; |
| 157 | + exports.addTickmark = addTickmark; |
| 158 | +}); |
0 commit comments