pen-scripts.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**
  2. * Front-end JavaScript.
  3. *
  4. * @package Pen
  5. */
  6. ;( function( $ ) {
  7. "use strict";
  8. $( document ).ready(
  9. function() {
  10. pen_trianglify();
  11. pen_shards();
  12. var $page = $( '#page' );
  13. if ( $page.children( '.pen_wrapper' ).length ) {
  14. $page.children( '.pen_wrapper' ).addClass( 'pen_not_visible' )
  15. .end().append( '<div class="pen_loading clearfix" role="alert" style="display:none"><div class="pen_wrap_icon"><div class="pen_icon"></div></div><ul><li>' + pen_js.text.loading + '</li></ul></div>' ).children( '.pen_loading' ).show();
  16. }
  17. if ( pen_function_exists( typeof Modernizr ) && Modernizr.mq( 'only all and (max-width:728px)' ) ) {
  18. if ( pen_function_exists( typeof $( window ).fitText ) && pen_js.font_resize.site_title === 'dynamic' ) {
  19. $( '#site-title' ).fitText();
  20. }
  21. if ( pen_js.font_resize.site_title === 'resize' ) {
  22. function pen_font_resize( $element, font_size ) {
  23. var parent_width = $element.parent().outerWidth( false ),
  24. element_width = $element.css( { position: 'fixed', whiteSspace: 'nowrap' } ).outerWidth( true );
  25. $element.css( { position: 'relative' } );
  26. font_size = font_size - 2;
  27. if ( font_size > 12 && element_width > parent_width ) {
  28. $element.animate( { fontSize: font_size } );
  29. pen_font_resize( $element, font_size );
  30. }
  31. }
  32. var $site_title = $( '#pen_header h1 a .site-title' );
  33. pen_font_resize( $site_title, parseInt( $site_title.css( 'font-size' ) ) );
  34. $( window ).resize(
  35. function() {
  36. if ( pen_function_exists( typeof Modernizr ) && Modernizr.mq( 'only all and (max-width:728px)' ) ) {
  37. pen_font_resize( $site_title, parseInt( $site_title.css( 'font-size' ) ) );
  38. } else {
  39. $site_title.css( { fontSize: '' } );
  40. }
  41. }
  42. );
  43. }
  44. }
  45. if ( pen_function_exists( typeof autosize ) ) {
  46. autosize( $page.find( 'textarea' ) );
  47. }
  48. if ( $( 'div#primary-menu' ).length ) {
  49. var $menu = $( 'div#primary-menu > ul' );
  50. } else if ( $( 'ul#primary-menu' ).length ) {
  51. var $menu = $( 'ul#primary-menu' );
  52. } else {
  53. var $menu = false;
  54. }
  55. if ( $menu && pen_function_exists( typeof $( window ).superfish ) ) {
  56. $menu.superfish(
  57. {
  58. animation: pen_js.navigation_easing,
  59. speed: parseInt( pen_js.animation_navigation_speed ),
  60. cssArrows: pen_js.navigation_arrows
  61. }
  62. );
  63. }
  64. if ( pen_js.navigation_mobile && pen_function_exists( typeof $( window ).slicknav ) ) {
  65. $menu.slicknav(
  66. {
  67. label: pen_js.text.menu
  68. }
  69. );
  70. }
  71. $( '.search-form' ).on(
  72. 'submit',
  73. function( event ) {
  74. var $search = $( this );
  75. if ( $.trim( $search.find( '.search-field' ).val() ) === '' ) {
  76. alert( pen_js.text.enter_keyword );
  77. event.preventDefault();
  78. }
  79. }
  80. );
  81. if ( '.pen_options_overview' ) {
  82. $( '#primary' ).find( '.pen_options_overview' ).each(
  83. function() {
  84. var $overview = $( this ),
  85. overview_id = $overview.attr( 'id' ),
  86. toggle_id = overview_id + '_toggle';
  87. $overview.addClass( 'pen_off_screen' )
  88. .prepend( '<a href="#" class="pen_close">' + pen_js.text.close + '</a>' )
  89. .before( '<a href="#" id="' + toggle_id + '" class="pen_options_overview_toggle pen_button pen_visible" title="' + pen_js.text.overview_options_post + '">' + pen_js.text.overview_options_post + '</a>' )
  90. .find( '.pen_close' ).on(
  91. 'click',
  92. function( event ) {
  93. $( '#' + toggle_id ).toggleClass( 'pen_visible' );
  94. $overview.toggleClass( 'pen_visible' );
  95. event.preventDefault();
  96. }
  97. );
  98. $( '#' + toggle_id ).on(
  99. 'click',
  100. function( event ) {
  101. $( this ).toggleClass( 'pen_visible' );
  102. $overview.toggleClass( 'pen_visible' );
  103. event.preventDefault();
  104. }
  105. );
  106. }
  107. );
  108. }
  109. $( '#pen_back' ).hide().on(
  110. 'click',
  111. function ( event ) {
  112. $( 'html, body' ).animate( { scrollTop: 0 }, { queue: false, duration: 1000 } );
  113. event.preventDefault();
  114. }
  115. );
  116. pen_sticky_header();
  117. }
  118. );
  119. $( window ).load(
  120. function() {
  121. var $page = $( '#page' ),
  122. $main = $( '#main' );
  123. if ( $page.children( '.pen_wrapper' ).length ) {
  124. $page.children( '.pen_loading' ).fadeOut(
  125. 100,
  126. function() {
  127. $page.children( '.pen_wrapper' ).removeClass( 'pen_not_visible' );
  128. $( this ).remove();
  129. }
  130. );
  131. }
  132. var $list = $( '#pen_masonry' );
  133. if ( $list.length ) {
  134. if ( pen_js.list_type === 'masonry' && pen_function_exists( typeof jQuery( window ).masonry ) ) {
  135. $list.masonry(
  136. {
  137. itemSelector: 'article.post,article.page',
  138. percentPosition: true,
  139. transitionDuration: 0
  140. }
  141. ).imagesLoaded(
  142. function() {
  143. $list.masonry( 'layout' );
  144. pen_content_height();
  145. if ( pen_function_exists( typeof pen_animation ) ) {
  146. var $items = $main.find( 'article.post,article.page' ),
  147. $thumbnails = $main.find( '.post-thumbnail' );
  148. pen_animation( $items, pen_js.animation_list );
  149. pen_animation( $thumbnails, pen_js.animation_list_thumbnails );
  150. }
  151. }
  152. );
  153. setTimeout(
  154. function() {
  155. $list.masonry( 'layout' );
  156. pen_content_height();
  157. },
  158. 5000
  159. );
  160. }
  161. } else {
  162. if ( pen_function_exists( typeof pen_animation ) ) {
  163. var $thumbnails = $main.find( '.post-thumbnail' );
  164. if ( $( 'body' ).hasClass( 'pen_multiple' ) ) {
  165. var $items = $main.find( 'article.post,article.page' );
  166. pen_animation( $items, pen_js.animation_list );
  167. pen_animation( $thumbnails, pen_js.animation_list_thumbnails );
  168. } else {
  169. pen_animation( $main, pen_js.animation_content );
  170. pen_animation( $thumbnails, pen_js.animation_content_thumbnails );
  171. }
  172. }
  173. }
  174. if ( pen_js.animation_comments ) {
  175. var $comments = $( '#comments .comment-list' );
  176. if ( $comments.length ) {
  177. $comments.children( 'li' ).addClass( 'pen_animate_on_scroll pen_custom_animation_' + pen_js.animation_comments );
  178. }
  179. }
  180. pen_animation( $page.find( '.pen_animate_on_scroll' ), 'automatic' );
  181. pen_content_height();
  182. $page.find( '.pen_jump_menu' ).each(
  183. function() {
  184. pen_jump_menu( $( this ) );
  185. }
  186. );
  187. }
  188. );
  189. $( window ).on(
  190. 'resize orientationchange',
  191. function() {
  192. $( '#main' ).find( 'article.post.animated,article.page.animated' ).removeClass( 'animated' ).addClass( 'animated_not' );
  193. pen_content_height();
  194. }
  195. );
  196. $( window ).scroll(
  197. function() {
  198. $( '#main' ).find( 'article.post.animated_not,article.page.animated' ).removeClass( 'animated_not' ).addClass( 'animated' );
  199. if ( $( this ).scrollTop() > 400 ) {
  200. $( '#pen_back' ).fadeIn( 200 );
  201. } else {
  202. $( '#pen_back' ).fadeOut( 200 );
  203. }
  204. }
  205. );
  206. function pen_sticky_header() {
  207. if ( pen_js.header_sticky && $( '#pen_header' ).length ) {
  208. var mobile = false,
  209. $window = $( window ),
  210. $body = $( 'body' ),
  211. layout_boxed = $body.hasClass( 'pen_width_boxed' ) ? true : false,
  212. $header = $( '#pen_header' );
  213. if ( pen_function_exists( typeof Modernizr ) && Modernizr.mq( 'only all and (max-width:728px)' ) ) {
  214. mobile = true;
  215. }
  216. $window.resize(
  217. function() {
  218. if ( pen_function_exists( typeof Modernizr ) && Modernizr.mq( 'only all and (max-width:728px)' ) ) {
  219. mobile = true;
  220. } else {
  221. mobile = false;
  222. }
  223. }
  224. );
  225. $window.on(
  226. 'load scroll resize orientationchange pen_update_sticky_header',
  227. function() {
  228. if ( layout_boxed ) {
  229. $header.css( { width: $( '#pen_section' ).outerWidth( true ) } );
  230. }
  231. if ( mobile || $window.outerHeight() < $header.outerHeight( true ) ) {
  232. $header.removeClass( 'pen_header_sticked' ).css( { left: '', position: '', top: '' } );
  233. $body.css( { paddingTop: '' } );
  234. } else {
  235. var header_top = 0,
  236. header_height = $header.removeClass( 'pen_header_sticked' ).outerHeight( true );
  237. if ( $( '#wpadminbar' ).length ) {
  238. var adminBarheight = $( '#wpadminbar' ).outerHeight( true );
  239. header_height += adminBarheight;
  240. header_top += adminBarheight;
  241. }
  242. if ( $window.scrollTop() ) {
  243. var header_offset = $header.offset();
  244. $header.css( { left: header_offset.left, position: 'fixed', top: header_top } ).addClass( 'pen_header_sticked' );
  245. $body.css( { paddingTop: header_height + 20 } );
  246. } else {
  247. $header.css( { left: '', position: '', top: '' } ).removeClass( 'pen_header_sticked' );
  248. $body.css( { paddingTop: '' } );
  249. }
  250. }
  251. }
  252. );
  253. }
  254. }
  255. })( jQuery );
  256. function pen_content_height() {
  257. var leftHeight = 0,
  258. rightHeight = 0,
  259. $content = jQuery( '#content' ),
  260. $left = jQuery( '#pen_left' ),
  261. $right = jQuery( '#pen_right' );
  262. if ( $left.length ) {
  263. leftHeight = $left.outerHeight( true );
  264. }
  265. if ( $right.length ) {
  266. rightHeight = $right.outerHeight( true );
  267. }
  268. var contentHeight = Math.max( leftHeight, rightHeight );
  269. if ( contentHeight ) {
  270. contentHeight += parseInt( $content.css( 'padding-bottom' ) );
  271. if ( contentHeight > $content.outerHeight( true ) ) {
  272. $content.css( 'min-height', contentHeight + 30 );
  273. }
  274. }
  275. }
  276. function pen_animation( $items, animation ) {
  277. if ( pen_function_exists( typeof jQuery( window ).waypoint ) && animation ) {
  278. $items.addClass( 'animated' ).css( 'visibility', 'hidden' ).waypoint(
  279. {
  280. handler: function( direction ) {
  281. var $item = jQuery( this.element ),
  282. custom_animation = this.element.className.match( /(^|\s)pen_custom_animation_\S+/g );
  283. if ( custom_animation && custom_animation[0] ) {
  284. animation = jQuery.trim( custom_animation[0].replace( 'pen_custom_animation_', '' ) );
  285. }
  286. if ( ! $item.hasClass( animation ) ) {
  287. $item.addClass( animation ).css( 'visibility', 'visible' );
  288. }
  289. },
  290. offset: '90%'
  291. }
  292. );
  293. }
  294. }
  295. function pen_shards() {
  296. if ( pen_function_exists( typeof jQuery( window ).shards ) ) {
  297. var $body = jQuery( 'body' ),
  298. background_image = $body.css( 'background-image' );
  299. if ( pen_js.shards_colors && background_image && background_image === 'none' ) {
  300. $body.prepend( '<div id="shards" style="left:0;height:100%;position:fixed;top:0;visibility:hidden;width:100%;" />' );
  301. var $shards = jQuery( '#shards' );
  302. $shards.shards( pen_js.shards_colors[0], pen_js.shards_colors[1], [0,0,0,0.2], 20, .8, 2, .15, true );
  303. var background = $shards.css( 'background-image' );
  304. $shards.remove();
  305. jQuery( 'body' ).addClass( 'pen_shards' ).removeClass( 'custom-background' );
  306. jQuery( 'head' ).append( "<style type=\"text/css\">body.pen_shards:before{background-image:" + background + " !important;content:'';left:0;height:100%;position:fixed;top:0;width:100%;will-change:transform;z-index:-1; }</style>" );
  307. }
  308. }
  309. }
  310. function pen_trianglify() {
  311. if ( pen_function_exists( typeof Trianglify ) ) {
  312. var $body = jQuery( 'body' ),
  313. background_image = $body.css( 'background-image' );
  314. if ( pen_js.trianglify_colors && background_image && background_image === 'none' ) {
  315. var pattern = Trianglify(
  316. {
  317. height: window.innerHeight,
  318. width: window.innerWidth,
  319. x_colors: pen_js.trianglify_colors,
  320. y_colors: 'match_x',
  321. cell_size: 80
  322. }
  323. );
  324. var svg = jQuery( '<div />' ).prepend( pattern.svg() ).html();
  325. svg = '<?xml version="1.0" ?>' + svg.replace( '<svg', '<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none"' );
  326. var dataURI = "data:image/svg+xml;base64, " + window.btoa( unescape( encodeURIComponent( svg ) ) );
  327. jQuery( 'body' ).addClass( 'pen_trianglify' ).removeClass( 'custom-background' );
  328. jQuery( 'head' ).append( "<style type=\"text/css\">body.pen_trianglify:before{background-image:url('" + dataURI + "') !important;background-size:cover;content:'';left:0;height:100%;position:fixed;top:0;width:100%;will-change:transform;z-index:-1; }</style>" );
  329. }
  330. }
  331. }
  332. function pen_jump_menu( $menu ) {
  333. var title = jQuery.trim( $menu.find( 'h4' ).text() );
  334. $menu.prepend( '<button type="button" class="pen_toggle" title="' + title + '"><span class="screen-reader-text">' + pen_js.text.expand_collapse + '</span></button>' )
  335. .find( '.pen_menu_wrapper' ).attr( 'aria-hidden', true )
  336. .end().removeClass( 'screen-reader-text' ).attr( 'aria-hidden', false )
  337. .find( 'h4 span' ).prepend( '<span class="pen_only" title="' + pen_js.text.theme_specific + '">' + pen_js.text.pen_theme + '</span>' );
  338. var $toggle = $menu.find( '.pen_toggle' ),
  339. timer;
  340. $menu.find( 'ul li a' ).each(
  341. function() {
  342. jQuery( this ).attr( 'title', jQuery.trim( jQuery( this ).text() ) ).attr( 'tabindex', '-1' );
  343. }
  344. );
  345. $toggle.on(
  346. 'click',
  347. function( event ) {
  348. var $wrapper = jQuery( '.pen_menu_wrapper', $menu );
  349. clearTimeout( timer );
  350. if ( $toggle.hasClass( 'pen_expanded' ) ) {
  351. $toggle.removeClass( 'pen_expanded' );
  352. $wrapper.addClass( 'screen-reader-text' ).attr( 'aria-hidden', true )
  353. .find( 'ul li a' ).attr( 'tabindex', '-1' );
  354. } else {
  355. $toggle.addClass( 'pen_expanded' );
  356. $wrapper.find( 'ul li a' ).removeAttr( 'tabindex' )
  357. .end().removeClass( 'screen-reader-text' ).attr( 'aria-hidden', false )
  358. .on(
  359. 'mouseleave',
  360. function() {
  361. clearTimeout( timer );
  362. timer = setTimeout(
  363. function() {
  364. $wrapper.stop().animate(
  365. { opacity: 0 },
  366. {
  367. duration: 2000,
  368. queue: false,
  369. complete: function() {
  370. $toggle.trigger( 'click' );
  371. }
  372. }
  373. );
  374. },
  375. 30000
  376. );
  377. }
  378. ).on(
  379. 'mouseenter',
  380. function() {
  381. $wrapper.stop().animate( { opacity: 1 }, { duration: 200, queue: false } );
  382. clearTimeout( timer );
  383. }
  384. );
  385. }
  386. event.preventDefault();
  387. }
  388. );
  389. }
  390. function pen_function_exists( type_of ) {
  391. if ( type_of !== 'undefined' && type_of !== undefined && type_of !== null ) {
  392. return true;
  393. }
  394. return false;
  395. }