view app/assets/javascripts/init.tabs.js @ 131:7c7601575358

Moved $('#markdown div') to variable.
author Edho Arief <edho@myconan.net>
date Mon, 12 Nov 2012 17:27:14 +0700
parents 64523785ad24
children e05e47f7d224
line wrap: on
line source

$(document).ready(function() {
  if ($('#paste-show').length !== 1) {
    return;
  }
  var raw = $('#plain pre').text(),
    markdown = $('#markdown div');

  markdown.html(marked(raw));
  markdown.find('pre').addClass('prettyprint');
  markdown.find('a').prop('rel', 'nofollow');

  $('#highlight pre').text(raw);

  window.prettyPrint && prettyPrint();

  update_state = function() {
    var paste = $('#paste'),
      mode = paste.data('mode'),
      width = paste.data('width'),
      qstring = '#!' + mode + '.' + width;
    if (width == 'auto') {
      qstring = '#!';
      if (mode != 'plain') {
        qstring += mode;
      }
    }
    window.location = qstring;
  };

  paste_resize = function(size) {
    var paste = $('#paste'),
      title = $('#paste-resize a.btn');
    title.html(title.html().replace(/(auto|\d+px)/, size));
    if (size == 'auto') {
      paste.css('width', '');
      paste.data('width', size);
    } else {
      paste.css('width', size);
      paste.data('width', size);
    }
    update_state();
  }

  $('#paste-mode a').click(function() {
    $('#paste').data('mode', $(this).prop('href').split('#')[1]);
    update_state();
  });
  $('#paste-resize ul a').click(function() {
    paste_resize($(this).data('size'));
    $('.open').removeClass('open');
    return false;
  });

  mode = window.location.hash.substr(2).split('.');
  if (mode[0] == 'highlight') { $('#paste-mode a:eq(1)').click(); }
  else if (mode[0] == 'markdown') { $('#paste-mode a:eq(2)').click(); };
  if (mode[1] && mode[1].match(/^\d+px$/)) { paste_resize(mode[1]); }
});