/* global fusionOptionName, FusionApp, FusionPageBuilderElements, fusionAppConfig, FusionPageBuilderApp, FusionPageBuilderViewManager */ var FusionPageBuilder = FusionPageBuilder || {}; ( function() { _.extend( FusionPageBuilder.Callback.prototype, { fusion_preview: function( name, value, args, view ) { var property = args.property, element = window.fusionAllElements[ view.model.get( 'element_type' ) ], selectors; if ( ! args.skip ) { view.changeParam( name, value ); } if ( ! value && '' !== value ) { return; } if ( '' === value && 'undefined' !== typeof element && 'undefined' !== typeof element.defaults && 'undefined' !== typeof element.defaults[ name ] ) { value = element.defaults[ name ]; } if ( 'undefined' !== typeof args.dimension ) { property = ( 'undefined' !== typeof args.property[ name ] ) ? args.property[ name ] : name.replace( /_/g, '-' ); } if ( 'undefined' !== typeof args.transform_to_vars && args.transform_to_vars ) { property = '--awb-' + name.replace( /_/g, '-' ); } if ( 'undefined' !== typeof args.unit ) { value = _.fusionGetValueWithUnit( value, args.unit ); } selectors = 'undefined' === typeof args.selector ? 'none' : args.selector.split( ',' ); _.each( selectors, function( selector ) { const $theElement = 'none' === selector.trim() ? view.$el : view.$el.find( selector.trim() ).first(); if ( 'string' === typeof property ) { $theElement.css( property, value ); } if ( 'object' === typeof property ) { _.each( args.property, function( singleProperty ) { $theElement.css( singleProperty, value ); } ); } } ); if ( 'fusion_builder_container' === view.model.get( 'element_type' ) ) { view.setValues(); } return { render: false }; }, fusion_update_flex_elements: function( name, value, args, view ) { var params = view.model.get( 'params' ), oldValue = 'undefined' !== typeof params.content_layout ? params.content_layout : 'column'; if ( ! args.skip ) { view.changeParam( name, value ); } // If we are switching between flex and block then we need to re-render inline elements. if ( ( 'block' === oldValue && 'block' !== value ) || ( 'block' === value && 'block' !== oldValue ) ) { view.model.children.each( function( child ) { var cid = child.attributes.cid, elementView = FusionPageBuilderViewManager.getView( cid ), elementType; if ( elementView ) { elementType = elementView.model.get( 'element_type' ); if ( 'fusion_title' === elementType || 'fusion_button' === elementType || 'fusion_text' === elementType || 'fusion_imageframe' === elementType ) { elementView.reRender(); } } } ); } // Add attribute to the option. jQuery( '[data-option-id="content_layout"]' ).attr( 'data-direction', value ); return { render: true }; }, fusion_container_margin: function( name, value, args, view ) { return this.fusion_preview( name, value, args, view ); }, fusion_column_margin: function( name, value, args, view ) { if ( ! args.skip ) { view.changeParam( name, value ); } if ( view.values.flex ) { if ( ! name.includes( 'medium' ) && ! name.includes( 'small' ) ) { view.$el.css( '--awb-' + name.replaceAll( '_', '-' ) + '-large', _.fusionGetValueWithUnit( value ) ); } else { view.$el.css( '--awb-' + name.replaceAll( '_', '-' ), _.fusionGetValueWithUnit( value ) ); } view.values = {}; view.setArgs(); view.validateArgs(); view.setExtraArgs(); view.setColumnMapData(); view.setResponsiveColumnStyles(); view.$el.find( '.fusion-column-responsive-styles' ).last().html( view.responsiveStyles ); const attr = view.buildAttr(); view.$el.attr( 'style', attr.style ); } else { view.$el.css( name.replace( '_', '-' ), value ); view.values[ name ] = value; } return { render: false }; }, fusion_column_padding: function( name, value, args, view ) { view.changeParam( name, value ); view.values[ name ] = value; view.$el.css( '--awb-' + name.replaceAll( '_', '-' ), _.fusionGetValueWithUnit( value ) ); return { render: false }; }, fusion_add_id: function( name, value, args, view ) { var $theEl; if ( ! args.skip ) { view.changeParam( name, value ); } $theEl = ( 'undefined' === typeof args.selector ) ? view.$el : view.$el.find( args.selector ); $theEl.attr( 'id', value ); return { render: false }; }, fusion_add_class: function( name, value, args, view ) { var $theEl, existingValue = view.model.attributes.params[ name ]; if ( ! args.skip ) { view.changeParam( name, value ); } $theEl = ( 'undefined' === typeof args.selector ) ? view.$el : view.$el.find( args.selector ); $theEl.removeClass( existingValue ); $theEl.addClass( value ); return { render: false }; }, fusion_toggle_class: function( name, value, args, view ) { var $theEl; if ( ! args.skip ) { view.changeParam( name, value ); } $theEl = ( 'undefined' === typeof args.selector ) ? view.$el : view.$el.find( args.selector ); if ( 'object' === typeof args.classes ) { _.each( args.classes, function( optionClass, optionValue ) { $theEl.removeClass( optionClass ); if ( value === optionValue ) { $theEl.addClass( optionClass ); } } ); } return { render: false }; }, fusion_cart_hide: function( name, value, args, view ) { if ( 'string' !== typeof args.selector ) { return { render: true }; } if ( ! args.skip ) { view.changeParam( name, value ); } if ( 'no' === value ) { view.$el.find( '.fusion-woo-cart' ).addClass( args.selector ); } else { view.$el.find( '.fusion-woo-cart' ).removeClass( args.selector ); } return { render: false }; }, fusion_ajax: function( name, value, modelData, args, cid, action, model, elementView ) { var params = jQuery.extend( true, {}, modelData.params ), ajaxData = {}; if ( 'undefined' !== typeof name && ! args.skip ) { params[ name ] = value; } ajaxData.params = jQuery.extend( true, {}, window.fusionAllElements[ modelData.element_type ].defaults, _.fusionCleanParameters( params ) ); jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'post', dataType: 'json', data: { action: action, model: ajaxData, option_name: 'string' === typeof fusionOptionName ? fusionOptionName : false, fusion_options: 'undefined' !== typeof FusionApp && 'object' === typeof FusionApp.settings ? jQuery.param( FusionApp.settings ) : false, fusion_meta: 'undefined' !== typeof FusionApp && 'object' === typeof FusionApp.data.postMeta ? jQuery.param( FusionApp.data.postMeta ) : false, fusion_load_nonce: fusionAppConfig.fusion_load_nonce, post_id: FusionApp.getDynamicPost( 'post_id' ), cid: cid } } ) .done( function( response ) { const skipRerender = 'undefined' !== typeof args.skipRerender ? args.skipRerender : args.skip; if ( 'undefined' === typeof model ) { model = FusionPageBuilderElements.find( function( scopedModel ) { return scopedModel.get( 'cid' ) == cid; // jshint ignore: line } ); } // This changes actual model. if ( 'undefined' !== typeof name && ! args.skip ) { elementView.changeParam( name, value ); } if ( 'image_id' === name && 'undefined' !== typeof response.image_data && 'undefined' !== typeof response.image_data.url && ! args.skip ) { elementView.changeParam( 'image', response.image_data.url ); } model.set( 'query_data', response ); if ( 'generated_element' !== model.get( 'type' ) ) { if ( 'undefined' == typeof elementView ) { elementView = FusionPageBuilderViewManager.getView( cid ); } if ( 'undefined' !== typeof elementView && ! skipRerender ) { elementView.reRender(); } } } ); }, fusion_do_shortcode: function( cid, content, parent, ajaxShortcodes ) { jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'post', dataType: 'json', data: { action: 'get_shortcode_render', content: content, shortcodes: 'undefined' !== typeof ajaxShortcodes ? ajaxShortcodes : '', fusion_load_nonce: fusionAppConfig.fusion_load_nonce, cid: cid, post_id: FusionApp.getDynamicPost( 'post_id' ) } } ) .done( function( response ) { var markup = {}, modelcid = cid, model, view; if ( 'undefined' !== typeof parent && parent ) { modelcid = parent; } model = FusionPageBuilderElements.find( function( scopedModel ) { return scopedModel.get( 'cid' ) == modelcid; // jshint ignore: line } ); view = FusionPageBuilderViewManager.getView( modelcid ); markup.output = FusionPageBuilderApp.addPlaceholder( content, response.content ); if ( view && 'function' === typeof view.filterOutput ) { markup.output = view.filterOutput( markup.output ); } markup.shortcode = content; if ( model ) { model.set( 'markup', markup ); } // If multi shortcodes, add each. if ( 'object' === typeof response.shortcodes ) { _.each( response.shortcodes, function( output, shortcode ) { FusionPageBuilderApp.extraShortcodes.addShortcode( shortcode, FusionPageBuilderApp.addPlaceholder( shortcode, output ) ); } ); } if ( 'undefined' !== typeof view ) { view.reRender( 'ajax' ); } if ( FusionPageBuilderApp.viewsToRerender ) { _.each( FusionPageBuilderApp.viewsToRerender, function( scopedCID ) { FusionPageBuilderViewManager.getView( scopedCID ).reRender( 'ajax' ); } ); FusionPageBuilderApp.viewsToRerender = []; } } ); }, fusion_code_mirror: function( name, value, args, view ) { // Save encoded value. if ( ! args.skip ) { view.changeParam( name, value ); } if ( FusionPageBuilderApp.base64Encode( FusionPageBuilderApp.base64Decode( value ) ) === value ) { value = FusionPageBuilderApp.base64Decode( value ); } // Update with decoded value. view.syntaxHighlighter.getDoc().setValue( value ); return { render: false }; }, dynamic_shortcode: function( args ) { if ( 'undefined' === typeof args.shortcode || '' === args.shortcode ) { return ''; } return jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'post', dataType: 'json', data: { action: 'get_shortcode_render', content: args.shortcode, fusion_load_nonce: fusionAppConfig.fusion_load_nonce, cid: false, post_id: FusionApp.getDynamicPost( 'post_id' ) } } ) .done( function( response ) { FusionPageBuilderApp.dynamicValues.setValue( args, response.content ); } ); }, post_featured_image: function( args ) { var ID = '', postMeta = FusionApp.getDynamicPost( 'post_meta' ), postName = FusionApp.getPost( 'post_type_name' ).toLowerCase(); postMeta._fusion = postMeta._fusion || {}; if ( 'undefined' !== typeof args.type && 'main' !== args.type ) { ID = postMeta._fusion[ 'kd_' + args.type + '_' + postName + '_id' ]; } else { ID = postMeta._thumbnail_id; } if ( 'undefined' === typeof ID || '' === ID ) { return ID; } return wp.media.attachment( ID ).fetch().then( function() { FusionPageBuilderApp.dynamicValues.setValue( args, wp.media.attachment( ID ).get( 'url' ) ); } ); }, fusion_get_object_title: function( args ) { if ( 'undefined' === typeof FusionApp.data ) { return ''; } return jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'get', dataType: 'json', data: { action: 'ajax_dynamic_data_default_callback', callback: FusionApp.data.dynamicOptions[ args.data ].callback[ 'function' ], args: args, fusion_load_nonce: fusionAppConfig.fusion_load_nonce, post_id: FusionApp.getDynamicPost( 'post_id' ), is_term: FusionApp.getDynamicPost( 'is_term' ) } } ) .done( function( response ) { FusionPageBuilderApp.dynamicValues.setValue( args, response.content ); } ); }, fusion_get_post_id: function() { return 'undefined' !== typeof FusionApp.data ? String( FusionApp.getDynamicPost( 'post_id' ) ) : ''; }, fusion_get_object_excerpt: function() { return 'undefined' !== typeof FusionApp.data ? FusionApp.getDynamicPost( 'post_excerpt' ) : ''; }, fusion_get_post_date: function( args ) { if ( 'undefined' === typeof FusionApp.data ) { return ''; } if ( 'undefined' === args.format || '' === args.format ) { return 'undefined' !== typeof args.type && 'modified' === args.type ? FusionApp.getDynamicPost( 'post_modified' ) : FusionApp.getDynamicPost( 'post_date' ); } return jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'get', dataType: 'json', data: { action: 'ajax_dynamic_data_default_callback', callback: FusionApp.data.dynamicOptions[ args.data ].callback[ 'function' ], args: args, fusion_load_nonce: fusionAppConfig.fusion_load_nonce, post_id: FusionApp.getDynamicPost( 'post_id' ) } } ) .done( function( response ) { FusionPageBuilderApp.dynamicValues.setValue( args, response.content ); } ); }, fusion_get_post_time: function( args ) { if ( 'undefined' === typeof FusionApp.data ) { return ''; } return jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'get', dataType: 'json', data: { action: 'ajax_dynamic_data_default_callback', callback: FusionApp.data.dynamicOptions[ args.data ].callback[ 'function' ], args: args, fusion_load_nonce: fusionAppConfig.fusion_load_nonce, post_id: FusionApp.getDynamicPost( 'post_id' ) } } ) .done( function( response ) { FusionPageBuilderApp.dynamicValues.setValue( args, response.content ); } ); }, fusion_get_post_terms: function( args ) { if ( 'undefined' === typeof FusionApp.data ) { return ''; } return jQuery.ajax( { url: fusionAppConfig.ajaxurl, type: 'get', dataType: 'json', data: { action: 'ajax_dynamic_data_default_callback', callback: FusionApp.data.dynamicOptions[ args.data ].callback[ 'function' ], args: args, fusion_load_nonce: fusionAppConfig.fusion_load_nonce, post_id: FusionApp.getDynamicPost( 'post_id' ) } } ) .done( function( response ) { FusionPageBuilderApp.dynamicValues.setValue( args, response.content ); } ); }, fusion_get_post_custom_field: function( args ) { var postMeta = FusionApp.getDynamicPost( 'post_meta' ); postMeta._fusion = postMeta._fusion || {}; return 'undefined' !== typeof postMeta[ args.key ] ? postMeta[ args.key ] : ''; }, fusion_get_page_option: function( args ) { var postMeta = FusionApp.getDynamicPost( 'post_meta' ); postMeta._fusion = postMeta._fusion || {}; return 'undefined' !== typeof postMeta._fusion[ args.data ] ? postMeta._fusion[ args.data ] : ''; }, fusion_get_site_title: function() { return 'undefined' !== typeof FusionApp.data ? FusionApp.data.site_title : ''; }, fusion_get_site_tagline: function() { return 'undefined' !== typeof FusionApp.data ? FusionApp.data.site_tagline : ''; }, fusion_get_logged_in_username: function() { return 'undefined' !== typeof FusionApp.data ? FusionApp.data.loggined_in_username : ''; }, awb_get_user_avatar: function() { return 'undefined' !== typeof FusionApp.data ? FusionApp.data.user_avatar : ''; }, fusion_get_site_url: function() { return 'undefined' !== typeof FusionApp.data ? FusionApp.data.site_url : ''; }, fusion_get_site_logo: function( args ) { var type = 'undefined' !== typeof args.type ? args.type : false, data = {}; if ( ! type ) { return ''; } switch ( type ) { case 'default_normal': return FusionApp.settings.logo.url; case 'default_retina': return FusionApp.settings.logo_retina.url; case 'sticky_normal': return FusionApp.settings.sticky_header_logo.url; case 'sticky_retina': return FusionApp.settings.sticky_header_logo_retina.url; case 'mobile_normal': return FusionApp.settings.mobile_logo.url; case 'mobile_retina': return FusionApp.settings.mobile_logo_retina.url; case 'all': data[ 'default' ] = { 'normal': FusionApp.settings.logo, 'retina': FusionApp.settings.logo_retina }; data.mobile = { 'normal': FusionApp.settings.mobile_logo, 'retina': FusionApp.settings.mobile_logo_retina }; data.sticky = { 'normal': FusionApp.settings.sticky_header_logo, 'retina': FusionApp.settings.sticky_header_logo_retina }; return JSON.stringify( data ); } return ''; }, fusion_menu: function( name, value, args, view ) { if ( ! args.skip ) { view.changeParam( name, value ); } const attrs = view.getTemplateAtts(); view.$el.find( 'nav' ).attr( 'style', attrs.attr.style ); // If the ajax markup is still there from initial load then data-count is wrong. view.$el.find( 'nav' ).attr( 'data-cid', view.model.get( 'cid' ) ); return { render: false }; }, fusion_submenu: function( name, value, args, view ) { if ( ! args.skip ) { view.changeParam( name, value ); } const attrs = view.getTemplateAtts(); view.$el.find( 'nav' ).attr( 'style', attrs.attr.style ); // If the ajax markup is still there from initial load then data-count is wrong. view.$el.find( 'nav' ).attr( 'data-cid', view.model.get( 'cid' ) ); return { render: false }; }, fusion_style_block: function( name, value, args, view ) { var styleEl; if ( ! args.skip ) { view.changeParam( name, value ); } const attrs = view.getTemplateAtts(); // Can't find base selector, markup likely wrong and needs updated. if ( ! view.$el.find( view.baseSelector ).length ) { return { render: true }; } styleEl = view.$el.find( 'style' ).first(); // When element is added there will be no