cube.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ( function( $, undefined ) {
  2. 'use strict';
  3. $(function() {
  4. var cube_found = 'cubeportfolio' in $.fn;
  5. var cube_loading = false;
  6. var win = $(window);
  7. var cube_narrow = function( el ) {
  8. requestAnimationFrame( function() {
  9. var inner = el.find( '.cbp-wrapper' );
  10. var outer = el.find( '.cbp-wrapper-outer' );
  11. if ( inner.width() <= outer.width() ) {
  12. el.addClass( 'vamtam-cube-narrow' );
  13. } else {
  14. el.removeClass( 'vamtam-cube-narrow' );
  15. }
  16. } );
  17. };
  18. var attempt_cube_load_callback = function() {
  19. $( '.vamtam-cubeportfolio[data-options]:not(.vamtam-cube-loaded)' ).filter( ':visible' ).each( function() {
  20. var self = $( this );
  21. var options = self.data( 'options' );
  22. if ( 'singlePageCallback' in options ) {
  23. options.singlePageCallback = cube_single_page[ options.singlePageCallback ];
  24. }
  25. self.on( 'initComplete.cbp', function() {
  26. if ( 'slider' === options.layoutMode ) {
  27. cube_narrow( self );
  28. win.on( 'resize.vamtamcube', function() {
  29. cube_narrow( self );
  30. } );
  31. }
  32. } );
  33. self.addClass( 'vamtam-cube-loaded' ).cubeportfolio( options );
  34. self.on( 'vamtam-video-resized', 'iframe, object, embed, video', function() {
  35. self.data('cubeportfolio').layoutAndAdjustment();
  36. } );
  37. this.addEventListener( 'vamtamlazyloaded', function() {
  38. self.data('cubeportfolio').layoutAndAdjustment();
  39. } );
  40. } );
  41. };
  42. // if there are cube instances, check that the script is loaded
  43. // otherwise - load it and prevent further calls to attempt_cube_load
  44. // until cube is available
  45. var attempt_cube_load = function() {
  46. if ( document.getElementsByClassName( 'vamtam-cubeportfolio' ).length ) {
  47. if ( cube_found ) {
  48. attempt_cube_load_callback();
  49. } else if ( ! cube_loading ) {
  50. cube_loading = true;
  51. var s = document.createElement('script');
  52. s.type = 'text/javascript';
  53. s.async = true;
  54. s.src = VAMTAM_FRONT.cube_path;
  55. s.onload = function() {
  56. cube_found = 'cubeportfolio' in $.fn;
  57. attempt_cube_load_callback();
  58. };
  59. document.getElementsByTagName('script')[0].before( s );
  60. }
  61. }
  62. };
  63. var cube_single_page = {
  64. portfolio: function( url ) {
  65. var t = this;
  66. $.ajax({
  67. url: url,
  68. type: 'GET',
  69. dataType: 'html'
  70. })
  71. .done(function(result) {
  72. t.updateSinglePage(result);
  73. attempt_cube_load();
  74. $( document ).trigger( 'vamtam-single-page-project-loaded' );
  75. })
  76. .fail(function() {
  77. t.updateSinglePage('AJAX Error! Please refresh the page!');
  78. });
  79. }
  80. };
  81. $( document ).bind( 'vamtam-attempt-cube-load', attempt_cube_load );
  82. attempt_cube_load();
  83. window.addEventListener( 'resize', window.VAMTAM.debounce( attempt_cube_load, 100 ), false );
  84. });
  85. } )( jQuery );