| @@ -32,12 +32,24 @@ | ||
| 32 | 32 | } |
| 33 | 33 | }); |
| 34 | 34 | })(wp.media.view); |
| 35 | 35 | |
| 36 | +function createPopupProBlocker( $ , e ) { | |
| 37 | + if ( ! visualizer.is_pro_user && e.target.classList.contains('viz-is-pro-chart') ) { | |
| 38 | + $("#overlay-visualizer").css("display", "block"); | |
| 39 | + $(".vizualizer-renew-notice-popup").css("display", "block"); | |
| 40 | + return true; | |
| 41 | + } | |
| 42 | + return false; | |
| 43 | +} | |
| 44 | + | |
| 36 | 45 | (function ($, vmv, vu) { |
| 37 | 46 | var resizeTimeout; |
| 38 | 47 | |
| 39 | 48 | $.fn.adjust = function () { |
| 49 | + if ( $( '#visualizer-library' ).hasClass( 'view-list' ) ) { | |
| 50 | + return this; | |
| 51 | + } | |
| 40 | 52 | return $(this).each(function () { |
| 41 | 53 | var width = $('#visualizer-library').width(), |
| 42 | 54 | margin = width * 0.02; |
| 43 | 55 | |
| @@ -75,20 +87,74 @@ | ||
| 75 | 87 | }); |
| 76 | 88 | $(this).parent('form').submit(); |
| 77 | 89 | }); |
| 78 | 90 | |
| 79 | - $('.visualizer-chart-shortcode').click(function (e) { | |
| 91 | + // Copy shortcode when clicking the code display in list view. | |
| 92 | + $( document ).on( 'click', '.viz-shortcode-display', function () { | |
| 93 | + var text = $( this ).text(); | |
| 94 | + var el = this; | |
| 95 | + | |
| 96 | + // Fallback copy method using a temporary textarea. Returns true on success. | |
| 97 | + var fallbackCopy = function ( value ) { | |
| 98 | + var ta = document.createElement( 'textarea' ); | |
| 99 | + ta.value = value; | |
| 100 | + document.body.appendChild( ta ); | |
| 101 | + ta.select(); | |
| 102 | + var succeeded = false; | |
| 103 | + try { | |
| 104 | + succeeded = document.execCommand( 'copy' ); | |
| 105 | + } catch ( e ) { | |
| 106 | + succeeded = false; | |
| 107 | + } | |
| 108 | + document.body.removeChild( ta ); | |
| 109 | + return succeeded; | |
| 110 | + }; | |
| 111 | + | |
| 112 | + // Apply temporary "copied" feedback. | |
| 113 | + var showCopiedFeedback = function () { | |
| 114 | + $( el ).addClass( 'viz-shortcode-copied' ); | |
| 115 | + setTimeout( function () { | |
| 116 | + $( el ).removeClass( 'viz-shortcode-copied' ); | |
| 117 | + }, 1200 ); | |
| 118 | + }; | |
| 119 | + | |
| 120 | + if ( navigator.clipboard && navigator.clipboard.writeText ) { | |
| 121 | + navigator.clipboard.writeText( text ).then( function () { | |
| 122 | + // Clipboard API succeeded. | |
| 123 | + showCopiedFeedback(); | |
| 124 | + } ).catch( function () { | |
| 125 | + // Clipboard API failed; fall back to textarea method. | |
| 126 | + if ( fallbackCopy( text ) ) { | |
| 127 | + showCopiedFeedback(); | |
| 128 | + } | |
| 129 | + } ); | |
| 130 | + } else { | |
| 131 | + // No Clipboard API; use fallback directly. | |
| 132 | + if ( fallbackCopy( text ) ) { | |
| 133 | + showCopiedFeedback(); | |
| 134 | + } | |
| 135 | + } | |
| 136 | + } ); | |
| 137 | + | |
| 138 | + $('.visualizer-chart-shortcode').click(function (event) { | |
| 139 | + | |
| 140 | + if ( createPopupProBlocker( $, event ) ) { | |
| 141 | + event.preventDefault(); | |
| 142 | + event.stopPropagation(); | |
| 143 | + return; | |
| 144 | + } | |
| 145 | + | |
| 80 | 146 | var range, selection; |
| 81 | 147 | |
| 82 | 148 | if (window.getSelection && document.createRange) { |
| 83 | 149 | selection = window.getSelection(); |
| 84 | 150 | range = document.createRange(); |
| 85 | - range.selectNodeContents(e.target); | |
| 151 | + range.selectNodeContents(event.target); | |
| 86 | 152 | selection.removeAllRanges(); |
| 87 | 153 | selection.addRange(range); |
| 88 | 154 | } else if (document.selection && document.body.createTextRange) { |
| 89 | 155 | range = document.body.createTextRange(); |
| 90 | - range.moveToElementText(e.target); | |
| 156 | + range.moveToElementText(event.target); | |
| 91 | 157 | range.select(); |
| 92 | 158 | } |
| 93 | 159 | }); |
| 94 | 160 | |
| @@ -101,9 +167,9 @@ | ||
| 101 | 167 | $('.visualizer-chart-edit').click(); |
| 102 | 168 | } |
| 103 | 169 | } ); |
| 104 | 170 | |
| 105 | - $('.add-new-chart').click(function () { | |
| 171 | + function openClassicBuilder() { | |
| 106 | 172 | var wnd = window, |
| 107 | 173 | view = new vmv.Chart({action: vu.create}); |
| 108 | 174 | vu.create = vu.create.replace(/[\?&]lang=[^&]+/, '').replace(/[\?&]parent_chart_id=[^&]+/, ''); |
| 109 | 175 | |
| @@ -121,13 +187,86 @@ | ||
| 121 | 187 | wnd.send_to_editor = function () { |
| 122 | 188 | wnd.location.href = vu.base.replace(/type=[a-zA-Z]*/, '').replace(/vaction/, ''); |
| 123 | 189 | }; |
| 124 | 190 | view.open(); |
| 191 | + } | |
| 125 | 192 | |
| 193 | + function closeAddNewMenus() { | |
| 194 | + $('.viz-add-new-group').removeClass('is-open'); | |
| 195 | + $('.viz-add-new-toggle').attr('aria-expanded', 'false'); | |
| 196 | + $('.viz-add-new-menu').attr('aria-hidden', 'true'); | |
| 197 | + } | |
| 198 | + | |
| 199 | + $(document).on('click', function() { | |
| 200 | + closeAddNewMenus(); | |
| 201 | + }); | |
| 202 | + | |
| 203 | + $(document).on('click', '.viz-add-new-toggle', function(event) { | |
| 204 | + event.preventDefault(); | |
| 205 | + event.stopPropagation(); | |
| 206 | + event.stopImmediatePropagation(); | |
| 207 | + var $group = $(this).closest('.viz-add-new-group'); | |
| 208 | + var isOpen = $group.hasClass('is-open'); | |
| 209 | + closeAddNewMenus(); | |
| 210 | + if ( ! isOpen ) { | |
| 211 | + $group.addClass('is-open'); | |
| 212 | + $(this).attr('aria-expanded', 'true'); | |
| 213 | + $group.find('.viz-add-new-menu').attr('aria-hidden', 'false'); | |
| 214 | + } | |
| 126 | 215 | return false; |
| 127 | 216 | }); |
| 128 | 217 | |
| 129 | - $('.visualizer-chart-edit').click(function () { | |
| 218 | + $(document).on('click', '.viz-add-new-item', function(event) { | |
| 219 | + event.preventDefault(); | |
| 220 | + event.stopPropagation(); | |
| 221 | + event.stopImmediatePropagation(); | |
| 222 | + var builder = $(this).data('viz-builder'); | |
| 223 | + closeAddNewMenus(); | |
| 224 | + | |
| 225 | + if ( builder === 'ai' ) { | |
| 226 | + if ( typeof window.vizOpenAIBuilderNew === 'function' ) { | |
| 227 | + window.vizOpenAIBuilderNew(); | |
| 228 | + return; | |
| 229 | + } | |
| 230 | + if ( typeof window.vizOpenChartChooser === 'function' ) { | |
| 231 | + var classicCallback = function() { | |
| 232 | + openClassicBuilder(); | |
| 233 | + }; | |
| 234 | + window.vizOpenChartChooser( classicCallback ); | |
| 235 | + return; | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + openClassicBuilder(); | |
| 240 | + }); | |
| 241 | + | |
| 242 | + $('.add-new-chart').click(function () { | |
| 243 | + // Hook for the React chart builder chooser modal. | |
| 244 | + // If the React app is loaded it sets window.vizOpenChartChooser and handles routing. | |
| 245 | + // It will call the classicCallback if the user picks the Classic Builder. | |
| 246 | + if (typeof window.vizOpenChartChooser === 'function') { | |
| 247 | + var classicCallback = function() { | |
| 248 | + openClassicBuilder(); | |
| 249 | + }; | |
| 250 | + window.vizOpenChartChooser(classicCallback); | |
| 251 | + return false; | |
| 252 | + } | |
| 253 | + openClassicBuilder(); | |
| 254 | + return false; | |
| 255 | + }); | |
| 256 | + | |
| 257 | + $('.visualizer-chart-edit').click(function (event) { | |
| 258 | + | |
| 259 | + if ( createPopupProBlocker( $, event ) ) { | |
| 260 | + return; | |
| 261 | + } | |
| 262 | + | |
| 263 | + // D3/AI charts open in the AI Builder instead of the classic modal. | |
| 264 | + if ( $(this).data('library') === 'd3' && typeof window.vizOpenAIBuilderEdit === 'function' ) { | |
| 265 | + window.vizOpenAIBuilderEdit( $(this).attr('data-chart') ); | |
| 266 | + return false; | |
| 267 | + } | |
| 268 | + | |
| 130 | 269 | var wnd = window; |
| 131 | 270 | var view = new vmv.Chart( { |
| 132 | 271 | action: vu.edit.indexOf('&chart') != -1 ? vu.edit : vu.edit + '&chart=' + $(this).attr('data-chart') |
| 133 | 272 | } ); |
| @@ -140,10 +279,20 @@ | ||
| 140 | 279 | view.open(); |
| 141 | 280 | |
| 142 | 281 | return false; |
| 143 | 282 | }); |
| 283 | + $(".visualizer-chart-clone").on("click", function ( event ) { | |
| 284 | + if ( createPopupProBlocker( $, event ) ) { | |
| 285 | + event.preventDefault(); | |
| 286 | + } | |
| 287 | + }); | |
| 144 | 288 | |
| 145 | - $(".visualizer-chart-export").on("click", function () { | |
| 289 | + $(".visualizer-chart-export").on("click", function (event) { | |
| 290 | + | |
| 291 | + if ( createPopupProBlocker( $, event ) ) { | |
| 292 | + return; | |
| 293 | + } | |
| 294 | + | |
| 146 | 295 | $.ajax({ |
| 147 | 296 | url: $(this).attr("data-chart"), |
| 148 | 297 | method: "get", |
| 149 | 298 | success: function (data, textStatus, jqXHR) { |
| @@ -162,9 +311,12 @@ | ||
| 162 | 311 | }); |
| 163 | 312 | return false; |
| 164 | 313 | }); |
| 165 | 314 | |
| 166 | - $(".visualizer-chart-image").on("click", function () { | |
| 315 | + $(".visualizer-chart-image").on("click", function (event) { | |
| 316 | + if ( createPopupProBlocker( $, event ) ) { | |
| 317 | + return; | |
| 318 | + } | |
| 167 | 319 | $('body').trigger('visualizer:action:specificchart', {action: 'image', id: $(this).attr("data-chart"), data: null, dataObj: {name: $(this).attr("data-chart-title")}}); |
| 168 | 320 | return false; |
| 169 | 321 | }); |
| 170 | 322 | |
| @@ -172,8 +324,14 @@ | ||
| 172 | 324 | if(location.href.indexOf('vaction=addnew') !== -1){ |
| 173 | 325 | $('.add-new-chart').first().trigger('click'); |
| 174 | 326 | } |
| 175 | 327 | |
| 328 | + //if vaction=edit is found as a GET request parameter, show the modal. | |
| 329 | + if(location.href.indexOf('vaction=edit') !== -1 && location.href.indexOf('chart=') !== -1){ | |
| 330 | + const chartId = location.href.split('chart=')[1].split('&')[0]; | |
| 331 | + $('.visualizer-chart-edit').attr('data-chart', chartId).trigger('click'); | |
| 332 | + } | |
| 333 | + | |
| 176 | 334 | $(window).resize(function () { |
| 177 | 335 | clearTimeout(resizeTimeout); |
| 178 | 336 | resizeTimeout = setTimeout(function () { |
| 179 | 337 | $('.visualizer-chart-canvas').adjust(); |
| @@ -184,5 +342,30 @@ | ||
| 184 | 342 | alert( $(this).attr('data-viz-error') ); |
| 185 | 343 | }); |
| 186 | 344 | $('.visualizer-chart:not(.visualizer-chart-display), .visualizer-library-pagination').fadeIn(500); |
| 187 | 345 | }); |
| 188 | -})(jQuery, visualizer.media.view, visualizer.urls); | |
| 346 | +})(jQuery, visualizer.media.view, visualizer.urls); | |
| 347 | + | |
| 348 | + | |
| 349 | +document.querySelectorAll('.visualizer-chart').forEach(function (chart) { | |
| 350 | + const translatable = chart.querySelector('.visualizer-languages-list'); | |
| 351 | + if ( ! translatable ) { | |
| 352 | + return; | |
| 353 | + } | |
| 354 | + | |
| 355 | + const chartId = chart.querySelector('.visualizer-chart-canvas')?.id?.replace('visualizer-', ''); | |
| 356 | + | |
| 357 | + if ( ! chartId ) { | |
| 358 | + return; | |
| 359 | + } | |
| 360 | + | |
| 361 | + const translatableActions = translatable.querySelectorAll('[data-lang_code]'); | |
| 362 | + translatableActions.forEach(function (action) { | |
| 363 | + action.addEventListener('click', function () { | |
| 364 | + window?.tiTrk?.with('visualizer')?.add({ | |
| 365 | + feature: 'chart-library', | |
| 366 | + featureComponent: 'chart-language-translations-used', | |
| 367 | + groupId: chartId, | |
| 368 | + }); | |
| 369 | + }); | |
| 370 | + }); | |
| 371 | +}); | |