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
86 changes: 51 additions & 35 deletions homework/content.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
'use strict';
(function(exports){

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

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

function resetWrapper() {
_wrapper.innerHTML = '';
}

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);
}

document.addEventListener('DOMContentLoaded', function(event) {
start();
});
})();

/**
* Create a instance of contentManager.
*
* @constructor
*/
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 tried to JSDoc it.

var ContentManager = function() {
/** @private */ this._wrapper = document.querySelector('#note-content-wrapper');
document.addEventListener('DOMContentLoaded', this.start.bind(this));
}

ContentManager.prototype = {

/** Initialization */
start() {
window.addEventListener('note-open', this.updateContent.bind(this));
Copy link
Contributor

Choose a reason for hiding this comment

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

To set this as the listener is better than an anonymous function.

},

/** Update note wrapper's content */
updateContent(event) {
var note = event.detail;
this.resetWrapper();
this.drawNote(note);
},

/** Clear the note wrapper */
resetWrapper() {
this._wrapper.innerHTML = '';
},

/** set content on wrapper */
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);
}
}

window.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
2 changes: 1 addition & 1 deletion homework/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = function(config) {

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Firefox'],
browsers: ['FirefoxDeveloper'],


// Continuous Integration mode
Expand Down
158 changes: 89 additions & 69 deletions homework/list.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,99 @@
'use strict';
(function(exports) {

(function() {
/**
* Create a instance of the ListManager.
*
* @constructor
*/
var ListManager = function() {
/** @private */ this._listNoteContent = [];
/** @private */ this._wrapper = document.querySelector('#note-list-wrapper');
document.addEventListener('DOMContentLoaded', this.start.bind(this));
}

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

function start() {
fetchList(function(data) {
updateList(data);
drawList();
preloadFirstNote();
});
window.addEventListener('click', function(event) {
onNoteOpen(event);
});
}
/** Initialize the listManager. */
start() {
this.fetchList()
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 have "Promisified" the chain and use it with the following one.

.then(this.updateList.bind(this))
.then(this.drawList.bind(this))
.then(this.preloadFirstNote.bind(this))
.catch(function(error) {
console.error(error);
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 necessary to re-throw the error after logging it. Since you will silent the error after this step.

});
window.addEventListener('click', this.onNoteOpen.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 }));
};
}
/**
* Get List form localhost
*
* @return {Promise} A start point of the work flow.
*/
fetchList(afterFetch) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you have already "Promisified" the function, the callback argument here is unnecessary.

return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', 'http://127.0.0.1:8000/demo-list-notes.json', true);
req.responseType = 'json';
req.onload = function () {
if (req.status === 200 )
resolve(req.response);
};
req.onerror = function () {
reject( new Error(req.statusText));
};
req.send();
});
},

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

function updateList(list) {
_listNoteContent = list;
}
/** Append all list title */
drawList() {
var list = this._listNoteContent;
var ul = document.createElement('ul');
ul.id = 'note-title-list';
var buff = document.createDocumentFragment();
list.forEach(insertItem.bind(buff));
ul.appendChild(buff);
this._wrapper.appendChild(ul);
},

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);
}
/** Show first note before user do anything. */
preloadFirstNote() {
if (this._listNoteContent.length !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To improve expressiveness you should move the condition outside the callee, and put it in the caller. Since there is only one branch of such if...else

var content = this._listNoteContent[0];
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.
}
};
xhr.send();
}
/**
* Trigger 'note-open' event while note opened
*
* @this {ListManager}
* @param {object} event The information such as event source, which is about event.
*/
onNoteOpen(event) {
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 a convention to prefix method with on for the corresponding function, so it should be renamed as 'onClick'

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

document.addEventListener('DOMContentLoaded', function(event) {
start();
});

})();
function insertItem(element, index) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Although it's a good try to create a method within one specific scope, it's still weird to see this in a manager styled file.

var li = document.createElement('li');
li.dataset.noteId = index;
li.classList.add('note-title');
li.textContent = element.title;
this.appendChild(li);
}
7 changes: 7 additions & 0 deletions homework/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
document.addEventListener('DOMContentLoaded', function(event) {
var contentManger = new ContentManager();
var listManager = new ListManager();
contentManger.start();
listManager.start();
});
2 changes: 1 addition & 1 deletion homework/test/test-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ describe('Test > ', function() {
});

it('will test some pure functions', function() {
// Write any pure function assertion here.
expect(true).to.be.true;
Copy link
Contributor

Choose a reason for hiding this comment

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

No. You should test a method defined in one manager.

});
});