demo.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*global SyntaxHighlighter*/
  2. SyntaxHighlighter.config.tagName = 'code';
  3. $(document).ready( function () {
  4. if ( ! $.fn.dataTable ) {
  5. return;
  6. }
  7. var dt110 = $.fn.dataTable.Api ? true : false;
  8. // Work around for WebKit bug 55740
  9. var info = $('div.info');
  10. if ( info.height() < 115 ) {
  11. info.css( 'min-height', '8em' );
  12. }
  13. var escapeHtml = function ( str ) {
  14. return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  15. };
  16. // css
  17. var cssContainer = $('div.tabs div.css');
  18. if ( $.trim( cssContainer.find('code').text() ) === '' ) {
  19. cssContainer.find('code, p:eq(0), div').css('display', 'none');
  20. }
  21. // init html
  22. var table = $('<p/>').append( $('table').clone() ).html();
  23. $('div.tabs div.table').append(
  24. '<code class="multiline brush: html;">\t\t\t'+
  25. escapeHtml( table )+
  26. '</code>'
  27. );
  28. //SyntaxHighlighter.highlight({}, $('#display-init-html')[0]);
  29. // Allow the demo code to run if DT 1.9 is used
  30. if ( dt110 ) {
  31. // json
  32. var ajaxTab = $('ul.tabs li').eq(3).css('display', 'none');
  33. $(document).on( 'init.dt', function ( e, settings ) {
  34. var api = new $.fn.dataTable.Api( settings );
  35. var show = function ( str ) {
  36. ajaxTab.css( 'display', 'block' );
  37. $('div.tabs div.ajax code').remove();
  38. // Old IE :-|
  39. try {
  40. str = JSON.stringify( str, null, 2 );
  41. } catch ( e ) {}
  42. $('div.tabs div.ajax').append(
  43. '<code class="multiline brush: js;">'+str+'</code>'
  44. );
  45. SyntaxHighlighter.highlight( {}, $('div.tabs div.ajax code')[0] );
  46. };
  47. // First draw
  48. var json = api.ajax.json();
  49. if ( json ) {
  50. show( json );
  51. }
  52. // Subsequent draws
  53. api.on( 'xhr.dt', function ( e, settings, json ) {
  54. show( json );
  55. } );
  56. } );
  57. // php
  58. var phpTab = $('ul.tabs li').eq(4).css('display', 'none');
  59. $(document).on( 'init.dt.demoSSP', function ( e, settings ) {
  60. if ( settings.oFeatures.bServerSide ) {
  61. if ( $.isFunction( settings.ajax ) ) {
  62. return;
  63. }
  64. $.ajax( {
  65. url: '../resources/examples.php',
  66. data: {
  67. src: settings.sAjaxSource || settings.ajax.url || settings.ajax
  68. },
  69. dataType: 'text',
  70. type: 'post',
  71. success: function ( txt ) {
  72. phpTab.css( 'display', 'block' );
  73. $('div.tabs div.php').append(
  74. '<code class="multiline brush: php;">'+txt+'</code>'
  75. );
  76. SyntaxHighlighter.highlight( {}, $('div.tabs div.php code')[0] );
  77. }
  78. } );
  79. }
  80. } );
  81. }
  82. else {
  83. $('ul.tabs li').eq(3).css('display', 'none');
  84. $('ul.tabs li').eq(4).css('display', 'none');
  85. }
  86. // Tabs
  87. $('ul.tabs').on( 'click', 'li', function () {
  88. $('ul.tabs li.active').removeClass('active');
  89. $(this).addClass('active');
  90. $('div.tabs>div')
  91. .css('display', 'none')
  92. .eq( $(this).index() ).css('display', 'block');
  93. } );
  94. $('ul.tabs li.active').click();
  95. } );