/* global FusionPageBuilderApp */ var FusionPageBuilder = FusionPageBuilder || {}; ( function() { jQuery( document ).ready( function() { // Blog Element View. FusionPageBuilder.fusion_blog = FusionPageBuilder.ElementView.extend( { /** * Are there any non landscape images, used for masonry layout. */ regularImagesFound: false, /** * Runs after view DOM is patched. * * @since 2.0 * @return {void} */ afterPatch: function() { this._refreshJs(); }, /** * Modify template attributes. * * @since 2.0 * @param {Object} atts - The attributes. * @return {Object} */ filterTemplateAtts: function( atts ) { var attributes = {}; // Validate values. this.validateValues( atts.values ); this.extras = atts.extras; this.values = atts.values; // Create attribute objects. attributes.attr = this.buildAttr( atts.values ); attributes.styles = this.buildStyles( atts.values ); attributes.blogPosts = ''; this.regularImagesFound = false; if ( 'undefined' !== typeof atts.query_data && 'undefined' !== typeof atts.query_data.posts ) { attributes.blogPosts = this.buildBlogPosts( atts ); // Add class if regular size images were found. if ( true === this.regularImagesFound ) { attributes.attr[ 'class' ] += ' fusion-masonry-has-vertical'; } } if ( 'undefined' !== typeof atts.query_data && 'undefined' !== typeof atts.query_data.max_num_pages ) { attributes.attrPostsContainer = this.buildPostsContainerAttr( atts ); if ( 'undefined' !== typeof atts.query_data.paged ) { attributes.paginationCode = this.buildPagination( atts ); } } attributes.load_more_text = atts.extras.load_more_text; attributes.load_more = atts.values.load_more && -1 !== atts.values.number_posts; // Any extras that need passed on. attributes.cid = this.model.get( 'cid' ); attributes.values = atts.values; attributes.query_data = atts.query_data; return attributes; }, /** * Modify values. * * @since 2.0 * @param {Object} values - The values. * @return {void} */ validateValues: function( values ) { values.blog_grid_column_spacing = _.fusionValidateAttrValue( values.blog_grid_column_spacing, '' ); values.scrolling = ( 'undefined' !== values.paging && 'no' == values.paging && 'pagination' === values.scrolling ) ? 'no' : values.scrolling; if ( -1 == values.number_posts ) { values.scrolling = 'no'; } // Add hyphens for alternate layout options. if ( 'large alternate' === values.layout ) { values.layout = 'large-alternate'; } else if ( 'medium alternate' === values.layout ) { values.layout = 'medium-alternate'; } values.load_more = false; if ( 'no' !== values.scrolling ) { if ( 'load_more_button' === values.scrolling ) { values.load_more = true; values.scrolling = 'infinite'; } } if ( 'undefined' !== typeof values.excerpt_length ) { values.excerpt_words = values.excerpt_length; } if ( '0' === values.blog_grid_column_spacing ) { values.blog_grid_column_spacing = '0.0'; } if ( 'object' !== typeof values.blog_grid_padding ) { values.blog_grid_padding = { top: '', right: '', bottom: '', left: '' }; } if ( 'undefined' !== typeof values.padding_top && '' !== values.padding_top ) { values.blog_grid_padding.top = values.padding_top; } if ( 'undefined' !== typeof values.padding_right && '' !== values.padding_right ) { values.blog_grid_padding.right = values.padding_right; } if ( 'undefined' !== typeof values.padding_bottom && '' !== values.padding_bottom ) { values.blog_grid_padding.bottom = values.padding_bottom; } if ( 'undefined' !== typeof values.padding_left && '' !== values.padding_left ) { values.blog_grid_padding.left = values.padding_left; } values.blog_grid_padding = [ _.fusionGetValueWithUnit( values.blog_grid_padding.top ), _.fusionGetValueWithUnit( values.blog_grid_padding.right ), _.fusionGetValueWithUnit( values.blog_grid_padding.bottom ), _.fusionGetValueWithUnit( values.blog_grid_padding.left ) ]; values.padding = 'object' === typeof values.blog_grid_padding ? values.blog_grid_padding.join( ' ' ) : values.blog_grid_padding; }, /** * Builds attributes. * * @since 2.0 * @param {Object} values - The values. * @return {Object} */ buildAttr: function( values ) { var attr = _.fusionVisibilityAtts( values.hide_on_mobile, { class: 'fusion-blog-shortcode fusion-blog-archive', style: this.getStyleVariables( values ) } ), blogLayout = '', cid = this.model.get( 'cid' ); // Set the correct layout class. blogLayout = 'fusion-blog-layout-' + values.layout; if ( 'timeline' === values.layout ) { blogLayout = 'fusion-blog-layout-timeline-wrapper'; } else if ( 'grid' === values.layout || 'masonry' === values.layout ) { blogLayout = 'fusion-blog-layout-grid-wrapper'; } if ( values.content_alignment && ( 'grid' === values.layout || 'masonry' === values.layout || 'timeline' === values.layout ) ) { attr[ 'class' ] += ' fusion-blog-layout-' + values.content_alignment; } attr[ 'class' ] += ' fusion-blog-shortcode-cid' + cid; attr[ 'class' ] += ' ' + blogLayout; attr[ 'class' ] += ' fusion-blog-' + values.scrolling; if ( 'yes' !== values.thumbnail ) { attr[ 'class' ] += ' fusion-blog-no-images'; } if ( '0' == values.blog_grid_column_spacing || '0px' === values.blog_grid_column_spacing ) { attr[ 'class' ] += ' fusion-no-col-space'; } if ( '' !== values[ 'class' ] ) { attr[ 'class' ] += ' ' + values[ 'class' ]; } if ( '' !== values.margin_top ) { attr.style += 'margin-top:' + values.margin_top + ';'; } if ( '' !== values.margin_right ) { attr.style += 'margin-right:' + values.margin_right + ';'; } if ( '' !== values.margin_bottom ) { attr.style += 'margin-bottom:' + values.margin_bottom + ';'; } if ( '' !== values.margin_left ) { attr.style += 'margin-left:' + values.margin_left + ';'; } if ( '' !== values.id ) { attr.id = values.id; } return attr; }, /** * Builds attributes for blog post container. * * @since 2.0 * @param {Object} atts - The attributes. * @return {Object} */ buildPostsContainerAttr: function( atts ) { var attr = { class: 'fusion-posts-container', style: '' }, values = atts.values, extras = atts.extras, queryData = atts.query_data, negativeMargin = ''; attr[ 'class' ] += ' fusion-posts-container-' + values.scrolling; if ( ! this.metaInfoCombined ) { attr[ 'class' ] += ' fusion-no-meta-info'; } if ( values.load_more ) { attr[ 'class' ] += ' fusion-posts-container-load-more'; } // Add class if rollover is enabled. if ( extras.image_rollover ) { attr[ 'class' ] += ' fusion-blog-rollover'; } attr[ 'data-pages' ] = queryData.max_num_pages; if ( 'grid' === values.layout || 'masonry' === values.layout ) { attr[ 'class' ] += ' fusion-blog-layout-grid fusion-blog-layout-grid-' + values.blog_grid_columns + ' isotope'; if ( 'masonry' === values.layout ) { attr[ 'class' ] += ' fusion-blog-layout-masonry'; if ( queryData.regular_images_found ) { attr[ 'class' ] += ' fusion-blog-layout-masonry-has-vertical'; } } if ( 'grid' === values.layout ) { if ( 'yes' === values.equal_heights ) { attr[ 'class' ] += ' fusion-blog-equal-heights'; } } if ( 'undefined' !== typeof values.blog_grid_column_spacing || 0 === parseInt( values.blog_grid_column_spacing, 10 ) ) { attr[ 'data-grid-col-space' ] = values.blog_grid_column_spacing; } negativeMargin = ( -1 ) * parseFloat( values.blog_grid_column_spacing ) / 2; attr.style = 'margin: ' + negativeMargin + 'px ' + negativeMargin + 'px 0;height:500px;'; } return attr; }, /** * Builds the pagination. * * @since 2.0 * @param {Object} atts - The attributes. * @return {string} */ buildPagination: function( atts ) { var globalPagination = atts.extras.pagination_global, globalStartEndRange = atts.extras.pagination_start_end_range_global, range = atts.extras.pagination_range_global, paged = '', pages = '', paginationCode = '', queryData = atts.query_data, values = atts.values; if ( -1 == values.number_posts ) { values.scrolling = 'no'; } if ( 'timeline' === values.layout ) { values.post_count = 1; } if ( 'no' !== values.scrolling ) { paged = queryData.paged; pages = queryData.max_num_pages; paginationCode = _.fusionPagination( pages, paged, range, values.scrolling, globalPagination, globalStartEndRange ); } return paginationCode; }, /** * Builds the blog posts HTML. * * @since 2.0 * @param {Object} atts - The values. * @return {string} */ buildBlogPosts: function( atts ) { var html = '', queryData = atts.query_data, values = atts.values, extras = atts.extras, imageSize = '', dateValues = {}, postCount = 1, prevPostMonth = null, prevPostYear = null, timelineDate = '', blogShortcodeLoop = {}, timelineAlign = '', beforeLoopAction = '', blogFusionPostWrapper = {}, color = '', colorCSS = '', metaInfoCombined = '', isZeroExcerpt = '', metaAll = ( 'yes' === values.meta_all ), metaAuthor = ( 'yes' === values.meta_author ), metaCategories = ( 'yes' === values.meta_categories ), metaComments = ( 'yes' === values.meta_comments ), metaDate = ( 'yes' === values.meta_date ), metaLink = ( 'yes' === values.meta_link ), mataTags = ( 'yes' === values.meta_tags ), stripHTML = ( 'yes' === values.strip_html ), thumbnail = ( 'yes' === values.thumbnail ), showTitle = ( 'yes' === atts.params.show_title || 'yes' === atts.params.title ), titleLink = ( 'yes' === values.title_link ), metaInfoSettings = {}, preTitleContent = '', metaData = '', contentSep = '', dateAndFormat = '', formatClass = '', blogShortcodePostContentWrapper = {}, link = '', linkTarget = '', linkIconTarget = '', postLinksTarget = '', blogShortcodePostTitle = {}, headerContent = '', content = '', readMoreContent = '', readMoreWrapperClass = 'fusion-alignright', gridTimeLineContent = '', readMoreLinkAttributes = {}, contentSepAttr = {}, contentSepTypes = '', postTitleTag = '', timelineTitleTag = '', self = this, isThereMetaAbove = false, isThereMetaBelow = false, isThereContent = false; // Initialize the time stamps for timeline month/year check. if ( 'timeline' === values.layout ) { postCount = 1; prevPostMonth = null; prevPostYear = null; } // Combine meta info into one variable. this.metaInfoCombined = metaAll * ( metaAuthor + metaDate + metaCategories + mataTags + metaComments + metaLink ); metaInfoCombined = this.metaInfoCombined; // Create boolean that holds info whether content should be excerpted. isZeroExcerpt = ( 'yes' === values.excerpt && 1 > values.excerpt_words ) ? 1 : 0; metaInfoSettings.post_meta = metaAll; metaInfoSettings.post_meta_author = metaAuthor; metaInfoSettings.post_meta_date = metaDate; metaInfoSettings.post_meta_cats = metaCategories; metaInfoSettings.post_meta_tags = mataTags; metaInfoSettings.post_meta_comments = metaComments; metaInfoSettings.disable_date_rich_snippet_pages = extras.disable_date_rich_snippet_pages; isThereMetaAbove = metaInfoCombined * ( metaAuthor + metaDate + metaCategories + mataTags ); isThereMetaBelow = metaInfoCombined * ( metaComments || metaLink ); isThereContent = 'no' === values.excerpt || ( 'yes' === values.excerpt && ! isZeroExcerpt ); _.each( queryData.posts, function( post ) { var footerContent = '', metaFooterContent, borderColor; readMoreContent = ''; headerContent = ''; preTitleContent = ''; // Work out correct image size. imageSize = 'blog-large'; imageSize = ( ! FusionPageBuilderApp.$el.hasClass( 'has-sidebar' ) ) ? 'full' : 'blog-large'; imageSize = ( 'medium' === values.layout || 'medium-alternate' === values.layout ) ? 'blog-medium' : imageSize; imageSize = ( 'undefined' !== typeof post.slideshow.featured_image_height && 'undefined' !== typeof post.slideshow.featured_image_width && '' !== post.slideshow.featured_image_height && '' !== post.slideshow.featured_image_width && 'auto' !== post.slideshow.featured_image_height && 'auto' !== post.slideshow.featured_image_width ) ? 'full' : imageSize; imageSize = ( 'auto' === post.slideshow.featured_image_height || 'auto' === post.slideshow.featured_image_width ) ? 'full' : imageSize; imageSize = ( 'grid' === values.layout || 'timeline' === values.layout ) ? 'full' : imageSize; post.slideshow.image_size = imageSize; if ( 'timeline' === values.layout ) { dateValues = {}; dateValues.prev_post_month = prevPostMonth; dateValues.post_month = post.post_month; dateValues.prev_post_year = prevPostYear; dateValues.post_year = post.post_year; timelineDate = ''; if ( dateValues.prev_post_month != dateValues.post_month || dateValues.prev_post_year != dateValues.post_year ) { if ( 1 < postCount ) { timelineDate = ''; } timelineTitleTag = self.getTitleTag( values, 'timeline_title' ); timelineDate += '<' + timelineTitleTag + ' class="fusion-timeline-date" style="background-color:' + values.grid_element_color + ';">' + post.timeline_date_format + '' + timelineTitleTag + '>'; timelineDate += '