Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions toolkit/components/viewconfig/content/PrefEdit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
"use strict";

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

var params = window.arguments[0]
, win = window.opener
, prefs = Services.prefs
, nameTB, valueElem, success = false;
;

async function init() {
document.getElementById('save').addEventListener("click", onSave);
document.getElementById('cancel').addEventListener("click", () => window.close());

nameTB = document.getElementById("name");
nameTB.value = params.prefCol;

/* params:
.prefCol - имя параметра
.typeCol - тип параметра
.valueCol - значение
.lockCol - PREF_IS_*
*/
switch (params.typeCol) {
case prefs.PREF_BOOL:
valueElem = document.getElementById("bool");
break;
case prefs.PREF_INT:
valueElem = document.getElementById("number");
break;
case prefs.PREF_STRING:
valueElem = document.getElementById("text");
}
valueElem.style.display = "";

document.getElementById("valueLabel").textContent =
await document.l10n.formatValue(
"config-modify-title", { type: win.gTypeStrs[params.typeCol] }
);

if (params.prefCol) { // изменение существующего параметра
document.title = params.prefCol;
valueElem.value = params.valueCol;
valueElem.focus();
} else { // создание нового параметра
document.title =
await document.l10n.formatValue(
"config-new-title", { type: win.gTypeStrs[params.typeCol] }
);
nameTB.parentElement.style.display = "";
nameTB.focus();
}

win = window.outerWidth;
window.sizeToContent();
window.outerWidth = win;
}

function onSave() {
var name = nameTB.value.trim(), value, tailPos;

if (params.typeCol == prefs.PREF_BOOL) {
// строки для ввода значения здесь нет, поэтому при отсутствии имени возврат в диалог
if (!name) return;

if (valueElem.selectedIndex < 0) {
// значение из списка не выбрано, поэтому попытаться использовать его из хвоста имени и вернуться в диалог
tailPos = name.lastIndexOf(";");
if (tailPos >=0) {
switch (name.substring(tailPos + 1).trim().toLowerCase()) {
case "false":
valueElem.selectedIndex = 0;
nameTB.value = name.substring(0, tailPos).trim();
break;
case "true":
valueElem.selectedIndex = 1;
nameTB.value = name.substring(0, tailPos).trim();
// default: ничего не менять
}
}
return;
}

// сохранить, только если тип совпадает (изменение) или параметра нет (создание)
switch (prefs.getPrefType(name)) {
case prefs.PREF_BOOL: // параметр существует
if (!params.prefCol) // вызывали создание нового параметра -
break; // вывалиться без сохранения
case prefs.PREF_INVALID: // параметр ещё не существует
prefs.setBoolPref(name, valueElem.selectedIndex);
}
} else { // PREF_INT, PREF_STRING
value = valueElem.value.trim();
// проверить заполненность хотя бы одного из полей
if (!(name || value)) return; // если нет - вернуться в диалог

// если заполнено только одно из полей, попытаться разбить содержимое другого поля по последней ";"
if (!name) {
name = value;
value = "";
}
if (!value) {
tailPos = name.indexOf(";");
if (tailPos >=0) {
nameTB.value = name.substring(0, tailPos).trim();
valueElem.value = name.substring(tailPos + 1).trim();
return;
}
}

if (params.typeCol == prefs.PREF_INT) {
if (!valueElem.reportValidity()) return; // значение - не целое число

// сохранить, только если тип совпадает (изменение) или параметра нет (создание)
switch (prefs.getPrefType(name)) {
case prefs.PREF_INT: // параметр существует
if (!params.prefCol) // вызывали создание нового параметра -
break; // вывалиться без сохранения
case prefs.PREF_INVALID: // параметр ещё не существует
prefs.setIntPref(name, parseInt(value));
}
} else { // PREF_STRING
// сохранить, только если тип совпадает (изменение) или параметра нет (создание)
switch (prefs.getPrefType(name)) {
case prefs.PREF_STRING: // параметр существует
if (!params.prefCol) // вызывали создание нового параметра -
break; // вывалиться без сохранения
case prefs.PREF_INVALID: // параметр ещё не существует
prefs.setCharPref(name, value);
}
}
}
prefs.savePrefFile(null);
params.prefCol = name;
success = true;
window.close();
}

