source: main/trunk/greenstone3/web/interfaces/default/js/debug_scripts.js@ 36027

Last change on this file since 36027 was 36027, checked in by cstephen, 2 years ago

Migrate to using jQuery3 and jQuery-UI-1.13.2; and integrate cookie consent manager

  • Property svn:executable set to *
File size: 29.9 KB
Line 
1function DebugWidget()
2{
3 //************************
4 //Private member variables
5 //************************
6
7 //The this variable
8 var _greenbug = this;
9
10 //Template tracker class
11 var TemplateTracker = function()
12 {
13 var templates = new Array();
14 var currentIndex = -1;
15
16 this.push = function(object)
17 {
18 if(currentIndex < templates.length - 1 && templates.length > 0)
19 {
20 templates.splice(currentIndex + 1, templates.length - 1 - currentIndex);
21 }
22 templates[++currentIndex] = object;
23
24 _forwardButton.button("option", "disabled", true);
25 if(templates.length > 1)
26 {
27 _backButton.button("option", "disabled", false);
28 }
29 }
30
31 this.next = function()
32 {
33 if(currentIndex == templates.length - 1)
34 {
35 return;
36 }
37
38 if(currentIndex + 1 == templates.length - 1)
39 {
40 _forwardButton.button("option", "disabled", true);
41 }
42 _backButton.button("option", "disabled", false);
43
44 return templates[++currentIndex];
45 }
46
47 this.previous = function()
48 {
49 if(currentIndex == 0)
50 {
51 return;
52 }
53
54 if(currentIndex - 1 == 0)
55 {
56 _backButton.button("option", "disabled", true);
57 }
58 _forwardButton.button("option", "disabled", false);
59
60 return templates[--currentIndex];
61 }
62
63 this.peekPrevious = function()
64 {
65 if(currentIndex == 0)
66 {
67 return;
68 }
69
70 return templates[currentIndex - 1];
71 }
72
73 this.peekNext = function()
74 {
75 if(currentIndex == templates.length - 1)
76 {
77 return;
78 }
79
80 return templates[currentIndex + 1];
81 }
82 }
83
84 var _templateTracker = new TemplateTracker();
85
86 //Debugger state-keeping variables
87 var _debugOn = false;
88 var _pauseSelector = false;
89 var _elements = new Array();
90 var _itemSelected = false; //Used to prevent multiple elements from being highlighted
91 var _editModeText = false;
92 var _fromSelection = false;
93 var _selectedInfoContainers = new Array();
94
95 //Page elements
96 var _mainDiv;
97
98 var _textEditor;
99 var _vEditor;
100
101 var _navArea;
102 var _fileSelector;
103 var _templateSelector;
104 var _editor;
105 var _editingDiv;
106 var _xmlStatusBar;
107
108 //Buttons
109 var _backButton;
110 var _forwardButton;
111 var _currentSelectionButton;
112 var _enableSelectorButton;
113 var _closeEditorButton;
114 var _saveButton;
115 var _swapEditorButton;
116
117 //Editor state-keeping variables
118 var _currentFileName;
119 var _currentLocation;
120 var _currentNodename;
121 var _currentName;
122 var _currentMatch;
123 var _currentNamespace;
124 var _currentXPath;
125 var _isVisualEditor = true;
126
127 var _styleFunctions = new Array();
128
129 //Used to reload the page while keeping the state of the editor
130 var partialPageReload = function(callback)
131 {
132 $.ajax(document.URL)
133 .done(function(response)
134 {
135 //Get the body text from the response
136 var bodyStartIndex = response.indexOf("<body");
137 var bodyEndIndex = response.indexOf("</body>");
138 var bodyText = response.substring(bodyStartIndex, bodyEndIndex + 7);
139
140 //Get the current top area and container
141 var topLevelTopArea = $("#topArea");
142 var topLevelContainer = $("#container");
143
144 //Create a temporary div and put the html into it
145 var tempDiv = $("<div>");
146 tempDiv.html(bodyText);
147
148 //Replace the contents of the old elements with the new elements
149 var topArea = tempDiv.find("#topArea");
150 var container = tempDiv.find("#container");
151 topLevelTopArea.html(topArea.html());
152 topLevelContainer.html(container.html());
153
154 //Update the events for the debug elements that currently don't have events associated with them
155 var debugElems = $('debug, [debug="true"]').filter(function(){return (!($.data(this, "events"))) ? true : false});
156 addMouseEventsToDebugElements(debugElems);
157 })
158 .fail(function()
159 {
160 alert("There was an error reloading the page, please reload manually.");
161 });
162
163 if(callback)
164 {
165 callback();
166 }
167 }
168
169 //Some functions need to be called after an element is added to the page. So we store them and call them later.
170 var callStyleFunctions = function()
171 {
172 for(var i = 0; i < _styleFunctions.length; i++)
173 {
174 var sFunction = _styleFunctions[i];
175 sFunction();
176 }
177 }
178
179 var changeToSelectedElement = function(templateIndex, templateList)
180 {
181 _templateSelector.children("select").empty();
182
183 for(var i = 0; i < templateList.length; i++)
184 {
185 _templateSelector.children("select").append($(templateList[i]).clone(true));
186 }
187
188 if(templateIndex === undefined)
189 {
190 _templateSelector.find("option").first().trigger("change", [true]);
191 }
192 else
193 {
194 _templateSelector.find("option").filter(function(){return $(this).data("index") == templateIndex}).first().trigger("change", [true]);
195 }
196 return;
197 }
198
199 var createNavButtons = function(buttonDiv)
200 {
201 var navButtonHolder = $("<div>").css("float", "left");
202
203 var backForwardFunction = function(e)
204 {
205 var template;
206 if($(e.target).attr("id") == "veBack")
207 {
208 template = _templateTracker.previous();
209 }
210 else
211 {
212 template = _templateTracker.next();
213 }
214
215 if(!template)
216 {
217 return;
218 }
219
220 _fileSelector.find("option").filter(function(){return $(this).data("index") == template.fileIndex}).prop("selected", true);
221
222 changeToSelectedElement(template.templateIndex, template.list);
223 }
224
225 _backButton = $("<button>Back button</button>").attr("id", "veBack");
226 _backButton.on("click", backForwardFunction);
227 _styleFunctions.push(function(){_backButton.button({icons:{primary:"ui-icon-triangle-1-w"}, text:false, disabled:true})});
228
229 _forwardButton = $("<button>Forward button</button>").attr("id", "veForwards");
230 _forwardButton.on("click", backForwardFunction);
231 _styleFunctions.push(function(){_forwardButton.button({icons:{primary:"ui-icon-triangle-1-e"}, text:false, disabled:true})});
232
233 //Changes the template list to what is currently selected
234 _currentSelectionButton = $("<button>Current selection button</button>");
235 _currentSelectionButton.on("click", function()
236 {
237 _fileSelector.find("option").eq(0).prop("selected", true);
238 changeToSelectedElement(undefined, _selectedInfoContainers);
239
240 var selectedCopy = new Array();
241 for(var i = 0; i < _selectedInfoContainers.length; i++)
242 {
243 selectedCopy[i] = _selectedInfoContainers[i];
244 }
245
246 _templateTracker.push({fileIndex:-1, templateIndex:0, list:selectedCopy});
247 });
248 _styleFunctions.push(function(){_currentSelectionButton.button({icons:{primary:"ui-icon-pencil"}, text:false, disabled:true})});
249
250 navButtonHolder.append(_backButton);
251 navButtonHolder.append(_forwardButton);
252 navButtonHolder.append(_currentSelectionButton);
253 buttonDiv.append(navButtonHolder);
254 }
255
256 //Create the area where the buttons are stored
257 var createControlButtons = function(buttonDiv)
258 {
259 //Used to enable the selector to get the templates of a particular area of the page
260 _enableSelectorButton = $("<button>Select an element</button>");
261 _enableSelectorButton.on("click", function()
262 {
263 _enableSelectorButton.button("option", "label", "Select new element");
264 $("a").on("click", function(e)
265 {
266 e.preventDefault();
267 });
268 _debugOn = true;
269 _pauseSelector = false;
270 _enableSelectorButton.button("option", "disabled", true);
271 });
272 _styleFunctions.push(function(){_enableSelectorButton.button({icons:{primary:"ui-icon-pencil"}})});
273
274 //Used to minimise/restore the editor
275 _closeEditorButton = $("<button>Close editor</button>");
276 _closeEditorButton.on("click", function()
277 {
278 if(_closeEditorButton.button("option", "label") == "Close editor")
279 {
280 _closeEditorButton.button("option", "label", "Open editor");
281 _editingDiv.hide();
282 }
283 else
284 {
285 _closeEditorButton.button("option", "label", "Close editor");
286 _editingDiv.show();
287 }
288 });
289 _closeEditorButton.css("float", "right");
290 _styleFunctions.push(function(){_closeEditorButton.button({icons:{secondary:"ui-icon-newwin"}, disabled:true})});
291
292 //Used to save any changes that have been made to this template
293 _saveButton = $("<button>Save changes</button>");
294 _saveButton.on("click", function()
295 {
296 if(_editor)
297 {
298 var xmlString;
299 if(_isVisualEditor)
300 {
301 _vEditor.savePendingEdits();
302 xmlString = new XMLSerializer().serializeToString(_vEditor.getXML());
303 }
304 else
305 {
306 xmlString = _editor.getValue();
307 }
308 xmlString = xmlString.replace(/&/g, "&amp;");
309
310 try
311 {
312 var xml = $.parseXML('<testContainer xmlns:xslt="http://www.w3.org/1999/XSL/Transform" 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>");
313 }
314 catch(error)
315 {
316 alert("Could not save as there is a problem with the XML.");
317 return;
318 }
319
320 var url = gs.xsltParams.library_name;
321 var parameters = {"a":"g", "rt":"r", "s":"SaveXMLTemplateToFile", "s1.locationName":_currentLocation, "s1.fileName":_currentFileName, "s1.interfaceName":gs.xsltParams.interface_name, "s1.siteName":gs.xsltParams.site_name, "s1.collectionName":gs.cgiParams.c, "s1.namespace":_currentNamespace, "s1.nodename":_currentNodename, "s1.xml":xmlString};
322
323 if(_currentName && _currentName.length > 0){parameters["s1.name"] = _currentName;}
324 if(_currentMatch && _currentMatch.length > 0){parameters["s1.match"] = _currentMatch;}
325 if(_currentXPath && _currentXPath.length > 0){parameters["s1.xpath"] = _currentXPath}
326
327 _saveButton.button("option", "disabled", true);
328 $.blockUI({message:'<div class="ui-state-active">Saving, please wait...</div>'});
329
330 $.post(url, parameters)
331 .done(function()
332 {
333 $.ajax(gs.xsltParams.library_name + "?a=s&sa=c")
334 .done(function()
335 {
336 partialPageReload(function(){$.unblockUI();});
337 })
338 .fail(function()
339 {
340 $.unblockUI();
341 alert("Error reloading collection.");
342 })
343 .always(function()
344 {
345 _saveButton.button("option", "disabled", false);
346 });
347 })
348 .fail(function()
349 {
350 alert("There was an error sending the request to the server, please try again.");
351 });
352 }
353 });
354 _styleFunctions.push(function(){_saveButton.button({icons:{primary:"ui-icon-disk"}, disabled:true})});
355
356 //Used to switch between the XML and Visual editors
357 _swapEditorButton = $("<button>Switch to XML editor</button>");
358 _swapEditorButton.button().on("click", function()
359 {
360 if(_vEditor && _textEditor)
361 {
362 if(_isVisualEditor)
363 {
364 _vEditor.savePendingEdits();
365 _vEditor.getMainDiv().hide();
366 var containerNode = _vEditor.getXML().firstChild;
367 var templateNode = containerNode.firstChild;
368 while(templateNode)
369 {
370 if(templateNode.nodeType == 1)
371 {
372 break;
373 }
374 templateNode = templateNode.nextSibling;
375 }
376 var xmlText = new XMLSerializer().serializeToString(templateNode);
377 _editor.setValue(xmlText);
378 _editor.clearSelection();
379 var UndoManager = require("ace/undomanager").UndoManager;
380 _editor.getSession().setUndoManager(new UndoManager());
381 _textEditor.show();
382 _swapEditorButton.button("option", "label", "Switch to visual editor");
383 _isVisualEditor = false;
384 _xmlStatusBar.show();
385 }
386 else
387 {
388 _textEditor.hide();
389 var xmlText = _editor.getValue();
390 _vEditor.getMainDiv().remove();
391 _vEditor = new visualXMLEditor(xmlText);
392 _editingDiv.append(_vEditor.getMainDiv());
393 _vEditor.selectRootElement();
394 _vEditor.getMainDiv().show();
395 _swapEditorButton.button("option", "label", "Switch to XML editor");
396 _isVisualEditor = true;
397 _xmlStatusBar.hide();
398 }
399 }
400 });
401 _styleFunctions.push(function(){_swapEditorButton.button({icons:{primary:"ui-icon-refresh"}})});
402
403 undoButton = $("<button>Undo</button>");
404 undoButton.on("click", function()
405 {
406 if(_isVisualEditor)
407 {
408 _vEditor.undo();
409 }
410 else
411 {
412 _editor.undo();
413 }
414 });
415 _styleFunctions.push(function(){undoButton.button({icons:{primary:"ui-icon-arrowreturnthick-1-w"}})});
416
417 buttonDiv.append(_enableSelectorButton);
418 buttonDiv.append(_closeEditorButton);
419 buttonDiv.append(_saveButton);
420 buttonDiv.append(_swapEditorButton);
421 buttonDiv.append(undoButton);
422 }
423
424 //Used to monitor the state of the XML in the XML editor and will notify the user if there is an error
425 var createXMLStatusBar = function(buttonDiv)
426 {
427 _xmlStatusBar = $("<span>");
428 _xmlStatusBar.css("padding", "5px");
429 _xmlStatusBar.addClass("ui-corner-all");
430 _styleFunctions.push(function(){_xmlStatusBar.hide();});
431
432 //Check the XML for errors every 2 seconds
433 setInterval(function()
434 {
435 if(_editor)
436 {
437 var xmlString = _editor.getValue();
438 try
439 {
440 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>");
441 }
442 catch(error)
443 {
444 console.log(error);
445 _xmlStatusBar.text("XML ERROR! (Mouse over for details)");
446 _xmlStatusBar.addClass("ui-state-error");
447 _xmlStatusBar.removeClass("ui-state-active");
448 _xmlStatusBar.attr("title", error);
449 _saveButton.button("option", "disabled", true);
450 _swapEditorButton.button("option", "disabled", true);
451 return;
452 }
453
454 _xmlStatusBar.text("XML OK!");
455 _xmlStatusBar.addClass("ui-state-active");
456 _xmlStatusBar.removeClass("ui-state-error");
457 _xmlStatusBar.removeAttr("title");
458 if(_saveButton.button("option", "label") == "Save changes")
459 {
460 _saveButton.button("option", "disabled", false);
461 }
462 if(_swapEditorButton.button("option", "label") == "Switch to visual editor")
463 {
464 _swapEditorButton.button("option", "disabled", false);
465 }
466 }
467 }, 2000);
468 buttonDiv.append(_xmlStatusBar);
469 }
470
471 //Create the elements that allow
472 var createFileAndTemplateSelectors = function(buttonDiv)
473 {
474 _templateSelector = $("<div>", {"id":"veTemplateSelector", "class":"ui-state-default ui-corner-all"});
475 _templateSelector.append($("<span>Templates: <span>"));
476 var templateSelectBox = $("<select>").append("<option>-- No templates --</option>");
477 templateSelectBox.change(function(e, triggered)
478 {
479 var selected = templateSelectBox.find(":selected");
480 var changeFunction = selected.data("changeFunction");
481 if(changeFunction)
482 {
483 changeFunction();
484 }
485
486 if(!triggered)
487 {
488 _templateTracker.push({fileIndex:_fileSelector.find(":selected").data("index"), templateIndex:selected.data("index"), list:templateSelectBox.children("option").clone(true)});
489 }
490 });
491 _templateSelector.append(templateSelectBox);
492 _fileSelector = $("<div>", {"id":"veFileSelector"});
493 buttonDiv.append(_fileSelector);
494 buttonDiv.append(_templateSelector);
495
496 //Populate the file selector
497 var url = gs.xsltParams.library_name + "?a=g&rt=r&s=GetXSLTFilesForCollection&s1.interfaceName=" + gs.xsltParams.interface_name + "&s1.siteName=" + gs.xsltParams.site_name + "&s1.collectionName=" + gs.cgiParams.c;
498 $.ajax(url)
499 .done(function(response)
500 {
501 var listStartIndex = response.indexOf("<fileListJSON>") + "<fileListJSON>".length;
502 var listEndIndex = response.indexOf("</fileListJSON>");
503
504 var listString = response.substring(listStartIndex, listEndIndex).replace(/&quot;/g, "\"").replace(/\\/g, "/");
505 var list = eval(listString);
506
507 var fileSelectBox = $("<select>");
508 fileSelectBox.append($("<option>-- Select a file --</option>").data("index", -1));
509 _fileSelector.addClass("ui-state-default");
510 _fileSelector.addClass("ui-corner-all");
511 _fileSelector.append("<span>Files: </span>");
512 _fileSelector.append(fileSelectBox);
513
514 for(var i = 0; i < list.length; i++)
515 {
516 var item = list[i];
517 var option = $("<option>" + item.path + " (" + item.location + ")</option>").data("index", i);
518 option.data("fileItem", item);
519 fileSelectBox.append(option);
520 }
521
522 fileSelectBox.change(function()
523 {
524 var selectedItem = fileSelectBox.find(":selected");
525
526 if(!selectedItem.data("fileItem"))
527 {
528 return;
529 }
530
531 _greenbug.populateTemplateSelectorFromFile(selectedItem.data("fileItem").path, selectedItem.data("fileItem").location, function(){_templateSelector.children("select").trigger("change")});
532 });
533 })
534 .fail(function()
535 {
536 console.log("Error retrieving XSLT files");
537 });
538 }
539
540 //The function that creates all the necessary elements for Greenbug
541 var createDebugDiv = function()
542 {
543 _mainDiv = $("<div>", {"id":"debugDiv"});
544 _mainDiv.css(
545 {
546 "position":"fixed",
547 "font-size":"0.8em",
548 "bottom":"0px",
549 "width":"100%",
550 "background":"white",
551 "border":"1px black solid",
552 "padding":"5px",
553 "z-index":100
554 });
555
556 _editingDiv = $("<div>");
557 var toolBarDiv = $("<div>");
558 toolBarDiv.css({"height":"40px"});
559 toolBarDiv.append("<div>", {style:"clear:both;"});
560
561 var buttonDiv = $("<div>");
562 toolBarDiv.append(buttonDiv);
563 createNavButtons(buttonDiv);
564 createControlButtons(buttonDiv);
565 createFileAndTemplateSelectors(buttonDiv);
566 createXMLStatusBar(buttonDiv);
567
568 _styleFunctions.push(function(){$(".ui-button").css({"margin-right":"0.5em"});});
569
570 _mainDiv.append(toolBarDiv);
571 _mainDiv.append(_editingDiv);
572 _mainDiv.append(_navArea);
573 }
574
575 //Clear all selected elements on the page
576 var clearAll = function()
577 {
578 _itemSelected = false;
579 $(_elements).each(function()
580 {
581 $(this).remove();
582 });
583 }
584
585 //Put a border around the given element
586 var highlightElement = function(e)
587 {
588 var topBorderDiv = $("<div>");
589 var bottomBorderDiv = $("<div>");
590 var leftBorderDiv = $("<div>");
591 var rightBorderDiv = $("<div>");
592
593 topBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":e.offset().left + "px", "height":"0px", "width":e.width() + "px", "border":"1px solid red"});
594 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"});
595 leftBorderDiv.css({"position":"absolute", "top":e.offset().top + "px", "left":e.offset().left + "px", "height":e.height() + "px", "width":"0px", "border":"1px solid red"});
596 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"});
597
598 $("body").append(topBorderDiv, bottomBorderDiv, leftBorderDiv, rightBorderDiv);
599
600 _elements.push(topBorderDiv);
601 _elements.push(bottomBorderDiv);
602 _elements.push(leftBorderDiv);
603 _elements.push(rightBorderDiv);
604 }
605
606 this.populateTemplateSelectorFromFile = function(filename, location, callback)
607 {
608 var getURL = gs.xsltParams.library_name + "?a=g&rt=r&s=GetTemplateListFromFile&s1.fileName=" + filename + "&s1.locationName=" + location + "&s1.interfaceName=" + gs.xsltParams.interface_name + "&s1.siteName=" + gs.xsltParams.site_name + "&s1.collectionName=" + gs.cgiParams.c;
609 $.ajax(getURL)
610 .done(function(templateResponse)
611 {
612 var templateListStart = templateResponse.indexOf("<templateList>") + "<templateList>".length;
613 var templateListEnd = templateResponse.indexOf("</templateList>");
614
615 if(templateListStart == "<templateList>".length - 1)
616 {
617 return;
618 }
619
620 var templateListString = templateResponse.substring(templateListStart, templateListEnd).replace(/&quot;/g, "\"");
621 var templateList = eval(templateListString);
622
623 _templateSelector.children("select").empty();
624 if(templateList.length == 0)
625 {
626 _templateSelector.children("select").append($("<option>-- No templates --</option>").data("index", -1));
627 }
628
629 for(var i = 0; i < templateList.length; i++)
630 {
631 var namespace = templateList[i].namespace;
632 var nodename = "template";
633 var name = templateList[i].name;
634 var match = templateList[i].match;
635 var xpath = templateList[i].xpath;
636
637 if(name)
638 {
639 name = templateList[i].name.replace(/&apos;/g, "'").replace(/&quot;/g, "\"").replace(/&amp;/g, "&");
640 }
641 if(match)
642 {
643 match = templateList[i].match.replace(/&apos;/g, "'").replace(/&quot;/g, "\"").replace(/&amp;/g, "&");
644 }
645
646 var infoContainer = $("<option>");
647
648 _elements.push(infoContainer);
649
650 addChangeEventToInfoContainer(infoContainer, filename, location, nodename, namespace, name, match, xpath);
651
652 if(name && name.length > 0)
653 {
654 infoContainer.text(name + ((xpath) ? (" (" + xpath + ")") : ""));
655 }
656 if(match && match.length > 0)
657 {
658 infoContainer.text(match + ((xpath) ? (" (" + xpath + ")") : ""));
659 }
660
661 infoContainer.data("index", i);
662 _templateSelector.children("select").append(infoContainer);
663 }
664
665 if(callback)
666 {
667 callback();
668 }
669 });
670 }
671
672 //Change the current template in the XML and Visual editor
673 this.changeCurrentTemplate = function(location, filename, nodename, namespace, name, match, xpath)
674 {
675 _currentFileName = filename;
676 _currentLocation = location;
677 _currentNodename = nodename;
678 _currentNamespace = namespace;
679 _currentName = name;
680 _currentMatch = match;
681 _currentXPath = xpath;
682
683 var responseName = "requestedNameTemplate";
684
685 var url = gs.xsltParams.library_name;
686 var params = {"a":"g", "rt":"r", "s":"GetXMLTemplateFromFile", "s1.fileName":filename, "s1.interfaceName":gs.xsltParams.interface_name, "s1.siteName":gs.xsltParams.site_name, "s1.collectionName":gs.cgiParams.c, "s1.locationName":location, "s1.namespace":namespace, "s1.nodename":nodename};
687
688 if(match && match.length > 0){params["s1.match"] = match; responseName = "requestedMatchTemplate";}
689 if(xpath && xpath.length > 0){params["s1.xpath"] = xpath}
690 if(name && name.length > 0){params["s1.name"] = name;}
691
692 $.post(url, params)
693 .done(function(response)
694 {
695 var template;
696 if(response.search(responseName) != -1)
697 {
698 var startIndex = response.indexOf("<" + responseName + ">") + responseName.length + 2;
699 var endIndex = response.indexOf("</" + responseName + ">");
700 template = response.substring(startIndex, endIndex);
701 }
702 else
703 {
704 return;
705 }
706
707 _textEditor = $("<div>", {"id":"textEditor"});
708 _textEditor.css({"width":"100%", "height":"300px"});
709 _textEditor.val(template);
710
711 if(_isVisualEditor)
712 {
713 _textEditor.hide();
714 }
715
716 _editingDiv.empty();
717 _editingDiv.append($("<p>Location: " + location + " <br/>Filename: " + filename + "</p>"));
718 _editingDiv.append(_textEditor);
719
720 _vEditor = new visualXMLEditor(template);
721 _editingDiv.append(_vEditor.getMainDiv());
722 _vEditor.setGreenbug(_greenbug);
723 _vEditor.selectRootElement();
724 $("#veToolboxDiv").height($("#veEditorContainer").height());
725
726 _vEditor.setFileLocation(location);
727 _vEditor.setFileName(filename);
728
729 if(!_isVisualEditor)
730 {
731 _vEditor.getMainDiv().hide();
732 }
733
734 _editor = ace.edit("textEditor");
735 _editor.getSession().setMode("ace/mode/xml");
736 _editor.getSession().setUseSoftTabs(false);
737 _editor.setValue(template);
738 _editor.clearSelection();
739 var UndoManager = require("ace/undomanager").UndoManager;
740 _editor.getSession().setUndoManager(new UndoManager());
741
742 _textEditor.css({"min-height":"200px", "border-top":"5px solid #444"});
743 _textEditor.resizable({handles: 'n', resize:function()
744 {
745 _textEditor.css({top:"0px"});
746 _editor.resize();
747 }});
748
749 _closeEditorButton.button("option", "disabled", false);
750 if(_closeEditorButton.button("option", "label") == "Open editor")
751 {
752 _closeEditorButton.button("option", "label", "Close editor");
753 _editingDiv.show();
754 }
755 })
756 .fail(function()
757 {
758 console.log("Error getting the XML template from the file");
759 });
760 }
761
762 //Store the function that is called when this template is selected from the list
763 var addChangeEventToInfoContainer = function(infoContainer, filename, location, nodename, namespace, name, match, xpath)
764 {
765 infoContainer.data("changeFunction", function()
766 {
767 _greenbug.changeCurrentTemplate(location, filename, nodename, namespace, name, match, xpath);
768 });
769 }
770
771 this.getTemplateTracker = function()
772 {
773 return _templateTracker;
774 }
775
776 //Turns a filename into it's location (i.e. interface/site/collection) and name
777 this.fileNameToLocationAndName = function(filepath)
778 {
779 var location;
780 var filename;
781 //Use the filepath to work out where this file is from
782 if(filepath.search(/[\/\\]interfaces[\/\\]/) != -1)
783 {
784 location = "interface";
785 filename = filepath.replace(/.*[\/\\]transform[\/\\]/, "");
786 }
787 else if(filepath.search(/[\/\\]sites[\/\\].*[\/\\]collect[\/\\].*[\/\\]etc[\/\\]/) != -1)
788 {
789 location = "collectionConfig";
790 filename = filepath.replace(/.*[\/\\]sites[\/\\].*[\/\\]collect[\/\\].*[\/\\]etc[\/\\]/, "");
791 }
792 else if(filepath.search(/[\/\\]sites[\/\\].*[\/\\]collect[\/\\].*[\/\\]transform[\/\\]/) != -1)
793 {
794 location = "collection";
795 filename = filepath.replace(/.*[\/\\]sites[\/\\].*[\/\\]collect[\/\\].*[\/\\]transform[\/\\]/, "");
796 }
797 else if(filepath.search(/[\/\\]sites[\/\\].*[\/\\]transform[\/\\]/) != -1)
798 {
799 location = "site";
800 filename = filepath.replace(/.*[\/\\]sites[\/\\].*[\/\\]transform[\/\\]/, "");
801 }
802
803 filename = filename.replace(/\\/g, "/");
804
805 return {location:location, filename:filename};
806 }
807
808 //Add the mouse events to the <debug> elemetns that are called when the selector is anabled
809 var addMouseEventsToDebugElements = function(debugElems)
810 {
811 debugElems.on("click", function(e)
812 {
813 if(_debugOn)
814 {
815 e.stopPropagation();
816 $("a").off("click");
817 _debugOn = false;
818 _pauseSelector = true;
819 _enableSelectorButton.button("option", "disabled", false);
820 _templateSelector.children("select").trigger("change", [true]);
821 _fileSelector.children("select").val("none");
822
823 var selectedCopy = new Array();
824 for(var i = 0; i < _selectedInfoContainers.length; i++)
825 {
826 selectedCopy[i] = _selectedInfoContainers[i];
827 }
828
829 _currentSelectionButton.button("option", "disabled", false);
830
831 _templateTracker.push({fileIndex:-1, list:selectedCopy, templateIndex:0});
832 }
833 });
834
835 debugElems.mouseover(function()
836 {
837 if(_debugOn && !_pauseSelector)
838 {
839 _fileSelector.find("select").val("none");
840
841 var nodes = new Array();
842 if($(this).is("table, tr"))
843 {
844 var size = parseInt($(this).attr("debugSize"));
845 for(var i = 0; i < size; i++)
846 {
847 var tempNode = $("<div>");
848 tempNode.tempAttrs = new Array();
849 $(this.attributes).each(function()
850 {
851 if(this.value.charAt(0) == '[')
852 {
853 var values = eval(this.value);
854 if(values[i] == "")
855 {
856 return;
857 }
858 tempNode.attr(this.name, values[i]);
859 tempNode.tempAttrs.push({name:this.name, value:values[i]});
860 }
861 });
862 nodes.push(tempNode);
863 }
864 }
865 else
866 {
867 nodes.push(this);
868 }
869
870 $(nodes).each(function()
871 {
872 var filepath = $(this).attr("filename");
873 var fullNodename = $(this).attr("nodename");
874 var colonIndex = fullNodename.indexOf(":");
875 var namespace = fullNodename.substring(0, colonIndex);
876 var nodename = fullNodename.substring(colonIndex + 1);
877 var name = $(this).attr("name");
878 var match = $(this).attr("match");
879 var xpath = $(this).attr("xpath");
880
881 var file = _greenbug.fileNameToLocationAndName(filepath);
882
883 var infoContainer = $("<option>");
884
885 _elements.push(infoContainer);
886
887 addChangeEventToInfoContainer(infoContainer, file.filename, file.location, nodename, namespace, name, match, xpath);
888
889 if(name && name.length > 0)
890 {
891 infoContainer.text(name + ((xpath) ? (" (" + xpath + ")") : ""));
892 }
893 if(match && match.length > 0)
894 {
895 infoContainer.text(match + ((xpath) ? (" (" + xpath + ")") : ""));
896 }
897
898 _templateSelector.children("select").append(infoContainer);
899 infoContainer.data("index", infoContainer.index());
900 _selectedInfoContainers.push(infoContainer.clone(true));
901 });
902
903 if(!_itemSelected)
904 {
905 _itemSelected = true;
906 highlightElement($(this));
907 }
908 }
909 });
910
911 debugElems.mouseout(function()
912 {
913 if(_debugOn && !_pauseSelector)
914 {
915 clearAll();
916 _templateSelector.children("select").empty();
917 _selectedInfoContainers = new Array();
918 }
919 });
920 }
921
922 //Remove <debug> elements from the title
923 var fixTitle = function()
924 {
925 $("title").text($("title").text().replace(/<[^>]*>/g, ""));
926 }
927
928 //Initialise Greenbug
929 this.init = function()
930 {
931 //We only want this on if we have debug elements in the page
932 var debugElems = $('debug, [debug="true"]');
933 if(!debugElems.length)
934 {
935 var enableGBButtonHolder = $("<div>", {"title":"Enable Greenbug", "id":"gbEnableButton", "class":"ui-state-default ui-corner-all"});
936 enableGBButtonHolder.append($("<img>", {"src":gs.imageURLs.greenBug}));
937 enableGBButtonHolder.on("click", function()
938 {
939 var url = document.URL;
940 url = url.replace(/[\?&]debug=0/g, "").replace(/[\?&]debug=1/g, "");
941
942 if(url.indexOf("?") == url.length - 1)
943 {
944 document.location.href = url += "debug=1";
945 }
946 else if(url.indexOf("?") != -1)
947 {
948 document.location.href = url += "&debug=1";
949 }
950 else
951 {
952 document.location.href = url += "?debug=1";
953 }
954 });
955 $("body").append(enableGBButtonHolder);
956 return;
957 }
958
959 createDebugDiv();
960 $("body").append(_mainDiv);
961
962 callStyleFunctions();
963
964 addMouseEventsToDebugElements(debugElems);
965 fixTitle();
966 }
967}
968
969//The code entry point
970$(window).on("load", function()
971{
972 var debugWidget = new DebugWidget();
973 debugWidget.init();
974});
Note: See TracBrowser for help on using the repository browser.