[26461] | 1 | function DebugWidget()
|
---|
| 2 | {
|
---|
[26492] | 3 | //************************
|
---|
[26461] | 4 | //Private member variables
|
---|
[26492] | 5 | //************************
|
---|
| 6 |
|
---|
| 7 | //Debugger state-keeping variables
|
---|
[26461] | 8 | var _debugOn = false;
|
---|
| 9 | var _pauseSelector = false;
|
---|
| 10 | var _elements = new Array();
|
---|
| 11 | var _itemSelected = false; //Used to prevent multiple elements from being highlighted
|
---|
[26492] | 12 |
|
---|
| 13 | //Page elements
|
---|
[26461] | 14 | var _mainDiv;
|
---|
| 15 | var _textDiv;
|
---|
[26492] | 16 | var _editingDiv;
|
---|
[26461] | 17 | var _unpauseButton;
|
---|
[26492] | 18 | var _closeEditorButtonButton;
|
---|
| 19 | var _xmlStatusBar;
|
---|
| 20 | var _saveButton;
|
---|
[26461] | 21 |
|
---|
[26492] | 22 | //Editor state-keeping variables
|
---|
| 23 | var _currentFilepath;
|
---|
| 24 | var _currentNodename;
|
---|
| 25 | var _currentName;
|
---|
| 26 | var _currentMatch;
|
---|
| 27 | var _currentNamespace;
|
---|
| 28 |
|
---|
[26461] | 29 | var createDebugDiv = function()
|
---|
| 30 | {
|
---|
[26492] | 31 | _mainDiv = $("<div>", {"id":"debugDiv"});
|
---|
[26461] | 32 | _mainDiv.css(
|
---|
| 33 | {
|
---|
| 34 | "position":"fixed",
|
---|
| 35 | "font-size":"0.7em",
|
---|
| 36 | "bottom":"0px",
|
---|
[26492] | 37 | "height":"300px",
|
---|
| 38 | "width":"100%",
|
---|
[26461] | 39 | "background":"white",
|
---|
| 40 | "border":"1px black solid",
|
---|
[26492] | 41 | "padding":"5px",
|
---|
| 42 | "z-index":100
|
---|
[26461] | 43 | });
|
---|
| 44 |
|
---|
[26492] | 45 | _editingDiv = $("<div>");
|
---|
[26461] | 46 | var toolBarDiv = $("<div>");
|
---|
[26492] | 47 | toolBarDiv.css({"height":"40px"});
|
---|
| 48 | var buttonDiv = $("<div>");
|
---|
| 49 | buttonDiv.css("float", "left");
|
---|
| 50 | toolBarDiv.append(buttonDiv);
|
---|
[26461] | 51 | _textDiv = $("<div>");
|
---|
[26492] | 52 | _textDiv.css({"overflow":"auto", "width":"100%", "height":"260px"});
|
---|
| 53 |
|
---|
[26461] | 54 | var pickElementButton = $("<input type=\"button\" value=\"Enable debugging\">");
|
---|
| 55 | pickElementButton.click(function()
|
---|
| 56 | {
|
---|
| 57 | if(!_debugOn)
|
---|
| 58 | {
|
---|
| 59 | pickElementButton.attr("value", "Disable debugging");
|
---|
| 60 | $("a").click(function(e)
|
---|
| 61 | {
|
---|
| 62 | e.preventDefault();
|
---|
| 63 | });
|
---|
| 64 | _debugOn = true;
|
---|
| 65 | }
|
---|
| 66 | else
|
---|
| 67 | {
|
---|
| 68 | pickElementButton.attr("value", "Enable debugging");
|
---|
| 69 | $("a").off("click");
|
---|
| 70 | clearAll();
|
---|
[26462] | 71 | _unpauseButton.attr("disabled", "disabled");
|
---|
| 72 | _pauseSelector = false;
|
---|
[26461] | 73 | _debugOn = false;
|
---|
| 74 | }
|
---|
| 75 | });
|
---|
[26492] | 76 |
|
---|
[26461] | 77 | _unpauseButton = $("<input type=\"button\" value=\"Select new element\" disabled=\"disabled\">");
|
---|
| 78 | _unpauseButton.click(function()
|
---|
| 79 | {
|
---|
| 80 | if(_pauseSelector)
|
---|
| 81 | {
|
---|
| 82 | _pauseSelector = false;
|
---|
| 83 | $(this).attr("disabled", "disabled");
|
---|
| 84 | }
|
---|
| 85 | });
|
---|
| 86 |
|
---|
[26492] | 87 | _closeEditorButton = $("<input type=\"button\" value=\"Close editor\" disabled=\"disabled\">");
|
---|
| 88 | _closeEditorButton.click(function()
|
---|
| 89 | {
|
---|
| 90 | if($(this).val() == "Close editor")
|
---|
| 91 | {
|
---|
| 92 | $(this).val("Open editor");
|
---|
| 93 | _editingDiv.hide();
|
---|
[26554] | 94 | _mainDiv.css("height", (_mainDiv.height() - 200) + "px");
|
---|
[26492] | 95 | }
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
| 98 | $(this).val("Close editor");
|
---|
| 99 | _editingDiv.show();
|
---|
[26554] | 100 | _mainDiv.css("height", (_mainDiv.height() + 200) + "px");
|
---|
[26492] | 101 | }
|
---|
| 102 | });
|
---|
| 103 |
|
---|
| 104 | _xmlStatusBar = $("<span>");
|
---|
| 105 | _xmlStatusBar.css("padding", "5px");
|
---|
| 106 |
|
---|
| 107 | //Check the XML for errors every 2 seconds
|
---|
| 108 | setInterval(function()
|
---|
| 109 | {
|
---|
| 110 | var editor = _editingDiv.find("textarea");
|
---|
| 111 | if(editor.length)
|
---|
| 112 | {
|
---|
| 113 | var xmlString = editor.val();
|
---|
| 114 | try
|
---|
| 115 | {
|
---|
| 116 | var xml = $.parseXML('<testContainer xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" xmlns:util="xalan://org.greenstone.gsdl3.util.XSLTUtil" xmlns:gslib="http://www.greenstone.org/skinning" xmlns:gsf="http://www.greenstone.org/greenstone3/schema/ConfigFormat">' + xmlString + "</testContainer>");
|
---|
| 117 | }
|
---|
| 118 | catch(error)
|
---|
| 119 | {
|
---|
| 120 | console.log(error);
|
---|
| 121 | _xmlStatusBar.text("XML ERROR! (Mouse over for details)");
|
---|
| 122 | _xmlStatusBar.css({"color":"white", "background":"red"});
|
---|
| 123 | _xmlStatusBar.attr("title", error);
|
---|
| 124 | _saveButton.attr("disabled", "disabled");
|
---|
| 125 | return;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | _xmlStatusBar.text("XML OK!");
|
---|
| 129 | _xmlStatusBar.css({"color":"white", "background": "green"});
|
---|
| 130 | _xmlStatusBar.removeAttr("title");
|
---|
[26554] | 131 | if(_saveButton.val() == "Save changes")
|
---|
| 132 | {
|
---|
| 133 | _saveButton.removeAttr("disabled");
|
---|
| 134 | }
|
---|
[26492] | 135 | }
|
---|
| 136 |
|
---|
| 137 | }, 2000);
|
---|
| 138 |
|
---|
| 139 | _saveButton = $("<input type=\"button\" value=\"Save changes\" disabled=\"disabled\">");
|
---|
| 140 | _saveButton.click(function()
|
---|
| 141 | {
|
---|
| 142 | var editor = _editingDiv.find("textarea");
|
---|
| 143 | if(editor.length)
|
---|
| 144 | {
|
---|
| 145 | var xmlString = editor.val();
|
---|
| 146 | try
|
---|
| 147 | {
|
---|
| 148 | var xml = $.parseXML('<testContainer xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" xmlns:util="xalan://org.greenstone.gsdl3.util.XSLTUtil" xmlns:gslib="http://www.greenstone.org/skinning" xmlns:gsf="http://www.greenstone.org/greenstone3/schema/ConfigFormat">' + xmlString + "</testContainer>");
|
---|
| 149 | }
|
---|
| 150 | catch(error)
|
---|
| 151 | {
|
---|
| 152 | alert("Could not save as there is a problem with the XML.");
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | var url = gs.xsltParams.library_name;
|
---|
| 157 | var parameters = {"a":"g", "rt":"r", "s":"SaveXMLTemplateToFile", "s1.filePath":_currentFilepath, "s1.namespace":_currentNamespace, "s1.nodename":_currentNodename, "s1.xml":xmlString};
|
---|
| 158 |
|
---|
| 159 | if(_currentName && _currentName.length > 0){parameters["s1.name"] = _currentName;}
|
---|
[26554] | 160 | if(_currentMatch && _currentMatch.length > 0){parameters["s1.match"] = _currentMatch;}
|
---|
[26492] | 161 |
|
---|
[26554] | 162 | _saveButton.val("Saving...");
|
---|
| 163 | _saveButton.attr("disabled", "disabled");
|
---|
| 164 |
|
---|
[26492] | 165 | $.post(url, parameters)
|
---|
| 166 | .success(function()
|
---|
| 167 | {
|
---|
[26554] | 168 | $.ajax(gs.xsltParams.library_name + "?a=s&sa=c")
|
---|
| 169 | .success(function()
|
---|
| 170 | {
|
---|
| 171 | alert("The template has been saved successfully.");
|
---|
| 172 | })
|
---|
| 173 | .error(function()
|
---|
| 174 | {
|
---|
| 175 | alert("Error reloading collection.");
|
---|
| 176 | })
|
---|
| 177 | .complete(function()
|
---|
| 178 | {
|
---|
| 179 | _saveButton.val("Save changes");
|
---|
| 180 | _saveButton.removeAttr("disabled");
|
---|
| 181 | });
|
---|
[26492] | 182 | })
|
---|
| 183 | .error(function()
|
---|
| 184 | {
|
---|
| 185 | alert("There was an error sending the request to the server, please try again.");
|
---|
| 186 | });
|
---|
| 187 | }
|
---|
| 188 | });
|
---|
| 189 |
|
---|
| 190 | var minimiseButton = $("<img>", {"src":gs.imageURLs.collapse});
|
---|
| 191 | minimiseButton.css({"cursor":"pointer", "float":"right", "margin-right":"20px"});
|
---|
| 192 | minimiseButton.click(function()
|
---|
| 193 | {
|
---|
| 194 | if($(this).attr("src") == gs.imageURLs.collapse)
|
---|
| 195 | {
|
---|
| 196 | _textDiv.hide();
|
---|
| 197 | $(this).attr("src", gs.imageURLs.expand);
|
---|
| 198 | _mainDiv.css("height", (_mainDiv.height() - 260) + "px");
|
---|
| 199 | }
|
---|
| 200 | else
|
---|
| 201 | {
|
---|
| 202 | _textDiv.show();
|
---|
| 203 | $(this).attr("src", gs.imageURLs.collapse);
|
---|
| 204 | _mainDiv.css("height", (_mainDiv.height() + 260) + "px");
|
---|
| 205 | }
|
---|
| 206 | });
|
---|
| 207 |
|
---|
| 208 | var clear = $("<span>");
|
---|
| 209 | clear.css("clear", "both");
|
---|
| 210 |
|
---|
| 211 | toolBarDiv.append(minimiseButton);
|
---|
| 212 | toolBarDiv.append(clear);
|
---|
| 213 |
|
---|
| 214 | buttonDiv.append(pickElementButton);
|
---|
| 215 | buttonDiv.append(_unpauseButton);
|
---|
| 216 | buttonDiv.append(_closeEditorButton);
|
---|
| 217 | buttonDiv.append(_xmlStatusBar);
|
---|
| 218 | buttonDiv.append(_saveButton);
|
---|
| 219 | _mainDiv.append(_editingDiv);
|
---|
[26461] | 220 | _mainDiv.append(toolBarDiv);
|
---|
| 221 | _mainDiv.append(_textDiv);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | var clearAll = function()
|
---|
| 225 | {
|
---|
| 226 | _itemSelected = false;
|
---|
| 227 | $(_elements).each(function()
|
---|
| 228 | {
|
---|
| 229 | $(this).remove();
|
---|
| 230 | });
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | var highlightElement = function(e)
|
---|
| 234 | {
|
---|
| 235 | var topBorderDiv = $("<div>");
|
---|
| 236 | var bottomBorderDiv = $("<div>");
|
---|
| 237 | var leftBorderDiv = $("<div>");
|
---|
| 238 | var rightBorderDiv = $("<div>");
|
---|
[26492] | 239 |
|
---|
[26461] | 240 | topBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":e.offset().left + "px", "height":"0px", "width":e.width() + "px", "border":"1px solid red"});
|
---|
| 241 | 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"});
|
---|
| 242 | leftBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":e.offset().left + "px", "height":e.height() + "px", "width":"0px", "border":"1px solid red"});
|
---|
| 243 | 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"});
|
---|
[26492] | 244 |
|
---|
[26461] | 245 | $("body").append(topBorderDiv, bottomBorderDiv, leftBorderDiv, rightBorderDiv);
|
---|
[26492] | 246 |
|
---|
[26461] | 247 | _elements.push(topBorderDiv);
|
---|
| 248 | _elements.push(bottomBorderDiv);
|
---|
| 249 | _elements.push(leftBorderDiv);
|
---|
| 250 | _elements.push(rightBorderDiv);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[26492] | 253 | var addMouseEventsToInfoContainer = function(infoContainer, filepath, nodename, namespace, name, match)
|
---|
| 254 | {
|
---|
| 255 | infoContainer.click(function()
|
---|
| 256 | {
|
---|
| 257 | _currentFilepath = filepath;
|
---|
| 258 | _currentNodename = nodename;
|
---|
| 259 | _currentNamespace = namespace;
|
---|
| 260 | _currentName = name;
|
---|
| 261 | _currentMatch = match;
|
---|
| 262 |
|
---|
[26554] | 263 | var responseName = "requestedNameTemplate";
|
---|
| 264 |
|
---|
[26492] | 265 | var url = gs.xsltParams.library_name + "?a=g&rt=r&s=RetrieveXMLTemplateFromFile&s1.filePath=" + _currentFilepath + "&s1.namespace=" + _currentNamespace + "&s1.nodename=" + _currentNodename;
|
---|
[26554] | 266 | if(_currentMatch && _currentMatch.length > 0){url += "&s1.match=" + _currentMatch; responseName = "requestedMatchTemplate";}
|
---|
[26492] | 267 | if(_currentName && _currentName.length > 0){url += "&s1.name=" + _currentName;}
|
---|
| 268 | $.ajax(url)
|
---|
| 269 | .success(function(response)
|
---|
| 270 | {
|
---|
| 271 | var template;
|
---|
[26554] | 272 | if(response.search(responseName) != -1)
|
---|
[26492] | 273 | {
|
---|
[26554] | 274 | var startIndex = response.indexOf("<" + responseName + ">") + responseName.length + 2;
|
---|
| 275 | var endIndex = response.indexOf("</" + responseName + ">");
|
---|
[26492] | 276 | template = response.substring(startIndex, endIndex);
|
---|
| 277 | }
|
---|
| 278 | else
|
---|
| 279 | {
|
---|
| 280 | return;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | var editArea = $("<textarea>");
|
---|
| 284 | editArea.css({"width":"98%", "height":"180px"});
|
---|
| 285 | editArea.val(template);
|
---|
| 286 |
|
---|
| 287 | _editingDiv.empty();
|
---|
| 288 | _editingDiv.append(editArea);
|
---|
| 289 | _editingDiv.css({"height":"190px"});
|
---|
| 290 |
|
---|
| 291 | _mainDiv.css({"height":"500px"});
|
---|
| 292 |
|
---|
| 293 | _closeEditorButton.removeAttr("disabled");
|
---|
| 294 | })
|
---|
| 295 | .error(function()
|
---|
| 296 | {
|
---|
| 297 | console.log("ERROR");
|
---|
| 298 | });
|
---|
| 299 | });
|
---|
| 300 | infoContainer.mouseover(function()
|
---|
| 301 | {
|
---|
| 302 | $(this).data("background", $(this).css("background"));
|
---|
| 303 | $(this).css("background", "yellow");
|
---|
| 304 | });
|
---|
| 305 | infoContainer.mouseout(function()
|
---|
| 306 | {
|
---|
| 307 | $(this).css("background", $(this).data("background"));
|
---|
| 308 | });
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[26461] | 311 | var addMouseEventsToDebugElements = function(debugElems)
|
---|
| 312 | {
|
---|
| 313 | debugElems.click(function()
|
---|
| 314 | {
|
---|
| 315 | if(_debugOn)
|
---|
| 316 | {
|
---|
| 317 | _pauseSelector = true;
|
---|
| 318 | _unpauseButton.removeAttr("disabled");
|
---|
| 319 | }
|
---|
| 320 | });
|
---|
| 321 |
|
---|
| 322 | debugElems.mouseover(function()
|
---|
| 323 | {
|
---|
| 324 | if(_debugOn && !_pauseSelector)
|
---|
| 325 | {
|
---|
[26554] | 326 | var nodes = new Array();
|
---|
| 327 | if($(this).is("table, tr"))
|
---|
| 328 | {
|
---|
| 329 | var size = parseInt($(this).attr("debugSize"));
|
---|
| 330 | for(var i = 0; i < size; i++)
|
---|
| 331 | {
|
---|
| 332 | var tempNode = $("<div>");
|
---|
| 333 | tempNode.tempAttrs = new Array();
|
---|
| 334 | $(this.attributes).each(function()
|
---|
| 335 | {
|
---|
| 336 | if(this.value.charAt(0) == '[')
|
---|
| 337 | {
|
---|
| 338 | var values = eval(this.value);
|
---|
| 339 | if(values[i] == "")
|
---|
| 340 | {
|
---|
| 341 | return;
|
---|
| 342 | }
|
---|
| 343 | tempNode.attr(this.name, values[i]);
|
---|
| 344 | tempNode.tempAttrs.push({name:this.name, value:values[i]});
|
---|
| 345 | }
|
---|
| 346 | });
|
---|
| 347 | nodes.push(tempNode);
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 | else
|
---|
| 351 | {
|
---|
| 352 | nodes.push(this);
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | $(nodes).each(function()
|
---|
| 356 | {
|
---|
| 357 | var filepath = $(this).attr("filename");
|
---|
| 358 | var fullNodename = $(this).attr("nodename");
|
---|
| 359 | var colonIndex = fullNodename.indexOf(":");
|
---|
| 360 | var namespace = fullNodename.substring(0, colonIndex);
|
---|
| 361 | var nodename = fullNodename.substring(colonIndex + 1);
|
---|
| 362 | var name = $(this).attr("name");
|
---|
| 363 | var match = $(this).attr("match");
|
---|
[26461] | 364 |
|
---|
[26554] | 365 | var infoContainer = $("<div>");
|
---|
| 366 | infoContainer.css({"cursor":"pointer", "border":"1px dashed #AAAAAA", "margin":"5px"});
|
---|
| 367 | var fromDIV = $("<div>");
|
---|
| 368 | var elementDIV = $("<div>");
|
---|
[26461] | 369 |
|
---|
[26554] | 370 | elementDIV.css("font-size", "1.1em");
|
---|
| 371 | fromDIV.css("font-size", "0.9em");
|
---|
[26492] | 372 |
|
---|
[26554] | 373 | infoContainer.append(fromDIV);
|
---|
| 374 | infoContainer.append(elementDIV);
|
---|
| 375 |
|
---|
| 376 | _elements.push(infoContainer);
|
---|
| 377 |
|
---|
| 378 | addMouseEventsToInfoContainer(infoContainer, filepath, nodename, namespace, name, match);
|
---|
| 379 |
|
---|
| 380 | var attrstr = "";
|
---|
| 381 | var illegalNames = ["nodename", "filename", "style", "debug", "id", "class"];
|
---|
[26461] | 382 |
|
---|
[26554] | 383 | var attributes = ((this.tempAttrs) ? this.tempAttrs : this.attributes);
|
---|
| 384 |
|
---|
| 385 | $(attributes).each(function()
|
---|
[26461] | 386 | {
|
---|
[26554] | 387 | for(var i = 0; i < illegalNames.length; i++)
|
---|
| 388 | {
|
---|
| 389 | if(this.name == illegalNames[i]){return;}
|
---|
| 390 | }
|
---|
| 391 | attrstr += this.name + "=\"" + this.value + "\" ";
|
---|
| 392 | });
|
---|
| 393 |
|
---|
| 394 | fromDIV.text("From " + filepath + ":");
|
---|
| 395 | elementDIV.text("<" + fullNodename + " " + attrstr + ">");
|
---|
| 396 |
|
---|
| 397 | _textDiv.prepend(infoContainer);
|
---|
[26461] | 398 | });
|
---|
| 399 |
|
---|
| 400 | if(!_itemSelected)
|
---|
| 401 | {
|
---|
| 402 | _itemSelected = true;
|
---|
| 403 | highlightElement($(this));
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 | });
|
---|
| 407 |
|
---|
| 408 | debugElems.mouseout(function()
|
---|
| 409 | {
|
---|
| 410 | if(_debugOn && !_pauseSelector)
|
---|
| 411 | {
|
---|
| 412 | clearAll();
|
---|
| 413 | }
|
---|
| 414 | });
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | this.init = function()
|
---|
| 418 | {
|
---|
| 419 | //We only want this on if we have debug elements in the page
|
---|
| 420 | var debugElems = $('debug, [debug="true"]');
|
---|
| 421 | if(!debugElems.length)
|
---|
| 422 | {
|
---|
| 423 | console.log("No debug tags present, debugging disabled.");
|
---|
| 424 | return;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | createDebugDiv();
|
---|
| 428 | $("body").append(_mainDiv);
|
---|
| 429 |
|
---|
| 430 | addMouseEventsToDebugElements(debugElems);
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | $(window).load(function()
|
---|
| 436 | {
|
---|
| 437 | var debugWidget = new DebugWidget();
|
---|
| 438 | debugWidget.init();
|
---|
| 439 | });
|
---|