Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fredrik Esseen 610 posts 906 karma points
    Aug 26, 2010 @ 13:09
    Fredrik Esseen
    0

    Attach fancybox to images in bodyText with xslt

    Hi!

    This is the scenario: A user adds an image with the RixhTextEditor and hit publish.

    I would like the image to be clickable to show a bigger version of it with fancybox. I was thinking that i could attach this in xslt by looping the content somehow?

     

  • Kasper Dyrvig 246 posts 379 karma points
    Aug 26, 2010 @ 14:04
    Kasper Dyrvig
    0

    I'm not completely sure how this would work, but probably you could do it with

    <xsl:value-of select="umbraco.library:Replace(bodyText, '&lt;img ', '&lt;img rel=&quot;fancybox&quot; ')"/>

    The best would be to control the situation a bit more. Fx add a mediapicker-datatype (set it to advanced) or the upload-datatype.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 26, 2010 @ 22:22
    Jan Skovgaard
    0

    Hi froad

    For it to work you need to add an anchor around the img...

    I don't think that XSLT will be the most proper way to achieve this. Since users with JavaScript disabled won't benefit from the gallery anyway it should be fairly easy to achive this using Jquery.

    I don't know if you have a class or an id for the content in your webpage like

     

    RTE content goes here...

     

    But asuming that you do I think you should be able to write something like this...(from the top of my head).

    $('#content img').each(function(){
    var imgSrc = $(this).attr('src').text();
    $('').insertBefore($this);
    $('').insertAfter($this);
    });


    I'm not sure this will work straight away, probably some typos in there but I hope you get the idea? :-)

    /Jan

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 26, 2010 @ 22:28
    Jan Skovgaard
    0

    Argh...the code formatting was messed up...

    The code

    $('#content img').each(function(){
    var imgSrc = $(this).attr('src').text();
    $('<a rel="fancybox" href="/forum/'&amp; imgSrc &amp;'">').insertBefore($this);</a>
    <a rel="fancybox" href="/forum/&amp; imgSrc &amp;">$('</a>').insertAfter($this);
    });

    /Jan

  • Fredrik Esseen 610 posts 906 karma points
    Aug 27, 2010 @ 08:18
    Fredrik Esseen
    0

    Thx!

    One more thing... I would like to show the small image as clickable but a bigger version when clicked; image_400.jpg.

    Im no js-guru, could the imagename be splitted like we do in xslt to get the bigger image..?

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 27, 2010 @ 11:15
    Jan Skovgaard
    0

    Yes that should be possible. If you know that all images are fixed with the ending "_400.jpg

    You should be able to do something like this...

    $('#content img').each(function(){
    var imgSrc = $(this).attr('src').text();
    var imgName = imgSrc.split('.');
    $('<a rel="fancybox" href="'&amp; imgName[0] &amp;'_400.jpg">').insertBefore($this);
    <a rel="fancybox" href="/forum/&amp; imgSrc &amp;">$('</a>').insertAfter($this);
    });

    Hope this helps...

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 27, 2010 @ 11:17
    Jan Skovgaard
    0

    Argh...really having trouble with the formatting of the code. THIS should be correct without any typos etc.

    $('#content img').each(function(){
    var imgSrc = $(this).attr('src').text();
    var imgName = imgSrc.split('.');
    $('<a rel="fancybox" href="'&amp; imgName[0] &amp;'_400.jpg">').insertBefore($this);
    $('</a>').insertAfter($this);
    });

  • Fredrik Esseen 610 posts 906 karma points
    Aug 27, 2010 @ 11:38
    Fredrik Esseen
    0

    Thx, Jan.

    I reeeeealy like you :)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 27, 2010 @ 11:41
    Jan Skovgaard
    0

    You're welcome. Hope that it works...otherwise let me know and we will get it to work ;-)

    /Jan

  • Fredrik Esseen 610 posts 906 karma points
    Aug 30, 2010 @ 14:10
    Fredrik Esseen
    0

    Theres something fishy with this line:

    $(

     

    '<a rel="fancybox" href="'&amp; imgName[0] &amp;'_500.jpg">').insertBefore($this); 

     

    I cant find the error, can you?

  • Rik Helsen 670 posts 873 karma points
    Aug 30, 2010 @ 14:44
    Rik Helsen
    0

    We use macro's for this, we have created 2 XSLT files:

    * one that expects you to select an image in the media section, and you get a input dialog for desired width & height of the thumbnail

    * one that expects a folder in the mediaselection to be selected (contentpicker) and loops through all the images below it (creating a mini-gallery of thumbnails)

    We automatically create resizes of all the images so we don't have people download large images if they don't have to.

     

     

  • Rik Helsen 670 posts 873 karma points
    Aug 30, 2010 @ 14:45
    Rik Helsen
    0

    A walkthrough (this was made for 4.0.3 - DOES NOT WORK FOR 4.5):

    1. install Fancybox (http://fancybox.net) and the ImageGen package for Umbraco
     
    2. include references in the masterpage head section:
     
    <link href="/css/fancybox/jquery.fancybox-1.3.0.css" media="screen" type="text/css" rel="stylesheet"/>
     <script type="text/javascript" src="/scripts/jquery-1.3.2.min.js"></script>
     <script type="text/javascript" src="/scripts/jquery.fancybox-1.3.0.pack.js"></script>
     <script type="text/javascript" src="/scripts/jquery.easing.1.3.js"></script>

    <script type="text/javascript">
    $(document).ready(function() {    
    $("a.fancybox").fancybox({
      'titlePosition'  : 'over'
     });
    });
     
     
    3. create a new XSLT file "LightboxGallery:
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform
     xmlns:msxml="urn:schemas-microsoft-com:xslt"
     xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
    <xsl:variable name="bigW" select="700"/>
    <xsl:variable name="bigH" select="700"/>
    <xsl:template match="/">
    <xsl:variable name="imageFolder" select="/macro/imageFolder/node/@id"/>
    <xsl:if test="$imageFolder&gt; 0">
      <xsl:variable name="images" select="umbraco.library:GetMedia($imageFolder, 1)" />
      <xsl:if test="count($images/node) &gt; 0">
        <xsl:for-each select="$images/node">

            <xsl:if test="./data[@alias = 'umbracoExtension'] = 'gif' or ./data[@alias = 'umbracoExtension'] = 'jpg' or ./data[@alias = 'umbracoExtension'] = 'jpeg' or ./data[@alias = 'umbracoExtension'] = 'png'">
              <a title="{@ImgDescription}">
         <xsl:attribute name= "class">fancybox gallery</xsl:attribute>
         <xsl:attribute name= "rel">group</xsl:attribute>
      <xsl:choose>
       <xsl:when test="./data[@alias = 'umbracoWidth'] &gt; ./data[@alias = 'umbracoHeight']">
       <xsl:attribute name= "href">/umbraco/imageGen.aspx?image=<xsl:value-of select="./data[@alias = 'umbracoFile']" />&amp;width=<xsl:value-of select="$bigW"/>&amp;constrain=true</xsl:attribute>
       </xsl:when>
       <xsl:otherwise>
       <xsl:attribute name= "href">/umbraco/imageGen.aspx?image=<xsl:value-of select="./data[@alias = 'umbracoFile']" />&amp;height=<xsl:value-of select="$bigH"/>&amp;constrain=true</xsl:attribute>
       </xsl:otherwise>
      </xsl:choose>
                <img src="/umbraco/ImageGen.ashx?image={./data[@alias = 'umbracoFile']}&amp;width=80&amp;height=80" alt="{./data [@alias = 'ImgDescription']}" title="{./data [@alias = 'ImgDescription']}"/>
              </a>
            </xsl:if>

        </xsl:for-each>
      </xsl:if>
    </xsl:if>

    </xsl:template>
    </xsl:stylesheet>
     
    4. in the macro, add a variable with alias and name "imageFolder" and type "mediaCurrent"

     

  • Fredrik Esseen 610 posts 906 karma points
    Aug 31, 2010 @ 16:39
    Fredrik Esseen
    0

    The solution with macro is ok, but then you miss the preview of the image and the layout in the editor gets messed up.

    Im trying the javascript solution but I still get error.

    <script type="text/javascript"> 
    $('#content img').each(function(){
    var imgSrc = $(this).attr('src').text();
    var imgName = imgSrc.split('.');
    var imgName2 = imgName + '_500.jpg';
    $('<a rel="fancybox" href="' + imgName2 + '">').insertBefore($this);
    $('</a>').insertAfter($this);
    });
    </script>

    Ive added this to the bottom of my document. The browser complains on the 3rd row: "Object doesn't support this property or method"

  • Rik Helsen 670 posts 873 karma points
    Sep 01, 2010 @ 09:14
    Rik Helsen
    0

    keep in mind that

    * the way you suggest doesn't allow you to put hyperlinks on images (say you have a page with some logo's of partner websites in de page content area
    * you can't deny specific images to have a popup either

    If you're working for a customer, this could become an issue (depends on how and where you use this javascript...

    As for your script, don't you need to include this?:

    1. $(document).ready(function() {
    2.     // put all your jQuery goodness in here.
    3. });
  • Rik Helsen 670 posts 873 karma points
    Sep 01, 2010 @ 09:16
    Rik Helsen
    0

    Also get rid of the .txt() after src

  • Fredrik Esseen 610 posts 906 karma points
    Sep 01, 2010 @ 11:54
    Fredrik Esseen
    0

    Thx!

    What do you mean with it doesnt allow to put hyperlink to an image?

    I finalyy made the code not to produce an error but the insertbefore and after doesnt seem to work:

    <script type="text/javascript"> 
    $(document).ready(function() {
    $('#content img').each(function(){
    alert(this);
    var imgSrc = $(this).attr('src');
    var imgName = imgSrc.split('.');
    var imgName2 = imgName + '_500.jpg';
    $('<a rel="fancybox" href="' + imgName2 + '">').insertBefore(this);
    $('</a>').insertAfter(this);
    });
    });
    </script>
  • Rik Helsen 670 posts 873 karma points
    Sep 01, 2010 @ 14:23
    Rik Helsen
    0

    >> "What do you mean with it doesnt allow to put hyperlink to an image?"

    You can't put a logo in the page content area and have it link to an external/internal page or website

  • Fredrik Esseen 610 posts 906 karma points
    Sep 29, 2010 @ 16:36
    Fredrik Esseen
    0

    Im still wrestling with this problem.

    Now I at least get some rendering with this code:

    $('.ContentPane img').each(function(){
    var imgSrc = $(this).attr('src');
    var imgName = imgSrc.split('.');
    var imgName2 = imgName + '_500.jpg';
    $('<a class="group" href="' + imgName2 + '">').insertBefore($(this));
    $('</a>').insertAfter($(this));
    });

    The problem is that the .insertAfter doesnt work as I want. The output looks like this:

    <a href="/media/244/Exbild,jpg_500.jpg" class="group"></a>
    <img height="247" width="248" alt="Exbild.jpg" src="/media/244/Exbild.jpg">

    As you see the </a> is not inserted after.

    Why?

  • Fredrik Esseen 610 posts 906 karma points
    Sep 30, 2010 @ 11:21
    Fredrik Esseen
    0

    Finally solved it:

    $('.ContentPane img').each(function(){
    var imgSrc = $(this).attr('src');
    var imgName = imgSrc.split('.');
    var imgName2 = imgName + '_500.jpg';
    var str = '<a class="group" href="' + imgName2 + '">' + '<img src="' + imgSrc + '"/>' + '</a>';
    $(this).replaceWith(str);
    });
Please Sign in or register to post replies

Write your reply to:

Draft