Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // External Dependencies
  2. import React from 'react';
  3. import $ from 'jquery';
  4. import { findDOMNode } from 'react-dom';
  5.  
  6. // Internal Dependencies
  7. import AjaxComponent from './../base/AjaxComponent';
  8. import './style.css';
  9.  
  10.  
  11. class Image extends AjaxComponent {
  12.  
  13.     static slug = 'dss_toolset_image';
  14.  
  15.     static css(props) {
  16.         const additionalCss = [];
  17.  
  18.         additionalCss.push([{
  19.             selector: '%%order_class%% .dss_svg_image',
  20.             declaration: "display: block;",
  21.         }]);
  22.  
  23.  
  24.         if ('on' === props.force_fullwidth) {
  25.             additionalCss.push([{
  26.                 selector: '%%order_class%%',
  27.                 declaration: "max-width: 100% !important;",
  28.             }]);
  29.  
  30.             additionalCss.push([{
  31.                 selector: '%%order_class%% .et_pb_image_wrap, %%order_class%% img',
  32.                 declaration: "width: 100%;",
  33.             }]);
  34.         }
  35.  
  36.         if ('' !== props.align) {
  37.             additionalCss.push([{
  38.                 selector: '%%order_class%%',
  39.                 declaration: `text-align: ${props.align}`,
  40.             }]);
  41.         }
  42.  
  43.         if ('center' !== props.align) {
  44.             additionalCss.push([{
  45.                 selector: '%%order_class%%',
  46.                 declaration: `margin-${props.align}: 0;`,
  47.             }]);
  48.         }
  49.  
  50.         if ('on' === props.use_overlay) {
  51.             additionalCss.push([{
  52.                 selector: '%%order_class%% .et_overlay:before',
  53.                 declaration: `color: ${props.overlay_icon_color}`,
  54.             }]);
  55.  
  56.             additionalCss.push([{
  57.                 selector: '%%order_class%% .et_overlay',
  58.                 declaration: `background-color: ${props.hover_overlay_color}`,
  59.             }]);
  60.         }
  61.  
  62.         return additionalCss;
  63.     }
  64.  
  65.     componentDidUpdate() {
  66.         const lightbox = findDOMNode(this.refs.lightbox);
  67.  
  68.         if (!lightbox) return;
  69.  
  70.         $(lightbox).magnificPopup({
  71.             type: 'image',
  72.             removalDelay: 500,
  73.             mainClass: 'mfp-fade',
  74.             zoom: {
  75.                 enabled: true,
  76.                 duration: 500,
  77.                 opener: function (element) {
  78.                     return element.find('img');
  79.                 }
  80.             }
  81.         });
  82.     }
  83.  
  84.     _shouldReload(oldProps, newProps) {
  85.         return oldProps.toolset_field !== newProps.toolset_field ||
  86.             oldProps.field_url !== newProps.field_url ||
  87.             oldProps.image_size !== newProps.image_size;
  88.     }
  89.  
  90.     _reloadData(props) {
  91.         return {
  92.             action: 'dss_toolset_image',
  93.             post_id: window.ETBuilderBackend.postId,
  94.             toolset_field: props.toolset_field,
  95.             field_url: props.field_url,
  96.             image_size: props.image_size,
  97.             load_image_attributes: props.load_image_attributes,
  98.             nonce: window.DsSuitToolsetBuilderData.nonces.dss_toolset_image
  99.         };
  100.     }
  101.  
  102.     render() {
  103.         return super.render();
  104.     }
  105.  
  106.     _render() {
  107.         return (
  108.             <div>
  109.                 {this._renderLabel()}
  110.                 {this._renderField()}
  111.             </div>
  112.         );
  113.     }
  114.  
  115.     _renderLabel() {
  116.         var label;
  117.         switch (this.props.toolset_label) {
  118.             case 'off':
  119.                 return null;
  120.             case 'custom':
  121.                 label = this.props.label_custom;
  122.                 break;
  123.             case 'field':
  124.                 label = this.props.label_prefix + this.state.result.field.name + this.props.label_suffix;
  125.                 break;
  126.             default:
  127.                 label = "";
  128.         }
  129.  
  130.         const Element = this.props.toolset_label_element || 'h3';
  131.  
  132.         return (
  133.             <Element className="dss_toolset_label">{label}</Element>
  134.         );
  135.     }
  136.  
  137.     _renderField() {
  138.         const props = this.props;
  139.         const state = this.state;
  140.         const utils = window.ET_Builder.API.Utils;
  141.  
  142.         var image_url = this.state.result.field_meta_value;
  143.         var image_alt = '';
  144.         var image_title = '';
  145.  
  146.         if ('off' === props.load_image_attributes) {
  147.             image_alt = props.image_alt;
  148.             image_title = props.image_title;
  149.         } else {
  150.             image_alt = state.result.image_alt;
  151.             image_title = state.result.image_title;
  152.         }
  153.  
  154.         if ('on' === props.toolset_field_hide_empty && (!image_url || '' === image_url)) {
  155.             return <div>{window.DsSuitToolsetBuilderData.i18n.empty_field_will_be_hidden}</div>;
  156.         }
  157.  
  158.         var overlay = null;
  159.         var additional_classes = [
  160.             'et_pb_image',
  161.             'et_pb_module',
  162.         ];
  163.  
  164.         //Get overlay
  165.         if ('on' === props.use_overlay) {
  166.             additional_classes.push('et_pb_has_overlay');
  167.             overlay = (<span data-icon={utils.processFontIcon(props.hover_icon)} className="et_overlay et_pb_inline_icon"></span>);
  168.         }
  169.  
  170.         //Create the image
  171.         var image = null;
  172.         if (state.result.image_thumb_url && '' !== state.result.image_thumb_url) {
  173.             image = (
  174.                 <span className="et_pb_image_wrap">
  175.                     <img className="et_pb_dss_toolset_image_image" src={state.result.image_thumb_url} alt={image_alt} />
  176.                     {overlay}
  177.                 </span>
  178.             );
  179.         }
  180.  
  181.         //Embedd image in lightbox or url if necessary
  182.         if ('on' === props.show_in_lightbox) {
  183.             image = (
  184.                 <a ref="lightbox" href={state.result.image_url} className="et_pb_lightbox_image" title={image_title}>
  185.                     {image}
  186.                 </a>
  187.             );
  188.         } else if ('off' !== props.link) {
  189.             var url = '';
  190.             switch (props.link) {
  191.                 case 'image':
  192.                     url = state.image_url;
  193.                     break;
  194.                 case 'custom_url':
  195.                     url = props.custom_url;
  196.                     break;
  197.                 case 'field_url':
  198.                     url = state.field_url;
  199.                     break;
  200.                 default:
  201.                     break;
  202.             }
  203.  
  204.             if ('' !== url) {
  205.                 image = (
  206.                     <a href={url}>
  207.                         {image}
  208.                     </a>
  209.                 );
  210.             }
  211.         }
  212.  
  213.         if ('on' === props.always_center_on_mobile) {
  214.             additional_classes.push('et_always_center_on_mobile');
  215.         }
  216.  
  217.         if (state.is_svg) {
  218.             additional_classes.push('dss_svg_image');
  219.         }
  220.  
  221.         return (
  222.             <div className={additional_classes.join(" ")}>
  223.                 {image}
  224.             </div>
  225.         );
  226.     }
  227. }
  228.  
  229. export default Image;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement