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

Unified 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, 4 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 side-by-side diff with in-line comments
Download patch
Index: Source/devtools/front_end/JumpHistoryManager.js
diff --git a/Source/devtools/front_end/JumpHistoryManager.js b/Source/devtools/front_end/JumpHistoryManager.js
new file mode 100644
index 0000000000000000000000000000000000000000..3d29425c83413cb9e5f0bea0aaee795ff2ffeb2d
--- /dev/null
+++ b/Source/devtools/front_end/JumpHistoryManager.js
@@ -0,0 +1,243 @@
+/*
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @interface
+ */
+WebInspector.JumpHistoryEntry = function()
+{
+}
+
+WebInspector.JumpHistoryEntry.prototype = {
+ /**
+ * @return {boolean}
+ */
+ valid: function() { },
+
+ /**
+ * @param {WebInspector.JumpHistoryEntry} entry
+ */
+ merge: function(entry) { },
+
+ /**
+ * @return {boolean}
+ */
+ reveal: function() { },
+
+ /**
+ * @param {WebInspector.JumpHistoryEntry} entry
+ */
+ equal: function(entry) { }
+}
+
+WebInspector.JumpHistoryDepth = 20;
+
+/**
+ * @constructor
+ */
+WebInspector.JumpHistoryManager = function()
+{
+ this._history = [];
+ this._index = -1;
+}
+
+WebInspector.JumpHistoryManager.ShortcutKeys = {
+ JumpToPreviousLocation: WebInspector.KeyboardShortcut.makeDescriptor(WebInspector.KeyboardShortcut.Keys.Minus, WebInspector.KeyboardShortcut.Modifiers.Alt),
+ JumpToNextLocation: WebInspector.KeyboardShortcut.makeDescriptor(WebInspector.KeyboardShortcut.Keys.Plus, WebInspector.KeyboardShortcut.Modifiers.Alt),
+}
+
+WebInspector.JumpHistoryManager.prototype = {
+ handleShortcut: function(event)
+ {
+ var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEvent(event);
+ if (shortcutKey === WebInspector.JumpHistoryManager.ShortcutKeys.JumpToPreviousLocation.key)
+ return this.rollback();
+ else if (shortcutKey === WebInspector.JumpHistoryManager.ShortcutKeys.JumpToNextLocation.key)
+ return this.rollover();
+ return false;
+ },
+
+ /**
+ * @param {WebInspector.JumpHistoryEntry} from
+ * @param {WebInspector.JumpHistoryEntry} to
+ */
+ jumpToPosition: function(from, to)
+ {
+ if (!WebInspector.experimentsSettings.jumpToPreviousLocation.isEnabled())
+ return;
+ if (this._muteJumpToPositionEvent) {
+ if (from)
+ this._historyEntryToUpdate.merge(from);
+ return;
+ }
+ if (from && !this.empty())
+ this.current().merge(from);
+ if (to)
+ this._push(to);
+ },
+
+ /**
+ * @return {boolean}
+ */
+ empty: function()
+ {
+ return !this._history.length;
+ },
+
+ /**
+ * @return {WebInspector.JumpHistoryEntry}
+ */
+ current: function()
+ {
+ if (this.empty())
+ return null;
+ return this._history[this._index];
+ },
+
+ /**
+ * @param {WebInspector.JumpHistoryEntry} entry
+ */
+ _push: function(entry)
+ {
+ if (!this.empty() && this.current().equal(entry))
+ return;
+ this._history.splice(++this._index, Infinity, entry);
+ if (this._history.length > WebInspector.JumpHistoryDepth) {
+ this._history.shift();
+ --this._index;
+ }
+ },
+
+ /**
+ * @param {function(WebInspector.JumpHistoryEntry):boolean} filterCallback
+ */
+ filterHistoryEntries: function(filterCallback)
+ {
+ for(var i = this._history.length - 1; i >= 0; --i) {
+ var entry = this._history[i];
+ if (filterCallback(entry)) {
+ this._history.remove(entry);
+ if (this._index >= i)
+ --this._index;
+ }
+ }
+ if (this._index < 0 && this._history.length > 0)
+ this._index = 0;
+ },
+
+ /**
+ * @return {?WebInspector.JumpHistoryEntry}
+ */
+ _previousValidEntry: function()
+ {
+ if (this.empty())
+ return null;
+ for (var validEntryIndex = this._index - 1; validEntryIndex >= 0; --validEntryIndex) {
+ if (this._history[validEntryIndex].valid())
+ break;
+ }
+ if (validEntryIndex < 0) {
+ this._history.splice(0, this._index);
+ this._index = 0;
+ return null;
+ }
+ this._history.splice(validEntryIndex + 1, this._index - validEntryIndex - 1);
+ this._index = validEntryIndex;
+ return this._history[this._index];
+ },
+
+ /**
+ * @return {?WebInspector.JumpHistoryEntry}
+ */
+ _nextValidEntry: function()
+ {
+ if (this.empty())
+ return null;
+ for (var validEntryIndex = this._index + 1; validEntryIndex < this._history.length; ++validEntryIndex) {
+ if (this._history[validEntryIndex].valid())
+ break;
+ }
+ if (validEntryIndex >= this._history.length) {
+ this._history.splice(this._index + 1);
+ return null;
+ }
+ this._history.splice(this._index + 1, validEntryIndex - this._index - 1);
+ ++this._index;
+ return this._history[this._index];
+ },
+
+ /**
+ * @param {WebInspector.JumpHistoryEntry} previousEntry
+ * @param {WebInspector.JumpHistoryEntry} newEntry
+ * @return {boolean}
+ */
+ _applyHistoryEntry: function(previousEntry, newEntry)
+ {
+ this._muteJumpToPositionEvent = true;
+ this._historyEntryToUpdate = previousEntry;
+ var didReveal = newEntry.reveal();
+ delete this._historyEntryToUpdate;
+ delete this._muteJumpToPositionEvent;
+ return didReveal;
+ },
+
+ /**
+ * @return {boolean}
+ */
+ rollback: function()
+ {
+ var previous = this.current();
+ do {
+ var entry = this._previousValidEntry();
+ if (!entry)
+ return false;
+ } while (!this._applyHistoryEntry(previous, entry));
+ return true;
+ },
+
+ /**
+ * @return {boolean}
+ */
+ rollover: function()
+ {
+ var previous = this.current();
+ do {
+ var entry = this._nextValidEntry();
+ if (!entry)
+ return false;
+ } while (!this._applyHistoryEntry(previous, entry));
+ return true;
+ }
+}
+
+/**
+ * @type {?WebInspector.JumpHistoryManager}
+ */
+WebInspector.jumpHistoryManager = null;

Powered by Google App Engine
This is Rietveld 408576698