Skip to content

Instantly share code, notes, and snippets.

@tfwio
Last active December 25, 2015 12:29
Show Gist options
  • Save tfwio/6977069 to your computer and use it in GitHub Desktop.
Save tfwio/6977069 to your computer and use it in GitHub Desktop.
semi-useless, undocumented crap especially because I cant remember where or why
(function($){
// TODO: adequate coding conventions
//¿useless!
$.fox = function()
{
var
a = { fun: function(name,action,mthd) { $(name).unbind(action,mthd); $(name).bind(action,mthd); } };
return false;
};
// used in the following boxInfo function
$.box = function(tagname)
{
this.tagName = tagname;
this.q = function() { return $(this.tagName); };
this.update = function()
{
var qt = this.q();
try { this.p = qt.position(); } catch (err) {}
if (this.p==null) this.p = {top: 0, left: 0};
this.top = this.p.top;
this.left = this.p.left;
try { this.o = qt.offset(); } catch (err) {}
if (this.o==null) this.o = {top: 0, left: 0};
this.offsetTop = this.o.top;
this.offsetLeft = this.o.left;
this.scrollLeft = qt.scrollLeft();
this.scrollTop = qt.scrollTop();
this.width = qt.width();
this.height = qt.height();
this.innerWidth = qt.innerWidth();
this.innerHeight = qt.innerHeight();
this.outerWidth = qt.outerWidth();
this.outerHeight = qt.outerHeight();
};
this.scrollTop = 0;
this.scrollLeft = 0;
this.width = 0;
this.height = 0;
this.top = 0;
this.left = 0;
this.innnerWidth = 0;
this.innerHeight = 0;
this.outerWidth = 0;
this.outerHeight = 0;
this.offsetTop = 0;
this.offsetLeft = 0;
this.update();
};
// our plugin
// you MUST supply a set the links variable.
$.boxInfo = function(me,o)
{
var
// innerbox = new box();
options = {
quiet: true,
// the object(s) that we are watching
links: ['div#newcontainer'],
targetOutputElement: o.targetOutputElement,
// for debugging
boxTemplate: String(
'<table style=\'background: white; font-size:10px;font-family: consolas; border: 1px solid black;\' width=\'100%\ border=\'1\'>'
+'<caption>{tag-name}—{now}</caption>'
+' <tr><th width=\'90\'>inner-width</th><td>{inner-width}</td></tr>'
+' <tr><th>inner-height</th><td>{inner-height}</td></tr>'
+' <tr><th>outer-width</th><td>{ow}</td></tr>'
+' <tr><th>outer-height</th><td>{oh}</td></tr>'
+' <tr><th>width</th><td>{width}</td></tr>'
+' <tr><th>height</th><td>{height}</td></tr>'
+' <tr><th>offset-x</th><td>{o-x}</td></tr>'
+' <tr><th>offset-y</th><td>{o-y}</td></tr>'
+' <tr><th>o-x</th><td>{ox}</td></tr>'
+' <tr><th>o-y</th><td>{oy}</td></tr>'
+' <tr><th>scroll-x</th><td>{s-x}</td></tr>'
+' <tr><th>scroll-y</th><td>{s-y}</td></tr>'
+'</table>'
)
},
parser = {
parse: function(tag,replacement)
{
var obj = $(tag==='window' ? window : tag);
var xo = new $.box(obj);
return replacement
.replace('{tag-name}', String(tag))
.replace('{o-x}', xo.left || 0)
.replace('{o-y}', xo.top || 0)
.replace('{width}', xo.width || 0)
.replace('{height}', xo.height || 0)
.replace('{inner-width}', xo.innerWidth || 0)
.replace('{inner-height}', xo.innerHeight || 0)
.replace('{ow}', xo.outerWidth || 0)
.replace('{oh}', xo.outerHeight || 0)
.replace('{ox}', xo.offsetLeft || 0)
.replace('{oy}', xo.offsetTop || 0)
.replace('{s-x}', xo.scrollLeft || 0)
.replace('{s-y}', xo.scrollTop || 0)
.replace('{now}', (Date.now()));
}
},
events = {
load: function(e) { if (!options.quiet) console.log(e); },
unload: function(e) { if (!options.quiet) console.log(e); },
scroll: function(e)
{
var iid = $(this).attr('id');
var b = new $.box(this);
var output = String(b.tagName+'-scroll: '+b.scrollLeft+','+b.scrollTop);
$(options.targetOutputElement).html(output);
if (!options.quiet) console.log(output);
if (!options.quiet) console.log(e);
},
resize: function()
{
var iid = $(this).attr('id');
var b = new $.box(this);
var output = String(b.tagName+'-resize: '+b.width+','+b.height);
$(options.targetOutputElement).html(output);
if (!options.quiet) console.log($(this).selector);
if (!options.quiet) console.log(output);
},
load: function()
{
if (!options.quiet) console.log('load');
if (!options.quiet) console.log($(this).selector);
},
beforeunload: function(e)
{
if (!options.quiet) console.log('beforeunload');
if (!options.quiet) console.log(e);
},
unload: function(e)
{
if (!options.quiet) console.log('unload');
if (!options.quiet) console.log(e);
},
focus: function(e)
{
if (!options.quiet) console.log('focus');
if (!options.quiet) console.log(e);
},
blur: function(e)
{
if (!options.quiet) console.log('blur');
if (!options.quiet) console.log(e);
},
keydown: function(e)
{
if (!options.quiet) console.log('keydown');
if (!options.quiet) console.log(e);
},
keypress: function(e)
{
if (!options.quiet) console.log(e);
},
mousemove: function(e)
{
if (!options.quiet) console.log("move: "+[String(e.clientX),String(e.clientY)].join(','));
if (!options.quiet) console.log($(this).context);
},
mouseover: function(e)
{
if (!options.quiet) console.log('mouseover');
if (!options.quiet) console.log(e);
},
mouseenter: function(e)
{
if (!options.quiet) console.log('mouseenter');
if (!options.quiet) console.log(e);
},
mouseleave: function(e)
{
if (!options.quiet) console.log('mouseleave');
if (!options.quiet) console.log(e);
}
},
a = {
fun: function(name,action,mthd) { return $(name).unbind(action,mthd).bind(action,mthd); },
print: function()
{
var elms = [];
$(attr2).each(function(i,mr) { try { elms.push(parser.parse(mr,options.boxTemplate)); } catch (ex) {} });
$(options.targetOutputElement).html(elms.join(''));
},
initialize: function()
{
//console.log(me);
console.log('watching {x} objects'.replace('{x}',options.links.length));
$(options.links).each(function(i,obj){
a.fun(obj,'scroll',events.scroll);
a.fun(obj,'resize',events.resize);
a.fun(obj,'mousemove',events.mousemove);
a.fun(obj,'keypress',events.keypress);
a.fun(obj,'load',events.load);
a.fun(obj,'focus',events.focus);
a.fun(obj,'blur',events.blur);
a.fun(obj,'unload',events.unload);
var box = new $.box(obj);
console.log(' '+box.tagName);
});
}
};
options = $.extend(true,options,o);
a.initialize();
};
$.fn.boxInfo = function(o) { return this.each(function(){ new $.boxInfo(this,o); }); };
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment