Comparing version 0.0.10 to 0.0.11
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
(function (root) { | ||
'use strict'; | ||
class Trollbox { | ||
constructor(config) { | ||
this.config = config; | ||
this.scriptId = 'FirebaseScript'; | ||
if (document.querySelector(`#${this.scriptId}`)) { | ||
class Trollbox { | ||
constructor(config) { | ||
this.config = config; | ||
this.scriptId = 'FirebaseScript'; | ||
if (document.querySelector(`#${this.scriptId}`)) { | ||
this.onLoad(); | ||
} else { | ||
const script = document.createElement('script'); | ||
script.id = this.scriptId; | ||
script.src = 'https://www.gstatic.com/firebasejs/4.3.1/firebase.js'; | ||
document.body.appendChild(script); | ||
script.onload = () => { | ||
this.onLoad(); | ||
} else { | ||
const script = document.createElement('script'); | ||
script.id = this.scriptId; | ||
script.src = 'https://www.gstatic.com/firebasejs/4.3.1/firebase.js'; | ||
document.body.appendChild(script); | ||
script.onload = () => { | ||
this.onLoad(); | ||
}; | ||
} | ||
this.onMessage = this.onMessage.bind(this); | ||
this.post = this.post.bind(this); | ||
this.onSubmit = this.onSubmit.bind(this); | ||
}; | ||
} | ||
onLoad() { | ||
this.config.user = this.config.user || 'anon'; | ||
this.initFirebase(); | ||
this.onMessage = this.onMessage.bind(this); | ||
this.post = this.post.bind(this); | ||
this.onSubmit = this.onSubmit.bind(this); | ||
} | ||
this.renderBox(); | ||
this.bindForm(); | ||
onLoad() { | ||
this.config.user = this.config.user || 'anon'; | ||
this.initFirebase(); | ||
this.ref.off('child_added', this.onMessage); | ||
this.ref.limitToFirst(100).on('child_added', this.onMessage); | ||
} | ||
this.renderBox(); | ||
this.bindForm(); | ||
initFirebase() { | ||
if (!this.db) { | ||
var app = window.firebase.initializeApp(this.config.firebase); | ||
this.db = app.database(); | ||
} | ||
this.ref.off('child_added', this.onMessage); | ||
this.ref.limitToFirst(100).on('child_added', this.onMessage); | ||
} | ||
this.initRef(); | ||
initFirebase() { | ||
if (!this.db) { | ||
var app = window.firebase.initializeApp(this.config.firebase); | ||
this.db = app.database(); | ||
} | ||
initRef() { | ||
const channel = (this.config.channel || '').replace(/[^a-zA-Z\d]/gi, '_'); | ||
this.ref = this.db.ref(`trollbox/${channel}`); | ||
} | ||
this.initRef(); | ||
} | ||
setChannel(channel) { | ||
this.config.channel = channel; | ||
this.onLoad(); | ||
} | ||
initRef() { | ||
const channel = (this.config.channel || '').replace(/[^a-zA-Z\d]/gi, '_'); | ||
this.ref = this.db.ref(`trollbox/${channel}`); | ||
} | ||
setUser(user) { | ||
this.config.user = user; | ||
this.onLoad(); | ||
} | ||
setChannel(channel) { | ||
this.config.channel = channel; | ||
this.onLoad(); | ||
} | ||
post(message) { | ||
this.ref.push().set({ | ||
user: this.config.user, | ||
message: message, | ||
date: Date.now() / 1e3 | 0 | ||
}); | ||
} | ||
setUser(user) { | ||
this.config.user = user; | ||
this.onLoad(); | ||
} | ||
onMessage(snapshot) { | ||
const value = snapshot.val(); | ||
post(message) { | ||
this.ref.push().set({ | ||
user: this.config.user, | ||
message: message, | ||
date: Date.now() / 1e3 | 0 | ||
}); | ||
} | ||
if (typeof value !== 'object') { | ||
return false; | ||
} | ||
onMessage(snapshot) { | ||
const value = snapshot.val(); | ||
this.addLog(value.user, value.message); | ||
if (typeof value !== 'object') { | ||
return false; | ||
} | ||
renderBox() { | ||
const selector = this.config.container; | ||
this.container = document.querySelector(selector); | ||
this.addLog(value.user, value.message); | ||
} | ||
// ugly, quick, and dirty | ||
this.container.innerHTML = ` | ||
<div class="TrollboxContainer"> | ||
<div class="TrollboxHeader"> | ||
Trollbox | ||
</div> | ||
<div class="TrollboxMessages"> | ||
<ul class="TrollboxMessagesList"> | ||
</ul> | ||
</div> | ||
<div class="TrollboxMessage"> | ||
<form class="TrollboxForm"> | ||
<input class="TrollboxInput" type="text" name="message" placeholder="Message (press enter to submit)" autocomplete="off" /> | ||
</form> | ||
</div> | ||
renderBox() { | ||
const selector = this.config.container; | ||
this.container = document.querySelector(selector); | ||
// ugly, quick, and dirty | ||
this.container.innerHTML = ` | ||
<div class="TrollboxContainer"> | ||
<div class="TrollboxHeader"> | ||
Trollbox | ||
</div> | ||
`; | ||
} | ||
<div class="TrollboxMessages"> | ||
<ul class="TrollboxMessagesList"> | ||
</ul> | ||
</div> | ||
<div class="TrollboxMessage"> | ||
<form class="TrollboxForm"> | ||
<input class="TrollboxInput" type="text" name="message" placeholder="Message (press enter to submit)" autocomplete="off" /> | ||
</form> | ||
</div> | ||
</div> | ||
`; | ||
} | ||
bindForm(post) { | ||
const form = this.container.querySelector('.TrollboxForm'); | ||
this.form = form; | ||
bindForm(post) { | ||
const form = this.container.querySelector('.TrollboxForm'); | ||
this.form = form; | ||
this.form.removeEventListener('submit', this.onSubmit); | ||
this.form.addEventListener('submit', this.onSubmit); | ||
} | ||
this.form.removeEventListener('submit', this.onSubmit); | ||
this.form.addEventListener('submit', this.onSubmit); | ||
} | ||
onSubmit(event) { | ||
event.preventDefault(); | ||
const input = event.target.message; | ||
const message = input.value; | ||
this.post(message); | ||
input.value = ''; | ||
onSubmit(event) { | ||
event.preventDefault(); | ||
const input = event.target.message; | ||
const message = input.value; | ||
this.post(message); | ||
input.value = ''; | ||
} | ||
addLog(user, message) { | ||
if (!(user && message)) { | ||
return false; | ||
} | ||
addLog(user, message) { | ||
if (!(user && message)) { | ||
return false; | ||
} | ||
const box = this.container.querySelector('.TrollboxMessages'); | ||
const list = this.container.querySelector('.TrollboxMessagesList'); | ||
const box = this.container.querySelector('.TrollboxMessages'); | ||
const list = this.container.querySelector('.TrollboxMessagesList'); | ||
list.innerHTML += `<li><strong>${this.escapeHtml(user)}:</strong> ${this.escapeHtml(message)}</li>`; | ||
list.innerHTML += `<li><strong>${this.escapeHtml(user)}:</strong> ${this.escapeHtml(message)}</li>`; | ||
box.scrollTop = box.scrollHeight; | ||
} | ||
box.scrollTop = box.scrollHeight; | ||
destroy() { | ||
if (this.ref) { | ||
this.ref.off('child_added', this.onMessage); | ||
} | ||
destroy() { | ||
if (this.ref) { | ||
this.ref.off('child_added', this.onMessage); | ||
} | ||
if (this.form) { | ||
this.form.removeEventListener('submit', this.onSubmit); | ||
} | ||
if (this.container) { | ||
this.container.innerHTML = ''; | ||
} | ||
const script = document.querySelector(`#${this.scriptId}`); | ||
if (script) { | ||
script.remove(); | ||
} | ||
if (this.form) { | ||
this.form.removeEventListener('submit', this.onSubmit); | ||
} | ||
escapeHtml(unsafe) { | ||
return unsafe.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, '"').replace(/'/g, '''); | ||
if (this.container) { | ||
this.container.innerHTML = ''; | ||
} | ||
} | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = Trollbox; | ||
const script = document.querySelector(`#${this.scriptId}`); | ||
if (script) { | ||
script.remove(); | ||
} | ||
exports.Trollbox = Trollbox; | ||
} else if (typeof define === 'function' && define.amd) { | ||
define([], function () { | ||
return Trollbox; | ||
}); | ||
} | ||
if (typeof window === 'object') { | ||
window.Trollbox = Trollbox; | ||
escapeHtml(unsafe) { | ||
return unsafe.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, '"').replace(/'/g, '''); | ||
} | ||
})(this); | ||
} | ||
module.exports = Trollbox; | ||
if (typeof window === 'object') { | ||
window.Trollbox = Trollbox; | ||
} | ||
},{}]},{},[1]); |
{ | ||
"name": "trollbox", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
260
trollbox.js
@@ -1,176 +0,162 @@ | ||
(function (root) { | ||
'use strict' | ||
class Trollbox { | ||
constructor (config) { | ||
this.config = config | ||
this.scriptId = 'FirebaseScript' | ||
if (document.querySelector(`#${this.scriptId}`)) { | ||
class Trollbox { | ||
constructor (config) { | ||
this.config = config | ||
this.scriptId = 'FirebaseScript' | ||
if (document.querySelector(`#${this.scriptId}`)) { | ||
this.onLoad() | ||
} else { | ||
const script = document.createElement('script') | ||
script.id = this.scriptId | ||
script.src = 'https://www.gstatic.com/firebasejs/4.3.1/firebase.js' | ||
document.body.appendChild(script) | ||
script.onload = () => { | ||
this.onLoad() | ||
} else { | ||
const script = document.createElement('script') | ||
script.id = this.scriptId | ||
script.src = 'https://www.gstatic.com/firebasejs/4.3.1/firebase.js' | ||
document.body.appendChild(script) | ||
script.onload = () => { | ||
this.onLoad() | ||
} | ||
} | ||
this.onMessage = this.onMessage.bind(this) | ||
this.post = this.post.bind(this) | ||
this.onSubmit = this.onSubmit.bind(this) | ||
} | ||
onLoad () { | ||
this.config.user = this.config.user || 'anon' | ||
this.initFirebase() | ||
this.onMessage = this.onMessage.bind(this) | ||
this.post = this.post.bind(this) | ||
this.onSubmit = this.onSubmit.bind(this) | ||
} | ||
this.renderBox() | ||
this.bindForm() | ||
onLoad () { | ||
this.config.user = this.config.user || 'anon' | ||
this.initFirebase() | ||
this.ref.off('child_added', this.onMessage) | ||
this.ref.limitToFirst(100) | ||
.on('child_added', this.onMessage) | ||
} | ||
this.renderBox() | ||
this.bindForm() | ||
initFirebase () { | ||
if (!this.db) { | ||
var app = window.firebase.initializeApp(this.config.firebase) | ||
this.db = app.database() | ||
} | ||
this.ref.off('child_added', this.onMessage) | ||
this.ref.limitToFirst(100) | ||
.on('child_added', this.onMessage) | ||
} | ||
this.initRef() | ||
initFirebase () { | ||
if (!this.db) { | ||
var app = window.firebase.initializeApp(this.config.firebase) | ||
this.db = app.database() | ||
} | ||
initRef () { | ||
const channel = (this.config.channel || '').replace(/[^a-zA-Z\d]/gi, '_') | ||
this.ref = this.db.ref(`trollbox/${channel}`) | ||
} | ||
this.initRef() | ||
} | ||
setChannel (channel) { | ||
this.config.channel = channel | ||
this.onLoad() | ||
} | ||
initRef () { | ||
const channel = (this.config.channel || '').replace(/[^a-zA-Z\d]/gi, '_') | ||
this.ref = this.db.ref(`trollbox/${channel}`) | ||
} | ||
setUser (user) { | ||
this.config.user = user | ||
this.onLoad() | ||
} | ||
setChannel (channel) { | ||
this.config.channel = channel | ||
this.onLoad() | ||
} | ||
post (message) { | ||
this.ref.push().set({ | ||
user: this.config.user, | ||
message: message, | ||
date: (Date.now() / 1e3) | 0 | ||
}) | ||
} | ||
setUser (user) { | ||
this.config.user = user | ||
this.onLoad() | ||
} | ||
onMessage (snapshot) { | ||
const value = snapshot.val() | ||
post (message) { | ||
this.ref.push().set({ | ||
user: this.config.user, | ||
message: message, | ||
date: (Date.now() / 1e3) | 0 | ||
}) | ||
} | ||
if (typeof value !== 'object') { | ||
return false | ||
} | ||
onMessage (snapshot) { | ||
const value = snapshot.val() | ||
this.addLog(value.user, value.message) | ||
if (typeof value !== 'object') { | ||
return false | ||
} | ||
renderBox () { | ||
const selector = this.config.container | ||
this.container = document.querySelector(selector) | ||
this.addLog(value.user, value.message) | ||
} | ||
// ugly, quick, and dirty | ||
this.container.innerHTML = ` | ||
<div class="TrollboxContainer"> | ||
<div class="TrollboxHeader"> | ||
Trollbox | ||
</div> | ||
<div class="TrollboxMessages"> | ||
<ul class="TrollboxMessagesList"> | ||
</ul> | ||
</div> | ||
<div class="TrollboxMessage"> | ||
<form class="TrollboxForm"> | ||
<input class="TrollboxInput" type="text" name="message" placeholder="Message (press enter to submit)" autocomplete="off" /> | ||
</form> | ||
</div> | ||
renderBox () { | ||
const selector = this.config.container | ||
this.container = document.querySelector(selector) | ||
// ugly, quick, and dirty | ||
this.container.innerHTML = ` | ||
<div class="TrollboxContainer"> | ||
<div class="TrollboxHeader"> | ||
Trollbox | ||
</div> | ||
` | ||
} | ||
<div class="TrollboxMessages"> | ||
<ul class="TrollboxMessagesList"> | ||
</ul> | ||
</div> | ||
<div class="TrollboxMessage"> | ||
<form class="TrollboxForm"> | ||
<input class="TrollboxInput" type="text" name="message" placeholder="Message (press enter to submit)" autocomplete="off" /> | ||
</form> | ||
</div> | ||
</div> | ||
` | ||
} | ||
bindForm (post) { | ||
const form = this.container.querySelector('.TrollboxForm') | ||
this.form = form | ||
bindForm (post) { | ||
const form = this.container.querySelector('.TrollboxForm') | ||
this.form = form | ||
this.form.removeEventListener('submit', this.onSubmit) | ||
this.form.addEventListener('submit', this.onSubmit) | ||
} | ||
this.form.removeEventListener('submit', this.onSubmit) | ||
this.form.addEventListener('submit', this.onSubmit) | ||
} | ||
onSubmit (event) { | ||
event.preventDefault() | ||
const input = event.target.message | ||
const message = input.value | ||
this.post(message) | ||
input.value = '' | ||
onSubmit (event) { | ||
event.preventDefault() | ||
const input = event.target.message | ||
const message = input.value | ||
this.post(message) | ||
input.value = '' | ||
} | ||
addLog (user, message) { | ||
if (!(user && message)) { | ||
return false | ||
} | ||
addLog (user, message) { | ||
if (!(user && message)) { | ||
return false | ||
} | ||
const box = this.container.querySelector('.TrollboxMessages') | ||
const list = this.container.querySelector('.TrollboxMessagesList') | ||
const box = this.container.querySelector('.TrollboxMessages') | ||
const list = this.container.querySelector('.TrollboxMessagesList') | ||
list.innerHTML += `<li><strong>${this.escapeHtml(user)}:</strong> ${this.escapeHtml(message)}</li>` | ||
list.innerHTML += `<li><strong>${this.escapeHtml(user)}:</strong> ${this.escapeHtml(message)}</li>` | ||
box.scrollTop = box.scrollHeight | ||
} | ||
box.scrollTop = box.scrollHeight | ||
destroy () { | ||
if (this.ref) { | ||
this.ref.off('child_added', this.onMessage) | ||
} | ||
destroy () { | ||
if (this.ref) { | ||
this.ref.off('child_added', this.onMessage) | ||
} | ||
if (this.form) { | ||
this.form.removeEventListener('submit', this.onSubmit) | ||
} | ||
if (this.container) { | ||
this.container.innerHTML = '' | ||
} | ||
const script = document.querySelector(`#${this.scriptId}`) | ||
if (script) { | ||
script.remove() | ||
} | ||
if (this.form) { | ||
this.form.removeEventListener('submit', this.onSubmit) | ||
} | ||
escapeHtml (unsafe) { | ||
return unsafe | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/'/g, '"') | ||
.replace(/'/g, ''') | ||
if (this.container) { | ||
this.container.innerHTML = '' | ||
} | ||
} | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = Trollbox | ||
const script = document.querySelector(`#${this.scriptId}`) | ||
if (script) { | ||
script.remove() | ||
} | ||
exports.Trollbox = Trollbox | ||
} else if (typeof define === 'function' && define.amd) { | ||
define([], function () { | ||
return Trollbox | ||
}) | ||
} | ||
if (typeof window === 'object') { | ||
window.Trollbox = Trollbox | ||
escapeHtml (unsafe) { | ||
return unsafe | ||
.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/'/g, '"') | ||
.replace(/'/g, ''') | ||
} | ||
} | ||
})(this); | ||
module.exports = Trollbox | ||
if (typeof window === 'object') { | ||
window.Trollbox = Trollbox | ||
} |
315555
350