-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24c6ba8
commit 200331a
Showing
11 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<div class="tweet"> | ||
<form id="tweet-form"> | ||
<input type="text" class="tweet-input"> | ||
</form> | ||
</div> | ||
|
||
<ul class="tweetlist" id="tweet-list"> | ||
<li class="tweetlist-item"> | ||
@omauriciosoares<br> | ||
Core.js rocks! | ||
</li> | ||
</ul> | ||
|
||
<script | ||
src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" | ||
crossorigin="anonymous"></script> | ||
<script type="module" src="./main.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {Core} from "../../src/core/core.js"; | ||
import "./tweet-form.js"; | ||
import "./tweet-list.js"; | ||
import "./tweet-counter.js"; | ||
|
||
|
||
Core.start('tweet-counter', 'first counter'); | ||
Core.start('tweet-form'); | ||
Core.start('tweet-list'); | ||
Core.start('tweet-counter', 'second counter'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
this file exports the common interface for the entire project | ||
common things: ui, network, event symbols, etc ... | ||
*/ | ||
|
||
export {$, fetcher, events, errors} | ||
|
||
const $ = window.$; | ||
|
||
// like fetch but throws on 400+ | ||
const fetcher = (url, options) => { | ||
return fetch(url, options).then((response) => { | ||
if (!response.ok) { | ||
throw response; | ||
} | ||
return response; | ||
}); | ||
}; | ||
|
||
const events = { | ||
receiveTweet: Symbol(), | ||
submitTweet: Symbol() | ||
}; | ||
|
||
const errors = { | ||
tweetTooLong: Symbol() | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Core} from "../../src/core/core.js"; | ||
import {$, fetcher, events} from "./projectCommon.js"; | ||
|
||
Core.register('tweet-counter', function(sandbox) { | ||
return { | ||
init: function() { | ||
this.counter = document.createElement("output") | ||
this.count = 0; | ||
this.updateCount(); | ||
document.body.appendChild(this.counter); | ||
this.listen(); | ||
}, | ||
|
||
listen: function() { | ||
sandbox.listen(events.receiveTweet, this.newTweet, this); | ||
}, | ||
|
||
updateCount: function() { | ||
this.counter.textContent = this.count; | ||
}, | ||
|
||
newTweet: function(data) { | ||
this.count++; | ||
this.updateCount(); | ||
}, | ||
|
||
destroy: function() { | ||
this.counter.remove(); | ||
} | ||
} | ||
}, true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {Core} from "../../src/core/core.js"; | ||
import {$, fetcher, events} from "./projectCommon.js"; | ||
|
||
|
||
Core.register('tweet-form', function(sandbox) { | ||
return { | ||
init: function() { | ||
this.$form = $('#tweet-form'); | ||
this.$input = this.$form.find('input'); | ||
|
||
this.addListeners(); | ||
}, | ||
|
||
addListeners: function() { | ||
this.$form.on('submit', this.onSubmit.bind(this)); | ||
}, | ||
|
||
onSubmit: function(e) { | ||
e.preventDefault(); | ||
|
||
var newTweet = this.$input[0].value; | ||
this.$input[0].value = ''; | ||
|
||
this.notify(events.submitTweet); | ||
}, | ||
|
||
notify: function(tweet) { | ||
sandbox.notify({ | ||
type: 'new-tweet', | ||
data: { | ||
tweet: tweet, | ||
author: '@omauriciosoares' | ||
} | ||
}); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Core} from "../../src/core/core.js"; | ||
import {$, fetcher, events} from "./projectCommon.js"; | ||
|
||
|
||
Core.register('tweet-list', function(sandbox) { | ||
return { | ||
init: function() { | ||
this.$list = $('#tweet-list'); | ||
|
||
this.listen(); | ||
}, | ||
|
||
listen: function() { | ||
sandbox.listen(events.receiveTweet, this.newTweet, this); | ||
}, | ||
|
||
newTweet: function(data) { | ||
var newTweetHtml = this.getHtml(data); | ||
|
||
this.$list.prepend(newTweetHtml); | ||
}, | ||
|
||
getHtml: function(data) { | ||
var li = $( | ||
`<li class="tweetlist-item"> | ||
${data.author}<br> | ||
${data.tweet} | ||
</li>`); | ||
li.append(); | ||
|
||
return li; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {Core} from "../../src/core/core.js"; | ||
import {$, fetcher, events, errors} from "./projectCommon.js"; | ||
|
||
|
||
Core.register('tweet-list', function(sandbox) { | ||
return { | ||
init: function() { | ||
this.maxLength = 100; | ||
|
||
this.listen(); | ||
}, | ||
|
||
listen: function() { | ||
sandbox.listen(events.submitTweet, this.validateTweet, this); | ||
}, | ||
|
||
validateTweet: function(data) { | ||
if (data.length > this.maxLength) { | ||
this.notify(events.error, errors.tweetTooLong); | ||
return; | ||
} | ||
this.notify(events.receiveTweet, data); | ||
} | ||
|
||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters