-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
12 changed files
with
135 additions
and
65 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,14 +1,51 @@ | ||
import { Controller } from 'stimulus'; | ||
import StimulusReflex from 'stimulus_reflex'; | ||
import Rails from '@rails/ujs' | ||
import { debounce } from 'lodash-es' | ||
import ApplicationController from './application_controller' | ||
|
||
export default class extends Controller { | ||
connect() { | ||
StimulusReflex.register(this) | ||
let lastMessageId | ||
const reload = controller => { | ||
controller.stimulate('ChatReflex#reload') | ||
} | ||
const debouncedReload = debounce(reload, 100) | ||
|
||
export default class extends ApplicationController { | ||
static get targets () { | ||
return ['list', 'input'] | ||
} | ||
|
||
connect () { | ||
super.connect() | ||
this.scroll(100) | ||
} | ||
|
||
post (event) { | ||
Rails.stopEverything(event) | ||
lastMessageId = Math.random() | ||
this.stimulate( | ||
'ChatReflex#post', | ||
this.element.dataset.color, | ||
this.inputTarget.value, | ||
lastMessageId | ||
) | ||
} | ||
|
||
increment(event) { | ||
console.log('increment') | ||
event.preventDefault() | ||
this.stimulate('ChatReflex#increment', 1) | ||
afterPost () { | ||
this.inputTarget.value = '' | ||
this.inputTarget.focus() | ||
this.scroll(1) | ||
} | ||
|
||
scroll (delay = 10) { | ||
const lists = document.querySelectorAll('[data-target="chat.list"]') | ||
setTimeout(() => { | ||
lists.forEach(e => (e.scrollTop = e.scrollHeight)) | ||
}, delay) | ||
} | ||
|
||
reload (event) { | ||
const { messageId } = event.detail | ||
if (messageId === lastMessageId) return | ||
debouncedReload(this) | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -1,20 +1,28 @@ | ||
import { Application } from 'stimulus' | ||
import StimulusReflex from 'stimulus_reflex' | ||
import WebsocketConsumer from 'sockpuppet-js' | ||
|
||
import CableReady from 'cable_ready' | ||
import debounced from 'debounced' | ||
import BookSearchController from './controllers/book_search_controller' | ||
import ExampleController from './controllers/example_controller' | ||
import ChatController from './controllers/chat_controller' | ||
|
||
debounced.initialize() | ||
//import TurboLinks from 'turbolinks' | ||
import TurboLinks from 'turbolinks' | ||
|
||
//TurboLinks.start() | ||
TurboLinks.start() | ||
|
||
const application = Application.start() | ||
const ssl = location.protocol !== 'https:' ? '' : 's'; | ||
const consumer = new WebsocketConsumer(`ws${ssl}://${location.hostname}:${location.port}/ws/sockpuppet-sync`) | ||
|
||
consumer.subscriptions.create('ChatChannel', { | ||
received (data) { | ||
if (data.cableReady) CableReady.perform(data.operations) | ||
} | ||
}) | ||
|
||
application.register("example", ExampleController) | ||
application.register("book-search", BookSearchController) | ||
application.register("chat", ChatController) | ||
StimulusReflex.initialize(application, { consumer, debug: 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
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 |
---|---|---|
@@ -1 +1,19 @@ | ||
chat demo | ||
|
||
<article id="red" | ||
data-controller="chat" | ||
data-action="chats:added@document->chat#reload cable-ready:after-morph@document->chat#scroll"> | ||
<span data-target="chat.list"> | ||
{% for chat in chats %} | ||
<aside class="message" style="min-height:auto;"> | ||
<p> | ||
{{chat.message}} | ||
</p> | ||
</aside> | ||
{% endfor %} | ||
</span> | ||
<form> | ||
<textarea data-target="chat.input" placeholder="Type your message.."></textarea> | ||
<button data-action="click->chat#post" type="submit"> Send </button> | ||
|
||
</form> | ||
</article> |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,15 +1,9 @@ | ||
from django.views.generic.base import TemplateView | ||
from .mixins import MixinBase | ||
from .mixins import BookSearchMixin | ||
|
||
|
||
class BookSearch(MixinBase, TemplateView): | ||
class BookSearch(BookSearchMixin, TemplateView): | ||
demo_template = "_book_search_demo.html" | ||
subtitle = 'Search Book' | ||
files = ( | ||
('core/reflexes/book_search_reflex.py', 'python', 'python3'), | ||
('core/views/book_search.py', 'python', 'python3'), | ||
('core/javascript/controllers/book_search_controller.js', 'javascript', 'javascript'), | ||
('core/templates/_book_search_demo.html', 'html', 'htmldjango'), | ||
) | ||
|
||
book_search = BookSearch.as_view() |
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
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