_viewsource.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //quick view source in new window links
  2. $.fn.addSourceLink = function(style){
  3. return $(this).each(function(){
  4. var link = $('<a href="#" data-'+ $.mobile.ns +'inline="true">View Source</a>'),
  5. src = src = $('<div></div>').append( $(this).clone() ).html(),
  6. page = $( "<div data-"+ $.mobile.ns +"role='dialog' data-"+ $.mobile.ns +"theme='a'>" +
  7. "<div data-"+ $.mobile.ns +"role='header' data-"+ $.mobile.ns +"theme='b'>" +
  8. "<a href='#' class='ui-btn-left' data-"+ $.mobile.ns +"icon='delete' data-"+ $.mobile.ns +"iconpos='notext'>Close</a>"+
  9. "<div class='ui-title'>jQuery Mobile Source Excerpt</div>"+
  10. "</div>"+
  11. "<div data-"+ $.mobile.ns +"role='content'></div>"+
  12. "</div>" )
  13. .appendTo( "body" )
  14. .page();
  15. $('<a href="#">View Source</a>')
  16. .buttonMarkup({
  17. icon: 'arrow-u',
  18. iconpos: 'notext'
  19. })
  20. .click(function(){
  21. var codeblock = $('<pre><code></code></pre>');
  22. src = src.replace(/&/gmi, '&amp;').replace(/"/gmi, '&quot;').replace(/>/gmi, '&gt;').replace(/</gmi, '&lt;').replace('data-'+ $.mobile.ns +'source="true"','');
  23. codeblock.find('code').append(src);
  24. var activePage = $(this).parents('.ui-page-active');
  25. page.find('.ui-content').append(codeblock);
  26. $.changePage(page, 'slideup',false);
  27. page.find('.ui-btn-left').click(function(){
  28. $.changePage(activepage, 'slideup',true);
  29. return false;
  30. });
  31. })
  32. .insertAfter(this);
  33. });
  34. };
  35. //set up view source links
  36. $('div').live('pagebeforecreate',function(){
  37. $(this).find('[data-'+ $.mobile.ns +'source="true"]').addSourceLink();
  38. });