UNPKG

ui5_easy_use

Version:

CLI tool for SAP ui5 and SAPUI5 projects to initialize apps, generate pages, insert form and table components, manage routing, and automate i18n bindings

157 lines (134 loc) 5.05 kB
sap.ui.define([], function () { "use strict"; return class DummyBackend { constructor(storageKey = "ez5DummyBackendRecords") { this.storageKey = storageKey; this.memoryRecords = this._getSeedRecords(); } async list() { return this._cloneRecords(this._readRecords()); } async create(record) { if (!record || typeof record !== "object" || Array.isArray(record)) { throw new Error("DummyBackend.create requires a record object."); } const records = this._readRecords(); const nextRecord = this._buildRecord(record, records); records.push(nextRecord); this._writeRecords(records); return this._cloneRecord(nextRecord); } async reset() { const records = this._getSeedRecords(); this._writeRecords(records); return this._cloneRecords(records); } // Form and table examples both use this shape, so keep new records aligned with the seed data. _buildRecord(record, records) { const status = record.Status || record.Highlight || "None"; return { EmployeeName: "", Country: "", Email: "", Time: "", Status: status, Highlight: status, ...record, EmployeeId: this._resolveEmployeeId(record, records), Highlight: status }; } _readRecords() { const storage = this._getStorage(); if (!storage) { return this._cloneRecords(this.memoryRecords); } const storedRecords = storage.getItem(this.storageKey); if (!storedRecords) { const seedRecords = this._getSeedRecords(); this._writeRecords(seedRecords); return seedRecords; } try { const parsedRecords = JSON.parse(storedRecords); return Array.isArray(parsedRecords) ? parsedRecords : this._getSeedRecords(); } catch (error) { console.warn("DummyBackend: failed to parse stored records; resetting seed data.", error); return this._getSeedRecords(); } } _writeRecords(records) { const nextRecords = this._cloneRecords(records); this.memoryRecords = nextRecords; const storage = this._getStorage(); if (storage) { storage.setItem(this.storageKey, JSON.stringify(nextRecords)); } } _resolveEmployeeId(record, records) { if (record.EmployeeId !== undefined && record.EmployeeId !== null && record.EmployeeId !== "") { return record.EmployeeId; } const numericIds = records .map((item) => Number(item.EmployeeId)) .filter((value) => Number.isFinite(value)); return numericIds.length ? Math.max(...numericIds) + 1 : 1; } _getStorage() { if (typeof window === "undefined" || !window.localStorage) { return null; } return window.localStorage; } _cloneRecords(records) { return records.map((record) => this._cloneRecord(record)); } _cloneRecord(record) { return { ...record }; } _getSeedRecords() { return [ { EmployeeId: 1, EmployeeName: "Zaid", Country: "Saudi Arabia", Email: "zaid@example.com", Time: "09:00 AM", Highlight: "Success" }, { EmployeeId: 2, EmployeeName: "Yazan", Country: "Egypt", Email: "yazan@example.com", Time: "10:30 AM", Highlight: "Information" }, { EmployeeId: 3, EmployeeName: "Ahmed", Country: "Jordan", Email: "ahmed@example.com", Time: "11:15 AM", Highlight: "Warning" }, { EmployeeId: 4, EmployeeName: "Sara", Country: "United Arab Emirates", Email: "sara@example.com", Time: "01:45 PM", Highlight: "None" }, { EmployeeId: 5, EmployeeName: "Lina", Country: "Kuwait", Email: "lina@example.com", Time: "03:00 PM", Highlight: "Success" } ]; } }; });