-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.dom-outline-1.0.js
141 lines (122 loc) · 5.5 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
(function($) {
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: {},
outlineActive: false
};
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;' +
'}' +
'.' + self.opts.namespace + '_box {' +
' background: rgba(0, 153, 204, 0.2);' +
' position: absolute;' +
' z-index: 1000000;' +
'}';
writeStylesheet(css);
self.initialized = true;
}
}
function createOutlineElements() {
self.elements.label = jQuery('<div>').addClass(self.opts.namespace + '_label').appendTo('body');
self.elements.top = jQuery('<div>').addClass(self.opts.namespace).appendTo('body');
self.elements.bottom = jQuery('<div>').addClass(self.opts.namespace).appendTo('body');
self.elements.left = jQuery('<div>').addClass(self.opts.namespace).appendTo('body');
self.elements.right = jQuery('<div>').addClass(self.opts.namespace).appendTo('body');
self.elements.box = jQuery('<div>').addClass(self.opts.namespace + '_box').appendTo('body');
}
function removeOutlineElements() {
if (self.outlineActive) {
jQuery.each(self.elements, function(name, element) {
element.remove();
});
self.elements = {}; // Clear elements reference to ensure they are recreated
self.outlineActive = false;
}
}
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 getScrollOffset() {
var scrollOffset = {
left: document.documentElement.scrollLeft || document.body.scrollLeft,
top: document.documentElement.scrollTop || document.body.scrollTop
};
return scrollOffset;
}
function updateOutlinePosition(element) {
var b = self.opts.borderWidth;
var scroll = getScrollOffset();
var pos = element.getBoundingClientRect();
var top = pos.top + scroll.top;
var left = pos.left + scroll.left;
var label_text = compileLabelText(element, pos.width, pos.height);
var label_top = Math.max(0, top - 20 - b, scroll.top);
var label_left = Math.max(0, left + scroll.left);
self.elements.label.css({ top: label_top, left: label_left }).text(label_text);
self.elements.top.css({ top: Math.max(0, top - b), left: left - b, width: pos.width + b + b, height: b });
self.elements.bottom.css({ top: top + pos.height, left: left - b, width: pos.width + b + b, height: b });
self.elements.left.css({ top: top - b, left: Math.max(0, left - b), width: b, height: pos.height + b + b });
self.elements.right.css({ top: top - b, left: left + pos.width, width: b, height: pos.height + b + b });
self.elements.box.css({ top: top, left: left, width: pos.width, height: pos.height });
self.outlineActive = true;
}
pub.start = function (element) {
removeOutlineElements(); // Ensure previous elements are cleared before starting
initStylesheet();
createOutlineElements();
updateOutlinePosition(element);
};
pub.stop = function () {
removeOutlineElements();
};
return pub;
};
window.DomOutline = DomOutline;
})(jQuery);