-
Notifications
You must be signed in to change notification settings - Fork 61
/
jquery.dom-outline-1.0.js
176 lines (156 loc) · 6.03 KB
/
jquery.dom-outline-1.0.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Firebug/Web Inspector Outline Implementation using jQuery
* Tested to work in Chrome, FF, Safari. Buggy in IE ;(
* Andrew Childs <[email protected]>
*
* Example Setup:
* var myClickHandler = function (element) { console.log('Clicked element:', element); }
* var myDomOutline = DomOutline({ onClick: myClickHandler, filter: '.debug' });
*
* Public API:
* myDomOutline.start();
* myDomOutline.stop();
*/
var DomOutline = function (options) {
options = options || {};
var pub = {};
var self = {
opts: {
namespace: options.namespace || 'DomOutline',
borderWidth: options.borderWidth || 2,
onClick: options.onClick || false,
filter: options.filter || false
},
keyCodes: {
BACKSPACE: 8,
ESC: 27,
DELETE: 46
},
active: false,
initialized: false,
elements: {}
};
function writeStylesheet(css) {
var element = document.createElement('style');
element.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(element);
if (element.styleSheet) {
element.styleSheet.cssText = css; // IE
} else {
element.innerHTML = css; // Non-IE
}
}
function initStylesheet() {
if (self.initialized !== true) {
var css = '' +
'.' + self.opts.namespace + ' {' +
' background: #09c;' +
' position: absolute;' +
' z-index: 1000000;' +
'}' +
'.' + self.opts.namespace + '_label {' +
' background: #09c;' +
' border-radius: 2px;' +
' color: #fff;' +
' font: bold 12px/12px Helvetica, sans-serif;' +
' padding: 4px 6px;' +
' position: absolute;' +
' text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);' +
' z-index: 1000001;' +
'}';
writeStylesheet(css);
self.initialized = true;
}
}
function createOutlineElements() {
self.elements.label = jQuery('<div></div>').addClass(self.opts.namespace + '_label').appendTo('body');
self.elements.top = jQuery('<div></div>').addClass(self.opts.namespace).appendTo('body');
self.elements.bottom = jQuery('<div></div>').addClass(self.opts.namespace).appendTo('body');
self.elements.left = jQuery('<div></div>').addClass(self.opts.namespace).appendTo('body');
self.elements.right = jQuery('<div></div>').addClass(self.opts.namespace).appendTo('body');
}
function removeOutlineElements() {
jQuery.each(self.elements, function(name, element) {
element.remove();
});
}
function compileLabelText(element, width, height) {
var label = element.tagName.toLowerCase();
if (element.id) {
label += '#' + element.id;
}
if (element.className) {
label += ('.' + jQuery.trim(element.className).replace(/ /g, '.')).replace(/\.\.+/g, '.');
}
return label + ' (' + Math.round(width) + 'x' + Math.round(height) + ')';
}
function getScrollTop() {
if (!self.elements.window) {
self.elements.window = jQuery(window);
}
return self.elements.window.scrollTop();
}
function updateOutlinePosition(e) {
if (e.target.className.indexOf(self.opts.namespace) !== -1) {
return;
}
if (self.opts.filter) {
if (!jQuery(e.target).is(self.opts.filter)) {
return;
}
}
pub.element = e.target;
var b = self.opts.borderWidth;
var scroll_top = getScrollTop();
var pos = pub.element.getBoundingClientRect();
var top = pos.top + scroll_top;
var label_text = compileLabelText(pub.element, pos.width, pos.height);
var label_top = Math.max(0, top - 20 - b, scroll_top);
var label_left = Math.max(0, pos.left - b);
self.elements.label.css({ top: label_top, left: label_left }).text(label_text);
self.elements.top.css({ top: Math.max(0, top - b), left: pos.left - b, width: pos.width + b, height: b });
self.elements.bottom.css({ top: top + pos.height, left: pos.left - b, width: pos.width + b, height: b });
self.elements.left.css({ top: top - b, left: Math.max(0, pos.left - b), width: b, height: pos.height + b });
self.elements.right.css({ top: top - b, left: pos.left + pos.width, width: b, height: pos.height + (b * 2) });
}
function stopOnEscape(e) {
if (e.keyCode === self.keyCodes.ESC || e.keyCode === self.keyCodes.BACKSPACE || e.keyCode === self.keyCodes.DELETE) {
pub.stop();
}
return false;
}
function clickHandler(e) {
pub.stop();
self.opts.onClick(pub.element);
return false;
}
pub.start = function () {
initStylesheet();
if (self.active !== true) {
self.active = true;
createOutlineElements();
jQuery('body').on('mousemove.' + self.opts.namespace, updateOutlinePosition);
jQuery('body').on('keyup.' + self.opts.namespace, stopOnEscape);
if (self.opts.onClick) {
setTimeout(function () {
jQuery('body').on('click.' + self.opts.namespace, function(e){
if (self.opts.filter) {
if (!jQuery(e.target).is(self.opts.filter)) {
return false;
}
}
clickHandler.call(this, e);
});
}, 50);
}
}
};
pub.stop = function () {
self.active = false;
removeOutlineElements();
jQuery('body').off('mousemove.' + self.opts.namespace)
.off('keyup.' + self.opts.namespace)
.off('click.' + self.opts.namespace);
};
return pub;
};