mirror of
https://github.com/amark/gun.git
synced 2025-10-14 00:59:35 +00:00
revise chat example
This commit is contained in:
parent
a85a7a6999
commit
ee919e7076
@ -11,12 +11,14 @@
|
|||||||
form .who { width: 10%; }
|
form .who { width: 10%; }
|
||||||
form .what { width: 80%; }
|
form .what { width: 80%; }
|
||||||
ul { list-style: none; padding: 0; }
|
ul { list-style: none; padding: 0; }
|
||||||
|
ul .when {color: #555; font-size: 12pt; float: right; display: none; }
|
||||||
|
li:hover .when {display: inline;}
|
||||||
</style>
|
</style>
|
||||||
<ul><li class="hide">
|
<ul><li class="hide">
|
||||||
<i class="when" style="color: #555; font-size: 12pt;">0</i>
|
|
||||||
<b class="who"></b>:
|
<b class="who"></b>:
|
||||||
<span class="what"></span>
|
<span class="what"></span>
|
||||||
<u class="hide sort">0</u>
|
<u class="hide sort">0</u>
|
||||||
|
<i class="when">0</i>
|
||||||
</li></ul>
|
</li></ul>
|
||||||
<form>
|
<form>
|
||||||
<input class="who" placeholder="alias">
|
<input class="who" placeholder="alias">
|
||||||
@ -25,34 +27,36 @@
|
|||||||
</form>
|
</form>
|
||||||
<script>
|
<script>
|
||||||
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
|
var $ = function(s, e){ return (e || document).querySelector(s) } // make native look like jQuery.
|
||||||
$.sort = function(when, e){ return (when > ($('.sort', e)[$.text] || -Infinity))? e : $.sort(when, e.previousSibling) }
|
;(function(){var H=HTMLElement,C=H.prototype; // jQuery chaining replacement!
|
||||||
|
for(var i in C){(function(j,i){if(j&&j!=i){
|
||||||
|
try{C[j]=C[j]||C[i]}catch(e){C[j]=function(){return this[i]}}
|
||||||
|
}}(i.replace(/[A-Z](.*)/g,''),i));
|
||||||
|
}}());
|
||||||
|
$.sort = function(g, e){ return (g > ($('.sort', e)[$.text] || -Infinity))? e : $.sort(g, e.previousSibling) }
|
||||||
$.text = document.body.textContent? 'textContent' : 'innerText'; // because browsers are stupid.
|
$.text = document.body.textContent? 'textContent' : 'innerText'; // because browsers are stupid.
|
||||||
($.model = $('ul li').cloneNode(true)).removeAttribute('class');
|
</script>
|
||||||
|
<script>
|
||||||
|
($.model = $('ul li').clone(true)).removeAttribute('class');
|
||||||
var gun = Gun(location.origin + '/gun');
|
var gun = Gun(location.origin + '/gun');
|
||||||
var chat = gun.get('example/chat/data').not(function(key){
|
var chat = gun.get('example/chat/data');
|
||||||
gun.put({1: {who: 'Welcome', what: "to the chat app!", when: 1}}).key(key);
|
|
||||||
});
|
|
||||||
chat.map().val(function(msg, field){
|
chat.map().val(function(msg, field){
|
||||||
var $ul = $('ul'), $last = $.sort(field, $ul.lastChild), $msg;
|
var $ul = $('ul'), $last = $.sort(field, $ul.last()), $msg;
|
||||||
($msg = $("#msg-" + field) || $ul.insertBefore($.model.cloneNode(true), $last.nextSibling)).id = 'msg-' + field;
|
($msg = $("#msg-" + field) || $ul.insertBefore($.model.clone(true), $last.next())).id = 'msg-' + field;
|
||||||
$('.who', $msg)[$.text] = msg.who;
|
$('.who', $msg)[$.text] = msg.who;
|
||||||
$('.what', $msg)[$.text] = msg.what;
|
$('.what', $msg)[$.text] = msg.what;
|
||||||
$('.when', $msg)[$.text] = new Date(msg.when).toLocaleTimeString().toLowerCase();
|
$('.when', $msg)[$.text] = new Date(msg.when).toLocaleString().toLowerCase();
|
||||||
$('.sort', $msg)[$.text] = field;
|
$('.sort', $msg)[$.text] = field;
|
||||||
if(document.body.scrollHeight - (window.scrollY + window.innerHeight) <= $ul.lastChild.scrollHeight + 50){
|
if(document.body.scrollHeight - (window.scrollY + window.innerHeight) <= $last.scrollHeight + 50){
|
||||||
window.scrollTo(0, document.body.scrollHeight);
|
window.scrollTo(0, document.body.scrollHeight);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('.who', $('form')).value = (document.cookie.match(/alias\=(.*?)(\&|$|\;)/i)||[])[1]||'';
|
$('.who', $('form')).value = (document.cookie.match(/alias\=(.*?)(\&|$|\;)/i)||[])[1]||'';
|
||||||
$('.what', $('form')).focus();
|
$('.what', $('form')).focus();
|
||||||
$('form').onsubmit = function(e){
|
$('form').onsubmit = function(e){
|
||||||
var msg = {};
|
var msg = {when: Gun.time.is()};
|
||||||
msg.when = Gun.time.is();
|
|
||||||
document.cookie = ('alias=' + (msg.who = $('.who', this).value || 'user' + Gun.text.random(6)));
|
document.cookie = ('alias=' + (msg.who = $('.who', this).value || 'user' + Gun.text.random(6)));
|
||||||
msg.what = $('.what', this).value || '';
|
msg.what = $('.what', this).value || '';
|
||||||
chat.path(msg.when + '_' + Gun.text.random(4)).put(msg);
|
if(msg.what){ chat.path(msg.when + '_' + Gun.text.random(4)).put(msg) }
|
||||||
$('.what', this).value = '';
|
$('.what', this).value = '';
|
||||||
return (e && e.preventDefault()), false;
|
return (e && e.preventDefault()), false;
|
||||||
};
|
};
|
||||||
|
2
gun.js
2
gun.js
@ -1128,7 +1128,7 @@
|
|||||||
if(!data){ return } // let the peers handle no data.
|
if(!data){ return } // let the peers handle no data.
|
||||||
if(err){ return cb(err) }
|
if(err){ return cb(err) }
|
||||||
cb(err, cb.node = data); // node
|
cb(err, cb.node = data); // node
|
||||||
cb(err, Gun.is.node.soul.ify({_: data._}, Gun.is.node.soul(data))); // end
|
cb(err, Gun.is.node.soul.ify({}, Gun.is.node.soul(data))); // end
|
||||||
cb(err, {}); // terminate
|
cb(err, {}); // terminate
|
||||||
});
|
});
|
||||||
}(soul, cb));
|
}(soul, cb));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user