1 | $(function(){
|
---|
2 |
|
---|
3 | // =================================
|
---|
4 | // init SHJS (syntax highlither)
|
---|
5 | // =================================
|
---|
6 | window.sh_highlightDocument();
|
---|
7 | $('pre.sh_sourceCode').parent()
|
---|
8 | .hover(function(){
|
---|
9 | var $div = $(this);
|
---|
10 | if ($div.data('hideTimeout')) {
|
---|
11 | clearTimeout($div.data('hideTimeout'));
|
---|
12 | $div.data('hideTimeout', 0);
|
---|
13 | }
|
---|
14 | if (!$div.data('active')) {
|
---|
15 | $div.data('showTimeout',
|
---|
16 | setTimeout(function(){
|
---|
17 | var $pre = $div.children().eq(-1),
|
---|
18 | scrollBarFix = 30,
|
---|
19 | width = $pre[0].scrollWidth > $pre.outerWidth() ?
|
---|
20 | Math.min($pre[0].scrollWidth + scrollBarFix, $(window).width() - scrollBarFix - $div.offset().left) :
|
---|
21 | $pre.outerWidth(),
|
---|
22 | height = document.defaultView && document.defaultView.getComputedStyle && parseFloat(document.defaultView.getComputedStyle($div[0], null).height) || $div.height();
|
---|
23 | $div.wrap('<div class="code-placeholder" style="position:relative;height:' + (height + parseInt($div.css('padding-top')) + parseInt($div.css('padding-bottom'))) + 'px;margin:' + $div.css('margin-top') + ' ' + $div.css('margin-right') + ' ' + $div.css('margin-bottom') + ' ' + $div.css('margin-left') + ';"></div>')
|
---|
24 | .css({position: 'absolute', width: width, height: height}).data('active', true)
|
---|
25 | .find('> a').fadeIn(500);
|
---|
26 | }, 500)
|
---|
27 | );
|
---|
28 | }
|
---|
29 | }, function(){
|
---|
30 | var $div = $(this);
|
---|
31 | if ($div.data('showTimeout')) {
|
---|
32 | clearTimeout($div.data('showTimeout'));
|
---|
33 | $div.data('showTimeout', 0);
|
---|
34 | }
|
---|
35 | if ($div.data('active') && !$div.data('plainView')) {
|
---|
36 | $div.data('hideTimeout',
|
---|
37 | setTimeout(function(){
|
---|
38 | $div.unwrap().css({position: 'relative', width: '', height: ''}).data('active', false)
|
---|
39 | .find('> a').hide();
|
---|
40 | }, 500)
|
---|
41 | );
|
---|
42 | }
|
---|
43 | })
|
---|
44 | .prepend('<a/>').find('> a').addClass('view-plain').attr('href', '#').text('View plain code')
|
---|
45 | .click(function(){
|
---|
46 | var $link = $(this),
|
---|
47 | $div = $link.parent(),
|
---|
48 | $pre = $link.next(),
|
---|
49 | $window = $(window),
|
---|
50 | scrollPos = [$window.scrollLeft(), $window.scrollTop()],
|
---|
51 | value = $pre.text(),
|
---|
52 | height = $pre.outerHeight() + (window.opera && window.opera.version && window.opera.version() < 10.5 ? $pre[0].scrollHeight / ((value.replace(/\r\n/g, '\n').match(/\n/g) || '').length + 1) : 0);
|
---|
53 | $('<textarea wrap="off"/>').css({width: $div.width(), height: height, '-moz-box-shadow': '0 5px 15px #888', '-webkit-box-shadow': '0 5px 15px #888', 'box-shadow': '0 5px 15px #888'})
|
---|
54 | .val(value).attr('readonly', 'readonly').insertBefore($pre).blur(function(){
|
---|
55 | $(this).next()/* pre */.css('visibility', 'inherit').prev()/* textarea */.remove();
|
---|
56 | $div.data('plainView', false).mouseleave();
|
---|
57 | });
|
---|
58 | $link.hide().next().next()/* pre */.css('visibility', 'hidden').prev()/* textarea */.focus();
|
---|
59 | // some browsers (like IE < 8) may mess up the scroll position when the textarea is focused
|
---|
60 | if (window.scrollTo)
|
---|
61 | window.scrollTo(scrollPos[0], scrollPos[1]);
|
---|
62 | $div.data('plainView', true);
|
---|
63 | return false;
|
---|
64 | });
|
---|
65 |
|
---|
66 | });
|
---|