michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Messaging.jsm"); michael@0: document.addEventListener("DOMContentLoaded", init, false); michael@0: michael@0: function dump(a) { michael@0: Services.console.logStringMessage(a); michael@0: } michael@0: michael@0: function init() { michael@0: let anchors = document.querySelectorAll(".maybe-later"); michael@0: for(let anchor of anchors) { michael@0: anchor.addEventListener("click", maybeLater, false); michael@0: } michael@0: document.getElementById("happy-link").addEventListener("click", function(evt) { michael@0: switchSection("happy"); michael@0: }, false); michael@0: document.getElementById("sad-link").addEventListener("click", function(evt) { michael@0: switchSection("sad"); michael@0: }, false); michael@0: michael@0: window.addEventListener("unload", uninit, false); michael@0: michael@0: document.getElementById("open-play-store").addEventListener("click", openPlayStore, false); michael@0: document.forms[0].addEventListener("submit", sendFeedback, false); michael@0: for (let anchor of document.querySelectorAll(".no-thanks")) { michael@0: anchor.addEventListener("click", evt => window.close(), false); michael@0: } michael@0: michael@0: let sumoLink = Services.urlFormatter.formatURLPref("app.support.baseURL"); michael@0: document.getElementById("sumo-link").href = sumoLink; michael@0: michael@0: window.addEventListener("popstate", function (aEvent) { michael@0: updateActiveSection(aEvent.state ? aEvent.state.section : "intro") michael@0: }, false); michael@0: michael@0: // Fill "Last visited site" input with most recent history entry URL. michael@0: Services.obs.addObserver(function observer(aSubject, aTopic, aData) { michael@0: document.getElementById("last-url").value = aData; michael@0: }, "Feedback:LastUrl", false); michael@0: michael@0: sendMessageToJava({ type: "Feedback:LastUrl" }); michael@0: } michael@0: michael@0: function uninit() { michael@0: Services.obs.removeObserver(this, "Feedback:LastUrl"); michael@0: } michael@0: michael@0: function switchSection(aSection) { michael@0: history.pushState({ section: aSection }, aSection); michael@0: updateActiveSection(aSection); michael@0: } michael@0: michael@0: function updateActiveSection(aSection) { michael@0: document.querySelector("section[active]").removeAttribute("active"); michael@0: document.getElementById(aSection).setAttribute("active", true); michael@0: } michael@0: michael@0: function openPlayStore() { michael@0: sendMessageToJava({ type: "Feedback:OpenPlayStore" }); michael@0: michael@0: window.close(); michael@0: } michael@0: michael@0: function maybeLater() { michael@0: window.close(); michael@0: michael@0: sendMessageToJava({ type: "Feedback:MaybeLater" }); michael@0: } michael@0: michael@0: function sendFeedback(aEvent) { michael@0: // Prevent the page from reloading. michael@0: aEvent.preventDefault(); michael@0: michael@0: let section = history.state.section; michael@0: michael@0: // Sanity check. michael@0: if (section != "sad") { michael@0: Cu.reportError("Trying to send feedback from an invalid section: " + section); michael@0: return; michael@0: } michael@0: michael@0: let sectionElement = document.getElementById(section); michael@0: let descriptionElement = sectionElement.querySelector(".description"); michael@0: michael@0: // Bail if the description value isn't valid. HTML5 form validation will take care michael@0: // of showing an error message for us. michael@0: if (!descriptionElement.validity.valid) michael@0: return; michael@0: michael@0: let data = new FormData(); michael@0: data.append("description", descriptionElement.value); michael@0: data.append("_type", 2); michael@0: michael@0: let urlElement = document.getElementById("last-url"); michael@0: // Bail if the URL value isn't valid. HTML5 form validation will take care michael@0: // of showing an error message for us. michael@0: if (!urlElement.validity.valid) michael@0: return; michael@0: michael@0: // Only send a URL string if the user provided one. michael@0: if (urlElement.value) { michael@0: data.append("add_url", true); michael@0: data.append("url", urlElement.value); michael@0: } michael@0: michael@0: let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); michael@0: data.append("device", sysInfo.get("device")); michael@0: data.append("manufacturer", sysInfo.get("manufacturer")); michael@0: michael@0: let req = new XMLHttpRequest(); michael@0: req.addEventListener("error", function() { michael@0: Cu.reportError("Error sending feedback to input.mozilla.org: " + req.statusText); michael@0: }, false); michael@0: req.addEventListener("abort", function() { michael@0: Cu.reportError("Aborted sending feedback to input.mozilla.org: " + req.statusText); michael@0: }, false); michael@0: michael@0: let postURL = Services.urlFormatter.formatURLPref("app.feedback.postURL"); michael@0: req.open("POST", postURL, true); michael@0: req.send(data); michael@0: michael@0: switchSection("thanks-" + section); michael@0: }