Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 0 additions & 16 deletions phoneApp/addUser.html

This file was deleted.

18 changes: 0 additions & 18 deletions phoneApp/contacts.html

This file was deleted.

16 changes: 0 additions & 16 deletions phoneApp/editContact.html

This file was deleted.

9 changes: 8 additions & 1 deletion phoneApp/user.html → phoneApp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div id="mountNode"></div>

<script type="text/javascript" src="user.js"></script>
<script type="text/javascript" src="js/contacts.js"></script>
<script type="text/javascript" src="js/addUser.js"></script>
<script type="text/javascript" src="js/keypad.js"></script>

<script type="text/javascript" src="js/editContact.js"></script>
<script type="text/javascript" src="js/user.js"></script>
<script type="text/javascript" src="js/app.js"></script>

</body>
</html>
26 changes: 5 additions & 21 deletions phoneApp/addUser.js → phoneApp/js/addUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//При удалении всех символов отобразить снова весь список

class AddUser {
constructor(){
this.render();
constructor(globalState){
this.state = globalState; //стал равен this.state-у со страницы App.js
}

buttonsHandler(){
Expand Down Expand Up @@ -54,8 +54,7 @@ class AddUser {
}

render() {
let shouldBeRendered = `
<header class="header">
return `<header class="header">
<div class="container top-radius">
<nav class="user-top-line">
<a href="user.html">Cansel</a>
Expand Down Expand Up @@ -94,23 +93,8 @@ class AddUser {
</div>
</div>
</div>
</main>
</main>`

<footer class="footer">
<div class="container bottom-radius">
<nav class="main-nav">
${this.renderLink({href:'contacts', glyphicon:'search', text:'Contacts', active: false})}
${this.renderLink({href:'keypad', glyphicon:'th', text:'Keypad', active: false})}
${this.renderLink({href:'edit-contact', glyphicon:'pencil', text:'Edit contact', active: true})}
${this.renderLink({href:'user', glyphicon:'user', text:'User', active: false})}
${this.renderLink({href:'add-user', glyphicon:'plus', text:'Add user', active: false})}
</nav>
</div>
</footer>`;

document.body.innerHTML = shouldBeRendered;
this.setEvents();
/*this.setEvents();*/
}
}

const addUser = new AddUser;
183 changes: 183 additions & 0 deletions phoneApp/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
class App {
constructor(){
this.state = { //равен ссылке, которая ведет на объект
people: [
{
name: 'Иван',
lastName: 'Петров',
email: '[email protected]'
},
{
name: 'Сергей',
lastName: 'Сергеев',
email: '[email protected]'
},
{
name: 'Иван',
lastName: 'Иванов',
email: '[email protected]'
},
{
name: 'Александр',
lastName: 'Александров',
email: '[email protected]'
},
{
name: 'Алекс',
lastName: 'Смирнов',
email: '[email protected]'
},
{
name: 'Сергей',
lastName: 'Волков',
email: '[email protected]'
},
{
name: 'Мария',
lastName: 'Шарапова',
email: '[email protected]'
},
{
name: 'Александр',
lastName: 'Винник',
email: '[email protected]'
},
{
name: 'Дарий',
lastName: 'Смирнов',
email: '[email protected]'
},
{
name: 'Елена',
lastName: 'Лещенко',
email: '[email protected]'
},
{
name: 'Ольга',
lastName: 'Новикова',
email: '[email protected]'
},
{
name: 'Наталья',
lastName: 'Шемякина',
email: '[email protected]'
},
{
name: 'Анна',
lastName: 'Донцова',
email: '[email protected]'
},
{
name: 'Влад',
lastName: 'Яма',
email: '[email protected]'
},
{
name: 'Кира',
lastName: 'Воробьева',
email: '[email protected]'
},
{
name: 'Виктор',
lastName: 'Кривенко',
email: '[email protected]'
}
],
activePage: 'contacts'
};

this.pages = {
contacts: new ContactsPage(this.state), // тут передали ссылку на this.state
adduser: new AddUser(this.state),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there a typo with formatting

keypad: new KeypadPage(this.state),
editcontact: new EditContact(this.state),
user: new User(this.state)
};

this.initializeRouter();
this.switchRouter();

}

initializeRouter () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure you can create an additional file and call it "Router.js" - and move all specific to navigation logic in.

Then initialize as other pages.

The main goal what we trying to solve the minimal splitting by responsibilities have you feel it?

const mountNode = document.getElementById('mountNode');
//console.log(mountNode);
mountNode.innerHTML = `
<div id="app"></div>
<footer class="footer">
<div class="container bottom-radius">
<nav class="main-nav">
${this.renderLink({href:'contacts', glyphicon:'search', text:'Contacts', active: true})}
${this.renderLink({href:'keypad', glyphicon:'th', text:'Keypad', active: false})}
${this.renderLink({href:'edit-contact', glyphicon:'pencil', text:'Edit contact', active: false})}
${this.renderLink({href:'user', glyphicon:'user', text:'User', active: false})}
${this.renderLink({href:'add-user', glyphicon:'plus', text:'Add user', active: false})}
</nav>
</div>
</footer>`;

//this.initializeRouterHandlers();
this.appDOMNode = mountNode.querySelector('#app'); // сюда будем делать рендер всех страниц
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getElementById working around in 10 times faster than querySelector, I guess it's better to switch it

// и это не будет затрагивать футер и его события
}

renderNewPage() {

this.appDOMNode.innerHTML = this.pages[this.state.activePage].render();
}

renderLink(options) {
let {href, glyphicon, text, active} = options;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be moved to Router.js also

let activeClass = active ? 'active' : '';

return `<a href="${href}" class="tab ${activeClass}">
<span class="glyphicon glyphicon-${glyphicon}" aria-hidden="true"></span>
<span class = "tab-text">${text}</span>
</a> `
}


switchRouter() {
const parent = document.querySelector('.main-nav');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make such selector once - and remember the link to it, no need to make it on every

parent.addEventListener('click', (e) => {
e.preventDefault();
//let target = e && e.target;
let target = e && e.target && (e.target.closest('a') || e.target.classList.contains('tab'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh... such checks look like a bit overhead.
You can add additional attributes to every link and indicate "user clicked on link" that way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on real word usage probably we should be always 100% be sure about our target

if (target == false) return;

//console.log(target);

if (target.classList.contains('active')) return;
if (target.classList.contains('tab')) {
let active = document.querySelector('.active');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

querySelector is always performance costly operation it better to omit every time you can do it.

So, for example, you could the same solution as we did with a slider in class.


active.classList.remove('active');
target.classList.add('active');

let href = target.getAttribute('href');
//console.log(href);
this.state.activePage = href.replace(/-/g, '').toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably href.replace(/-/g, '').toLowerCase() such transformation adding new mental layout for understanding your app. You could just make "already" prepared data in the HTML without requires to transform it in future.

Or you can just use your HTML-attribute to indicate path of navigation and build "Map" to connect your active page

//console.log(this.state.activePage);
this.renderNewPage();
}
return;
})
}
// updateView() {
// const activePage = this.state.activePage;
// this.pages[activePage].updateState(this.state); //updateState делаем на каждой странице
//
// }
render() {
const {activePage} = this.state;
//const activePage = this.state.activePage; // то же самое

// this.updateView();
this.appDOMNode.innerHTML = this.pages[activePage].render(); // и отрендерь ту страничку,
// которая сейчас указана как activePage в this.state

}
}

const app = new App();
app.render();
Loading