Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task7 #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"angular": "1.6.1",
"whatwg-fetch": "2.0.1"
}
}
14 changes: 14 additions & 0 deletions task7/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Components</title>
</head>
<body>
<div ng-app="components">
<loading></loading>
<topic-list></topic-list>
</div>
<script src="../node_modules/angular/angular.js"></script>
<script src="index.js"></script>
</body>
</html>
104 changes: 104 additions & 0 deletions task7/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
angular.module('components', [])

.service('topicResource', function() {
let topicList = [
{
title: '1 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
},
{
title: '2 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
},
{
title: '3 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
},
{
title: '4 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
},
{
title: '5 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
},
{
title: '6 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
},
{
title: '7 title',
description: 'asds asd sad dasd as dsa ds ad as das das'
}
];

this.getTopics = getTopics;
this.getTopicCount = getTopicCount;

function getTopics(offset, limit) {
return topicList.slice(offset, limit);
}

function getTopicCount() {
return topicList.length;
}
})

.component('loading', {

Choose a reason for hiding this comment

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

It is recommended practice to separate components in different files. But for this task leave it as is :)

Copy link
Owner Author

Choose a reason for hiding this comment

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

yes! I know it :)

template: '<div>Loading...</div>'
})

.component('topic', {
templateUrl: 'templates/topic.html',
controllerAs: 'topicCtrl',
bindings: {
topic: '<'
}
})

.component('topicList', {
templateUrl: 'templates/topicList.html',
controller: topicListController,
controllerAs: 'topicListCtrl'
})

.component('paginator', {
templateUrl: 'templates/paginator.html',
controller: paginatorController,
controllerAs: 'paginatorCtrl',
bindings: {
page: '<',
totalCount: '<',
perPage: '<',
onPageChange: '&'
}
})

function topicListController(topicResource) {
const vm = this;
const LIMIT = 5;

vm.topicCount = topicResource.getTopicCount();
vm.topicPerPage = LIMIT;
vm.onPageChange = onPageChange;
vm.topicCount = 5;
vm.topicCount = 6;
onPageChange(1);

function onPageChange(page) {
vm.currentPage = page;
let offset = (page - 1) * LIMIT;
vm.topicList = topicResource.getTopics(offset, offset + LIMIT);
}
}

function paginatorController() {
const vm = this;

vm.$onChanges = function(changedObj) {
if ('totalCount' in changedObj) {
let pageCount = Math.ceil(changedObj.totalCount.currentValue / changedObj.perPage.currentValue);
vm.pages = [...Array(pageCount).keys()].map((v) => v + 1);
}
}
}
8 changes: 8 additions & 0 deletions task7/templates/paginator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<a ng-repeat="page in paginatorCtrl.pages", ng-click="paginatorCtrl.onPageChange({page})">
<span ng-if="page == paginatorCtrl.page">
<b>{{page}}</b>
</span>
<span ng-if="page != paginatorCtrl.page">
{{page}}
</span>
</a>
4 changes: 4 additions & 0 deletions task7/templates/topic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<acrticle>
<h4>{{topicCtrl.topic.title}}</h4>
<p>{{topicCtrl.topic.description}}</p>
</acrticle>
5 changes: 5 additions & 0 deletions task7/templates/topicList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Topic list</h1>
<div class="topics">
<topic ng-repeat="topic in topicListCtrl.topicList", topic="topic"></topic>
</div>
<paginator page="topicListCtrl.currentPage", total-count="topicListCtrl.topicCount", per-page="topicListCtrl.topicPerPage", on-page-change="topicListCtrl.onPageChange(page)"></paginator>