Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/child/commands.js

Issue 29363476: Issue 2879 - Move element selection into the content process (Closed) Base URL: https://hg.adblockplus.org/elemhidehelper
Patch Set: Created Nov. 17, 2016, 1:17 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/child/commands.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/child/commands.js
@@ -0,0 +1,147 @@
+/*
+ * This Source Code is subject to the terms of the Mozilla Public License
+ * version 2.0 (the "License"). You can obtain a copy of the License at
+ * http://mozilla.org/MPL/2.0/.
+ */
+
+"use strict";
+
+let messageManager = require("messageManager");
+let {getNodeInfo} = require("./nodeInfo");
+let {
+ state, selectElement, setAnchorElement, stopSelection
+} = require("./selection");
+let {getParentElement} = require("./utils");
+
+messageManager.addMessageListener("ElemHideHelper:Command", onCommand);
+
+onShutdown.add(() =>
+{
+ messageManager.removeMessageListener("ElemHideHelper:Command", onCommand);
+});
+
+function onCommand(message)
+{
+ let command = message.data;
+ if (typeof exports[command] == "function")
+ exports[command]();
+}
+
+function quit()
+{
+ stopSelection();
+}
+exports.quit = quit;
+
+function select()
+{
+ if (!state.selectedElement)
+ return;
+
+ messageManager.sendAsyncMessage(
+ "ElemHideHelper:SelectionSucceeded",
+ getNodeInfo(state.selectedElement)
+ );
+ stopSelection();
saroyanm 2016/11/23 17:44:38 Why not call stopSelection in SelectionSucceeded m
Wladimir Palant 2016/11/24 14:02:01 stopSelection() is a general cleanup method. It do
+}
+exports.select = select;
+
+function wider()
+{
+ if (!state.selectedElement)
+ return;
+
+ let newElement = getParentElement(state.selectedElement);
+ if (!newElement)
+ return;
+
+ state.isUserSelected = true;
+ selectElement(newElement);
+}
+exports.wider = wider;
+
+function narrower()
+{
+ if (!state.selectedElement)
+ return;
+
+ // Search selected element in the parent chain, starting with the ancho
+ // element. We need to select the element just before the selected one.
+ let e = state.anchorElement;
+ let newElement = null;
+ while (e && e != state.selectedElement)
+ {
+ newElement = e;
+ e = getParentElement(e);
+ }
+
+ if (!e || !newElement)
+ return;
+
+ state.isUserSelected = true;
+ selectElement(newElement);
+}
+exports.narrower = narrower;
+
+function lock()
+{
+ if (!state.selectedElement)
+ return;
+
+ if (state.lockedAnchor)
+ {
+ setAnchorElement(state.lockedAnchor);
+ state.lockedAnchor = null;
+ }
+ else
+ state.lockedAnchor = state.anchorElement;
+}
+exports.lock = lock;
+
+let blinkState = null;
Wladimir Palant 2016/11/17 13:54:48 I moved the properties related to the blink functi
saroyanm 2016/11/23 17:44:38 Acknowledged.
+
+function stopBlinking()
+{
+ blinkState.timer.cancel();
+ if (!Cu.isDeadWrapper(blinkState.element))
+ blinkState.element.style.visibility = blinkState.origVisibility;
+ blinkState = null;
+}
+
+function doBlink()
Wladimir Palant 2016/11/17 13:54:48 This was a closure in the original code, having it
saroyanm 2016/11/23 17:44:38 Acknowledged.
+{
+ if (Cu.isDeadWrapper(blinkState.element))
+ {
+ stopBlinking();
+ return;
+ }
+
+ blinkState.counter++;
+ blinkState.element.style.setProperty(
+ "visibility",
+ (blinkState.counter % 2 == 0 ? "visible" : "hidden"),
+ "important"
+ );
Wladimir Palant 2016/11/17 13:54:48 Original code was simply assigning element.style.v
saroyanm 2016/11/23 17:44:38 Acknowledged.
+ if (blinkState.counter == 6)
+ stopBlinking();
+}
+
+function blinkElement()
+{
+ if (!state.selectedElement)
+ return;
+
+ if (blinkState)
+ stopBlinking();
+
+ blinkState = {
+ counter: 0,
+ element: state.selectedElement,
+ origVisibility: state.selectedElement.style.visibility,
+ timer: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer)
+ };
+
+ blinkState.timer.initWithCallback(doBlink, 250,
+ Ci.nsITimer.TYPE_REPEATING_SLACK);
+}
+exports.blinkElement = blinkElement;

Powered by Google App Engine
This is Rietveld