window.addEventListener('load', init);
43 changes: 43 additions & 0 deletions toolkit/components/viewconfig/content/PrefEdit.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/"?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="localization" href="toolkit/about/aboutConfig.ftl"/>
<link rel="localization" href="browser/newtab/newtab.ftl"/>
<style>
input {
margin-left: 0;
}
input[type="text"], textarea {
width: 100%;
box-sizing: border-box;
}
textarea {
resize: none;
}
</style>
<script src="PrefEdit.js"/>
</head>
<body>
<div style="display:none;">
<label for="name" id="nameLabel" data-l10n-id="config-new-prompt"/><br/>
<input id="name" type="text"/>
</div>
<div>
<label for="number" id="valueLabel"/><br/>
<input id="number" type="number" style="display:none;"/>
<textarea id="text" style="display:none;"/>
<select id="bool" size="3" style="display:none;">
<option value="false">false</option>
<option value="true">true</option>
</select>
</div>
<div style="text-align:center; margin-top:8px;">
<button id="save" type="button" data-l10n-id="newtab-topsites-save-button"/>
<button id="cancel" type="button" data-l10n-id="newtab-topsites-cancel-button"/>
</div>
</body>
</html>
95 changes: 15 additions & 80 deletions toolkit/components/viewconfig/content/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,38 +710,13 @@ function ResetSelected() {
}

async function NewPref(type) {
recordTelemetryOnce("CreateNew");
var result = { value: "" };
var dummy = { value: 0 };

let [newTitle, newPrompt] = await document.l10n.formatValues([
{ id: "config-new-title", args: { type: gTypeStrs[type] } },
{ id: "config-new-prompt" },
]);

if (
Services.prompt.prompt(window, newTitle, newPrompt, result, null, dummy)
) {
result.value = result.value.trim();
if (!result.value) {
return;
}

var pref;
if (result.value in gPrefHash) {
pref = gPrefHash[result.value];
} else {
pref = {
prefCol: result.value,
lockCol: PREF_IS_DEFAULT_VALUE,
typeCol: type,
valueCol: "",
};
}
if (ModifyPref(pref)) {
setTimeout(gotoPref, 0, result.value);
}
}
var pref = { prefCol: ""
, valueCol: ""
, typeCol: type
, lockCol: PREF_IS_DEFAULT_VALUE
};
if (ModifyPref(pref))
setTimeout(gotoPref, 0, pref.prefCol);
}

function gotoPref(pref) {
Expand All @@ -757,58 +732,18 @@ function gotoPref(pref) {
}

async function ModifyPref(entry) {
if (entry.lockCol == PREF_IS_LOCKED) {
if (entry.lockCol == PREF_IS_LOCKED)
return false;
}

let [title] = await document.l10n.formatValues([
{ id: "config-modify-title", args: { type: gTypeStrs[entry.typeCol] } },
]);

if (entry.typeCol == nsIPrefBranch.PREF_BOOL) {
var check = { value: entry.valueCol == "false" };
if (
!entry.valueCol &&
!Services.prompt.select(
window,
title,
entry.prefCol,
[false, true],
check
)
) {
return false;
}
gPrefBranch.setBoolPref(entry.prefCol, check.value);
} else {
var result = { value: entry.valueCol };
var dummy = { value: 0 };
if (
!Services.prompt.prompt(window, title, entry.prefCol, result, null, dummy)
) {
return false;
}
if (entry.typeCol == nsIPrefBranch.PREF_INT) {
// | 0 converts to integer or 0; - 0 to float or NaN.
// Thus, this check should catch all cases.
var val = result.value | 0;
if (val != result.value - 0) {
const [err_title, err_text] = await document.l10n.formatValues([
{ id: "config-nan-title" },
{ id: "config-nan-text" },
]);

Services.prompt.alert(window, err_title, err_text);
return false;
}
gPrefBranch.setIntPref(entry.prefCol, val);
} else {
gPrefBranch.setStringPref(entry.prefCol, result.value);
if (entry.prefCol && entry.typeCol == nsIPrefBranch.PREF_BOOL) {
switch (entry.valueCol) {
case "false":
case "true":
gPrefBranch.setBoolPref(entry.prefCol, entry.valueCol == "false");
return true;
}
}

Services.prefs.savePrefFile(null);
return true;
return window.openDialog("chrome://global/content/PrefEdit.xhtml", "PrefEdit", "modal,centerscreen,resizable,width=300", entry).success;
}

function recordTelemetryOnce(categoryLabel) {
Expand Down
2 changes: 2 additions & 0 deletions toolkit/components/viewconfig/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
toolkit.jar:
content/global/config.xhtml (content/config.xhtml)
content/global/config.js (content/config.js)
content/global/PrefEdit.xhtml (content/PrefEdit.xhtml)
content/global/PrefEdit.js (content/PrefEdit.js)