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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* All top-level definitions here are exports. */
6 /* eslint no-unused-vars: [2, {"vars": "local"}] */
8 const PROPERTIES_URL = "chrome://devtools/locale/styleeditor.properties";
10 import { loader } from "resource://devtools/shared/loader/Loader.sys.mjs";
12 const gStringBundle = Services.strings.createBundle(PROPERTIES_URL);
16 loader.lazyRequireGetter(
19 "resource://devtools/client/framework/menu.js"
21 loader.lazyRequireGetter(
24 "resource://devtools/client/framework/menu-item.js"
27 const PREF_AT_RULES_SIDEBAR = "devtools.styleeditor.showAtRulesSidebar";
28 const PREF_ORIG_SOURCES = "devtools.source-map.client-service.enabled";
31 * Returns a localized string with the given key name from the string bundle.
35 * Optional arguments to format in the string.
38 export function getString(name) {
40 if (arguments.length == 1) {
41 return gStringBundle.GetStringFromName(name);
43 const rest = Array.prototype.slice.call(arguments, 1);
44 return gStringBundle.formatStringFromName(name, rest);
48 "L10N error. '" + name + "' is missing from " + PROPERTIES_URL
54 * Retrieve or set the text content of an element.
56 * @param DOMElement root
57 * The element to use for querySelector.
58 * @param string selector
59 * Selector string for the element to get/set the text content.
60 * @param string textContent
61 * Optional text to set.
63 * Text content of matching element or null if there were no element
66 export function text(root, selector, textContent) {
67 const element = root.querySelector(selector);
72 if (textContent === undefined) {
73 return element.textContent;
75 element.textContent = textContent;
80 * Show file picker and return the file user selected.
83 * Optional nsIFile or string representing the filename to auto-select.
84 * @param boolean toSave
85 * If true, the user is selecting a filename to save.
86 * @param nsIWindow parentWindow
87 * Optional parent window. If null the parent window of the file picker
88 * will be the window of the attached input element.
90 * The callback method, which will be called passing in the selected
91 * file or null if the user did not pick one.
92 * @param AString suggestedFilename
93 * The suggested filename when toSave is true.
95 export function showFilePicker(
102 if (typeof path == "string") {
104 if (Services.io.extractScheme(path) == "file") {
105 const uri = Services.io.newURI(path);
106 const file = uri.QueryInterface(Ci.nsIFileURL).file;
115 const file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
116 file.initWithPath(path);
125 // "path" is an nsIFile
130 const fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
131 const mode = toSave ? fp.modeSave : fp.modeOpen;
132 const key = toSave ? "saveStyleSheet" : "importStyleSheet";
133 const fpCallback = function (result) {
134 if (result == Ci.nsIFilePicker.returnCancel) {
141 if (toSave && suggestedFilename) {
142 fp.defaultString = suggestedFilename;
145 fp.init(parentWindow.browsingContext, getString(key + ".title"), mode);
146 fp.appendFilter(getString(key + ".filter"), "*.css");
147 fp.appendFilters(fp.filterAll);
152 * Returns a Popup Menu for the Options ("gear") Button
153 * @param {function} toggleOrigSources
154 * To toggle the original source pref
155 * @param {function} toggleAtRulesSidebar
156 * To toggle the pref to show at-rules side bar
157 * @return {object} popupMenu
158 * A Menu object holding the MenuItems
160 export function optionsPopupMenu(toggleOrigSources, toggleAtRulesSidebar) {
161 const popupMenu = new lazy.Menu();
164 id: "options-origsources",
165 label: getString("showOriginalSources.label"),
166 accesskey: getString("showOriginalSources.accesskey"),
168 checked: Services.prefs.getBoolPref(PREF_ORIG_SOURCES),
169 click: () => toggleOrigSources(),
174 id: "options-show-at-rules",
175 label: getString("showAtRulesSidebar.label"),
176 accesskey: getString("showAtRulesSidebar.accesskey"),
178 checked: Services.prefs.getBoolPref(PREF_AT_RULES_SIDEBAR),
179 click: () => toggleAtRulesSidebar(),