Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: Source/devtools/front_end/JumpHistoryManager.js

Issue 23474010: DevTools: "Jump between editing locations" experiment (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: making reveal() to return boolean value Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * @interface
33 */
34 WebInspector.JumpHistoryEntry = function()
35 {
36 }
37
38 WebInspector.JumpHistoryEntry.prototype = {
39 /**
40 * @return {boolean}
41 */
42 valid: function() { },
43
44 /**
45 * @param {WebInspector.JumpHistoryEntry} entry
46 */
47 merge: function(entry) { },
48
49 /**
50 * @return {boolean}
51 */
52 reveal: function() { },
53
54 /**
55 * @param {WebInspector.JumpHistoryEntry} entry
56 */
57 equal: function(entry) { }
58 }
59
60 WebInspector.JumpHistoryDepth = 20;
61
62 /**
63 * @constructor
64 */
65 WebInspector.JumpHistoryManager = function()
66 {
67 this._history = [];
68 this._index = -1;
69 }
70
71 WebInspector.JumpHistoryManager.ShortcutKeys = {
72 JumpToPreviousLocation: WebInspector.KeyboardShortcut.makeDescriptor(WebInsp ector.KeyboardShortcut.Keys.Minus, WebInspector.KeyboardShortcut.Modifiers.Alt),
73 JumpToNextLocation: WebInspector.KeyboardShortcut.makeDescriptor(WebInspecto r.KeyboardShortcut.Keys.Plus, WebInspector.KeyboardShortcut.Modifiers.Alt),
74 }
75
76 WebInspector.JumpHistoryManager.prototype = {
77 handleShortcut: function(event)
78 {
79 var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
80 if (shortcutKey === WebInspector.JumpHistoryManager.ShortcutKeys.JumpToP reviousLocation.key)
81 return this.rollback();
82 else if (shortcutKey === WebInspector.JumpHistoryManager.ShortcutKeys.Ju mpToNextLocation.key)
83 return this.rollover();
84 return false;
85 },
86
87 /**
88 * @param {WebInspector.JumpHistoryEntry} from
89 * @param {WebInspector.JumpHistoryEntry} to
90 */
91 jumpToPosition: function(from, to)
92 {
93 if (!WebInspector.experimentsSettings.jumpToPreviousLocation.isEnabled() )
94 return;
95 if (this._muteJumpToPositionEvent) {
96 if (from)
97 this._historyEntryToUpdate.merge(from);
98 return;
99 }
100 if (from && !this.empty())
101 this.current().merge(from);
102 if (to)
103 this._push(to);
104 },
105
106 /**
107 * @return {boolean}
108 */
109 empty: function()
110 {
111 return !this._history.length;
112 },
113
114 /**
115 * @return {WebInspector.JumpHistoryEntry}
116 */
117 current: function()
118 {
119 if (this.empty())
120 return null;
121 return this._history[this._index];
122 },
123
124 /**
125 * @param {WebInspector.JumpHistoryEntry} entry
126 */
127 _push: function(entry)
128 {
129 if (!this.empty() && this.current().equal(entry))
130 return;
131 this._history.splice(++this._index, Infinity, entry);
132 if (this._history.length > WebInspector.JumpHistoryDepth) {
133 this._history.shift();
134 --this._index;
135 }
136 },
137
138 /**
139 * @param {function(WebInspector.JumpHistoryEntry):boolean} filterCallback
140 */
141 filterHistoryEntries: function(filterCallback)
142 {
143 for(var i = this._history.length - 1; i >= 0; --i) {
144 var entry = this._history[i];
145 if (filterCallback(entry)) {
146 this._history.remove(entry);
147 if (this._index >= i)
148 --this._index;
149 }
150 }
151 if (this._index < 0 && this._history.length > 0)
152 this._index = 0;
153 },
154
155 /**
156 * @return {?WebInspector.JumpHistoryEntry}
157 */
158 _previousValidEntry: function()
159 {
160 if (this.empty())
161 return null;
162 for (var validEntryIndex = this._index - 1; validEntryIndex >= 0; --vali dEntryIndex) {
163 if (this._history[validEntryIndex].valid())
164 break;
165 }
166 if (validEntryIndex < 0) {
167 this._history.splice(0, this._index);
168 this._index = 0;
169 return null;
170 }
171 this._history.splice(validEntryIndex + 1, this._index - validEntryIndex - 1);
172 this._index = validEntryIndex;
173 return this._history[this._index];
174 },
175
176 /**
177 * @return {?WebInspector.JumpHistoryEntry}
178 */
179 _nextValidEntry: function()
180 {
181 if (this.empty())
182 return null;
183 for (var validEntryIndex = this._index + 1; validEntryIndex < this._hist ory.length; ++validEntryIndex) {
184 if (this._history[validEntryIndex].valid())
185 break;
186 }
187 if (validEntryIndex >= this._history.length) {
188 this._history.splice(this._index + 1);
189 return null;
190 }
191 this._history.splice(this._index + 1, validEntryIndex - this._index - 1) ;
192 ++this._index;
193 return this._history[this._index];
194 },
195
196 /**
197 * @param {WebInspector.JumpHistoryEntry} previousEntry
198 * @param {WebInspector.JumpHistoryEntry} newEntry
199 * @return {boolean}
200 */
201 _applyHistoryEntry: function(previousEntry, newEntry)
202 {
203 this._muteJumpToPositionEvent = true;
204 this._historyEntryToUpdate = previousEntry;
205 var didReveal = newEntry.reveal();
206 delete this._historyEntryToUpdate;
207 delete this._muteJumpToPositionEvent;
208 return didReveal;
209 },
210
211 /**
212 * @return {boolean}
213 */
214 rollback: function()
215 {
216 var previous = this.current();
217 do {
218 var entry = this._previousValidEntry();
219 if (!entry)
220 return false;
221 } while (!this._applyHistoryEntry(previous, entry));
222 return true;
223 },
224
225 /**
226 * @return {boolean}
227 */
228 rollover: function()
229 {
230 var previous = this.current();
231 do {
232 var entry = this._nextValidEntry();
233 if (!entry)
234 return false;
235 } while (!this._applyHistoryEntry(previous, entry));
236 return true;
237 }
238 }
239
240 /**
241 * @type {?WebInspector.JumpHistoryManager}
242 */
243 WebInspector.jumpHistoryManager = null;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698