1 | function DebugWidget()
|
---|
2 | {
|
---|
3 | //Private member variables
|
---|
4 | var _debugOn = false;
|
---|
5 | var _pauseSelector = false;
|
---|
6 | var _elements = new Array();
|
---|
7 | var _itemSelected = false; //Used to prevent multiple elements from being highlighted
|
---|
8 | var _mainDiv;
|
---|
9 | var _textDiv;
|
---|
10 | var _unpauseButton;
|
---|
11 |
|
---|
12 | var createDebugDiv = function()
|
---|
13 | {
|
---|
14 | _mainDiv = $("<div>", {"id":"debugDiv", "class":"ui-corner-all"});
|
---|
15 | _mainDiv.css(
|
---|
16 | {
|
---|
17 | "position":"fixed",
|
---|
18 | "font-size":"0.7em",
|
---|
19 | "bottom":"0px",
|
---|
20 | "height":"25%",
|
---|
21 | "width":"100%",
|
---|
22 | "background":"white",
|
---|
23 | "border":"1px black solid",
|
---|
24 | "padding":"5px"
|
---|
25 | });
|
---|
26 |
|
---|
27 | var toolBarDiv = $("<div>");
|
---|
28 | toolBarDiv.css({"height":"15%"});
|
---|
29 | _textDiv = $("<div>");
|
---|
30 | _textDiv.css({"overflow":"auto", "width":"100%", "height":"85%", "margin-top":"5px"});
|
---|
31 | var pickElementButton = $("<input type=\"button\" value=\"Enable debugging\">");
|
---|
32 | pickElementButton.click(function()
|
---|
33 | {
|
---|
34 | if(!_debugOn)
|
---|
35 | {
|
---|
36 | pickElementButton.attr("value", "Disable debugging");
|
---|
37 | $("a").click(function(e)
|
---|
38 | {
|
---|
39 | e.preventDefault();
|
---|
40 | });
|
---|
41 | _debugOn = true;
|
---|
42 | }
|
---|
43 | else
|
---|
44 | {
|
---|
45 | pickElementButton.attr("value", "Enable debugging");
|
---|
46 | $("a").off("click");
|
---|
47 | clearAll();
|
---|
48 | _debugOn = false;
|
---|
49 | }
|
---|
50 | });
|
---|
51 | _unpauseButton = $("<input type=\"button\" value=\"Select new element\" disabled=\"disabled\">");
|
---|
52 | _unpauseButton.click(function()
|
---|
53 | {
|
---|
54 | if(_pauseSelector)
|
---|
55 | {
|
---|
56 | _pauseSelector = false;
|
---|
57 | $(this).attr("disabled", "disabled");
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | toolBarDiv.append(pickElementButton);
|
---|
62 | toolBarDiv.append(_unpauseButton);
|
---|
63 | _mainDiv.append(toolBarDiv);
|
---|
64 | _mainDiv.append(_textDiv);
|
---|
65 | }
|
---|
66 |
|
---|
67 | var clearAll = function()
|
---|
68 | {
|
---|
69 | _itemSelected = false;
|
---|
70 | $(_elements).each(function()
|
---|
71 | {
|
---|
72 | $(this).remove();
|
---|
73 | });
|
---|
74 | }
|
---|
75 |
|
---|
76 | var highlightElement = function(e)
|
---|
77 | {
|
---|
78 | var topBorderDiv = $("<div>");
|
---|
79 | var bottomBorderDiv = $("<div>");
|
---|
80 | var leftBorderDiv = $("<div>");
|
---|
81 | var rightBorderDiv = $("<div>");
|
---|
82 |
|
---|
83 | topBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":e.offset().left + "px", "height":"0px", "width":e.width() + "px", "border":"1px solid red"});
|
---|
84 | bottomBorderDiv.css({"position":"absolute", "top":(e.offset().top + e.height()) + "px", "left":e.offset().left + "px", "height":"0px", "width":e.width() + "px", "border":"1px solid red"});
|
---|
85 | leftBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":e.offset().left + "px", "height":e.height() + "px", "width":"0px", "border":"1px solid red"});
|
---|
86 | rightBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":(e.offset().left + e.width()) + "px", "height":e.height() + "px", "width":"0px", "border":"1px solid red"});
|
---|
87 |
|
---|
88 | $("body").append(topBorderDiv, bottomBorderDiv, leftBorderDiv, rightBorderDiv);
|
---|
89 |
|
---|
90 | _elements.push(topBorderDiv);
|
---|
91 | _elements.push(bottomBorderDiv);
|
---|
92 | _elements.push(leftBorderDiv);
|
---|
93 | _elements.push(rightBorderDiv);
|
---|
94 | }
|
---|
95 |
|
---|
96 | var addMouseEventsToDebugElements = function(debugElems)
|
---|
97 | {
|
---|
98 | debugElems.click(function()
|
---|
99 | {
|
---|
100 | if(_debugOn)
|
---|
101 | {
|
---|
102 | _pauseSelector = true;
|
---|
103 | _unpauseButton.removeAttr("disabled");
|
---|
104 | }
|
---|
105 | });
|
---|
106 |
|
---|
107 | debugElems.mouseover(function()
|
---|
108 | {
|
---|
109 | if(_debugOn && !_pauseSelector)
|
---|
110 | {
|
---|
111 | var surroundingDiv = $("<div>");
|
---|
112 | surroundingDiv.css("cursor","pointer");
|
---|
113 | var fromDIV = $("<div>");
|
---|
114 | var elementDIV = $("<div>");
|
---|
115 | elementDIV.css("margin-bottom", "0.6em");
|
---|
116 |
|
---|
117 | surroundingDiv.append(fromDIV);
|
---|
118 | surroundingDiv.append(elementDIV);
|
---|
119 |
|
---|
120 | _elements.push(surroundingDiv);
|
---|
121 |
|
---|
122 | surroundingDiv.click(function()
|
---|
123 | {
|
---|
124 | var editArea = $("<textarea>");
|
---|
125 | $.ajax();
|
---|
126 | });
|
---|
127 |
|
---|
128 | surroundingDiv.mouseover(function()
|
---|
129 | {
|
---|
130 | $(this).data("background", $(this).css("background"));
|
---|
131 | $(this).css("background", "yellow");
|
---|
132 | });
|
---|
133 |
|
---|
134 | surroundingDiv.mouseout(function()
|
---|
135 | {
|
---|
136 | $(this).css("background", $(this).data("background"));
|
---|
137 | });
|
---|
138 |
|
---|
139 | var nodeName = $(this).attr("nodename");
|
---|
140 | var filename = $(this).attr("filename");
|
---|
141 | var attrstr = "";
|
---|
142 | var illegalNames = ["nodename", "filename", "style", "debug", "id", "class"];
|
---|
143 |
|
---|
144 | $(this.attributes).each(function()
|
---|
145 | {
|
---|
146 | for(var i = 0; i < illegalNames.length; i++)
|
---|
147 | {
|
---|
148 | if(this.name == illegalNames[i]){return;}
|
---|
149 | }
|
---|
150 | attrstr += this.name + "=\"" + this.value + "\" ";
|
---|
151 | });
|
---|
152 |
|
---|
153 | fromDIV.text("From " + filename + ":");
|
---|
154 | elementDIV.text("<" + nodeName + " " + attrstr + ">");
|
---|
155 |
|
---|
156 | _textDiv.prepend(surroundingDiv);
|
---|
157 |
|
---|
158 | if(!_itemSelected)
|
---|
159 | {
|
---|
160 | _itemSelected = true;
|
---|
161 | highlightElement($(this));
|
---|
162 | }
|
---|
163 | }
|
---|
164 | });
|
---|
165 |
|
---|
166 | debugElems.mouseout(function()
|
---|
167 | {
|
---|
168 | if(_debugOn && !_pauseSelector)
|
---|
169 | {
|
---|
170 | clearAll();
|
---|
171 | }
|
---|
172 | });
|
---|
173 | }
|
---|
174 |
|
---|
175 | this.init = function()
|
---|
176 | {
|
---|
177 | //We only want this on if we have debug elements in the page
|
---|
178 | var debugElems = $('debug, [debug="true"]');
|
---|
179 | if(!debugElems.length)
|
---|
180 | {
|
---|
181 | console.log("No debug tags present, debugging disabled.");
|
---|
182 | return;
|
---|
183 | }
|
---|
184 |
|
---|
185 | createDebugDiv();
|
---|
186 | $("body").append(_mainDiv);
|
---|
187 |
|
---|
188 | addMouseEventsToDebugElements(debugElems);
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|
192 |
|
---|
193 | $(window).load(function()
|
---|
194 | {
|
---|
195 | var debugWidget = new DebugWidget();
|
---|
196 | debugWidget.init();
|
---|
197 | });
|
---|