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