-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from SergeyKazarinov/routing
Routing
- Loading branch information
Showing
21 changed files
with
383 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import localStorageFn from '@src/helpers/localStorage'; | ||
import { IRootState } from '@src/store/store.types'; | ||
|
||
export const toHTML = (key: string) => { | ||
const model = localStorageFn<IRootState>(key); | ||
const id = key.split(':')[1]; | ||
|
||
if (!model) { | ||
return ''; | ||
} | ||
|
||
return /* html */ ` | ||
<li class="dashboard__record"> | ||
<a href="#excel/${id}" class="dashboard__link"> ${model.title} </a> | ||
<strong class="dashboard__create-date"> | ||
${new Date(model.dateTable).toLocaleDateString()} ${new Date(model.dateTable).toLocaleTimeString()} | ||
</strong> | ||
</li> | ||
`; | ||
}; | ||
|
||
export const getAllKeys = () => { | ||
const keys = []; | ||
for (let i = 0; i < localStorage.length; i++) { | ||
const key = localStorage.key(i); | ||
if (key?.includes('excel')) { | ||
keys.push(key); | ||
} | ||
} | ||
return keys; | ||
}; | ||
|
||
export const createTable = () => { | ||
const keys = getAllKeys(); | ||
|
||
if (!keys.length) { | ||
return ` | ||
<p>Таблицы отсутствуют</p> | ||
`; | ||
} | ||
|
||
return ` | ||
<div class="dashboard__list-header"> | ||
<span>Название</span> | ||
<span>Дата открытия</span> | ||
</div> | ||
<ul class="dashboard__list"> | ||
${keys.map(toHTML).join('')} | ||
</ul> | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class ActiveRoute { | ||
/** | ||
* Геттер получения текущего адреса | ||
* | ||
* @static | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
static get path() { | ||
return window.location.hash.slice(1); | ||
} | ||
|
||
/** | ||
* Геттер получения параметра таблицы | ||
* | ||
* @static | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
static get param() { | ||
return ActiveRoute.path.split('/')[1]; | ||
} | ||
|
||
static navigate(path: string) { | ||
window.location.hash = path; | ||
} | ||
} | ||
|
||
export default ActiveRoute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Dom } from '../dom/dom'; | ||
|
||
interface IPage { | ||
getRoot(): Dom; | ||
afterRender(): void; | ||
destroy(): void; | ||
} | ||
|
||
class Page implements IPage { | ||
protected params: string | undefined; | ||
|
||
constructor(params?: string) { | ||
this.params = params; | ||
} | ||
|
||
/** | ||
* Метод выбрасывает ошибку, если не реализован в классах наследников | ||
* | ||
* @returns {Element} | ||
*/ | ||
getRoot(): Dom { | ||
throw new Error('error page'); | ||
} | ||
|
||
afterRender(): void {} | ||
|
||
destroy(): void {} | ||
} | ||
|
||
export default Page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import DashboardPage from '@src/pages/DashboardPage'; | ||
import ExcelPage from '@src/pages/ExcelPage'; | ||
import $, { Dom } from '../dom/dom'; | ||
import ActiveRoute from './ActiveRoute'; | ||
import Page from './Page'; | ||
|
||
interface IRoutesParams { | ||
dashboard: new (...arg: any[]) => DashboardPage; | ||
excel: new (...arg: any[]) => ExcelPage; | ||
} | ||
|
||
interface IRouter { | ||
/** | ||
* Инициализация роутинга | ||
* Добавление слушателя события на изменения hash | ||
*/ | ||
init(): void; | ||
|
||
/** | ||
* Метод рендеринга страницы (компонента) | ||
*/ | ||
changePageHandler(): void; | ||
/** | ||
* Метод при размонтировании | ||
* Удаляет слушатели событий | ||
*/ | ||
destroy(): void; | ||
} | ||
|
||
class Router implements IRouter { | ||
private $placeholder: Dom; | ||
|
||
private routes: IRoutesParams; | ||
|
||
private page: null | Page; | ||
|
||
constructor(selector: string, routes: IRoutesParams) { | ||
if (!selector) { | ||
throw new Error('Selector is not provided in Router'); | ||
} | ||
|
||
this.$placeholder = $(selector); | ||
this.routes = routes; | ||
this.page = null; | ||
|
||
this.changePageHandler = this.changePageHandler.bind(this); | ||
|
||
this.init(); | ||
} | ||
|
||
init() { | ||
window.addEventListener('hashchange', this.changePageHandler); | ||
this.changePageHandler(); | ||
} | ||
|
||
changePageHandler() { | ||
this.$placeholder.clear(); | ||
if (this.page) { | ||
this.page.destroy(); | ||
} | ||
const PageClass = ActiveRoute.path.includes('excel') ? this.routes.excel : this.routes.dashboard; | ||
this.page = new PageClass(ActiveRoute.param); | ||
this.$placeholder.append(this.page.getRoot()); | ||
this.page.afterRender(); | ||
} | ||
|
||
destroy() { | ||
window.removeEventListener('hashchange', this.changePageHandler); | ||
} | ||
} | ||
|
||
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,10 @@ | ||
import Excel from './components/excel/Excel'; | ||
import Formula from './components/formula/Formula'; | ||
import Header from './components/header/Header'; | ||
import Table from './components/table/Table'; | ||
import Toolbar from './components/toolbar/Toolbar'; | ||
import { EXCEL_STATE } from './consts/localStorage'; | ||
import debounce from './helpers/debounce'; | ||
import localStorageFn from './helpers/localStorage'; | ||
import Router from './core/routes/Router'; | ||
import DashboardPage from './pages/DashboardPage'; | ||
import ExcelPage from './pages/ExcelPage'; | ||
import './scss/index.scss'; | ||
import createStore from './store/createStore'; | ||
import { initialState } from './store/initialState'; | ||
import rootReducer from './store/rootReducer'; | ||
|
||
const store = createStore(rootReducer, initialState); | ||
|
||
const stateListener = debounce(<S>(state: S) => { | ||
console.info(state); | ||
localStorageFn(EXCEL_STATE, state); | ||
}, 500); | ||
|
||
store.subscribe(stateListener); | ||
|
||
const excel = new Excel<Header | Toolbar | Formula | Table>('#app', { | ||
components: [Header, Toolbar, Formula, Table], | ||
store, | ||
// eslint-disable-next-line | ||
new Router('#app', { | ||
dashboard: DashboardPage, | ||
excel: ExcelPage, | ||
}); | ||
|
||
excel.render(); |
Oops, something went wrong.