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