Skip to content
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
67 changes: 36 additions & 31 deletions homework/content.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
'use strict';

(function() {
var _wrapper = document.querySelector('#note-content-wrapper');
(function(exports) {

function start() {
window.addEventListener('note-open', function(event) {
var note = event.detail;
resetWrapper();
drawNote(note);
});
}
var ContentManager = function() {
this._wrapper = null;
};

function resetWrapper() {
_wrapper.innerHTML = '';
}
ContentManager.prototype = {
start() {
this._wrapper = document.querySelector('#note-content-wrapper');
window.addEventListener('note-open', this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Good to see you bound an instance (this) instead of binding a function.

/*(function(event) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Commented code could cause troubles. Remove them.

var note = event.detail;
resetWrapper();
drawNote(note);
}).bind(this));
*/
},

function drawNote(note) {
var title = note.title;
var h = document.createElement('h2');
h.textContent = title;
var passages = note.passages;
var buff = document.createDocumentFragment();
passages.forEach(function(passage) {
var p = document.createElement('p');
p.classList.add('note-passage');
p.textContent = passage;
buff.appendChild(p);
});
_wrapper.appendChild(h);
_wrapper.appendChild(buff);
}
resetWrapper() {
this._wrapper.innerHTML = '';
},

document.addEventListener('DOMContentLoaded', function(event) {
start();
});
})();
drawNote(note) {
var title = note.title;
var h = document.createElement('h2');
h.textContent = title;
var passages = note.passages;
var buff = document.createDocumentFragment();
passages.forEach(function(passage) {
var p = document.createElement('p');
p.classList.add('note-passage');
p.textContent = passage;
buff.appendChild(p);
});
this._wrapper.appendChild(h);
this._wrapper.appendChild(buff);
}
}
exports.ContentManager = ContentManager;
})(window);
1 change: 1 addition & 0 deletions homework/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<title> Homework - Note List </title>
<script defer src="list.js"></script>
<script defer src="content.js"></script>
<script defer src="main.js"></script>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
Expand Down
142 changes: 73 additions & 69 deletions homework/list.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,83 @@
'use strict';

(function() {
(function(exports) {

var _listNoteContent = [];
var _wrapper = document.querySelector('#note-list-wrapper');
var ListManager = function() {
var _listNoteContent = [];
var _wrapper = null; };

function start() {
fetchList(function(data) {
updateList(data);
drawList();
preloadFirstNote();
});
window.addEventListener('click', function(event) {
onNoteOpen(event);
});
}
ListManager.prototype = {
start() {
this._wrapper = document.querySelector('#note-list-wrapper');
this.fetchList().then((function(data) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend to use Arrow function: () => { } instead of binding this manually.
See:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Lexical_this

this.updateList(data);
this.drawList();
this.preloadFirstNote();
}).bind(this));

function onNoteOpen(event) {
if (event.target.classList.contains('note-title')) {
var id = event.target.dataset.noteId;
var content = _listNoteContent[id];
window.dispatchEvent(new CustomEvent('note-open',
{ detail: content }));
};
}
window.addEventListener('click', (function(event) {
Copy link
Contributor

Choose a reason for hiding this comment

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

While you use binding this in the previous addEventListener, you should keep the convention consistent. And, It's a convention to prefix method with on for the corresponding function, so it should be renamed as 'onClick'.

this.onNoteOpen(event);
}).bind(this));
},

function preloadFirstNote() {
if (_listNoteContent.length !== 0) {
var content = _listNoteContent[0];
window.dispatchEvent(new CustomEvent('note-open',
{ detail: content }));
}
}

function updateList(list) {
_listNoteContent = list;
}

function drawList() {
var list = _listNoteContent;
var ul = document.createElement('ul');
ul.id = 'note-title-list';
var buff = document.createDocumentFragment();
list.forEach(function(note, i) {
var li = document.createElement('li');
li.dataset.noteId = i;
li.classList.add('note-title');
li.textContent = note.title;
// Note: buff is captured, so we now have a
// little closure naturally.
buff.appendChild(li);
});
ul.appendChild(buff);
_wrapper.appendChild(ul);
}
onNoteOpen(event) {
if (event.target.classList.contains('note-title')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: It's better to put the condition outside a function wholly wrapped by only one branch.

var id = event.target.dataset.noteId;
var content = this._listNoteContent[id];
window.dispatchEvent(new CustomEvent('note-open',
{ detail: content }));
};
},

function fetchList(afterFetch) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8000/demo-list-notes.json', true);
xhr.responseType = 'json';
xhr.onreadystatechange = function(e) {
// Watch out: we have a mysterious unknown 'this'.
if (this.readyState === 4 && this.status === 200) {
var listData = this.response;
// The flow ends here.
afterFetch(listData);
} else if (this.status !== 200 ){
// Ignore error in this case.
preloadFirstNote() {
if (this._listNoteContent.length !== 0) {
var content = this._listNoteContent[0];
window.dispatchEvent(new CustomEvent('note-open',
{ detail: content }));
}
};
xhr.send();
}
},

document.addEventListener('DOMContentLoaded', function(event) {
start();
});
updateList(list) {
this._listNoteContent = list;
},

})();
drawList() {
var list = this._listNoteContent;
var ul = document.createElement('ul');
ul.id = 'note-title-list';
var buff = document.createDocumentFragment();
list.forEach(function(note, i) {
var li = document.createElement('li');
li.dataset.noteId = i;
li.classList.add('note-title');
li.textContent = note.title;
// Note: buff is captured, so we now have a
// little closure naturally.
buff.appendChild(li);
});
ul.appendChild(buff);
this._wrapper.appendChild(ul);
},

fetchList(afterFetch) {
return new Promise((function(resolve, reject) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Good. You have successfully "Promisified" it.

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8000/demo-list-notes.json', true);
xhr.responseType = 'json';
xhr.onreadystatechange = function(e) {
// Watch out: we have a mysterious unknown 'this'.
if (this.readyState === 4 && this.status === 200) {
var listData = this.response;
// The flow ends here.
resolve(listData);
} else if (this.status !== 200 ){
// Ignore error in this case.
reject(xhr);
}
};
xhr.send();
}).bind(this));
}
};
exports.ListManager = ListManager;
})(window);
8 changes: 8 additions & 0 deletions homework/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

document.addEventListener('DOMContentLoaded', function(event) {
var listManager = new ListManager();
var contentManager = new ContentManager();
listManager.start();
contentManager.start();
});
8 changes: 7 additions & 1 deletion homework/test/test-list.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
describe('Test > ', function() {
var sub;
beforeEach(function() {
sub = new ContentManager();
});

it('will test some pure functions', function() {
it('test reset wrapper', function() {
// Write any pure function assertion here.
sub._wrapper = document.createElement('p');
sub._wrapper.innerHTML = "G_G";
sub.resetWrapper();
assert.equal(sub._wrapper.innerHTML , "" ,"Fail!")
Copy link
Contributor

Choose a reason for hiding this comment

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

It's good to see you tested a real method rather than a simple function.

});
});