Advertisement
gaz_lloyd

Untitled

Mar 24th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $('.js-tooltip-wrapper').each(function () {
  2.     var $span,
  3.     $text,
  4.     $arrow,
  5.     $wrapper,
  6.     $close,
  7.     resizeEvent,
  8.     hasArrow,
  9.     align,
  10.     name,
  11.     size,
  12.     $currspan;
  13.    
  14.     $wrapper = $(this);
  15.     name = $wrapper.attr('data-tooltip-for');
  16.     hasArrow = $wrapper.attr('data-tooltip-arrow').toLowerCase() == 'yes';
  17.     align = $wrapper.attr('data-tooltip-align');
  18.     size = parseInt($wrapper.attr('data-tooltip-arrow-size'), 10);
  19.     if (isNaN(size)) {
  20.         size = 10;
  21.     }
  22.    
  23.     $text = $wrapper.find('.js-tooltip-text');
  24.    
  25.     $wrapper.removeClass('hidden')
  26.         .css({
  27.             position: 'absolute',
  28.             'z-index': 900001,
  29.             'max-width': '80%',
  30.             'padding': '5px',
  31.             border: '2px solid #428bb5',
  32.             background: '#fff',
  33.             'box-shadow': '5px 5px 5px -2px #333',
  34.         });
  35.    
  36.    
  37.     $span = $('span.js-tooltip-click[data-tooltip-name="' + name + '"]');
  38.     $span.removeClass('hidden')
  39.         .text('?')
  40.         .css({
  41.             color: 'white',
  42.             background: 'gray',
  43.             'border-radius': '2em',
  44.             border: '2px solid grey',
  45.             cursor: 'pointer',
  46.             padding: '0 0.3em',
  47.         })
  48.         .attr('title', 'Click for explanation, click again to close');
  49.    
  50.    
  51.    
  52.     $arrow = $('<div>');
  53.     $arrow.addClass('js-tooltip-arrow')
  54.         .css({
  55.             position: 'absolute',
  56.             top: ($wrapper.outerHeight() * 0.3) + 'px',
  57.             left: ('-' + (size+2) + 'px'), // width of arrow + width of text div border
  58.             'z-index': 900001,
  59.             width: 0,
  60.             height: 0,
  61.             'margin-top': ('-' + (size/2) + 'px'),
  62.             'border-color': 'transparent',
  63.             'border-style': 'solid',
  64.             'border-width': size + 'px', //actual width of the arrow
  65.         });
  66.    
  67.     $close = $('<button>');
  68.     $close.html('<img src="http://slot1.images.wikia.nocookie.net/__cb1458766163/common/skins/oasis/images/icon_close.png">')
  69.         .addClass('close wikia-chiclet-button')
  70.         .click(function(){
  71.             $wrapper.hide();
  72.             $currspan = null;
  73.         })
  74.         .css({
  75.             float: 'right',
  76.         });
  77.     $text.prepend($close);
  78.    
  79.     if (hasArrow) {
  80.         $wrapper.prepend($arrow);
  81.     }
  82.    
  83.     resizeEvent = function () {
  84.         if ($currspan == null) {
  85.             return;
  86.         }
  87.         var offset, width;
  88.         offset = $currspan.position();
  89.         width = $currspan.outerWidth();
  90.         $wrapper.css({
  91.                 top: (offset.top - $wrapper.outerHeight()*0.3) + 'px',
  92.         });
  93.         $arrow.css({
  94.                 top: ($wrapper.outerHeight() * 0.3) + 'px',
  95.         });
  96.        
  97.         if (offset.left > 0.6 * $('#mw-content-text').width()) {
  98.             $wrapper.css({
  99.                 right: (($('#mw-content-text').width() - offset.left) - 5 + size) + 'px',
  100.             });
  101.             $arrow.css({
  102.                 left: 'unset',
  103.                 right: ('-' + (size+2) + 'px'),
  104.                 'border-left-color': '#428bb5',
  105.                 'border-right-width': 0,
  106.                 'border-right-color': 'transparent',
  107.                 'border-left-width': size + 'px',
  108.             });
  109.         } else {
  110.             $wrapper.css({
  111.                 left: (offset.left + width - 5 + size) + 'px',
  112.             });
  113.             $arrow.css({
  114.                 right: 'unset',
  115.                 left: ('-' + (size+2) + 'px'),
  116.                 'border-right-color': '#428bb5',
  117.                 'border-left-width': 0,
  118.                 'border-left-color': 'transparent',
  119.                 'border-right-width': size + 'px',
  120.             });
  121.         }
  122.        
  123.         //$arrow.css('left', ($wrapper.outerHeight() * 0.3) + 'px');
  124.     };
  125.        
  126.     $(window).resize(resizeEvent);
  127.     $span.click(function () {
  128.         $currspan = $(this);
  129.         $wrapper.toggle();
  130.         resizeEvent();
  131.     });
  132.    
  133.     $wrapper.hide();
  134.     $span.show();
  135.     $('#WikiaArticle').css('position','unset')
  136.         .parent().css('position','relative'); //move relative up a level to allow overlap
  137.     resizeEvent();
  138. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement