annotate console/console.js @ 106:c06ec02217ee

many changes
author Sonny Piers <sonny@fastmail.net>
date Tue, 26 Jun 2012 12:02:14 +0200
parents
children 5cb4733c5189
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
106
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
1 'use strict';
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
2
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
3 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
4
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
5 if(!Lightstring)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
6 var Lightstring = {};
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
7
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
8 Lightstring.console = {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
9 invertedScroll: true,
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
10 log: function(aLog) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
11 var stanza = vkbeautify.xml(aLog.data, 2, ' ');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
12 var entry = document.createElement('div');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
13 entry.classList.add('entry');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
14 entry.classList.add(aLog.dir);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
15 var header = document.createElement('header');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
16 //FIXME date? should come from the origin?
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
17 header.textContent = aLog.dir + ': ';
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
18 entry.appendChild(header)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
19 var pre = document.createElement('pre');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
20 pre.textContent = stanza;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
21 entry.appendChild(pre);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
22 var entriesEl = document.querySelector('#entries')
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
23 entriesEl.appendChild(entry);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
24 },
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
25 filter: function(aFilter) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
26 var entries = document.querySelectorAll('.entry pre');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
27 for (var i = 0, length = entries.length; i < length; i++) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
28 var entry = entries[i];
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
29 if (!entry.textContent.match(aFilter))
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
30 entry.parentNode.hidden = true;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
31 else
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
32 entry.parentNode.hidden = false;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
33
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
34 //FIXME use the Mutation Observer? get back to the previous scroll state?
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
35 this.scrollToBottom();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
36 }
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
37 },
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
38 send: function(aData) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
39 Lightstring.console.source.postMessage({
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
40 'send': aData}, document.location.protocol + '//' + document.location.host);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
41 },
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
42 scrollToBottom: function() {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
43 this.entriesEl.scrollTop = (this.entriesEl.scrollHeight - this.entriesEl.clientHeight);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
44 }
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
45 };
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
46
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
47 (function() {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
48 document.addEventListener('DOMContentLoaded', function() {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
49
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
50 var entriesEl = document.getElementById('entries');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
51 entriesEl.addEventListener('scroll', function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
52 if (entriesEl.scrollTop === (entriesEl.scrollHeight - entriesEl.clientHeight))
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
53 Lightstring.console.invertedScroll = true;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
54 else
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
55 Lightstring.console.invertedScroll = false;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
56 })
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
57
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
58 new MutationObserver(function(mutations) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
59 if(Lightstring.console.invertedScroll === true)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
60 Lightstring.console.scrollToBottom();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
61 }).observe(entriesEl, {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
62 childList: true,
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
63 // attributes: false,
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
64 // characterData: false
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
65 });
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
66
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
67 Lightstring.console.entriesEl = entriesEl;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
68 if (Lightstring.console.invertedScroll)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
69 Lightstring.console.scrollToBottom();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
70
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
71 window.addEventListener("message", function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
72 if(!Lightstring.console.source)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
73 Lightstring.console.source = e.source;
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
74
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
75 Lightstring.console.log(e.data);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
76 }, false);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
77
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
78 document.getElementById('input').addEventListener('submit', function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
79 e.preventDefault();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
80 Lightstring.console.send(this.elements['field'].value)
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
81 });
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
82 document.getElementById('clear').addEventListener('click', function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
83 Lightstring.console.entriesEl.innerHTML = '';
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
84 });
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
85 //FIXME allow xpath, xquery, E4X, whatever XML query syntax
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
86 document.getElementById('filter').addEventListener('input', function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
87 Lightstring.console.filter(this.value);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
88 });
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
89 document.getElementById('input').addEventListener('keypress', function(e) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
90 if (e.keyCode === 13) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
91 if (e.shiftKey) {
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
92 e.preventDefault();
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
93 var event = document.createEvent('Event');
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
94 event.initEvent('submit', true, true);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
95 this.dispatchEvent(event);
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
96 }
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
97 }
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
98 });
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
99 });
c06ec02217ee many changes
Sonny Piers <sonny@fastmail.net>
parents:
diff changeset
100 })();