|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 let Cc = Components.classes; |
|
8 let Ci = Components.interfaces; |
|
9 let Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm"); |
|
12 Cu.import("resource://gre/modules/Messaging.jsm"); |
|
13 document.addEventListener("DOMContentLoaded", init, false); |
|
14 |
|
15 function dump(a) { |
|
16 Services.console.logStringMessage(a); |
|
17 } |
|
18 |
|
19 function init() { |
|
20 let anchors = document.querySelectorAll(".maybe-later"); |
|
21 for(let anchor of anchors) { |
|
22 anchor.addEventListener("click", maybeLater, false); |
|
23 } |
|
24 document.getElementById("happy-link").addEventListener("click", function(evt) { |
|
25 switchSection("happy"); |
|
26 }, false); |
|
27 document.getElementById("sad-link").addEventListener("click", function(evt) { |
|
28 switchSection("sad"); |
|
29 }, false); |
|
30 |
|
31 window.addEventListener("unload", uninit, false); |
|
32 |
|
33 document.getElementById("open-play-store").addEventListener("click", openPlayStore, false); |
|
34 document.forms[0].addEventListener("submit", sendFeedback, false); |
|
35 for (let anchor of document.querySelectorAll(".no-thanks")) { |
|
36 anchor.addEventListener("click", evt => window.close(), false); |
|
37 } |
|
38 |
|
39 let sumoLink = Services.urlFormatter.formatURLPref("app.support.baseURL"); |
|
40 document.getElementById("sumo-link").href = sumoLink; |
|
41 |
|
42 window.addEventListener("popstate", function (aEvent) { |
|
43 updateActiveSection(aEvent.state ? aEvent.state.section : "intro") |
|
44 }, false); |
|
45 |
|
46 // Fill "Last visited site" input with most recent history entry URL. |
|
47 Services.obs.addObserver(function observer(aSubject, aTopic, aData) { |
|
48 document.getElementById("last-url").value = aData; |
|
49 }, "Feedback:LastUrl", false); |
|
50 |
|
51 sendMessageToJava({ type: "Feedback:LastUrl" }); |
|
52 } |
|
53 |
|
54 function uninit() { |
|
55 Services.obs.removeObserver(this, "Feedback:LastUrl"); |
|
56 } |
|
57 |
|
58 function switchSection(aSection) { |
|
59 history.pushState({ section: aSection }, aSection); |
|
60 updateActiveSection(aSection); |
|
61 } |
|
62 |
|
63 function updateActiveSection(aSection) { |
|
64 document.querySelector("section[active]").removeAttribute("active"); |
|
65 document.getElementById(aSection).setAttribute("active", true); |
|
66 } |
|
67 |
|
68 function openPlayStore() { |
|
69 sendMessageToJava({ type: "Feedback:OpenPlayStore" }); |
|
70 |
|
71 window.close(); |
|
72 } |
|
73 |
|
74 function maybeLater() { |
|
75 window.close(); |
|
76 |
|
77 sendMessageToJava({ type: "Feedback:MaybeLater" }); |
|
78 } |
|
79 |
|
80 function sendFeedback(aEvent) { |
|
81 // Prevent the page from reloading. |
|
82 aEvent.preventDefault(); |
|
83 |
|
84 let section = history.state.section; |
|
85 |
|
86 // Sanity check. |
|
87 if (section != "sad") { |
|
88 Cu.reportError("Trying to send feedback from an invalid section: " + section); |
|
89 return; |
|
90 } |
|
91 |
|
92 let sectionElement = document.getElementById(section); |
|
93 let descriptionElement = sectionElement.querySelector(".description"); |
|
94 |
|
95 // Bail if the description value isn't valid. HTML5 form validation will take care |
|
96 // of showing an error message for us. |
|
97 if (!descriptionElement.validity.valid) |
|
98 return; |
|
99 |
|
100 let data = new FormData(); |
|
101 data.append("description", descriptionElement.value); |
|
102 data.append("_type", 2); |
|
103 |
|
104 let urlElement = document.getElementById("last-url"); |
|
105 // Bail if the URL value isn't valid. HTML5 form validation will take care |
|
106 // of showing an error message for us. |
|
107 if (!urlElement.validity.valid) |
|
108 return; |
|
109 |
|
110 // Only send a URL string if the user provided one. |
|
111 if (urlElement.value) { |
|
112 data.append("add_url", true); |
|
113 data.append("url", urlElement.value); |
|
114 } |
|
115 |
|
116 let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); |
|
117 data.append("device", sysInfo.get("device")); |
|
118 data.append("manufacturer", sysInfo.get("manufacturer")); |
|
119 |
|
120 let req = new XMLHttpRequest(); |
|
121 req.addEventListener("error", function() { |
|
122 Cu.reportError("Error sending feedback to input.mozilla.org: " + req.statusText); |
|
123 }, false); |
|
124 req.addEventListener("abort", function() { |
|
125 Cu.reportError("Aborted sending feedback to input.mozilla.org: " + req.statusText); |
|
126 }, false); |
|
127 |
|
128 let postURL = Services.urlFormatter.formatURLPref("app.feedback.postURL"); |
|
129 req.open("POST", postURL, true); |
|
130 req.send(data); |
|
131 |
|
132 switchSection("thanks-" + section); |
|
133 } |