diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..a32e5e0 Binary files /dev/null and b/.DS_Store differ diff --git a/homework/content.js b/homework/content.js index 710be28..aa1330a 100644 --- a/homework/content.js +++ b/homework/content.js @@ -1,21 +1,19 @@ 'use strict'; -(function() { +(function(exports) { var _wrapper = document.querySelector('#note-content-wrapper'); - function start() { - window.addEventListener('note-open', function(event) { - var note = event.detail; - resetWrapper(); - drawNote(note); - }); + var ContentManager = function() { + this._wrapper = document.querySelector('#note-content-wrapper'); } - function resetWrapper() { - _wrapper.innerHTML = ''; - } + ContentManager.prototype = { + + resetWrapper() { + this._wrapper.innerHTML = ''; + }, - function drawNote(note) { + drawNote(note) { var title = note.title; var h = document.createElement('h2'); h.textContent = title; @@ -27,11 +25,24 @@ p.textContent = passage; buff.appendChild(p); }); - _wrapper.appendChild(h); - _wrapper.appendChild(buff); + this._wrapper.appendChild(h); + this._wrapper.appendChild(buff); + }, + + handleEvent(event) { + switch(event.type) { + case 'note-open': + var note = event.detail; + this.resetWrapper(); + this.drawNote(note); + break; + } + }, + + start() { + window.addEventListener('note-open', this); } - - document.addEventListener('DOMContentLoaded', function(event) { - start(); - }); -})(); +}; + + exports.ContentManager = ContentManager; + })(window); \ No newline at end of file diff --git a/homework/index.html b/homework/index.html index e387963..975d252 100644 --- a/homework/index.html +++ b/homework/index.html @@ -5,6 +5,7 @@ Homework - Note List + diff --git a/homework/list.js b/homework/list.js index 21eddbc..ad35f6b 100644 --- a/homework/list.js +++ b/homework/list.js @@ -1,44 +1,34 @@ 'use strict'; -(function() { +(function(exports) { +var ListManager = function() { var _listNoteContent = []; var _wrapper = document.querySelector('#note-list-wrapper'); +}; - function start() { - fetchList(function(data) { - updateList(data); - drawList(); - preloadFirstNote(); - }); - window.addEventListener('click', function(event) { - onNoteOpen(event); - }); - } - - function onNoteOpen(event) { +ListManager.prototype = { + onClick(event) { if (event.target.classList.contains('note-title')) { var id = event.target.dataset.noteId; - var content = _listNoteContent[id]; + var content = this._listNoteContent[id]; window.dispatchEvent(new CustomEvent('note-open', { detail: content })); }; - } - - function preloadFirstNote() { - if (_listNoteContent.length !== 0) { - var content = _listNoteContent[0]; + }, + + preloadFirstNote() { + var content = this_listNoteContent[0]; window.dispatchEvent(new CustomEvent('note-open', { detail: content })); - } - } - - function updateList(list) { - _listNoteContent = list; - } - - function drawList() { - var list = _listNoteContent; + }, + + updateList(list) { + this._listNoteContent = list; + }, + + drawList() { + var list = this._listNoteContent; var ul = document.createElement('ul'); ul.id = 'note-title-list'; var buff = document.createDocumentFragment(); @@ -52,28 +42,42 @@ buff.appendChild(li); }); ul.appendChild(buff); - _wrapper.appendChild(ul); - } + this._wrapper.appendChild(ul); + }, - 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. - } - }; + fetchList() { + return new Promise(function(resolve, reject) { + 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. + } + }); xhr.send(); - } - - document.addEventListener('DOMContentLoaded', function(event) { - start(); }); + }, + + start() { + this.fetchList().then((function(response){ + this.updateList(response); + this.drawList(); + if (this_listNoteContent.length !== 0){ + this.preloadFirstNote(); + } + }).bind(this)).catch((function(error) { + }).bind(this)); + window.addEventListener('click', (function(event) { + this.onClick(event); + }).bind(this)); + } +}; -})(); + exports.ListManager = ListManager; +})(window); diff --git a/homework/main.js b/homework/main.js new file mode 100644 index 0000000..3b317d8 --- /dev/null +++ b/homework/main.js @@ -0,0 +1,8 @@ +'use strict'; + +document.addEventListener('DOMContentLoaded', function(event) { + var contentManager = new ContentManager(); + var listManager = new ListManager(); + contentManager.start(); + listManager.start(); +}); \ No newline at end of file diff --git a/homework/test/add.js b/homework/test/add.js new file mode 100644 index 0000000..6c6220f --- /dev/null +++ b/homework/test/add.js @@ -0,0 +1,3 @@ +function add(num1, num2){ + return num1 + num2; +} \ No newline at end of file diff --git a/homework/test/test-list.js b/homework/test/test-list.js index 441fc45..fae0c38 100644 --- a/homework/test/test-list.js +++ b/homework/test/test-list.js @@ -1,8 +1,12 @@ -describe('Test > ', function() { - beforeEach(function() { - }); +var assert = require('assert'); +var add = require('./add.js'); - it('will test some pure functions', function() { +describe('Test_add', function() { + it('will test add function', function(done) { // Write any pure function assertion here. + it('add', function(){ + assert.equal('2', add.add(1, 1)); + }) + done(); }); -}); +}); \ No newline at end of file diff --git a/the-badass-todo-list-part-1/.DS_Store b/the-badass-todo-list-part-1/.DS_Store new file mode 100644 index 0000000..b8e6aa8 Binary files /dev/null and b/the-badass-todo-list-part-1/.DS_Store differ