UNPKG

5.01 kBJavaScriptView Raw
1var snabbdom = require('../../snabbdom.js');
2var patch = snabbdom.init([
3 require('../../modules/class').default,
4 require('../../modules/props').default,
5 require('../../modules/style').default,
6 require('../../modules/eventlisteners').default,
7]);
8var h = require('../../h.js').default;
9
10var vnode;
11
12var nextKey = 11;
13var margin = 8;
14var sortBy = 'rank';
15var totalHeight = 0;
16var originalData = [
17 {rank: 1, title: 'The Shawshank Redemption', desc: 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', elmHeight: 0},
18 {rank: 2, title: 'The Godfather', desc: 'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.', elmHeight: 0},
19 {rank: 3, title: 'The Godfather: Part II', desc: 'The early life and career of Vito Corleone in 1920s New York is portrayed while his son, Michael, expands and tightens his grip on his crime syndicate stretching from Lake Tahoe, Nevada to pre-revolution 1958 Cuba.', elmHeight: 0},
20 {rank: 4, title: 'The Dark Knight', desc: 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.', elmHeight: 0},
21 {rank: 5, title: 'Pulp Fiction', desc: 'The lives of two mob hit men, a boxer, a gangster\'s wife, and a pair of diner bandits intertwine in four tales of violence and redemption.', elmHeight: 0},
22 {rank: 6, title: 'Schindler\'s List', desc: 'In Poland during World War II, Oskar Schindler gradually becomes concerned for his Jewish workforce after witnessing their persecution by the Nazis.', elmHeight: 0},
23 {rank: 7, title: '12 Angry Men', desc: 'A dissenting juror in a murder trial slowly manages to convince the others that the case is not as obviously clear as it seemed in court.', elmHeight: 0},
24 {rank: 8, title: 'The Good, the Bad and the Ugly', desc: 'A bounty hunting scam joins two men in an uneasy alliance against a third in a race to find a fortune in gold buried in a remote cemetery.', elmHeight: 0},
25 {rank: 9, title: 'The Lord of the Rings: The Return of the King', desc: 'Gandalf and Aragorn lead the World of Men against Sauron\'s army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.', elmHeight: 0},
26 {rank: 10, title: 'Fight Club', desc: 'An insomniac office worker looking for a way to change his life crosses paths with a devil-may-care soap maker and they form an underground fight club that evolves into something much, much more...', elmHeight: 0},
27];
28var data = [
29 originalData[0],
30 originalData[1],
31 originalData[2],
32 originalData[3],
33 originalData[4],
34 originalData[5],
35 originalData[6],
36 originalData[7],
37 originalData[8],
38 originalData[9],
39];
40
41function changeSort(prop) {
42 sortBy = prop;
43 data.sort((a, b) => {
44 if (a[prop] > b[prop]) {
45 return 1;
46 }
47 if (a[prop] < b[prop]) {
48 return -1;
49 }
50 return 0;
51 });
52 render();
53}
54
55function add() {
56 var n = originalData[Math.floor(Math.random() * 10)];
57 data = [{rank: nextKey++, title: n.title, desc: n.desc, elmHeight: 0}].concat(data);
58 render();
59 render();
60}
61
62function remove(movie) {
63 data = data.filter((m) => { return m !== movie; });
64 render();
65}
66
67function movieView(movie) {
68 return h('div.row', {
69 key: movie.rank,
70 style: {opacity: '0', transform: 'translate(-200px)',
71 delayed: {transform: `translateY(${movie.offset}px)`, opacity: '1'},
72 remove: {opacity: '0', transform: `translateY(${movie.offset}px) translateX(200px)`}},
73 hook: {insert: (vnode) => { movie.elmHeight = vnode.elm.offsetHeight; }},
74 }, [
75 h('div', {style: {fontWeight: 'bold'}}, movie.rank),
76 h('div', movie.title),
77 h('div', movie.desc),
78 h('div.btn.rm-btn', {on: {click: [remove, movie]}}, 'x'),
79 ]);
80}
81
82function render() {
83 data = data.reduce((acc, m) => {
84 var last = acc[acc.length - 1];
85 m.offset = last ? last.offset + last.elmHeight + margin : margin;
86 return acc.concat(m);
87 }, []);
88 totalHeight = data[data.length - 1].offset + data[data.length - 1].elmHeight;
89 vnode = patch(vnode, view(data));
90}
91
92function view(data) {
93 return h('div', [
94 h('h1', 'Top 10 movies'),
95 h('div', [
96 h('a.btn.add', {on: {click: add}}, 'Add'),
97 'Sort by: ',
98 h('span.btn-group', [
99 h('a.btn.rank', {class: {active: sortBy === 'rank'}, on: {click: [changeSort, 'rank']}}, 'Rank'),
100 h('a.btn.title', {class: {active: sortBy === 'title'}, on: {click: [changeSort, 'title']}}, 'Title'),
101 h('a.btn.desc', {class: {active: sortBy === 'desc'}, on: {click: [changeSort, 'desc']}}, 'Description'),
102 ]),
103 ]),
104 h('div.list', {style: {height: totalHeight+'px'}}, data.map(movieView)),
105 ]);
106}
107
108window.addEventListener('DOMContentLoaded', () => {
109 var container = document.getElementById('container');
110 vnode = patch(container, view(data));
111 render();
112});