Monday, December 8, 2008

"Got to here" or a Javascript Debugger written with prototype.js

Maybe I'll use this instead of interspersed lines of alert('got to here').

// ---------- ---------- ---------- dbgCursorSet
function dbgCursorSet(evt, mouseAction) {
if (mouseAction == 'over') {
evt.element().setStyle({cursor:"pointer"});
}
else if (mouseAction == 'out') {
evt.element().setStyle({cursor:"default"});
}
}

// ---------- ---------- ---------- Debbuger class
var Debugger = Class.create ({

// ---------- ---------- ---------- initialize
initialize: function() {
this.on = 0;
this.msg = '';

this.dbgHeader = '
' +
'Debug  ' +
'[resume]  ' +
'[pause]  ' +
'[clear]
';

this.dbgMsgLines = $A();

this.lineTemplate = '
#{msg}
';
this.tpl = new Template(this.lineTemplate);

this.divMain = document.createElement('div');
Element.extend(this.divMain);
this.divMain.id = "dbgDivMain";
},

// ---------- ---------- ---------- On
On: function() {
this.on = 1;
document.body.appendChild(this.divMain).show();
if ($('dbgPause')) {
$('dbgPause').setStyle({ color: '#333333' });
}
if ($('dbgResume')) {
$('dbgResume').setStyle({ color: '#cccccc' });
}
},

// ---------- ---------- ---------- Off
Off: function() {
this.on = 0;

if ($('dbgPause')) {
$('dbgPause').setStyle({ color: '#cccccc' });
}
if ($('dbgResume')) {
$('dbgResume').setStyle({ color: '#333333' });
}
},

// ---------- ---------- ---------- Clear
Clear: function() {
this.dbgMsgLines.clear();
this.Write('', true);
},

// ---------- ---------- ---------- Write
Write: function(s, override) {
if (this.on == 1 || override) {
this.msg = s;

this.dbgMsgLines.push(this.tpl.evaluate(this));
var divUpdated = this.dbgHeader;
this.dbgMsgLines.each(function(s) {
divUpdated += s;
})

$('dbgDivMain').update(divUpdated);

$$('.dbgLink').each ( function(ele) {
$(ele).observe ('mouseover', dbgCursorSet.bindAsEventListener(this, 'over'));
$(ele).observe ('mouseout', dbgCursorSet.bindAsEventListener(this, 'out'));
});

if (this.on == 1) {
$('dbgPause').setStyle({ color: '#333333' });
$('dbgResume').setStyle({ color: '#cccccc' });
}
else {
$('dbgPause').setStyle({ color: '#cccccc' });
$('dbgResume').setStyle({ color: '#333333' });
}

$('dbgClear').observe ('click', this.Clear.bindAsEventListener(this));
$('dbgPause').observe ('click', this.Off.bindAsEventListener(this));
$('dbgResume').observe ('click', this.On.bindAsEventListener(this));
}
},

});

No comments: