2017-06-29 21:19:35 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-10-19 07:31:33 +03:00
|
|
|
/* import-globals-from in-content/extensionControlled.js */
|
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
|
|
|
|
ChromeUtils.import("resource:///modules/SitePermissions.jsm");
|
2017-06-29 21:19:35 +03:00
|
|
|
|
2018-04-30 20:29:18 +03:00
|
|
|
const sitePermissionsL10n = {
|
|
|
|
"desktop-notification": {
|
|
|
|
window: "permissions-site-notification-window",
|
|
|
|
description: "permissions-site-notification-desc",
|
|
|
|
disableLabel: "permissions-site-notification-disable-label",
|
|
|
|
disableDescription: "permissions-site-notification-disable-desc",
|
|
|
|
},
|
|
|
|
"geo": {
|
|
|
|
window: "permissions-site-location-window",
|
|
|
|
description: "permissions-site-location-desc",
|
|
|
|
disableLabel: "permissions-site-location-disable-label",
|
|
|
|
disableDescription: "permissions-site-location-disable-desc",
|
|
|
|
},
|
|
|
|
"camera": {
|
|
|
|
window: "permissions-site-camera-window",
|
|
|
|
description: "permissions-site-camera-desc",
|
|
|
|
disableLabel: "permissions-site-camera-disable-label",
|
|
|
|
disableDescription: "permissions-site-camera-disable-desc",
|
|
|
|
},
|
|
|
|
"microphone": {
|
|
|
|
window: "permissions-site-microphone-window",
|
|
|
|
description: "permissions-site-microphone-desc",
|
|
|
|
disableLabel: "permissions-site-microphone-disable-label",
|
|
|
|
disableDescription: "permissions-site-microphone-disable-desc",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
function Permission(principal, type, capability, l10nId) {
|
2017-06-29 21:19:35 +03:00
|
|
|
this.principal = principal;
|
|
|
|
this.origin = principal.origin;
|
|
|
|
this.type = type;
|
|
|
|
this.capability = capability;
|
2018-04-26 02:24:39 +03:00
|
|
|
this.l10nId = l10nId;
|
2017-06-29 21:19:35 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 19:42:12 +03:00
|
|
|
const PERMISSION_STATES = [SitePermissions.ALLOW, SitePermissions.BLOCK, SitePermissions.PROMPT];
|
2018-10-19 07:31:33 +03:00
|
|
|
const NOTIFICATIONS_PERMISSION_OVERRIDE_KEY = "webNotificationsDisabled";
|
|
|
|
const NOTIFICATIONS_PERMISSION_PREF = "permissions.default.desktop-notification";
|
2017-10-02 19:42:12 +03:00
|
|
|
|
2017-06-29 21:19:35 +03:00
|
|
|
var gSitePermissionsManager = {
|
|
|
|
_type: "",
|
|
|
|
_isObserving: false,
|
|
|
|
_permissions: new Map(),
|
|
|
|
_permissionsToChange: new Map(),
|
|
|
|
_permissionsToDelete: new Map(),
|
|
|
|
_list: null,
|
|
|
|
_removeButton: null,
|
|
|
|
_removeAllButton: null,
|
2017-08-08 09:49:47 +03:00
|
|
|
_searchBox: null,
|
2017-12-20 20:02:20 +03:00
|
|
|
_checkbox: null,
|
|
|
|
_currentDefaultPermissionsState: null,
|
|
|
|
_defaultPermissionStatePrefName: null,
|
2017-06-29 21:19:35 +03:00
|
|
|
|
|
|
|
onLoad() {
|
|
|
|
let params = window.arguments[0];
|
2018-04-30 20:29:18 +03:00
|
|
|
document.mozSubdialogReady = this.init(params);
|
2017-06-29 21:19:35 +03:00
|
|
|
},
|
|
|
|
|
2018-04-30 20:29:18 +03:00
|
|
|
async init(params) {
|
2017-06-29 21:19:35 +03:00
|
|
|
if (!this._isObserving) {
|
|
|
|
Services.obs.addObserver(this, "perm-changed");
|
|
|
|
this._isObserving = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._type = params.permissionType;
|
|
|
|
this._list = document.getElementById("permissionsBox");
|
|
|
|
this._removeButton = document.getElementById("removePermission");
|
|
|
|
this._removeAllButton = document.getElementById("removeAllPermissions");
|
2017-08-08 09:49:47 +03:00
|
|
|
this._searchBox = document.getElementById("searchBox");
|
2017-12-20 20:02:20 +03:00
|
|
|
this._checkbox = document.getElementById("permissionsDisableCheckbox");
|
2018-10-19 07:31:33 +03:00
|
|
|
this._disableExtensionButton = document.getElementById("disableNotificationsPermissionExtension");
|
|
|
|
this._permissionsDisableDescription = document.getElementById("permissionsDisableDescription");
|
2017-06-29 21:19:35 +03:00
|
|
|
|
|
|
|
let permissionsText = document.getElementById("permissionsText");
|
2018-03-14 17:57:15 +03:00
|
|
|
|
2018-06-22 16:13:53 +03:00
|
|
|
let l10n = sitePermissionsL10n[this._type];
|
2018-04-30 20:29:18 +03:00
|
|
|
document.l10n.setAttributes(permissionsText, l10n.description);
|
|
|
|
document.l10n.setAttributes(this._checkbox, l10n.disableLabel);
|
2018-10-19 07:31:33 +03:00
|
|
|
document.l10n.setAttributes(this._permissionsDisableDescription, l10n.disableDescription);
|
2018-04-30 20:29:18 +03:00
|
|
|
document.l10n.setAttributes(document.documentElement, l10n.window);
|
2017-06-29 21:19:35 +03:00
|
|
|
|
2018-04-30 20:29:18 +03:00
|
|
|
await document.l10n.translateElements([
|
|
|
|
permissionsText,
|
|
|
|
this._checkbox,
|
2018-10-19 07:31:33 +03:00
|
|
|
this._permissionsDisableDescription,
|
2018-04-30 20:29:18 +03:00
|
|
|
document.documentElement,
|
|
|
|
]);
|
2017-06-29 21:19:35 +03:00
|
|
|
|
2018-10-19 07:31:33 +03:00
|
|
|
// Initialize the checkbox state and handle showing notification permission UI
|
|
|
|
// when it is disabled by an extension.
|
2017-12-20 20:02:20 +03:00
|
|
|
this._defaultPermissionStatePrefName = "permissions.default." + this._type;
|
2018-10-19 07:31:33 +03:00
|
|
|
this._watchPermissionPrefChange();
|
2018-06-06 13:13:26 +03:00
|
|
|
|
2017-06-29 21:19:35 +03:00
|
|
|
this._loadPermissions();
|
2018-06-22 16:13:53 +03:00
|
|
|
this.buildPermissionsList();
|
2017-08-08 09:49:47 +03:00
|
|
|
|
|
|
|
this._searchBox.focus();
|
2017-06-29 21:19:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit() {
|
|
|
|
if (this._isObserving) {
|
|
|
|
Services.obs.removeObserver(this, "perm-changed");
|
|
|
|
this._isObserving = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
if (topic !== "perm-changed")
|
|
|
|
return;
|
|
|
|
|
2018-02-28 20:51:33 +03:00
|
|
|
let permission = subject.QueryInterface(Ci.nsIPermission);
|
2017-06-29 21:19:35 +03:00
|
|
|
|
2017-10-02 19:42:12 +03:00
|
|
|
// Ignore unrelated permission types and permissions with unknown states.
|
|
|
|
if (permission.type !== this._type || !PERMISSION_STATES.includes(permission.capability))
|
2017-06-29 21:19:35 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (data == "added") {
|
|
|
|
this._addPermissionToList(permission);
|
2017-08-16 18:57:02 +03:00
|
|
|
this.buildPermissionsList();
|
2017-06-29 21:19:35 +03:00
|
|
|
} else if (data == "changed") {
|
|
|
|
let p = this._permissions.get(permission.principal.origin);
|
|
|
|
p.capability = permission.capability;
|
2018-04-26 02:24:39 +03:00
|
|
|
p.l10nId = this._getCapabilityString(permission.capability);
|
2017-06-29 21:19:35 +03:00
|
|
|
this._handleCapabilityChange(p);
|
2017-08-16 18:57:02 +03:00
|
|
|
this.buildPermissionsList();
|
2017-06-29 21:19:35 +03:00
|
|
|
} else if (data == "deleted") {
|
|
|
|
this._removePermissionFromList(permission.principal.origin);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleCapabilityChange(perm) {
|
|
|
|
let permissionlistitem = document.getElementsByAttribute("origin", perm.origin)[0];
|
|
|
|
let menulist = permissionlistitem.getElementsByTagName("menulist")[0];
|
|
|
|
menulist.selectedItem =
|
|
|
|
menulist.getElementsByAttribute("value", perm.capability)[0];
|
|
|
|
},
|
|
|
|
|
2018-10-19 07:31:33 +03:00
|
|
|
_handleCheckboxUIUpdates() {
|
|
|
|
let pref = Services.prefs.getPrefType(this._defaultPermissionStatePrefName);
|
|
|
|
if (pref != Services.prefs.PREF_INVALID) {
|
|
|
|
this._currentDefaultPermissionsState = Services.prefs.getIntPref(this._defaultPermissionStatePrefName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._currentDefaultPermissionsState === null) {
|
|
|
|
this._checkbox.setAttribute("hidden", true);
|
|
|
|
this._permissionsDisableDescription.setAttribute("hidden", true);
|
|
|
|
} else if (this._currentDefaultPermissionsState == SitePermissions.BLOCK) {
|
|
|
|
this._checkbox.checked = true;
|
|
|
|
} else {
|
|
|
|
this._checkbox.checked = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Services.prefs.prefIsLocked(this._defaultPermissionStatePrefName)) {
|
|
|
|
this._checkbox.disabled = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for changes to the permissions.default.* pref and make
|
|
|
|
* necessary changes to the UI.
|
|
|
|
*/
|
|
|
|
_watchPermissionPrefChange() {
|
|
|
|
this._handleCheckboxUIUpdates();
|
|
|
|
|
|
|
|
if (this._type == "desktop-notification") {
|
|
|
|
this._handleWebNotificationsDisable();
|
|
|
|
|
|
|
|
this._disableExtensionButton.addEventListener(
|
|
|
|
"command",
|
|
|
|
makeDisableControllingExtension(PREF_SETTING_TYPE, NOTIFICATIONS_PERMISSION_OVERRIDE_KEY)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let observer = () => {
|
|
|
|
this._handleCheckboxUIUpdates();
|
|
|
|
if (this._type == "desktop-notification") {
|
|
|
|
this._handleWebNotificationsDisable();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Services.prefs.addObserver(this._defaultPermissionStatePrefName, observer);
|
|
|
|
window.addEventListener("unload", () => {
|
|
|
|
Services.prefs.removeObserver(this._defaultPermissionStatePrefName, observer);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the UI update for web notifications disable by extensions.
|
|
|
|
*/
|
|
|
|
async _handleWebNotificationsDisable() {
|
|
|
|
let prefLocked = Services.prefs.prefIsLocked(NOTIFICATIONS_PERMISSION_PREF);
|
|
|
|
if (prefLocked) {
|
|
|
|
// An extension can't control these settings if they're locked.
|
|
|
|
hideControllingExtension(NOTIFICATIONS_PERMISSION_OVERRIDE_KEY);
|
|
|
|
} else {
|
|
|
|
let isControlled = await handleControllingExtension(PREF_SETTING_TYPE, NOTIFICATIONS_PERMISSION_OVERRIDE_KEY);
|
|
|
|
this._checkbox.disabled = isControlled;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-29 21:19:35 +03:00
|
|
|
_getCapabilityString(capability) {
|
|
|
|
let stringKey = null;
|
|
|
|
switch (capability) {
|
|
|
|
case Services.perms.ALLOW_ACTION:
|
2018-04-26 02:24:39 +03:00
|
|
|
stringKey = "permissions-capabilities-allow";
|
2017-06-29 21:19:35 +03:00
|
|
|
break;
|
|
|
|
case Services.perms.DENY_ACTION:
|
2018-04-26 02:24:39 +03:00
|
|
|
stringKey = "permissions-capabilities-block";
|
2017-06-29 21:19:35 +03:00
|
|
|
break;
|
|
|
|
case Services.perms.PROMPT_ACTION:
|
2018-04-26 02:24:39 +03:00
|
|
|
stringKey = "permissions-capabilities-prompt";
|
2017-06-29 21:19:35 +03:00
|
|
|
break;
|
2018-04-26 02:24:39 +03:00
|
|
|
default:
|
2018-06-22 16:13:53 +03:00
|
|
|
throw new Error(`Unknown capability: ${capability}`);
|
2017-06-29 21:19:35 +03:00
|
|
|
}
|
2018-04-26 02:24:39 +03:00
|
|
|
return stringKey;
|
2017-06-29 21:19:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_addPermissionToList(perm) {
|
2017-10-02 19:42:12 +03:00
|
|
|
// Ignore unrelated permission types and permissions with unknown states.
|
|
|
|
if (perm.type !== this._type || !PERMISSION_STATES.includes(perm.capability))
|
2017-06-29 21:19:35 +03:00
|
|
|
return;
|
2018-04-26 02:24:39 +03:00
|
|
|
let l10nId = this._getCapabilityString(perm.capability);
|
|
|
|
let p = new Permission(perm.principal, perm.type, perm.capability, l10nId);
|
2017-06-29 21:19:35 +03:00
|
|
|
this._permissions.set(p.origin, p);
|
|
|
|
},
|
|
|
|
|
|
|
|
_removePermissionFromList(origin) {
|
|
|
|
this._permissions.delete(origin);
|
|
|
|
let permissionlistitem = document.getElementsByAttribute("origin", origin)[0];
|
2018-04-24 22:42:26 +03:00
|
|
|
if (permissionlistitem) {
|
|
|
|
permissionlistitem.remove();
|
|
|
|
}
|
2017-06-29 21:19:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_loadPermissions() {
|
|
|
|
// load permissions into a table.
|
2018-08-19 05:27:33 +03:00
|
|
|
for (let nextPermission of Services.perms.enumerator) {
|
2017-06-29 21:19:35 +03:00
|
|
|
this._addPermissionToList(nextPermission);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_createPermissionListItem(permission) {
|
2018-08-25 03:16:27 +03:00
|
|
|
let richlistitem = document.createXULElement("richlistitem");
|
2017-06-29 21:19:35 +03:00
|
|
|
richlistitem.setAttribute("origin", permission.origin);
|
2018-08-25 03:16:27 +03:00
|
|
|
let row = document.createXULElement("hbox");
|
2017-06-29 21:19:35 +03:00
|
|
|
row.setAttribute("flex", "1");
|
|
|
|
|
2018-08-25 03:16:27 +03:00
|
|
|
let hbox = document.createXULElement("hbox");
|
|
|
|
let website = document.createXULElement("label");
|
2017-06-29 21:19:35 +03:00
|
|
|
website.setAttribute("value", permission.origin);
|
|
|
|
website.setAttribute("width", "50");
|
|
|
|
hbox.setAttribute("class", "website-name");
|
|
|
|
hbox.setAttribute("flex", "3");
|
|
|
|
hbox.appendChild(website);
|
|
|
|
|
2018-08-25 03:16:27 +03:00
|
|
|
let menulist = document.createXULElement("menulist");
|
|
|
|
let menupopup = document.createXULElement("menupopup");
|
2017-06-29 21:19:35 +03:00
|
|
|
menulist.setAttribute("flex", "1");
|
|
|
|
menulist.setAttribute("width", "50");
|
2017-09-08 16:41:02 +03:00
|
|
|
menulist.setAttribute("class", "website-status");
|
2017-06-29 21:19:35 +03:00
|
|
|
menulist.appendChild(menupopup);
|
|
|
|
let states = SitePermissions.getAvailableStates(permission.type);
|
|
|
|
for (let state of states) {
|
2017-07-11 00:33:37 +03:00
|
|
|
// Work around the (rare) edge case when a user has changed their
|
|
|
|
// default permission type back to UNKNOWN while still having a
|
|
|
|
// PROMPT permission set for an origin.
|
|
|
|
if (state == SitePermissions.UNKNOWN &&
|
|
|
|
permission.capability == SitePermissions.PROMPT) {
|
|
|
|
state = SitePermissions.PROMPT;
|
|
|
|
} else if (state == SitePermissions.UNKNOWN) {
|
2017-06-29 21:19:35 +03:00
|
|
|
continue;
|
2017-07-11 00:33:37 +03:00
|
|
|
}
|
2018-08-25 03:16:27 +03:00
|
|
|
let m = document.createXULElement("menuitem");
|
2018-04-26 02:24:39 +03:00
|
|
|
document.l10n.setAttributes(m, this._getCapabilityString(state));
|
2017-06-29 21:19:35 +03:00
|
|
|
m.setAttribute("value", state);
|
|
|
|
menupopup.appendChild(m);
|
|
|
|
}
|
|
|
|
menulist.value = permission.capability;
|
|
|
|
|
|
|
|
menulist.addEventListener("select", () => {
|
|
|
|
this.onPermissionChange(permission, Number(menulist.selectedItem.value));
|
|
|
|
});
|
|
|
|
|
|
|
|
row.appendChild(hbox);
|
|
|
|
row.appendChild(menulist);
|
|
|
|
richlistitem.appendChild(row);
|
2018-04-26 02:24:39 +03:00
|
|
|
return richlistitem;
|
2017-06-29 21:19:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onWindowKeyPress(event) {
|
|
|
|
if (event.keyCode == KeyEvent.DOM_VK_ESCAPE)
|
|
|
|
window.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
onPermissionKeyPress(event) {
|
|
|
|
if (!this._list.selectedItem)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (event.keyCode == KeyEvent.DOM_VK_DELETE ||
|
|
|
|
(AppConstants.platform == "macosx" &&
|
|
|
|
event.keyCode == KeyEvent.DOM_VK_BACK_SPACE)) {
|
|
|
|
this.onPermissionDelete();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_setRemoveButtonState() {
|
|
|
|
if (!this._list)
|
|
|
|
return;
|
|
|
|
|
2017-08-07 11:42:15 +03:00
|
|
|
let hasSelection = this._list.selectedIndex >= 0;
|
|
|
|
let hasRows = this._list.itemCount > 0;
|
2017-06-29 21:19:35 +03:00
|
|
|
this._removeButton.disabled = !hasSelection;
|
|
|
|
this._removeAllButton.disabled = !hasRows;
|
|
|
|
},
|
|
|
|
|
|
|
|
onPermissionDelete() {
|
|
|
|
let richlistitem = this._list.selectedItem;
|
|
|
|
let origin = richlistitem.getAttribute("origin");
|
|
|
|
let permission = this._permissions.get(origin);
|
|
|
|
|
|
|
|
this._removePermissionFromList(origin);
|
|
|
|
this._permissionsToDelete.set(permission.origin, permission);
|
|
|
|
|
|
|
|
this._setRemoveButtonState();
|
|
|
|
},
|
|
|
|
|
|
|
|
onAllPermissionsDelete() {
|
|
|
|
for (let permission of this._permissions.values()) {
|
|
|
|
this._removePermissionFromList(permission.origin);
|
|
|
|
this._permissionsToDelete.set(permission.origin, permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._setRemoveButtonState();
|
|
|
|
},
|
|
|
|
|
|
|
|
onPermissionSelect() {
|
|
|
|
this._setRemoveButtonState();
|
2018-11-02 08:07:45 +03:00
|
|
|
|
|
|
|
// If any item is selected, it should be the only item tabable
|
|
|
|
// in the richlistbox for accessibility reasons.
|
|
|
|
this._list.children.forEach((item) => {
|
|
|
|
let menulist = item.getElementsByTagName("menulist")[0];
|
|
|
|
if (!item.selected) {
|
|
|
|
menulist.setAttribute("tabindex", -1);
|
|
|
|
} else {
|
|
|
|
menulist.removeAttribute("tabindex");
|
|
|
|
}
|
|
|
|
});
|
2017-06-29 21:19:35 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onPermissionChange(perm, capability) {
|
|
|
|
let p = this._permissions.get(perm.origin);
|
|
|
|
if (p.capability == capability)
|
|
|
|
return;
|
|
|
|
p.capability = capability;
|
2018-04-26 02:24:39 +03:00
|
|
|
p.l10nId = this._getCapabilityString(capability);
|
2017-06-29 21:19:35 +03:00
|
|
|
this._permissionsToChange.set(p.origin, p);
|
|
|
|
|
|
|
|
// enable "remove all" button as needed
|
|
|
|
this._setRemoveButtonState();
|
|
|
|
},
|
|
|
|
|
|
|
|
onApplyChanges() {
|
|
|
|
// Stop observing permission changes since we are about
|
|
|
|
// to write out the pending adds/deletes and don't need
|
|
|
|
// to update the UI
|
|
|
|
this.uninit();
|
|
|
|
|
|
|
|
for (let p of this._permissionsToChange.values()) {
|
|
|
|
let uri = Services.io.newURI(p.origin);
|
|
|
|
SitePermissions.set(uri, p.type, p.capability);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let p of this._permissionsToDelete.values()) {
|
|
|
|
let uri = Services.io.newURI(p.origin);
|
|
|
|
SitePermissions.remove(uri, p.type);
|
|
|
|
}
|
2017-12-20 20:02:20 +03:00
|
|
|
|
|
|
|
if (this._checkbox.checked) {
|
|
|
|
Services.prefs.setIntPref(this._defaultPermissionStatePrefName, SitePermissions.BLOCK);
|
|
|
|
} else if (this._currentDefaultPermissionsState == SitePermissions.BLOCK) {
|
|
|
|
Services.prefs.setIntPref(this._defaultPermissionStatePrefName, SitePermissions.UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:19:35 +03:00
|
|
|
window.close();
|
|
|
|
},
|
2017-08-08 09:49:47 +03:00
|
|
|
|
2018-06-22 16:13:53 +03:00
|
|
|
buildPermissionsList(sortCol) {
|
2017-08-08 09:49:47 +03:00
|
|
|
// Clear old entries.
|
|
|
|
let oldItems = this._list.querySelectorAll("richlistitem");
|
|
|
|
for (let item of oldItems) {
|
|
|
|
item.remove();
|
|
|
|
}
|
2018-04-26 02:24:39 +03:00
|
|
|
let frag = document.createDocumentFragment();
|
2017-08-08 09:49:47 +03:00
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
let permissions = Array.from(this._permissions.values());
|
2017-08-16 18:57:02 +03:00
|
|
|
|
2017-08-08 09:49:47 +03:00
|
|
|
let keyword = this._searchBox.value.toLowerCase().trim();
|
2018-04-26 02:24:39 +03:00
|
|
|
for (let permission of permissions) {
|
2017-08-16 18:57:02 +03:00
|
|
|
if (keyword && !permission.origin.includes(keyword)) {
|
2017-08-08 09:49:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
let richlistitem = this._createPermissionListItem(permission);
|
|
|
|
frag.appendChild(richlistitem);
|
2017-08-08 09:49:47 +03:00
|
|
|
}
|
2017-08-16 18:57:02 +03:00
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
// Sort permissions.
|
|
|
|
this._sortPermissions(this._list, frag, sortCol);
|
|
|
|
|
|
|
|
this._list.appendChild(frag);
|
|
|
|
|
2017-08-08 09:49:47 +03:00
|
|
|
this._setRemoveButtonState();
|
|
|
|
},
|
2017-08-16 18:57:02 +03:00
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
_sortPermissions(list, frag, column) {
|
2017-08-16 18:57:02 +03:00
|
|
|
let sortDirection;
|
|
|
|
|
|
|
|
if (!column) {
|
|
|
|
column = document.querySelector("treecol[data-isCurrentSortCol=true]");
|
|
|
|
sortDirection = column.getAttribute("data-last-sortDirection") || "ascending";
|
|
|
|
} else {
|
|
|
|
sortDirection = column.getAttribute("data-last-sortDirection");
|
|
|
|
sortDirection = sortDirection === "ascending" ? "descending" : "ascending";
|
|
|
|
}
|
|
|
|
|
|
|
|
let sortFunc = null;
|
|
|
|
switch (column.id) {
|
|
|
|
case "siteCol":
|
|
|
|
sortFunc = (a, b) => {
|
2018-04-26 02:24:39 +03:00
|
|
|
return comp.compare(a.getAttribute("origin"), b.getAttribute("origin"));
|
2017-08-16 18:57:02 +03:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "statusCol":
|
|
|
|
sortFunc = (a, b) => {
|
2018-04-26 02:24:39 +03:00
|
|
|
return parseInt(a.querySelector("menulist").value) >
|
|
|
|
parseInt(b.querySelector("menulist").value);
|
2017-08-16 18:57:02 +03:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
let comp = new Services.intl.Collator(undefined, {
|
2018-08-31 08:59:17 +03:00
|
|
|
usage: "sort",
|
2018-04-26 02:24:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
let items = Array.from(frag.querySelectorAll("richlistitem"));
|
|
|
|
|
2017-08-16 18:57:02 +03:00
|
|
|
if (sortDirection === "descending") {
|
2018-04-26 02:24:39 +03:00
|
|
|
items.sort((a, b) => sortFunc(b, a));
|
2017-08-16 18:57:02 +03:00
|
|
|
} else {
|
2018-04-26 02:24:39 +03:00
|
|
|
items.sort(sortFunc);
|
2017-08-16 18:57:02 +03:00
|
|
|
}
|
|
|
|
|
2018-04-26 02:24:39 +03:00
|
|
|
// Re-append items in the correct order:
|
|
|
|
items.forEach(item => frag.appendChild(item));
|
|
|
|
|
|
|
|
let cols = list.querySelectorAll("treecol");
|
2017-08-16 18:57:02 +03:00
|
|
|
cols.forEach(c => {
|
|
|
|
c.removeAttribute("data-isCurrentSortCol");
|
|
|
|
c.removeAttribute("sortDirection");
|
|
|
|
});
|
|
|
|
column.setAttribute("data-isCurrentSortCol", "true");
|
|
|
|
column.setAttribute("sortDirection", sortDirection);
|
|
|
|
column.setAttribute("data-last-sortDirection", sortDirection);
|
|
|
|
},
|
2017-06-29 21:19:35 +03:00
|
|
|
};
|