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

Side by Side Diff: block.js

Issue 29336084: Issue 2426 - Open block.html as a popup window (Closed)
Patch Set: Addressed some initial feedback Created Feb. 10, 2016, 2:18 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 function init() 18 var searchParams = {};
19 {
20 // Attach event listeners
21 window.addEventListener("keydown", onKeyDown, false);
22 window.addEventListener("dragstart", onDragStart, false);
23 window.addEventListener("dragend", onDragEnd, false);
24
25 $("#addButton").click(addFilters);
26 $("#cancelButton").click(closeDialog.bind(null, false));
27
28 // Apply jQuery UI styles
29 $("button").button();
30
31 ext.backgroundPage.sendMessage(
32 {
33 type: "forward",
34 expectsResponse: true,
35 payload:
36 {
37 type: "clickhide-init",
38 width: Math.max(document.body.offsetWidth || document.body.scrollWidth),
39 height: Math.max(document.body.offsetHeight || document.body.scrollHeight)
40 }
41 },
42 function(response)
43 {
44 document.getElementById("filters").value = response.filters.join("\n");
45 });
46
47 document.getElementById("filters").focus();
48 }
49 window.addEventListener("load", init, false);
50 19
51 function onKeyDown(event) 20 function onKeyDown(event)
52 { 21 {
53 if (event.keyCode == 27) 22 if (event.keyCode == 27)
54 { 23 {
55 event.preventDefault(); 24 event.preventDefault();
56 closeDialog(); 25 closeDialog();
57 } 26 }
58 else if (event.keyCode == 13 && !event.shiftKey && !event.ctrlKey) 27 else if (event.keyCode == 13 && !event.shiftKey && !event.ctrlKey)
59 { 28 {
60 event.preventDefault(); 29 event.preventDefault();
61 addFilters(); 30 addFilters();
62 } 31 }
63 } 32 }
64 33
65 function addFilters() 34 function addFilters()
66 { 35 {
67 ext.backgroundPage.sendMessage( 36 ext.backgroundPage.sendMessage(
68 { 37 {
69 type: "add-filters", 38 type: "add-filters",
70 text: document.getElementById("filters").value 39 text: document.getElementById("filters").value
71 }, 40 },
72 41 function(response)
73 function(response) 42 {
74 { 43 if (response.status == "ok")
75 if (response.status == "ok") 44 closeDialog(true);
76 closeDialog(true); 45 else
77 else 46 alert(response.error);
78 alert(response.error); 47 });
79 }
80 );
81 } 48 }
82 49
83 function closeDialog(success) 50 function closeDialog(success)
84 { 51 {
85 ext.backgroundPage.sendMessage( 52 ext.backgroundPage.sendMessage(
53 {
54 type: "forward",
55 targetPageId: searchParams.targetPageId,
56 payload:
86 { 57 {
87 type: "forward", 58 type: "blockelement-finished",
88 payload: 59 remove: (typeof success == "boolean" ? success : false)
89 {
90 type: "clickhide-close",
91 remove: (typeof success == "boolean" ? success : false)
92 }
93 } 60 }
94 ); 61 });
62 window.close();
95 } 63 }
96 64
97 var dragStartX; 65 function init()
98 var dragStartY; 66 {
99 var dragEndX = null; 67 // Parse the search parameters. We expect both the targetPageId and suggested
100 var dragEndY = null; 68 // filters to be provided.
69 window.location.search.substr(1).split("&").forEach(function(pair)
70 {
71 var parts = pair.split("=");
72 searchParams[parts[0]] = JSON.parse(decodeURIComponent(parts[1]));
73 });
101 74
102 function onDragStart(event) 75 // Attach event listeners
103 { 76 window.addEventListener("keydown", onKeyDown, false);
104 var element = document.elementFromPoint(event.clientX, event.clientY); 77
105 if (element && element.localName == "textarea") 78 document.getElementById("addButton").addEventListener("click", addFilters);
79 document.getElementById("cancelButton").addEventListener(
80 "click", closeDialog.bind(null, false)
81 );
82
83 // Apply jQuery UI styles
84 $("button").button();
85
86 document.getElementById("filters").value = searchParams.filters.join("\n");
87 document.getElementById("filters").focus();
88
89 ext.onMessage.addListener(function(msg, sender, sendResponse)
106 { 90 {
107 // Don't drag the dialog when the user has clicked into the textarea. 91 switch (msg.type)
108 // Most likely the user just wants to focus it or select text there.
109 event.preventDefault();
110 }
111 else
112 {
113 dragStartX = event.screenX;
114 dragStartY = event.screenY;
115 }
116 }
117
118 function onDragEnd(event)
119 {
120 if (dragEndX == null)
121 dragEndX = event.screenX;
122 if (dragEndY == null)
123 dragEndY = event.screenY;
124
125 ext.backgroundPage.sendMessage({
126 type: "forward",
127 payload:
128 { 92 {
129 type: "clickhide-move", 93 case "blockelement-close-popup":
130 x: dragEndX - dragStartX, 94 window.close();
131 y: dragEndY - dragStartY 95 break;
132 } 96 }
133 }); 97 });
134 98
135 dragStartX = null; 99 window.removeEventListener("load", init);
136 dragStartY = null;
137 dragEndX = null;
138 dragEndY = null;
139 } 100 }
140 101 window.addEventListener("load", init, false);
141 // The coordinates in the dragend event are unreliable on Safari. So we
142 // need to get the destination coordinates from the drag event instead.
143 // However on Chrome, the coordinates in the drag event are unreliable.
144 // So we need to get the coordinates from dragend event there.
145 if (navigator.userAgent.indexOf(" Version/") != -1)
146 {
147 window.addEventListener("drag", function(event)
148 {
149 dragEndX = event.screenX;
150 dragEndY = event.screenY;
151 }, false);
152 }
OLDNEW
« no previous file with comments | « block.html ('k') | blockElement.postload.js » ('j') | chrome/ext/background.js » ('J')

Powered by Google App Engine
This is Rietveld