JAL-2094 new classes ColorI, Colour added
[jalview.git] / src / jalview / io / HtmlSvgOutput.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.FeatureRenderer;
25 import jalview.datamodel.AlignmentExportData;
26 import jalview.datamodel.SequenceI;
27 import jalview.gui.AlignViewport;
28 import jalview.gui.AlignmentPanel;
29 import jalview.gui.HTMLOptions;
30 import jalview.gui.IProgressIndicator;
31 import jalview.gui.OOMWarning;
32 import jalview.math.AlignmentDimension;
33 import jalview.util.ColorUtils;
34 import jalview.util.MessageManager;
35
36 import java.awt.Color;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.print.Printable;
40 import java.awt.print.PrinterException;
41 import java.io.File;
42 import java.io.FileOutputStream;
43
44 import org.jfree.graphics2d.svg.SVGGraphics2D;
45 import org.jfree.graphics2d.svg.SVGHints;
46
47 public class HtmlSvgOutput
48 {
49   AlignViewport av;
50
51   FeatureRenderer fr;
52
53   AlignmentPanel ap;
54
55   public HtmlSvgOutput(File file, AlignmentPanel ap)
56   {
57     this.av = ap.av;
58     this.ap = ap;
59     fr = ap.cloneFeatureRenderer();
60     generateHtmlSvgOutput(file);
61   }
62
63   public void generateHtmlSvgOutput(File file)
64   {
65     IProgressIndicator pIndicator = ap.alignFrame;
66     long pSessionId = System.currentTimeMillis();
67     try
68     {
69       boolean headless = (System.getProperty("java.awt.headless") != null && System
70               .getProperty("java.awt.headless").equals("true"));
71       if (file == null)
72       {
73         if (pIndicator != null && !headless)
74         {
75           pIndicator.setProgressBar(MessageManager.formatMessage(
76                   "status.waiting_for_user_to_select_output_file", "HTML"),
77                   pSessionId);
78         }
79         JalviewFileChooser chooser = getHTMLChooser();
80         chooser.setFileView(new jalview.io.JalviewFileView());
81         chooser.setDialogTitle(ap.alignFrame.getTitle());
82         chooser.setToolTipText(MessageManager.getString("action.save"));
83         int value = chooser.showSaveDialog(ap.alignFrame);
84
85         if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
86         {
87           jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
88                   .getSelectedFile().getParent());
89           file = chooser.getSelectedFile();
90         }
91         else
92         {
93
94           if (pIndicator != null && !headless)
95         {
96             pIndicator.setProgressBar(MessageManager.formatMessage(
97                     "status.cancelled_image_export_operation", "HTML"),
98                     pSessionId);
99           }
100           return;
101         }
102       }
103
104       AlignmentDimension aDimension = ap.getAlignmentDimension();
105       SVGGraphics2D g1 = new SVGGraphics2D(aDimension.getWidth(),
106               aDimension.getHeight());
107       SVGGraphics2D g2 = new SVGGraphics2D(aDimension.getWidth(),
108               aDimension.getHeight());
109
110       String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING",
111               "Prompt each time");
112
113       // If we need to prompt, and if the GUI is visible then
114       // Prompt for rendering style
115       if (renderStyle.equalsIgnoreCase("Prompt each time")
116               && !(System.getProperty("java.awt.headless") != null && System
117                       .getProperty("java.awt.headless").equals("true")))
118       {
119         HTMLOptions svgOption = new HTMLOptions();
120         renderStyle = svgOption.getValue();
121
122         if (renderStyle == null || svgOption.cancelled)
123         {
124           return;
125         }
126       }
127
128       if (renderStyle.equalsIgnoreCase("lineart"))
129       {
130         g1.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
131                 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
132         g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
133                 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
134       }
135       printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, g1,
136               g2);
137
138       String titleSvgData = g1.getSVGDocument();
139       String alignSvgData = g2.getSVGDocument();
140       String jsonData = null;
141       boolean isEmbbedBioJSON = Boolean.valueOf(jalview.bin.Cache
142               .getDefault("EXPORT_EMBBED_BIOJSON", "true"));
143       if (isEmbbedBioJSON)
144       {
145         AlignExportSettingI exportSettings = new AlignExportSettingI()
146         {
147           @Override
148           public boolean isExportHiddenSequences()
149           {
150             return true;
151           }
152
153           @Override
154           public boolean isExportHiddenColumns()
155           {
156             return true;
157           }
158
159           @Override
160           public boolean isExportAnnotations()
161           {
162             return true;
163           }
164
165           @Override
166           public boolean isExportFeatures()
167           {
168             return true;
169           }
170
171           @Override
172           public boolean isExportGroups()
173           {
174             return true;
175           }
176
177           @Override
178           public boolean isCancelled()
179           {
180             return false;
181           }
182
183         };
184         AlignmentExportData exportData = jalview.gui.AlignFrame
185                 .getAlignmentForExport(JSONFile.FILE_DESC, av,
186                         exportSettings);
187         jsonData = new FormatAdapter(ap, exportData.getSettings())
188                 .formatSequences(JSONFile.FILE_DESC,
189                         exportData.getAlignment(),
190                         exportData.getOmitHidden(),
191                         exportData.getStartEndPostions(),
192                         av.getColumnSelection());
193       }
194       String htmlData = getHtml(titleSvgData, alignSvgData, jsonData);
195       FileOutputStream out = new FileOutputStream(file);
196       out.write(htmlData.getBytes());
197       out.flush();
198       out.close();
199       if (!(System.getProperty("java.awt.headless") != null && System
200               .getProperty("java.awt.headless").equals("true")))
201       {
202         jalview.util.BrowserLauncher.openURL("file:///" + file);
203       }
204       if (pIndicator != null && !headless)
205       {
206         pIndicator.setProgressBar(MessageManager.formatMessage(
207                 "status.export_complete", "HTML"), pSessionId);
208       }
209     } catch (OutOfMemoryError err)
210     {
211       System.out.println("########################\n" + "OUT OF MEMORY "
212               + file + "\n" + "########################");
213       new OOMWarning("Creating Image for " + file, err);
214     } catch (Exception e)
215     {
216       e.printStackTrace();
217       pIndicator.setProgressBar(MessageManager.formatMessage(
218               "info.error_creating_file", "HTML"), pSessionId);
219     }
220   }
221
222   static JalviewFileChooser getHTMLChooser()
223   {
224     return new jalview.io.JalviewFileChooser(
225             jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
226             new String[] { "html" },
227             new String[] { "Hypertext Markup Language" },
228             "Hypertext Markup Language");
229   }
230
231   public int printUnwrapped(int pwidth, int pheight, int pi, Graphics... pg)
232           throws PrinterException
233   {
234     int idWidth = ap.getVisibleIdWidth(false);
235     FontMetrics fm = ap.getFontMetrics(av.getFont());
236     int scaleHeight = av.getCharHeight() + fm.getDescent();
237
238     pg[0].setColor(Color.white);
239     pg[0].fillRect(0, 0, pwidth, pheight);
240     pg[0].setFont(av.getFont());
241
242     // //////////////////////////////////
243     // / How many sequences and residues can we fit on a printable page?
244     int totalRes = (pwidth - idWidth) / av.getCharWidth();
245     int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
246     int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
247
248     // ///////////////////////////
249     // / Only print these sequences and residues on this page
250     int startRes;
251
252     // ///////////////////////////
253     // / Only print these sequences and residues on this page
254     int endRes;
255
256     // ///////////////////////////
257     // / Only print these sequences and residues on this page
258     int startSeq;
259
260     // ///////////////////////////
261     // / Only print these sequences and residues on this page
262     int endSeq;
263     startRes = (pi % pagesWide) * totalRes;
264     endRes = (startRes + totalRes) - 1;
265
266     if (endRes > (av.getAlignment().getWidth() - 1))
267     {
268       endRes = av.getAlignment().getWidth() - 1;
269     }
270     startSeq = (pi / pagesWide) * totalSeq;
271     endSeq = startSeq + totalSeq;
272     if (endSeq > av.getAlignment().getHeight())
273     {
274       endSeq = av.getAlignment().getHeight();
275     }
276     int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
277             * pheight;
278     if (av.isShowAnnotation())
279     {
280       pagesHigh += ap.getAnnotationPanel().adjustPanelHeight() + 3;
281     }
282     pagesHigh /= pheight;
283     if (pi >= (pagesWide * pagesHigh))
284     {
285       return Printable.NO_SUCH_PAGE;
286     }
287
288     // draw Scale
289     pg[1].translate(0, 0);
290     ap.getScalePanel().drawScale(pg[1], startRes, endRes, pwidth - idWidth,
291             scaleHeight);
292     pg[1].translate(-idWidth, scaleHeight);
293
294     // //////////////
295     // Draw the ids
296     Color currentColor = null;
297     Color currentTextColor = null;
298     pg[0].translate(0, scaleHeight);
299     pg[0].setFont(ap.getIdPanel().getIdCanvas().getIdfont());
300     SequenceI seq;
301     for (int i = startSeq; i < endSeq; i++)
302     {
303       seq = av.getAlignment().getSequenceAt(i);
304       if ((av.getSelectionGroup() != null)
305               && av.getSelectionGroup().getSequences(null).contains(seq))
306       {
307         currentColor = Color.gray;
308         currentTextColor = Color.black;
309       }
310       else
311       {
312         currentColor = ColorUtils.getColor(av.getSequenceColour(seq));
313         currentTextColor = Color.black;
314       }
315       pg[0].setColor(currentColor);
316       pg[0].fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
317               av.getCharHeight());
318       pg[0].setColor(currentTextColor);
319       int xPos = 0;
320       if (av.isRightAlignIds())
321       {
322         fm = pg[0].getFontMetrics();
323         xPos = idWidth
324                 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
325                 - 4;
326       }
327       pg[0].drawString(seq.getDisplayId(av.getShowJVSuffix()), xPos,
328               (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
329                       - (av.getCharHeight() / 5));
330     }
331     pg[0].setFont(av.getFont());
332     pg[0].translate(idWidth, 0);
333
334     // draw main sequence panel
335     pg[1].translate(idWidth, 0);
336     ap.getSeqPanel().seqCanvas.drawPanel(pg[1], startRes, endRes, startSeq,
337             endSeq, 0);
338     if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
339     {
340       // draw annotation label - need to offset for current scroll position
341       int offset = -ap.getAlabels().getScrollOffset();
342       pg[0].translate(0, offset);
343       pg[0].translate(-idWidth - 3,
344               (endSeq - startSeq) * av.getCharHeight() + 3);
345       ap.getAlabels().drawComponent(pg[0], idWidth);
346       pg[0].translate(idWidth + 3, 0);
347       pg[0].translate(0, -offset);
348
349       // draw annotation - need to offset for current scroll position
350       pg[1].translate(0, offset);
351       pg[1].translate(-idWidth - 3,
352               (endSeq - startSeq) * av.getCharHeight() + 3);
353       pg[1].translate(idWidth + 3, 0);
354       ap.getAnnotationPanel().renderer.drawComponent(
355               ap.getAnnotationPanel(), av, pg[1], -1, startRes, endRes + 1);
356       pg[1].translate(0, -offset);
357     }
358
359     return Printable.PAGE_EXISTS;
360   }
361
362   private String getHtml(String titleSvg, String alignmentSvg,
363           String jsonData)
364   {
365     StringBuilder htmlSvg = new StringBuilder();
366     htmlSvg.append("<html>\n");
367     if (jsonData != null)
368     {
369       htmlSvg.append("<button onclick=\"javascipt:openJalviewUsingCurrentUrl();\">Launch in Jalview</button> &nbsp;");
370       htmlSvg.append("<input type=\"submit\" value=\"View raw BioJSON Data\" onclick=\"jQuery.facebox({ div:'#seqData' }); return false;\" />");
371       htmlSvg.append("<div style=\"display: none;\" name=\"seqData\" id=\"seqData\" >"
372               + jsonData + "</div>");
373       htmlSvg.append("<br/>&nbsp;");
374     }
375     htmlSvg.append("\n<style type=\"text/css\"> "
376             + "div.parent{ width:100%;<!-- overflow: auto; -->}\n"
377             + "div.titlex{ width:11%; float: left; }\n"
378             + "div.align{ width:89%; float: right; }\n"
379             + "div.main-container{ border: 2px solid blue; border: 2px solid blue; width: 99%;   min-height: 99%; }\n"
380             + ".sub-category-container {overflow-y: scroll; overflow-x: hidden; width: 100%; height: 100%;}\n"
381             + "object {pointer-events: none;}");
382     if (jsonData != null)
383     {
384       // facebox style sheet for displaying raw BioJSON data
385       htmlSvg.append("#facebox { position: absolute;  top: 0;   left: 0; z-index: 100; text-align: left; }\n"
386               + "#facebox .popup{ position:relative; border:3px solid rgba(0,0,0,0); -webkit-border-radius:5px;"
387               + "-moz-border-radius:5px; border-radius:5px; -webkit-box-shadow:0 0 18px rgba(0,0,0,0.4); -moz-box-shadow:0 0 18px rgba(0,0,0,0.4);"
388               + "box-shadow:0 0 18px rgba(0,0,0,0.4); }\n"
389               + "#facebox .content { display:table; width: 98%; padding: 10px; background: #fff; -webkit-border-radius:4px; -moz-border-radius:4px;"
390               + " border-radius:4px; }\n"
391               + "#facebox .content > p:first-child{ margin-top:0; }\n"
392               + "#facebox .content > p:last-child{ margin-bottom:0; }\n"
393               + "#facebox .close{ position:absolute; top:5px; right:5px; padding:2px; background:#fff; }\n"
394               + "#facebox .close img{ opacity:0.3; }\n"
395               + "#facebox .close:hover img{ opacity:1.0; }\n"
396               + "#facebox .loading { text-align: center; }\n"
397               + "#facebox .image { text-align: center;}\n"
398               + "#facebox img { border: 0;  margin: 0; }\n"
399               + "#facebox_overlay { position: fixed; top: 0px; left: 0px; height:100%; width:100%; }\n"
400               + ".facebox_hide { z-index:-100; }\n"
401               + ".facebox_overlayBG { background-color: #000;  z-index: 99;  }");
402     }
403
404     htmlSvg.append("</style>");
405     htmlSvg.append("<div class=\"main-container\" \n>");
406     htmlSvg.append("<div class=\"titlex\">\n");
407     htmlSvg.append("<div class=\"sub-category-container\"> \n");
408     htmlSvg.append(titleSvg);
409     htmlSvg.append("</div>");
410     htmlSvg.append("</div>\n\n<!-- ========================================================================================== -->\n\n");
411     htmlSvg.append("<div class=\"align\" >");
412     htmlSvg.append(
413             "<div class=\"sub-category-container\"> <div style=\"overflow-x: scroll;\">")
414             .append(alignmentSvg).append("</div></div>").append("</div>");
415     htmlSvg.append("</div>");
416
417     htmlSvg.append("<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js\"></script>\n"
418             + "<script language=\"JavaScript\" type=\"text/javascript\"  src=\"http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js\"></script>\n"
419             + "<script>\n"
420             + "var subCatContainer = $(\".sub-category-container\");\n"
421             + "subCatContainer.scroll(\nfunction() {\n"
422             + "subCatContainer.scrollTop($(this).scrollTop());\n});\n");
423
424     htmlSvg.append("</script>\n");
425
426     // javascript for launching file in Jalview
427
428     htmlSvg.append("<script language=\"JavaScript\">\n");
429     htmlSvg.append("function openJalviewUsingCurrentUrl(){\n");
430     htmlSvg.append("    var json = JSON.parse(document.getElementById(\"seqData\").innerHTML);\n");
431     htmlSvg.append("    var jalviewVersion = json['appSettings'].version;\n");
432     htmlSvg.append("    var url = json['appSettings'].webStartUrl;\n");
433     htmlSvg.append("    var myForm = document.createElement(\"form\");\n\n");
434     htmlSvg.append("    var heap = document.createElement(\"input\");\n");
435     htmlSvg.append("    heap.setAttribute(\"name\", \"jvm-max-heap\") ;\n");
436     htmlSvg.append("    heap.setAttribute(\"value\", \"2G\");\n\n");
437     htmlSvg.append("    var target = document.createElement(\"input\");\n");
438     htmlSvg.append("    target.setAttribute(\"name\", \"open\");\n");
439     htmlSvg.append("    target.setAttribute(\"value\", document.URL);\n\n");
440     htmlSvg.append("    var jvVersion = document.createElement(\"input\");\n");
441     htmlSvg.append("    jvVersion.setAttribute(\"name\", \"version\") ;\n");
442     htmlSvg.append("    jvVersion.setAttribute(\"value\", jalviewVersion);\n\n");
443     htmlSvg.append("    myForm.action = url;\n");
444     htmlSvg.append("    myForm.appendChild(heap);\n");
445     htmlSvg.append("    myForm.appendChild(target);\n");
446     htmlSvg.append("    myForm.appendChild(jvVersion);\n");
447     htmlSvg.append("    document.body.appendChild(myForm);\n");
448     htmlSvg.append("    myForm.submit() ;\n");
449     htmlSvg.append("    document.body.removeChild(myForm);\n");
450     htmlSvg.append("}\n");
451
452     // jquery facebox for displaying raw BioJSON data");
453     if (jsonData != null)
454     {
455       htmlSvg.append("/* Facebox (for jQuery)\n");
456       htmlSvg.append("* version: 1.3\n");
457       htmlSvg.append(" * @requires jQuery v1.2 or later\n");
458       htmlSvg.append(" * @homepage https://github.com/defunkt/facebox\n");
459       htmlSvg.append(" * Licensed under the MIT:\n");
460       htmlSvg.append(" *   http://www.opensource.org/licenses/mit-license.php\n");
461       htmlSvg.append(" * Copyright Forever Chris Wanstrath, Kyle Neath\n");
462       htmlSvg.append(" * Usage:\n");
463       htmlSvg.append(" *  jQuery(document).ready(function() {\n");
464       htmlSvg.append(" *    jQuery('a[rel*=facebox]').facebox()\n");
465       htmlSvg.append(" *  })\n");
466       htmlSvg.append(" *  <a href=\"#terms\" rel=\"facebox\">Terms</a>\n");
467       htmlSvg.append(" *    Loads the #terms div in the box\n");
468       htmlSvg.append(" *  <a href=\"terms.html\" rel=\"facebox\">Terms</a>\n");
469       htmlSvg.append(" *    Loads the terms.html page in the box\n");
470       htmlSvg.append(" *  <a href=\"terms.png\" rel=\"facebox\">Terms</a>\n");
471       htmlSvg.append(" *    Loads the terms.png image in the box\n");
472       htmlSvg.append(" *  You can also use it programmatically:\n");
473       htmlSvg.append(" *    jQuery.facebox('some html')\n");
474       htmlSvg.append(" *    jQuery.facebox('some html', 'my-groovy-style')\n");
475       htmlSvg.append(" *  The above will open a facebox with \"some html\" as the content.\n");
476       htmlSvg.append(" *    jQuery.facebox(function($) {\n");
477       htmlSvg.append(" *      $.get('blah.html', function(data) { $.facebox(data) })\n");
478       htmlSvg.append(" *    })\n");
479       htmlSvg.append(" *  The above will show a loading screen before the passed function is called,\n");
480       htmlSvg.append(" *  allowing for a better ajaxy experience.\n");
481       htmlSvg.append(" *  The facebox function can also display an ajax page, an image, or the contents of a div:\n");
482       htmlSvg.append(" *    jQuery.facebox({ ajax: 'remote.html' })\n");
483       htmlSvg.append(" *    jQuery.facebox({ ajax: 'remote.html' }, 'my-groovy-style')\n");
484       htmlSvg.append(" *    jQuery.facebox({ image: 'stairs.jpg' })\n");
485       htmlSvg.append(" *    jQuery.facebox({ image: 'stairs.jpg' }, 'my-groovy-style')\n");
486       htmlSvg.append(" *    jQuery.facebox({ div: '#box' })\n");
487       htmlSvg.append(" *    jQuery.facebox({ div: '#box' }, 'my-groovy-style')\n");
488       htmlSvg.append(" *    Want to close the facebox?  Trigger the 'close.facebox' document event:\n");
489       htmlSvg.append(" *    jQuery(document).trigger('close.facebox')\n");
490       htmlSvg.append(" *  Facebox also has a bunch of other hooks:\n");
491       htmlSvg.append(" *    loading.facebox\n");
492       htmlSvg.append(" *    beforeReveal.facebox\n");
493       htmlSvg.append(" *    reveal.facebox (aliased as 'afterReveal.facebox')\n");
494       htmlSvg.append(" *    init.facebox\n");
495       htmlSvg.append(" *    afterClose.facebox\n");
496       htmlSvg.append(" *  Simply bind a function to any of these hooks:\n");
497       htmlSvg.append(" *   $(document).bind('reveal.facebox', function() { ...stuff to do after the facebox and contents are revealed... })\n");
498       htmlSvg.append(" *\n");
499       htmlSvg.append(" */\n");
500       htmlSvg.append("(function($) {\n");
501       htmlSvg.append("  $.facebox = function(data, klass) {\n");
502       htmlSvg.append("    $.facebox.loading()\n");
503       htmlSvg.append("    if (data.ajax) fillFaceboxFromAjax(data.ajax, klass)\n");
504       htmlSvg.append("    else if (data.image) fillFaceboxFromImage(data.image, klass)\n");
505       htmlSvg.append("    else if (data.div) fillFaceboxFromHref(data.div, klass)\n");
506       htmlSvg.append("    else if ($.isFunction(data)) data.call($)\n");
507       htmlSvg.append("    else $.facebox.reveal(data, klass)\n");
508       htmlSvg.append("  }\n");
509
510       htmlSvg.append("  $.extend($.facebox, {\n");
511       htmlSvg.append("    settings: {\n");
512       htmlSvg.append("      opacity      : 0.2,\n");
513       htmlSvg.append("      overlay      : true,\n");
514       htmlSvg.append("      loadingImage : 'https://raw.githubusercontent.com/jalview/biojson/gh-pages/images/loading.gif',\n");
515       htmlSvg.append("      closeImage   : 'https://raw.githubusercontent.com/jalview/biojson/gh-pages/images/cancel.png',\n");
516       htmlSvg.append("      imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],\n");
517       htmlSvg.append("      faceboxHtml  : '<div  id=\"facebox\" style=\"display:none; width: 95%; height: 85%; overflow: auto;\"> ");
518       htmlSvg.append("      <div class=\"popup\"> ");
519       htmlSvg.append("        <div class=\"content\"> ");
520       htmlSvg.append("        </div> ");
521       htmlSvg.append("        <a href=\"#\" class=\"close\"></a> ");
522       htmlSvg.append("      </div> ");
523       htmlSvg.append("    </div>'\n");
524       htmlSvg.append("    },      \n");
525       htmlSvg.append("    loading: function() {\n");
526       htmlSvg.append("      init()\n");
527       htmlSvg.append("      if ($('#facebox .loading').length == 1) return true\n");
528       htmlSvg.append("      showOverlay()      \n");
529       htmlSvg.append("      $('#facebox .content').empty().\n");
530       htmlSvg.append("        append('<div class=\"loading\"><img src=\"'+$.facebox.settings.loadingImage+'\"/></div>')\n");
531       htmlSvg.append("      $('#facebox').show().css({\n");
532       htmlSvg.append("        top:    getPageScroll()[1] + (getPageHeight() / 10),\n");
533       htmlSvg.append("        left:    $(window).width() / 2 - ($('#facebox .popup').outerWidth() / 2)\n");
534       htmlSvg.append("      })      \n");
535       htmlSvg.append("      $(document).bind('keydown.facebox', function(e) {\n");
536       htmlSvg.append("       if (e.keyCode == 27) $.facebox.close()\n");
537       htmlSvg.append("        return true\n");
538       htmlSvg.append("      })\n");
539       htmlSvg.append("      $(document).trigger('loading.facebox')\n");
540       htmlSvg.append("    },\n");
541       htmlSvg.append("    reveal: function(data, klass) {\n");
542       htmlSvg.append("      $(document).trigger('beforeReveal.facebox')\n");
543       htmlSvg.append("      if (klass) $('#facebox .content').addClass(klass)\n");
544       htmlSvg.append("      $('#facebox .content').empty().append('<pre><code>'+JSON.stringify(JSON.parse(data),null,4)+'</pre></code>')\n");
545       htmlSvg.append("      $('#facebox .popup').children().fadeIn('normal')\n");
546       htmlSvg.append("      $('#facebox').css('left', $(window).width() / 2 - ($('#facebox .popup').outerWidth() / 2))\n");
547       htmlSvg.append("      $(document).trigger('reveal.facebox').trigger('afterReveal.facebox')\n");
548       htmlSvg.append("    },      \n");
549       htmlSvg.append("    close: function() {\n");
550       htmlSvg.append("      $(document).trigger('close.facebox')\n");
551       htmlSvg.append("      return false\n");
552       htmlSvg.append("    }\n");
553       htmlSvg.append("  })\n");
554       htmlSvg.append("  $.fn.facebox = function(settings) {\n");
555       htmlSvg.append("    if ($(this).length == 0) return    \n");
556       htmlSvg.append("    init(settings)      \n");
557       htmlSvg.append("    function clickHandler() {\n");
558       htmlSvg.append("      $.facebox.loading(true)      \n");
559       htmlSvg.append("      // support for rel=\"facebox.inline_popup\" syntax, to add a class\n");
560       htmlSvg.append("      // also supports deprecated \"facebox[.inline_popup]\" syntax\n");
561       htmlSvg.append("      var klass = this.rel.match(/facebox\\[?\\.(\\w+)\\]?/)\n");
562       htmlSvg.append("      if (klass) klass = klass[1]\n");
563       htmlSvg.append("      fillFaceboxFromHref(this.href, klass)\n");
564       htmlSvg.append("      return false\n");
565       htmlSvg.append("    }      \n");
566       htmlSvg.append("    return this.bind('click.facebox', clickHandler)\n");
567       htmlSvg.append("  }\n");
568       htmlSvg.append("  // called one time to setup facebox on this page\n");
569       htmlSvg.append("  function init(settings) {\n");
570       htmlSvg.append("    if ($.facebox.settings.inited) return true\n");
571       htmlSvg.append("    else $.facebox.settings.inited = true\n");
572       htmlSvg.append("    $(document).trigger('init.facebox')\n");
573       htmlSvg.append("    makeCompatible()\n");
574       htmlSvg.append("    var imageTypes = $.facebox.settings.imageTypes.join('|')\n");
575       htmlSvg.append("    $.facebox.settings.imageTypesRegexp = new RegExp('\\\\.(' + imageTypes + ')(\\\\?.*)?$', 'i')\n");
576
577       htmlSvg.append("    if (settings) $.extend($.facebox.settings, settings)\n");
578       htmlSvg.append("    $('body').append($.facebox.settings.faceboxHtml)\n");
579
580       htmlSvg.append("    var preload = [ new Image(), new Image() ]\n");
581       htmlSvg.append("    preload[0].src = $.facebox.settings.closeImage\n");
582       htmlSvg.append("    preload[1].src = $.facebox.settings.loadingImage\n");
583
584       htmlSvg.append("    $('#facebox').find('.b:first, .bl').each(function() {\n");
585       htmlSvg.append("      preload.push(new Image())\n");
586       htmlSvg.append("      preload.slice(-1).src = $(this).css('background-image').replace(/url\\((.+)\\)/, '$1')\n");
587       htmlSvg.append("    })\n");
588
589       htmlSvg.append("    $('#facebox .close')\n");
590       htmlSvg.append("      .click($.facebox.close)\n");
591       htmlSvg.append("      .append('<img src=\"'\n");
592       htmlSvg.append("              + $.facebox.settings.closeImage\n");
593       htmlSvg.append("              + '\" class=\"close_image\" title=\"close\">')\n");
594       htmlSvg.append("  }\n");
595
596       htmlSvg.append("  // getPageScroll() by quirksmode.com\n");
597       htmlSvg.append("  function getPageScroll() {\n");
598       htmlSvg.append("    var xScroll, yScroll;\n");
599       htmlSvg.append("    if (self.pageYOffset) {\n");
600       htmlSvg.append("      yScroll = self.pageYOffset;\n");
601       htmlSvg.append("      xScroll = self.pageXOffset;\n");
602       htmlSvg.append("    } else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict\n");
603       htmlSvg.append("      yScroll = document.documentElement.scrollTop;\n");
604       htmlSvg.append("      xScroll = document.documentElement.scrollLeft;\n");
605       htmlSvg.append("    } else if (document.body) {// all other Explorers\n");
606       htmlSvg.append("      yScroll = document.body.scrollTop;\n");
607       htmlSvg.append("      xScroll = document.body.scrollLeft;\n");
608       htmlSvg.append("    }\n");
609       htmlSvg.append("    return new Array(xScroll,yScroll)\n");
610       htmlSvg.append("  }\n");
611
612       // Adapted from getPageSize() by quirksmode.com");
613       htmlSvg.append("  function getPageHeight() {\n");
614       htmlSvg.append("    var windowHeight\n");
615       htmlSvg.append("    if (self.innerHeight) {    // all except Explorer\n");
616       htmlSvg.append("      windowHeight = self.innerHeight;\n");
617       htmlSvg.append("    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode\n");
618       htmlSvg.append("      windowHeight = document.documentElement.clientHeight;\n");
619       htmlSvg.append("    } else if (document.body) { // other Explorers\n");
620       htmlSvg.append("      windowHeight = document.body.clientHeight;\n");
621       htmlSvg.append("    }\n");
622       htmlSvg.append("    return windowHeight\n");
623       htmlSvg.append("  }\n");
624
625       htmlSvg.append("  // Backwards compatibility\n");
626       htmlSvg.append("  function makeCompatible() {\n");
627       htmlSvg.append("    var $s = $.facebox.settings      \n");
628       htmlSvg.append("    $s.loadingImage = $s.loading_image || $s.loadingImage\n");
629       htmlSvg.append("    $s.closeImage = $s.close_image || $s.closeImage\n");
630       htmlSvg.append("    $s.imageTypes = $s.image_types || $s.imageTypes\n");
631       htmlSvg.append("    $s.faceboxHtml = $s.facebox_html || $s.faceboxHtml\n");
632       htmlSvg.append("  }\n");
633
634       htmlSvg.append("  // Figures out what you want to display and displays it\n");
635       htmlSvg.append("  // formats are:\n");
636       htmlSvg.append("  //     div: #id\n");
637       htmlSvg.append("  //   image: blah.extension\n");
638       htmlSvg.append("  //    ajax: anything else\n");
639       htmlSvg.append("  function fillFaceboxFromHref(href, klass) {\n");
640       htmlSvg.append("    // div\n");
641       htmlSvg.append("    if (href.match(/#/)) {\n");
642       htmlSvg.append("      var url    = window.location.href.split('#')[0]\n");
643       htmlSvg.append("      var target = href.replace(url,'')\n");
644       htmlSvg.append("      if (target == '#') return\n");
645       htmlSvg.append("      $.facebox.reveal($(target).html(), klass)\n");
646
647       htmlSvg.append("    // image\n");
648       htmlSvg.append("    } else if (href.match($.facebox.settings.imageTypesRegexp)) {\n");
649       htmlSvg.append("      fillFaceboxFromImage(href, klass)\n");
650       htmlSvg.append("    // ajax\n");
651       htmlSvg.append("    } else {\n");
652       htmlSvg.append("      fillFaceboxFromAjax(href, klass)\n");
653       htmlSvg.append("    }\n");
654       htmlSvg.append("  }\n");
655
656       htmlSvg.append("  function fillFaceboxFromImage(href, klass) {\n");
657       htmlSvg.append("    var image = new Image()\n");
658       htmlSvg.append("    image.onload = function() {\n");
659       htmlSvg.append("      $.facebox.reveal('<div class=\"image\"><img src=\"' + image.src + '\" /></div>', klass)\n");
660       htmlSvg.append("    }\n");
661       htmlSvg.append("    image.src = href\n");
662       htmlSvg.append("   }\n");
663
664       htmlSvg.append("  function fillFaceboxFromAjax(href, klass) {\n");
665       htmlSvg.append("    $.facebox.jqxhr = $.get(href, function(data) { $.facebox.reveal(data, klass) })\n");
666       htmlSvg.append("  }\n");
667
668       htmlSvg.append("  function skipOverlay() {\n");
669       htmlSvg.append("    return $.facebox.settings.overlay == false || $.facebox.settings.opacity === null\n");
670       htmlSvg.append("  }\n");
671
672       htmlSvg.append("  function showOverlay() {\n");
673       htmlSvg.append("    if (skipOverlay()) return\n");
674
675       htmlSvg.append("    if ($('#facebox_overlay').length == 0)\n");
676       htmlSvg.append("      $(\"body\").append('<div id=\"facebox_overlay\" class=\"facebox_hide\"></div>')\n");
677
678       htmlSvg.append("    $('#facebox_overlay').hide().addClass(\"facebox_overlayBG\")\n");
679       htmlSvg.append("      .css('opacity', $.facebox.settings.opacity)\n");
680       htmlSvg.append("      .click(function() { $(document).trigger('close.facebox') })\n");
681       htmlSvg.append("       .fadeIn(200)\n");
682       htmlSvg.append("    return false\n");
683       htmlSvg.append("  }\n");
684
685       htmlSvg.append("  function hideOverlay() {\n");
686       htmlSvg.append("    if (skipOverlay()) return      \n");
687       htmlSvg.append("    $('#facebox_overlay').fadeOut(200, function(){\n");
688       htmlSvg.append("      $(\"#facebox_overlay\").removeClass(\"facebox_overlayBG\")\n");
689       htmlSvg.append("      $(\"#facebox_overlay\").addClass(\"facebox_hide\")\n");
690       htmlSvg.append("      $(\"#facebox_overlay\").remove()\n");
691       htmlSvg.append("    })      \n");
692       htmlSvg.append("    return false\n");
693       htmlSvg.append("  }\n");
694
695       htmlSvg.append("  $(document).bind('close.facebox', function() {\n");
696       htmlSvg.append("    if ($.facebox.jqxhr) {\n");
697       htmlSvg.append("      $.facebox.jqxhr.abort()\n");
698       htmlSvg.append("      $.facebox.jqxhr = null\n");
699       htmlSvg.append("    }\n");
700       htmlSvg.append("    $(document).unbind('keydown.facebox')\n");
701       htmlSvg.append("    $('#facebox').fadeOut(function() {\n");
702       htmlSvg.append("      $('#facebox .content').removeClass().addClass('content')\n");
703       htmlSvg.append("      $('#facebox .loading').remove()\n");
704       htmlSvg.append("      $(document).trigger('afterClose.facebox')\n");
705       htmlSvg.append("    })\n");
706       htmlSvg.append("    hideOverlay()\n");
707       htmlSvg.append("  })\n");
708
709       htmlSvg.append("})(jQuery);\n");
710
711     }
712
713     htmlSvg.append("</script>\n");
714     htmlSvg.append("</html>");
715     return htmlSvg.toString();
716   }
717 }