From bc1238073e2936a9c2425254e52ee2b97007948c Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 25 Oct 2020 15:35:11 +0100 Subject: [PATCH] Convert `Ui/User/List` to TypeScript --- .../js/WoltLabSuite/Core/Ui/User/List.js | 128 ++++++++++------- .../ts/WoltLabSuite/Core/Ui/Pagination.ts | 2 +- .../ts/WoltLabSuite/Core/Ui/User/List.js | 117 --------------- .../ts/WoltLabSuite/Core/Ui/User/List.ts | 134 ++++++++++++++++++ 4 files changed, 211 insertions(+), 170 deletions(-) delete mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.js create mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.ts diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/List.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/List.js index 9391b7aeaa..42f6230e9b 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/List.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/User/List.js @@ -1,102 +1,126 @@ /** * Object-based user list. * - * @author Alexander Ebert - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Ui/User/List + * @author Alexander Ebert + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Ui/User/List */ -define(['Ajax', 'Core', 'Dictionary', 'Dom/Util', 'Ui/Dialog', 'WoltLabSuite/Core/Ui/Pagination'], function (Ajax, Core, Dictionary, DomUtil, UiDialog, UiPagination) { +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +define(["require", "exports", "../../Ajax", "../../Core", "../../Dom/Util", "../Dialog", "../Pagination"], function (require, exports, Ajax, Core, Util_1, Dialog_1, Pagination_1) { "use strict"; + Ajax = __importStar(Ajax); + Core = __importStar(Core); + Util_1 = __importDefault(Util_1); + Dialog_1 = __importDefault(Dialog_1); + Pagination_1 = __importDefault(Pagination_1); /** * @constructor */ - function UiUserList(options) { this.init(options); } - UiUserList.prototype = { + class UiUserList { /** * Initializes the user list. * - * @param {object} options list of initialization options + * @param {object} options list of initialization options */ - init: function (options) { - this._cache = new Dictionary(); - this._pageCount = 0; - this._pageNo = 1; - this._options = Core.extend({ + constructor(options) { + this.cache = new Map(); + this.pageCount = 0; + this.pageNo = 1; + this.options = Core.extend({ className: '', dialogTitle: '', - parameters: {} + parameters: {}, }, options); - }, + } /** * Opens the user list. */ - open: function () { - this._pageNo = 1; - this._showPage(); - }, + open() { + this.pageNo = 1; + this.showPage(); + } /** * Shows the current or given page. - * - * @param {int=} pageNo page number */ - _showPage: function (pageNo) { + showPage(pageNo) { if (typeof pageNo === 'number') { - this._pageNo = ~~pageNo; + this.pageNo = +pageNo; } - if (this._pageCount !== 0 && (this._pageNo < 1 || this._pageNo > this._pageCount)) { - throw new RangeError("pageNo must be between 1 and " + this._pageCount + " (" + this._pageNo + " given)."); + if (this.pageCount !== 0 && (this.pageNo < 1 || this.pageNo > this.pageCount)) { + throw new RangeError("pageNo must be between 1 and " + this.pageCount + " (" + this.pageNo + " given)."); } - if (this._cache.has(this._pageNo)) { - var dialog = UiDialog.open(this, this._cache.get(this._pageNo)); - if (this._pageCount > 1) { - var element = elBySel('.jsPagination', dialog.content); + if (this.cache.has(this.pageNo)) { + const dialog = Dialog_1.default.open(this, this.cache.get(this.pageNo)); + if (this.pageCount > 1) { + const element = dialog.content.querySelector('.jsPagination'); if (element !== null) { - new UiPagination(element, { - activePage: this._pageNo, - maxPage: this._pageCount, - callbackSwitch: this._showPage.bind(this) + new Pagination_1.default(element, { + activePage: this.pageNo, + maxPage: this.pageCount, + callbackSwitch: this.showPage.bind(this), }); } // scroll to the list start - var container = dialog.content.parentNode; + const container = dialog.content.parentElement; if (container.scrollTop > 0) { container.scrollTop = 0; } } } else { - this._options.parameters.pageNo = this._pageNo; + this.options.parameters.pageNo = this.pageNo; Ajax.api(this, { - parameters: this._options.parameters + parameters: this.options.parameters, }); } - }, - _ajaxSuccess: function (data) { + } + _ajaxSuccess(data) { if (data.returnValues.pageCount !== undefined) { - this._pageCount = ~~data.returnValues.pageCount; + this.pageCount = ~~data.returnValues.pageCount; } - this._cache.set(this._pageNo, data.returnValues.template); - this._showPage(); - }, - _ajaxSetup: function () { + this.cache.set(this.pageNo, data.returnValues.template); + this.showPage(); + } + _ajaxSetup() { return { data: { actionName: 'getGroupedUserList', - className: this._options.className, - interfaceName: 'wcf\\data\\IGroupedUserListAction' - } + className: this.options.className, + interfaceName: 'wcf\\data\\IGroupedUserListAction', + }, }; - }, - _dialogSetup: function () { + } + _dialogSetup() { return { - id: DomUtil.getUniqueId(), + id: Util_1.default.getUniqueId(), options: { - title: this._options.dialogTitle + title: this.options.dialogTitle, }, - source: null + source: null, }; } - }; + } return UiUserList; }); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Pagination.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Pagination.ts index 91d17df057..033d73ad34 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Pagination.ts +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/Pagination.ts @@ -277,5 +277,5 @@ interface PaginationOptions { activePage: number; maxPage: number; callbackShouldSwitch?: CallbackShouldSwitch | null, - callbackSwitch?: CallbackShouldSwitch | null, + callbackSwitch?: CallbackSwitch | null, } diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.js b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.js deleted file mode 100644 index e14f162188..0000000000 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.js +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Object-based user list. - * - * @author Alexander Ebert - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module WoltLabSuite/Core/Ui/User/List - */ -define(['Ajax', 'Core', 'Dictionary', 'Dom/Util', 'Ui/Dialog', 'WoltLabSuite/Core/Ui/Pagination'], function(Ajax, Core, Dictionary, DomUtil, UiDialog, UiPagination) { - "use strict"; - - /** - * @constructor - */ - function UiUserList(options) { this.init(options); } - UiUserList.prototype = { - /** - * Initializes the user list. - * - * @param {object} options list of initialization options - */ - init: function(options) { - this._cache = new Dictionary(); - this._pageCount = 0; - this._pageNo = 1; - - this._options = Core.extend({ - className: '', - dialogTitle: '', - parameters: {} - }, options); - }, - - /** - * Opens the user list. - */ - open: function() { - this._pageNo = 1; - this._showPage(); - }, - - /** - * Shows the current or given page. - * - * @param {int=} pageNo page number - */ - _showPage: function(pageNo) { - if (typeof pageNo === 'number') { - this._pageNo = ~~pageNo; - } - - if (this._pageCount !== 0 && (this._pageNo < 1 || this._pageNo > this._pageCount)) { - throw new RangeError("pageNo must be between 1 and " + this._pageCount + " (" + this._pageNo + " given)."); - } - - if (this._cache.has(this._pageNo)) { - var dialog = UiDialog.open(this, this._cache.get(this._pageNo)); - - if (this._pageCount > 1) { - var element = elBySel('.jsPagination', dialog.content); - if (element !== null) { - new UiPagination(element, { - activePage: this._pageNo, - maxPage: this._pageCount, - - callbackSwitch: this._showPage.bind(this) - }); - } - - // scroll to the list start - var container = dialog.content.parentNode; - if (container.scrollTop > 0) { - container.scrollTop = 0; - } - } - } - else { - this._options.parameters.pageNo = this._pageNo; - - Ajax.api(this, { - parameters: this._options.parameters - }); - } - }, - - _ajaxSuccess: function(data) { - if (data.returnValues.pageCount !== undefined) { - this._pageCount = ~~data.returnValues.pageCount; - } - - this._cache.set(this._pageNo, data.returnValues.template); - this._showPage(); - }, - - _ajaxSetup: function() { - return { - data: { - actionName: 'getGroupedUserList', - className: this._options.className, - interfaceName: 'wcf\\data\\IGroupedUserListAction' - } - }; - }, - - _dialogSetup: function() { - return { - id: DomUtil.getUniqueId(), - options: { - title: this._options.dialogTitle - }, - source: null - }; - } - }; - - return UiUserList; -}); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.ts new file mode 100644 index 0000000000..1162949a56 --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Ui/User/List.ts @@ -0,0 +1,134 @@ +/** + * Object-based user list. + * + * @author Alexander Ebert + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Ui/User/List + */ + +import * as Ajax from '../../Ajax'; +import * as Core from '../../Core'; +import DomUtil from '../../Dom/Util'; +import UiDialog from '../Dialog'; +import UiPagination from '../Pagination'; +import { CallbackObject as AjaxCallbackObject, DatabaseObjectActionResponse, RequestOptions } from '../../Ajax/Data'; +import { CallbackObject as DialogCallbackObject, DialogData, DialogSettings } from '../Dialog/Data'; + +/** + * @constructor + */ +class UiUserList implements AjaxCallbackObject, DialogCallbackObject { + private readonly cache = new Map(); + private readonly options: AjaxRequestOptions; + private pageCount = 0; + private pageNo = 1; + + /** + * Initializes the user list. + * + * @param {object} options list of initialization options + */ + constructor(options: AjaxRequestOptions) { + this.options = Core.extend({ + className: '', + dialogTitle: '', + parameters: {}, + }, options) as AjaxRequestOptions; + } + + /** + * Opens the user list. + */ + open() { + this.pageNo = 1; + this.showPage(); + } + + /** + * Shows the current or given page. + */ + private showPage(pageNo?: number): void { + if (typeof pageNo === 'number') { + this.pageNo = +pageNo; + } + + if (this.pageCount !== 0 && (this.pageNo < 1 || this.pageNo > this.pageCount)) { + throw new RangeError("pageNo must be between 1 and " + this.pageCount + " (" + this.pageNo + " given)."); + } + + if (this.cache.has(this.pageNo)) { + const dialog = UiDialog.open(this, this.cache.get(this.pageNo)) as DialogData; + + if (this.pageCount > 1) { + const element = dialog.content.querySelector('.jsPagination') as HTMLElement; + if (element !== null) { + new UiPagination(element, { + activePage: this.pageNo, + maxPage: this.pageCount, + + callbackSwitch: this.showPage.bind(this), + }); + } + + // scroll to the list start + const container = dialog.content.parentElement!; + if (container.scrollTop > 0) { + container.scrollTop = 0; + } + } + } else { + this.options.parameters.pageNo = this.pageNo; + + Ajax.api(this, { + parameters: this.options.parameters, + }); + } + } + + _ajaxSuccess(data: AjaxResponse): void { + if (data.returnValues.pageCount !== undefined) { + this.pageCount = ~~data.returnValues.pageCount; + } + + this.cache.set(this.pageNo, data.returnValues.template); + this.showPage(); + } + + _ajaxSetup(): RequestOptions { + return { + data: { + actionName: 'getGroupedUserList', + className: this.options.className, + interfaceName: 'wcf\\data\\IGroupedUserListAction', + }, + }; + } + + _dialogSetup(): DialogSettings { + return { + id: DomUtil.getUniqueId(), + options: { + title: this.options.dialogTitle, + }, + source: null, + }; + } +} + +export = UiUserList + +interface AjaxRequestOptions { + className: string; + dialogTitle: string; + parameters: { + [key: string]: any; + }; +} + +interface AjaxResponse extends DatabaseObjectActionResponse { + returnValues: { + pageCount?: number; + template: string; + } +} -- 2.20.1