PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.2.6
Gallery : FooGallery v3.2.6
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / js / admin-foogallery-edit.js
foogallery / js Last commit date
admin-foogallery-attachment-autosave.js 1 month ago admin-foogallery-edit.js 1 month ago admin-foogallery-editor.js 1 month ago admin-foogallery-elementor.js 1 month ago admin-page-foogallery-extensions-modal.js 1 month ago admin-page-foogallery-extensions.js 1 month ago admin-page-foogallery-settings.js 1 month ago admin-tinymce.js 1 month ago admin-uploader.js 1 month ago foogallery-command-palette.js 1 month ago foogallery.admin.datasources.js 1 month ago foogallery.admin.min.js 1 month ago foogallery.admin.template.js 1 month ago
admin-foogallery-edit.js
1587 lines
1 FooGallery.autoEnabled = false;
2
3 (function (FOOGALLERY, $, undefined) {
4
5 FOOGALLERY.media_uploader = false;
6 FOOGALLERY.previous_post_id = 0;
7 FOOGALLERY.attachments = [];
8 FOOGALLERY.selected_attachment_id = 0;
9 FOOGALLERY.selected_gallery_template = '';
10 FOOGALLERY.dropzoneUploader = null;
11 FOOGALLERY.dropzoneInitialized = false;
12 FOOGALLERY.suppressPreviewRefresh = false;
13 FOOGALLERY.lockedSettingFieldSelector = '.foogallery_template_field[data-foogallery-locked]';
14
15 // Used for selecting files from the media modal.
16 FOOGALLERY.current_media_selector_modals = false;
17 FOOGALLERY.current_media_selector_input = false;
18
19 FOOGALLERY.enforceLockedSettingFields = function($container) {
20 $container.find(FOOGALLERY.lockedSettingFieldSelector)
21 .find(':input, range-input')
22 .attr('disabled', true);
23 };
24
25 FOOGALLERY.enableUnlockedSettingFields = function($container) {
26 $container.find(':input').not(function() {
27 return $(this).closest(FOOGALLERY.lockedSettingFieldSelector).length > 0;
28 }).prop('disabled', false);
29
30 $container.find('range-input').each(function() {
31 if ($(this).closest(FOOGALLERY.lockedSettingFieldSelector).length === 0) {
32 this.removeAttribute('disabled');
33 }
34 });
35
36 FOOGALLERY.enforceLockedSettingFields($container);
37 };
38
39 // Viewport controller for responsive preview
40 FOOGALLERY.previewActionsController = {
41 currentViewport: 'desktop',
42
43 init: function() {
44 this.bindEvents();
45 this.setViewport('desktop'); // Default to desktop
46 },
47
48 bindEvents: function() {
49 var self = this;
50
51 // Refresh buttons click
52 $('.foogallery-preview-actions .foogallery-preview-refresh-btn').on('click', function(e) {
53 e.preventDefault();
54 e.stopPropagation(); // Prevent event bubbling
55 FOOGALLERY.reloadGalleryPreview();
56 });
57
58 // Viewport button clicks
59 $('.foogallery-preview-actions .foogallery-viewport-btn').on('click', function(e) {
60 e.preventDefault();
61 e.stopPropagation(); // Prevent event bubbling
62 var viewport = $(this).data('viewport');
63 self.setViewport(viewport);
64 });
65
66 var $currentButton = $('.foogallery-items-view-switch-container a.current'),
67 currentValue = $currentButton.data('value');
68
69 if ( currentValue === 'preview' ) {
70 //show the viewport buttons
71 $('.foogallery-preview-actions').show();
72 } else {
73 $('.foogallery-preview-actions').hide();
74 }
75 },
76
77 setViewport: function(viewport) {
78 if (!viewport) return;
79
80 this.currentViewport = viewport;
81
82 // Update button states
83 $('.foogallery-viewport-btn').removeClass('active');
84 $('.foogallery-viewport-btn[data-viewport="' + viewport + '"]').addClass('active');
85
86 // Update preview wrapper classes
87 var $wrapper = $('.foogallery-preview-wrapper');
88
89 // If wrapper doesn't exist, create it by wrapping the preview container
90 if ($wrapper.length === 0) {
91 var $preview = $('.foogallery_preview_container');
92 if ($preview.length > 0) {
93 $preview.wrap('<div class="foogallery-preview-wrapper viewport-desktop"></div>');
94 $wrapper = $('.foogallery-preview-wrapper');
95 }
96 }
97
98 if ($wrapper.length > 0) {
99 $wrapper.removeClass('viewport-desktop viewport-tablet viewport-mobile')
100 .addClass('viewport-' + viewport);
101 }
102 }
103 };
104
105 FOOGALLERY.calculateAttachmentIds = function() {
106 var sorted = [];
107 $('.foogallery-attachments-list li:not(.add-attachment)').each(function() {
108 if ( $(this).data('attachment-id') ) {
109 sorted.push($(this).data('attachment-id'));
110 }
111 });
112
113 $('#foogallery_attachments').val( sorted.join(',') );
114
115 // Force the datasource to be 'media_library' so that we do not get into a strange state with other datasources that were previously selected.
116 $('#foogallery_datasource').val('media_library');
117
118 $('.foogallery_preview_container').addClass('foogallery-preview-force-refresh');
119 };
120
121 FOOGALLERY.calculateHiddenAreas = function() {
122 FOOGALLERY.showHiddenAreas( FOOGALLERY.attachments.length === 0 );
123 };
124
125 FOOGALLERY.showHiddenAreas = function( show ) {
126 if ( show ) {
127 $('.foogallery-items-add').removeClass('foogallery-hidden');
128 $('.foogallery-attachments-list-container').addClass('foogallery-hidden');
129 $('.foogallery-items-empty').removeClass('foogallery-hidden');
130 } else {
131 $('.foogallery-items-add').addClass('foogallery-hidden');
132 $('.foogallery-attachments-list-container').removeClass('foogallery-hidden');
133 $('.foogallery-items-empty').addClass('foogallery-hidden');
134 }
135 };
136
137 FOOGALLERY.initAttachments = function() {
138 var attachments = $('#foogallery_attachments').val();
139 if (attachments) {
140 FOOGALLERY.attachments = $.map(attachments.split(','), function (value) {
141 return parseInt(value, 10);
142 });
143 }
144 };
145
146 FOOGALLERY.galleryTemplateChanged = function(reloadPreview) {
147 var selectedTemplate = FOOGALLERY.getSelectedTemplate(),
148 $settingsToShow = $('.foogallery-settings-container-' + selectedTemplate),
149 $settingsToHide = $('.foogallery-settings-container').not($settingsToShow),
150 $currentTab = $settingsToHide.find('.foogallery-vertical-tab.foogallery-tab-active'),
151 currentTab = $currentTab.data('name'),
152 currentChildTab = $currentTab.find('.foogallery-vertical-child-tab.foogallery-tab-active').data('name'),
153 previousSelectedTemplate = FOOGALLERY.selected_gallery_template;
154
155 FOOGALLERY.selected_gallery_template = selectedTemplate;
156
157 var previousSuppressState = FOOGALLERY.suppressPreviewRefresh;
158 FOOGALLERY.suppressPreviewRefresh = true;
159
160 var persistedCount = 0;
161
162 //check for persist settings
163 $('.foogallery-settings-container-' + previousSelectedTemplate + ' [data-foogallery-persist]').each(function() {
164 var $this = $(this),
165 settingId = $this.data('foogallery-setting-id'),
166 settingType = $this.data('foogallery-setting-type'),
167 $newSetting = $settingsToShow.find('[data-foogallery-setting-id="' + settingId + '"]');
168
169 if ( $this.hasClass('foogallery_template_field_template_hidden') ) {
170 return;
171 }
172
173 if ( $newSetting.length ) {
174 switch ( settingType ) {
175 case 'radio':
176 case 'icon':
177 case 'htmlicon':
178 var selectedRadio = $this.find('input:checked').val();
179 if ( selectedRadio !== undefined ) {
180 var $newRadio = $newSetting.find('input[value="' + selectedRadio + '"]'),
181 currentRadio = $newSetting.find('input:checked').val();
182 if ( $newRadio.length && currentRadio !== selectedRadio ) {
183 $newRadio.prop('checked', true).trigger('change');
184 persistedCount++;
185 }
186 }
187 break;
188 case 'checkbox':
189 var isChecked = $this.find('input[type="checkbox"]').is(':checked'),
190 $newCheckbox = $newSetting.find('input[type="checkbox"]');
191 if ( $newCheckbox.length && $newCheckbox.is(':checked') !== isChecked ) {
192 $newCheckbox.prop('checked', isChecked).trigger('change');
193 persistedCount++;
194 }
195 break;
196 case 'select':
197 var selectVal = $this.find('select').val(),
198 $newSelect = $newSetting.find('select');
199 if ( $newSelect.length && $newSelect.val() !== selectVal ) {
200 $newSelect.val(selectVal).trigger('change');
201 persistedCount++;
202 }
203 break;
204 case 'text':
205 case 'textarea':
206 case 'number':
207 case 'slider':
208 var $input = $this.find(':input, range-input').first(),
209 value = $input.is('range-input') ? $input.attr('value') : $input.val(),
210 $newInput = $newSetting.find(':input, range-input').first(),
211 currentValue = $newInput.is('range-input') ? $newInput.attr('value') : ($newInput.is(':checkbox') ? $newInput.is(':checked') : $newInput.val());
212
213 if ( $newInput.length && currentValue !== value ) {
214 if ( $newInput.is('range-input') ) {
215 $newInput.attr('value', value);
216 } else if ( $newInput.is(':checkbox') ) {
217 $newInput.prop('checked', $input.is(':checked'));
218 } else {
219 $newInput.val(value);
220 }
221 $newInput.trigger('change');
222 persistedCount++;
223 }
224 break;
225 default:
226 console.log('Field type ' + settingType + ' is not supported for persisted settings.');
227 break;
228 }
229 }
230 });
231
232 console.log('Persisted ' + persistedCount + ' settings.');
233
234 FOOGALLERY.suppressPreviewRefresh = previousSuppressState;
235
236 //hide all template fields
237 $settingsToHide.hide()
238 .removeClass('foogallery-settings-container-active')
239 .find(':input, range-input').attr('disabled', true);
240
241 //show all fields for the selected template only
242 $settingsToShow.show()
243 .addClass('foogallery-settings-container-active');
244
245 FOOGALLERY.enableUnlockedSettingFields($settingsToShow);
246
247 if (currentTab) {
248 currentTab = currentTab.replace( previousSelectedTemplate, selectedTemplate );
249
250 //ensure the previously active tab is clicked
251 $settingsToShow.find('.foogallery-vertical-tab[data-name="' + currentTab + '"]').trigger('click');
252
253 if (currentChildTab) {
254 currentChildTab = currentChildTab.replace( previousSelectedTemplate, selectedTemplate );
255
256 //ensure the previously active child tab is clicked
257 $settingsToShow.find('.foogallery-vertical-child-tab[data-name="' + currentChildTab + '"]').trigger('click');
258 }
259 }
260
261 // always ensure a tab is clicked
262 if ( $settingsToShow.find('.foogallery-vertical-tab.foogallery-tab-active').length === 0 ) {
263 $settingsToShow.find('.foogallery-vertical-tab:first').trigger('click');
264 }
265
266 //trigger a change so custom template js can do something
267 FOOGALLERY.triggerTemplateChangedEvent();
268
269 if (reloadPreview) {
270 FOOGALLERY.reloadGalleryPreview();
271 }
272 };
273
274 FOOGALLERY.handleSettingFieldChange = function(reloadPreview, setContainerHeight) {
275
276 //make sure the fields that should be hidden or shown are doing what they need to do
277 FOOGALLERY.handleSettingsShowRules();
278
279 //update the gallery preview
280 FOOGALLERY.updateGalleryPreview(reloadPreview, setContainerHeight);
281 };
282
283 FOOGALLERY.updateGalleryPreview = function( initGallery, setContainerHeight ) {
284 var $preview = $('.foogallery_preview_container .foogallery'),
285 $preview_container = $('.foogallery_preview_container');
286
287 if ( setContainerHeight ) {
288 $preview_container.css('height', $preview_container.height());
289 }
290
291 //this allows any extensions to hook into the template change event
292 $('body').trigger('foogallery-gallery-preview-updated' + FOOGALLERY.getSelectedTemplate() );
293
294 //this handles all built-in templates that use the FooGallery core client side JS
295 if ( $preview.data('fg-common-fields') ) {
296 if ( initGallery ) {
297 $preview.foogallery( {}, function() {
298 $preview_container.css( 'height', '' );
299 if ( !$preview_container.find('.foogallery').data('foogallery-lightbox') ) {
300 $preview_container.find(".fg-thumb").off("click.foogallery").on("click", function (e) {
301 e.preventDefault();
302 });
303 }
304 } );
305 } else {
306 $preview.foogallery( 'layout' );
307 $preview_container.css( 'height', '' );
308 }
309 } else {
310 //reset the height to what it should be
311 $preview_container.css('height', '');
312 }
313 };
314
315 FOOGALLERY.reloadGalleryPreview = function() {
316 if ( FOOGALLERY.suppressPreviewRefresh ) {
317 return;
318 }
319
320 //make sure the fields that should be hidden or shown are doing what they need to do
321 FOOGALLERY.handleSettingsShowRules();
322
323 var $preview_container = $('.foogallery_preview_container'),
324 $preview = $preview_container.find('.foogallery');
325
326 //set the preview height so there is no jump
327 $preview_container.css('height', $preview_container.height());
328
329 //build up all the data to generate a preview
330 var $shortcodeFields = $('.foogallery-settings-container-active .foogallery-metabox-settings .foogallery_template_field[data-foogallery-preview*="shortcode"]'),
331 data = [],
332 foogallery_id = $('#post_ID').val();
333
334 if ($shortcodeFields.length) {
335 data = $shortcodeFields.find(':input').serializeArray();
336 $shortcodeFields.find('range-input:not(:disabled)').each(function() {
337 data.push({name: $(this).attr('name'), value: $(this).val()});
338 });
339 }
340
341 //clear any items just in case
342 window['foogallery-gallery-' + foogallery_id + '_items'] = null;
343
344 //add additional data for the preview
345 data.push({name: 'foogallery_id', value: foogallery_id});
346 data.push({name: 'foogallery_template', value: FOOGALLERY.getSelectedTemplate()});
347
348 //include other preview fields
349 var previewData = $('[data-foogallery-preview="include"]').serializeArray();
350 data = data.concat(previewData);
351
352 //data.push({name: 'foogallery_attachments', value: $('#foogallery_attachments').val()});
353 data.push({name: 'foogallery_datasource', value: $('#foogallery_datasource').val()});
354 data.push({name: 'foogallery_datasource_value', value: $('#_foogallery_datasource_value').val()});
355
356 //add data needed for the ajax call
357 data.push({name: 'action', value: 'foogallery_preview'});
358 data.push({name: 'foogallery_preview_nonce', value: $('#foogallery_preview').val()});
359 data.push({name: '_wp_http_referer', value: encodeURIComponent($('input[name="_wp_http_referer"]').val())});
360
361 $('#foogallery_preview_spinner').addClass('is-active');
362 $preview_container.addClass('loading');
363
364 if ($preview.length > 0){
365 var fg = $preview.data("__FooGallery__");
366 if (fg instanceof FooGallery.Template){
367 fg.destroy(false);
368 }
369 }
370
371 $.ajax({
372 type: "POST",
373 url: ajaxurl,
374 data: data,
375 cache: false,
376 success: function(data) {
377 $('.foogallery_preview_container .foogallery').foogallery("destroy");
378
379 //updated the preview
380 $preview_container.html(data);
381 $('#foogallery_preview_spinner').removeClass('is-active');
382 $preview_container.removeClass('loading foogallery-preview-force-refresh');
383
384 FOOGALLERY.updateGalleryPreview(true, true);
385 }
386 });
387 };
388
389 FOOGALLERY.handleSettingsShowRules = function() {
390 var selectedTemplate = FOOGALLERY.getSelectedTemplate();
391
392 //hide any fields that need to be hidden initially
393 $('.foogallery-settings-container-active .foogallery_template_field[data-foogallery-hidden]').hide()
394 .addClass('foogallery_template_field_template_hidden')
395 .find(':input, range-input').attr('disabled', true);
396
397 $('.foogallery-settings-container-active .foogallery_template_field[data-foogallery-show-when-field]').each(function(index, item) {
398 var $item = $(item),
399 itemLabel = $item.find('th label').text(),
400 fieldId = $item.data('foogallery-show-when-field'),
401 fieldValue = $item.data('foogallery-show-when-field-value'),
402 fieldOperator = $item.data('foogallery-show-when-field-operator'),
403 $fieldRow = $('.foogallery_template_field_template_id-' + selectedTemplate + '-' + fieldId),
404 $fieldSelector = $fieldRow.data('foogallery-value-selector'),
405 fieldValueAttribute = $fieldRow.data('foogallery-value-attribute'),
406 $field = $fieldRow.find($fieldSelector),
407 showField = false;
408
409 if ( $fieldRow.length === 0 ) {
410 // No matching field was found, which means we can un-hide
411 showField = true;
412 } else {
413
414 if ( $fieldRow.hasClass( 'foogallery_template_field_template_hidden' ) ) {
415 // The field we are checking is hidden, which means we should not rely on it
416 } else {
417
418 $field.each(function () {
419 var actualFieldValue = fieldValueAttribute ? $(this).attr(fieldValueAttribute) : $(this).val();
420
421 if (fieldOperator === '!==') {
422 if (actualFieldValue !== fieldValue) {
423 showField = true;
424 }
425 } else if (fieldOperator === 'regex') {
426 var re = new RegExp(fieldValue);
427 if (re.test(actualFieldValue)) {
428 showField = true;
429 }
430 } else if (fieldOperator === 'indexOf') {
431 if (actualFieldValue.indexOf(fieldValue) !== -1) {
432 showField = true;
433 }
434 } else if (actualFieldValue === fieldValue) {
435 showField = true;
436 }
437 });
438 }
439
440 }
441
442 if (showField) {
443 $item.show()
444 .removeClass('foogallery_template_field_template_hidden');
445
446 FOOGALLERY.enableUnlockedSettingFields($item);
447 $item.find('.colorpicker').not(function() {
448 return $(this).closest(FOOGALLERY.lockedSettingFieldSelector).length > 0;
449 }).spectrum("enable");
450 FOOGALLERY.enforceLockedSettingFields($item);
451 }
452 });
453
454 FOOGALLERY.handleSettingsTabs();
455 };
456
457 FOOGALLERY.handleSettingsTabs = function() {
458 var $activeSettings = $('.foogallery-settings-container-active'),
459 $tabs = $activeSettings.find('.foogallery-vertical-tab');
460
461 $tabs.each(function() {
462 var $tab = $(this),
463 tabName = $tab.data('name'),
464 $content = $activeSettings.find('.foogallery-tab-content[data-name="' + tabName + '"]'),
465 hasVisibleFields = $content.find('tr:not(.foogallery_template_field_template_hidden)').length > 0;
466
467 $tab.find('.foogallery-vertical-child-tab').each(function() {
468 var $childTab = $(this),
469 childName = $childTab.data('name'),
470 $childContent = $activeSettings.find('.foogallery-tab-content[data-name="' + childName + '"]'),
471 childHasVisibleFields = $childContent.find('tr:not(.foogallery_template_field_template_hidden)').length > 0;
472
473 if (childHasVisibleFields) {
474 $childTab.show();
475 hasVisibleFields = true;
476 } else {
477 $childTab.hide().removeClass('foogallery-tab-active');
478 $childContent.removeClass('foogallery-tab-active');
479 }
480 });
481
482 if (hasVisibleFields) {
483 $tab.show();
484 } else {
485 $tab.hide().removeClass('foogallery-tab-active');
486 $content.removeClass('foogallery-tab-active');
487 }
488 });
489
490 var $activeTab = $activeSettings.find('.foogallery-vertical-tab.foogallery-tab-active:visible'),
491 $visibleTabs = $activeSettings.find('.foogallery-vertical-tab:visible');
492
493 if ($activeTab.length === 0 && $visibleTabs.length) {
494 $visibleTabs.first().trigger('click');
495 } else if ($activeTab.length) {
496 var $visibleChildren = $activeTab.find('.foogallery-vertical-child-tab:visible');
497
498 if ($visibleChildren.length && $visibleChildren.filter('.foogallery-tab-active').length === 0) {
499 $visibleChildren.first().trigger('click');
500 }
501 }
502 };
503
504 FOOGALLERY.initSettings = function() {
505 //move the template selector into the metabox heading
506 var $metabox_heading = $('#foogallery_settings .hndle span');
507 //This check is done to accommodate a markup change in WP 5.5
508 if ( $metabox_heading.length === 0 ) {
509 $metabox_heading = $('#foogallery_settings .hndle');
510 $metabox_heading.addClass( 'foogallery-custom-metabox-header' );
511 }
512
513 $('.foogallery-template-selector').appendTo( $metabox_heading ).removeClass('foogallery-hidden');
514
515 //remove the loading spinner
516 $('.foogallery-gallery-items-metabox-title').remove();
517
518 var $items_metabox_heading = $('#foogallery_items .hndle span');
519 //This check is done to accommodate a markup change in WP 5.5
520 if ( $items_metabox_heading.length === 0 ) {
521 $items_metabox_heading = $('#foogallery_items .hndle');
522 $items_metabox_heading.addClass( 'foogallery-custom-metabox-header' );
523 }
524
525 $('.foogallery-items-view-switch-container').appendTo( $items_metabox_heading ).removeClass('foogallery-hidden');
526
527 //Bind to the Manage / Preview buttons
528 $('.foogallery-items-view-switch-container a').on('click', function(e) {
529 e.stopPropagation();
530
531 var $currentButton = $('.foogallery-items-view-switch-container a.current'),
532 currentSelector = $currentButton.data('container'),
533 $nextButton = $(this),
534 nextSelector = $nextButton.data('container'),
535 value = $nextButton.data('value');
536
537 //if the preview button is already selected, and we are clicking it again, then force a preview refresh
538 if ( currentSelector === nextSelector && value === 'preview' ) {
539 $('.foogallery_preview_container').addClass('foogallery-preview-force-refresh');
540 }
541
542 if ( value === 'preview' ) {
543 //show the viewport buttons
544 $('.foogallery-preview-actions').show();
545 } else {
546 $('.foogallery-preview-actions').hide();
547 }
548
549 //toggle the views
550 $(currentSelector).hide();
551 $(nextSelector).show();
552
553 //toggle the switch button
554 $currentButton.removeClass('current');
555 $nextButton.addClass('current');
556
557 //set the input so that it is saved
558 $('#foogallery_items_view_input').val(value);
559
560 if ( $('.foogallery_preview_container').is(':visible') ) {
561 FOOGALLERY.updateGalleryPreview(false, false);
562
563 //check if there is no preview
564 if ( !$.trim( $('.foogallery_preview_container').html() ) ||
565 $( '.foogallery_preview_container.foogallery-preview-force-refresh').length > 0 ) {
566 FOOGALLERY.reloadGalleryPreview();
567 }
568 }
569 });
570
571 FOOGALLERY.previewActionsController.init();
572
573 $(function() {
574
575 // Prevent inputs in settings meta box headings opening/closing contents.
576 $( '#foogallery_settings' ).find( '.hndle' ).off( 'click.postboxes' );
577
578 $( '#foogallery_settings' ).on( 'click', '.hndle', function( event ) {
579
580 // If the user clicks on some form input inside the h3 the box should not be toggled.
581 if ( $( event.target ).filter( 'input, option, label, select' ).length ) {
582 return;
583 }
584
585 $( '#foogallery_settings' ).toggleClass( 'closed' );
586 });
587
588 // Prevent inputs in items meta box headings opening/closing contents.
589 $( '#foogallery_items' ).find( '.hndle' ).off( 'click.postboxes' );
590
591 $( '#foogallery_items' ).on( 'click', '.hndle', function( event ) {
592
593 // If the user clicks on some form input inside the h3 the box should not be toggled.
594 if ( $( event.target ).filter( 'input, option, label, select' ).length ) {
595 return;
596 }
597
598 $( '#foogallery_items' ).toggleClass( 'closed' );
599 });
600 });
601
602 //hook into settings fields changes
603 $('.foogallery-metabox-settings .foogallery_template_field[data-foogallery-change-selector]').each(function(index, item) {
604 var $fieldContainer = $(item),
605 selector = $fieldContainer.data('foogallery-change-selector');
606
607 $fieldContainer.find(selector).on('change', function() {
608 if ( $fieldContainer.data('foogallery-preview') && $fieldContainer.data('foogallery-preview').indexOf('shortcode') !== -1 ) {
609 FOOGALLERY.reloadGalleryPreview();
610 } else {
611 FOOGALLERY.handleSettingFieldChange( $fieldContainer.data('foogallery-preview') && $fieldContainer.data('foogallery-preview').indexOf('class') !== -1, true );
612 }
613 });
614 });
615
616 $(document).on('change', 'input[name="foogallery_sort"]', function() {
617 $('.foogallery_preview_container').addClass('foogallery-preview-force-refresh');
618
619 if ( $('.foogallery_preview_container').is(':visible') ) {
620 FOOGALLERY.reloadGalleryPreview();
621 }
622 });
623
624 //trigger this onload too!
625 FOOGALLERY.galleryTemplateChanged(false);
626
627 //force hidden field state to be correct on load
628 FOOGALLERY.handleSettingFieldChange(true, false);
629 };
630
631 FOOGALLERY.getSelectedTemplate = function() {
632 return $('#FooGallerySettings_GalleryTemplate').val();
633 };
634
635 FOOGALLERY.triggerTemplateChangedEvent = function() {
636 $('body').trigger('foogallery-gallery-template-changed-' + FOOGALLERY.getSelectedTemplate() );
637 };
638
639 FOOGALLERY.triggerAttachmentsChangedEvent = function(action, attachmentId) {
640 var attachments = $.isArray(FOOGALLERY.attachments) ? FOOGALLERY.attachments.slice(0) : [];
641
642 $('body').trigger('foogallery-attachments-changed', [attachments, action || '', parseInt(attachmentId, 10) || 0]);
643 };
644
645 FOOGALLERY.addAttachmentToGalleryList = function(attachment) {
646
647 if ($.inArray(attachment.id, FOOGALLERY.attachments) !== -1) return;
648
649 var $template = $($('#foogallery-attachment-template').val());
650
651 $template.attr('data-attachment-id', attachment.id);
652
653 $template.find('img').attr('src', attachment.src);
654
655 if (attachment.subtype) {
656 $template.find('.attachment-preview.type-image').addClass('subtype-' + attachment.subtype);
657 }
658
659 if ( $('.foogallery-attachments-list').hasClass('foogallery-add-media-button-start') ) {
660 $('.foogallery-attachments-list .datasource-medialibrary').after($template);
661 } else {
662 $('.foogallery-attachments-list .datasource-medialibrary').before($template);
663 }
664
665 FOOGALLERY.attachments.push( attachment.id );
666
667 FOOGALLERY.calculateAttachmentIds();
668
669 FOOGALLERY.calculateHiddenAreas();
670
671 $('.foogallery_preview_container').addClass('foogallery-preview-force-refresh');
672
673 FOOGALLERY.triggerAttachmentsChangedEvent('add', attachment.id);
674 };
675
676 FOOGALLERY.removeAttachmentFromGalleryList = function(id) {
677 var index = $.inArray(id, FOOGALLERY.attachments);
678 if (index !== -1) {
679 FOOGALLERY.attachments.splice(index, 1);
680 }
681 $('.foogallery-attachments-list [data-attachment-id="' + id + '"]').remove();
682
683 FOOGALLERY.calculateAttachmentIds();
684
685 FOOGALLERY.calculateHiddenAreas();
686
687 FOOGALLERY.triggerAttachmentsChangedEvent('remove', id);
688 };
689
690 FOOGALLERY.showAttachmentInfoModal = function(id) {
691 FOOGALLERY.openMediaModal( id );
692 };
693
694 FOOGALLERY.openMediaModal = function(selected_attachment_id) {
695 if (!selected_attachment_id) { selected_attachment_id = 0; }
696
697 var modal_style = $('#foogallery-image-edit-modal').data('modal_style');
698
699 if ( selected_attachment_id > 0 && modal_style === 'on') {
700 FOOGALLERY.openAttachmentModal( selected_attachment_id );
701 } else {
702 FOOGALLERY.selected_attachment_id = selected_attachment_id;
703
704 if (FOOGALLERY.media_uploader !== false){
705 FOOGALLERY.media_uploader.open();
706 return;
707 }
708
709 var createModal = $.isFunction(wp.foogallery) ? wp.foogallery : wp.media;
710
711 // Create our FooGallery media frame (use the child Select when available so extensions can override it).
712 FOOGALLERY.media_uploader = createModal({
713 frame: ( wp.foogallery && wp.foogallery.media && wp.foogallery.media.Select ) ? "select-child" : "select",
714 multiple: 'add',
715 title: FOOGALLERY.mediaModalTitle,
716 button: {
717 text: FOOGALLERY.mediaModalButtonText
718 },
719 library: {
720 type: "image"
721 }
722 }).on("select", function(){
723 var attachments = FOOGALLERY.media_uploader.state().get('selection').toJSON();
724
725 $.each(attachments, function(i, item) {
726 if ( item && item.id ) {
727 var attachment = {
728 id: item.id,
729 src: null,
730 subtype: null
731 };
732 if ( item.sizes && item.sizes.thumbnail) {
733 attachment.src = item.sizes.thumbnail.url;
734 } else {
735 //thumbnail could not be found for whatever reason. Default to the full image URL
736 attachment.src = item.url;
737 }
738 if ( item.subtype ) {
739 attachment.subtype = item.subtype;
740 }
741
742 FOOGALLERY.addAttachmentToGalleryList(attachment);
743 } else {
744 //there was a problem adding the item! Move on to the next
745 alert( 'There was a problem adding the item to the gallery!' );
746 }
747 });
748 })
749 .on( 'open', function() {
750 var selection = FOOGALLERY.media_uploader.state().get('selection');
751 if (selection && !$.isFunction(wp.foogallery)) {
752 //clear any previous selections
753 selection.reset();
754 }
755
756 if (FOOGALLERY.selected_attachment_id > 0) {
757 var attachment = wp.media.attachment(FOOGALLERY.selected_attachment_id);
758 attachment.fetch();
759 selection.add( attachment ? [ attachment ] : [] );
760 } else {
761 //would be nice to have all previously added media selected
762 }
763 });
764
765 // Finally, open the modal
766 FOOGALLERY.media_uploader.open();
767 }
768 };
769
770 FOOGALLERY.initUsageMetabox = function() {
771 $('#foogallery_create_page').on('click', function(e) {
772 e.preventDefault();
773
774 $('#foogallery_create_page_spinner').addClass('is-active');
775 var data = 'action=foogallery_create_gallery_page' +
776 '&foogallery_id=' + $('#post_ID').val() +
777 '&foogallery_create_gallery_page_nonce=' + $('#foogallery_create_gallery_page_nonce').val() +
778 '&_wp_http_referer=' + encodeURIComponent($('input[name="_wp_http_referer"]').val());
779
780 $.ajax({
781 type: "POST",
782 url: ajaxurl,
783 data: data,
784 success: function(data) {
785 if (data && data.success) {
786 //refresh page
787 location.reload();
788 return;
789 }
790
791 var message = data && data.data && data.data.message ? data.data.message : 'Unable to create page.';
792 alert(message);
793 $('#foogallery_create_page_spinner').removeClass('is-active');
794 },
795 error: function(xhr) {
796 var message = xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message ? xhr.responseJSON.data.message : 'Unable to create page.';
797 alert(message);
798 $('#foogallery_create_page_spinner').removeClass('is-active');
799 }
800 });
801 });
802 };
803
804 FOOGALLERY.initThumbCacheMetabox = function() {
805 $('#foogallery_clear_thumb_cache').on('click', function(e) {
806 e.preventDefault();
807
808 $('#foogallery_clear_thumb_cache_spinner').addClass('is-active');
809 var data = 'action=foogallery_clear_gallery_thumb_cache' +
810 '&foogallery_id=' + $('#post_ID').val() +
811 '&foogallery_clear_gallery_thumb_cache_nonce=' + $('#foogallery_clear_gallery_thumb_cache_nonce').val() +
812 '&_wp_http_referer=' + encodeURIComponent($('input[name="_wp_http_referer"]').val());
813
814 $.ajax({
815 type: "POST",
816 url: ajaxurl,
817 data: data,
818 success: function(data) {
819 var message = data && data.data && data.data.message ? data.data.message : data;
820 alert(message);
821 $('#foogallery_clear_thumb_cache_spinner').removeClass('is-active');
822 },
823 error: function(xhr) {
824 var message = xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message ? xhr.responseJSON.data.message : 'There was a problem clearing the thumbnail cache.';
825 alert(message);
826 $('#foogallery_clear_thumb_cache_spinner').removeClass('is-active');
827 }
828 });
829 });
830 };
831
832 FOOGALLERY.initShortcodeHelpers = function() {
833 var $toggle = $('#foogallery_toggle_shortcode_helpers');
834
835 if ( !$toggle.length ) {
836 return;
837 }
838
839 var helperSelector = '.foogallery-shortcode-argument-helper, .foogallery-shortcode-choice-helper, .foogallery-shortcode-select-helper',
840 copyHelperText = function(text) {
841 if (navigator.clipboard && navigator.clipboard.writeText) {
842 navigator.clipboard.writeText(text).catch(fallbackCopy);
843 } else {
844 fallbackCopy();
845 }
846
847 function fallbackCopy() {
848 var $temp = $('<textarea class="foogallery-shortcode-hidden"></textarea>');
849 $temp.val(text)
850 .attr('readonly', 'readonly')
851 .css({ position: 'absolute', left: '-9999px', top: '0' });
852 $('body').append($temp);
853 $temp[0].select();
854
855 try {
856 document.execCommand('copy');
857 } catch (err) {
858 console.log('Oops, unable to copy!');
859 }
860
861 $temp.remove();
862 }
863 };
864
865 $(document)
866 .off('mousedown.foogalleryShortcodeHelper', helperSelector)
867 .on('mousedown.foogalleryShortcodeHelper', helperSelector, function(e) {
868 e.preventDefault();
869 e.stopPropagation();
870 })
871 .off('click.foogalleryShortcodeHelper', helperSelector)
872 .on('click.foogalleryShortcodeHelper', helperSelector, function(e) {
873 e.preventDefault();
874 e.stopPropagation();
875
876 copyHelperText($(this).find('code').text());
877 });
878
879 $('.foogallery-metabox-settings label[data-setting]').each(function() {
880 var $label = $(this),
881 settingId = $label.data('setting');
882
883 if ( !settingId || $label.find('.foogallery-shortcode-argument-helper').length ) {
884 return;
885 }
886
887 $label.append(
888 $('<span class="foogallery-shortcode-argument-helper" aria-hidden="true"></span>').append(
889 $('<code></code>').text(settingId)
890 )
891 );
892 });
893
894 $('.foogallery_metabox_field-radio > label').each(function() {
895 var $label = $(this),
896 $input = $label.find('input[type="radio"]').first(),
897 choiceValue = $input.val(),
898 choiceHelperText = choiceValue === '' ? '<empty>' : choiceValue;
899
900 if ( !$input.length || typeof choiceValue === 'undefined' || $label.find('.foogallery-shortcode-choice-helper').length ) {
901 return;
902 }
903
904 $label.append(
905 $('<span class="foogallery-shortcode-choice-helper foogallery-shortcode-choice-helper-radio" aria-hidden="true"></span>').append(
906 $('<code></code>').text(choiceHelperText)
907 )
908 );
909 });
910
911 $('.foogallery_metabox_field-htmlicon > label[for]').each(function() {
912 var $label = $(this),
913 inputId = $label.attr('for'),
914 $input = $('#' + inputId),
915 choiceValue = $input.val(),
916 choiceHelperText = choiceValue === '' ? '<empty>' : choiceValue;
917
918 if ( !$input.length || typeof choiceValue === 'undefined' || $label.next('.foogallery-shortcode-choice-helper-htmlicon').length ) {
919 return;
920 }
921
922 var $helper = $('<span class="foogallery-shortcode-choice-helper foogallery-shortcode-choice-helper-htmlicon" aria-hidden="true"></span>').append(
923 $('<code></code>').text(choiceHelperText)
924 );
925
926 $label.after($helper);
927
928 if ( !$input.parent().hasClass('foogallery-shortcode-choice-group-htmlicon') ) {
929 $input.add($label).add($helper).wrapAll('<span class="foogallery-shortcode-choice-group-htmlicon"></span>');
930 }
931 });
932
933 $('.foogallery_metabox_field-select select').each(function() {
934 var $select = $(this),
935 $helper = $select.siblings('.foogallery-shortcode-select-helper'),
936 updateHelper = function() {
937 var selectedValue = $select.val(),
938 helperText = selectedValue === '' ? '<empty>' : selectedValue;
939
940 if ( typeof selectedValue === 'undefined' ) {
941 return;
942 }
943
944 $helper.find('code').text(helperText);
945 };
946
947 if ( !$helper.length ) {
948 $helper = $('<span class="foogallery-shortcode-select-helper" aria-hidden="true"></span>').append(
949 $('<code></code>')
950 );
951 $select.after($helper);
952 }
953
954 updateHelper();
955 $select.off('change.foogalleryShortcodeHelper').on('change.foogalleryShortcodeHelper', updateHelper);
956 });
957
958 var showLabel = $toggle.data('show-label') || 'Show Argument Helpers',
959 hideLabel = $toggle.data('hide-label') || 'Hide Argument Helpers',
960 stateClass = 'foogallery-show-argument-helpers',
961 updateState = function(show) {
962 $('body').toggleClass(stateClass, show);
963 $toggle.attr('aria-expanded', show ? 'true' : 'false').text(show ? hideLabel : showLabel);
964 };
965
966 updateState(false);
967
968 $toggle.on('click', function(e) {
969 e.preventDefault();
970 updateState( !$('body').hasClass(stateClass) );
971 });
972 };
973
974 FOOGALLERY.adminReady = function () {
975 $('.upload_image_button').on('click', function(e) {
976 e.preventDefault();
977 FOOGALLERY.mediaModalTitle = $(this).data( 'uploader-title' );
978 FOOGALLERY.mediaModalButtonText = $(this).data( 'uploader-button-text' );
979 FOOGALLERY.openMediaModal(0);
980 });
981
982 $('.remove_all_media').on('click', function(e) {
983 $('.foogallery-attachments-list a.remove').trigger('click');
984 });
985
986 $(document).on('foogallery-datasource-changed', function(e, activeDatasource) {
987 FOOGALLERY.showHiddenAreas( activeDatasource === 'media_library' );
988 });
989
990 FOOGALLERY.initAttachments();
991
992 FOOGALLERY.initSettings();
993
994 FOOGALLERY.initUsageMetabox();
995
996 FOOGALLERY.initThumbCacheMetabox();
997
998 FOOGALLERY.initShortcodeHelpers();
999
1000 FOOGALLERY.initAttachmentModal();
1001
1002 FOOGALLERY.initMediaSelector();
1003
1004 $('.foogallery-attachments-list')
1005 .on('click' ,'a.remove', function(e) {
1006 e.preventDefault();
1007 var $selected = $(this).parents('li:first'),
1008 attachment_id = $selected.data('attachment-id');
1009 FOOGALLERY.removeAttachmentFromGalleryList(attachment_id);
1010 })
1011 .on('click' ,'a.info', function() {
1012 var $selected = $(this).parents('li:first'),
1013 attachment_id = $selected.data('attachment-id');
1014 FOOGALLERY.showAttachmentInfoModal(attachment_id);
1015 })
1016 .sortable({
1017 items: 'li:not(.add-attachment)',
1018 distance: 10,
1019 placeholder: 'attachment placeholder',
1020 stop : function() {
1021 FOOGALLERY.calculateAttachmentIds();
1022 }
1023 });
1024
1025 //init any colorpickers
1026 $('.colorpicker').spectrum({
1027 preferredFormat: "rgb",
1028 showInput: true,
1029 clickoutFiresChange: true
1030 });
1031
1032 if (typeof IntersectionObserver === "undefined") {
1033 $(".foogallery-attachments-list .attachment .thumbnail img").each(function(i, img){
1034 var $img = $(img);
1035 $img.attr("src", $img.data("src"));
1036 });
1037 } else {
1038 //lazy loading of images on the gallery edit page
1039 var io = new IntersectionObserver(function (entries) {
1040 entries.forEach(function (entry) {
1041 if (entry.isIntersecting) {
1042 var $target = $(entry.target);
1043 $target.attr("src", $target.data("src"));
1044 io.unobserve(entry.target);
1045 }
1046 });
1047 }, {
1048 root: $(".foogallery-attachments-list").get(0)
1049 });
1050
1051 $(".foogallery-attachments-list .attachment .thumbnail img").each(function(i, img){
1052 io.observe(img);
1053 });
1054 }
1055
1056 $('.foogallery-admin-promo-dismiss').on('click', function(e) {
1057 e.preventDefault();
1058 alert( 'If you want to turn off these promotional messages forever, goto FooGallery Settings -> Advanced, and set the "Disable PRO Promotions" setting. Thank you for using FooGallery :)')
1059 } );
1060
1061 $('.foogallery-attachment-modal-toggle').on('click', function(e) {
1062 e.preventDefault();
1063 $(this).attr('disabled', 'disabled');
1064
1065 $.ajax({
1066 type: "POST",
1067 url: ajaxurl,
1068 data: {
1069 action: 'foogallery_attachment_modal_toggle',
1070 'nonce' : $(this).data('nonce')
1071 },
1072 success: function(data) {
1073 alert(data);
1074 //refresh page
1075 location.reload();
1076 }
1077 });
1078 } );
1079
1080 FOOGALLERY.initDropzone();
1081 };
1082
1083 FOOGALLERY.initMediaSelector = function() {
1084 $(document).on('click', '.foogallery-media-selector-choose', function(e){
1085 e.preventDefault();
1086
1087 var $el = $( this );
1088
1089 if ( $el.data( 'input' ) ) {
1090 // The input has been set by data-input="#my-input"
1091 FOOGALLERY.current_media_selector_input = $( $el.data('input') );
1092 } else {
1093 // Try and be smart and find the input.
1094 FOOGALLERY.current_media_selector_input = $el.parent().find( 'input[type="text"]')
1095 }
1096
1097 // If we do not have an input then get out!
1098 if ( FOOGALLERY.current_media_selector_input.length === 0 ) {
1099 return;
1100 }
1101
1102 var
1103 modalTitle = $el.data('modal-title') ? $el.data( 'modal-title' ) : 'Choose Image',
1104 modalButton = $el.data('modal-button') ? $el.data( 'modal-button' ) : 'Select Image',
1105 modalMultiple = $el.data('modal-multiple' ) ? $el.data( 'modal-multiple' ) === 'yes' : true;
1106 states = [
1107 // Main states.
1108 new wp.media.controller.Library( {
1109 library: wp.media.query(),
1110 multiple: modalMultiple,
1111 title: modalTitle,
1112 priority: 20,
1113 filterable: 'uploaded',
1114 } ),
1115 ];
1116
1117 // keep an array of modals, based off the modal title
1118
1119 //first check if modals have been initialized
1120 FOOGALLERY.current_media_selector_modals = FOOGALLERY.current_media_selector_modals || {};
1121
1122 // Check if we have a modal for this title
1123 if ( FOOGALLERY.current_media_selector_modals[modalTitle] ) {
1124 FOOGALLERY.current_media_selector_modals[modalTitle].open();
1125 return;
1126 }
1127
1128 // Create the media frame.
1129 FOOGALLERY.current_media_selector_modals[modalTitle] = wp.media.frames.downloadable_file = wp.media(
1130 {
1131 // Set the title of the modal.
1132 title: modalTitle,
1133 library: {
1134 type: '',
1135 },
1136 button: {
1137 text: modalButton,
1138 },
1139 multiple: modalMultiple,
1140 states: states,
1141 }
1142 );
1143
1144 // When an image is selected, run a callback.
1145 FOOGALLERY.current_media_selector_modals[modalTitle].on( 'select', function () {
1146 var file_path = '',
1147 selection = FOOGALLERY.current_media_selector_modals[modalTitle].state().get( 'selection' );
1148
1149 selection.map( function ( attachment ) {
1150 attachment = attachment.toJSON();
1151 if ( attachment.url ) {
1152 file_path = attachment.url;
1153 }
1154 } );
1155
1156 FOOGALLERY.current_media_selector_input.val( file_path ).trigger( 'change' );
1157 } );
1158
1159 // Finally, open the modal.
1160 FOOGALLERY.current_media_selector_modals[modalTitle].open();
1161 });
1162
1163 $(document).on('click', '.foogallery-media-selector-clear', function(e){
1164 e.preventDefault();
1165
1166 var $el = $( this );
1167
1168 if ( $el.data( 'input' ) ) {
1169 // The input has been set by data-input="#my-input"
1170 FOOGALLERY.current_media_selector_input = $( $el.data('input') );
1171 } else {
1172 // Try and be smart and find the input.
1173 FOOGALLERY.current_media_selector_input = $el.parent().find( 'input[type="text"]')
1174 }
1175
1176 // If we do not have an input then get out!
1177 if ( FOOGALLERY.current_media_selector_input.length === 0 ) {
1178 return;
1179 }
1180
1181 // Clear the input.
1182 FOOGALLERY.current_media_selector_input.val( '' ).trigger( 'change' );
1183 });
1184 };
1185
1186 FOOGALLERY.initAttachmentModal = function() {
1187 //close attachments modal
1188 jQuery('#foogallery-image-edit-modal .media-modal-close').on('click', function() {
1189 var $content = jQuery('#foogallery-image-edit-modal'),
1190 $wrapper = jQuery('#foogallery-image-edit-modal .media-frame-content .attachment-details'),
1191 $loader = jQuery('#foogallery-image-edit-modal .media-frame-content .spinner');
1192
1193 $content.hide();
1194 $wrapper.addClass('not-loaded');
1195 $loader.addClass('is-active');
1196 });
1197
1198 // Attachment taxonomy button toggles
1199 $(document).on('click', '#foogallery-panel-taxonomies ul a', function(e){
1200 var term_id = parseInt( $(this).data('term-id') ),
1201 $ul = $(this).parents('ul').first();
1202 if ( !isNaN(term_id) && term_id > 0 ) {
1203 $(this).toggleClass('button-primary');
1204 var terms = [];
1205 $ul.find('a.button-primary').each(function () {
1206 term_id = $(this).data('term-id');
1207 terms.push(term_id);
1208 });
1209 var terms_str = terms.toString();
1210 $('#foogallery_attachment_taxonomy_' + $ul.data('taxonomy') + '_selected').val(terms_str);
1211 } else {
1212 var action = $(this).data('action');
1213 if ( action === 'add' ) {
1214 $ul.find('.foogallery_attachment_taxonomy_add').toggle();
1215 $ul.find('.taxonomy_add input').focus();
1216 } else if ( action === 'save' || action === 'cancel' ){
1217 var term_to_add = $ul.find('input.foogallery_attachment_taxonomy_add').val();
1218 if ( term_to_add && term_to_add.length > 0 && action === 'save' ) {
1219 var nonce = $('#foogallery-panel-taxonomies').data('nonce');
1220 $.ajax({
1221 type: "POST",
1222 url: ajaxurl,
1223 data: {
1224 'img_id': $('.foogallery-image-edit-main').data('img_id'),
1225 'nonce': nonce,
1226 'action': 'foogallery_attachment_modal_taxonomy_add',
1227 'taxonomy' : $ul.data('taxonomy'),
1228 'term': term_to_add
1229 },
1230 cache: false,
1231 success: function(res) {
1232 var html = '<li><a href="javascript:void(0);" class="button button-small recently-added" data-term-id="' + res.data.id + '">' + res.data.name + '</a></li>';
1233 $ul.find('.taxonomy_add').before(html);
1234 $ul.find('a.recently-added').removeClass('recently-added').trigger('click');
1235 $ul.find('input.foogallery_attachment_taxonomy_add').val('');
1236 $ul.find('.foogallery_attachment_taxonomy_add').toggle();
1237 }
1238 });
1239 } else {
1240 $ul.find('input.foogallery_attachment_taxonomy_add').val('');
1241 $ul.find('.foogallery_attachment_taxonomy_add').toggle();
1242 }
1243 } else if ( action === 'remove' ) {
1244 // Deselect all taxonomies
1245 $ul.find('a.button.button-small').removeClass('button-primary');
1246 $('#foogallery_attachment_taxonomy_' + $ul.data('taxonomy') + '_selected').val('');
1247 }
1248 }
1249 });
1250
1251 $(document).on('click', '#attachments-data-save-btn', function(e){
1252 e.preventDefault();
1253 FOOGALLERY.saveAttachmentModal();
1254 });
1255
1256 $(document).on('click', '.copy-attachment-file-url', function(e) {
1257 $('#attachments-foogallery-file-url').select();
1258 document.execCommand('copy');
1259 });
1260
1261 $(document).on('click', '#foogallery-image-edit-modal .foogallery-img-modal-tab-wrapper', function(e) {
1262 var panel = '#' + $(this).data('tab_id');
1263 $('#foogallery-image-edit-modal .tab-panel').removeClass('active');
1264 $(panel).addClass('active');
1265 $('#foogallery-image-edit-modal').data( 'current_tab', $(this).data('tab_id') );
1266 });
1267
1268 $(document).on('click', '#foogallery_clear_img_thumb_cache', function(e) {
1269 e.preventDefault();
1270
1271 $('#foogallery_clear_img_thumb_cache_spinner').addClass('is-active');
1272 var data = 'action=foogallery_clear_attachment_thumb_cache' +
1273 '&attachment_id=' + $('.foogallery-image-edit-main').data('img_id') +
1274 '&foogallery_clear_attachment_thumb_cache_nonce=' + $('#foogallery_clear_attachment_thumb_cache_nonce').val() +
1275 '&_wp_http_referer=' + encodeURIComponent($('input[name="_wp_http_referer"]').val());
1276
1277 $.ajax({
1278 type: "POST",
1279 url: ajaxurl,
1280 data: data,
1281 success: function(data) {
1282 var message = data && data.data && data.data.message ? data.data.message : data;
1283 alert(message);
1284 $('#foogallery_clear_img_thumb_cache_spinner').removeClass('is-active');
1285 },
1286 error: function(xhr) {
1287 var message = xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message ? xhr.responseJSON.data.message : 'There was a problem clearing the thumbnail cache.';
1288 alert(message);
1289 $('#foogallery_clear_img_thumb_cache_spinner').removeClass('is-active');
1290 }
1291 });
1292 });
1293
1294 $(document).on('click', '#foogallery-image-edit-modal .edit-media-header button:not(.media-modal-close)', function(e) {
1295 e.preventDefault();
1296
1297 $(this).attr( 'disabled', 'disabled' );
1298 var selected_attachment_id = parseInt( $(this).data('attachment') ),
1299 autosave = jQuery('#attachment-modal-autosave').is(':checked');
1300 if ( selected_attachment_id > 0 ) {
1301
1302 // Check if we must save the data first!
1303 if ( autosave ) {
1304 FOOGALLERY.saveAttachmentModal(function () {
1305 $(this).prop( 'disabled', false );
1306 FOOGALLERY.openAttachmentModal(selected_attachment_id);
1307 });
1308 } else {
1309 $(this).prop( 'disabled', false );
1310 FOOGALLERY.openAttachmentModal(selected_attachment_id);
1311 }
1312 }
1313 });
1314 };
1315
1316 FOOGALLERY.saveAttachmentModal = function(callback) {
1317 var data = $('#foogallery_attachment_modal_save_form').serialize(),
1318 $spinner = $('.foogallery-image-edit-footer .spinner');
1319 $spinner.addClass('is-active');
1320 $.ajax({
1321 type: "POST",
1322 url: ajaxurl,
1323 data: data,
1324 cache: false,
1325 success: function(res) {
1326 $spinner.removeClass('is-active');
1327 if ( $.isFunction(callback) ) {
1328 callback();
1329 }
1330 }
1331 });
1332 };
1333
1334 FOOGALLERY.openAttachmentModal = function(img_id) {
1335 var $content = jQuery('#foogallery-image-edit-modal'),
1336 $wrapper = jQuery('#foogallery-image-edit-modal .media-frame-content .attachment-details'),
1337 $loader = jQuery('#foogallery-image-edit-modal .media-frame-content .spinner'),
1338 nonce = $content.data('nonce'),
1339 gallery_id = $content.data('gallery_id'),
1340 current_tab = $content.data('current_tab');
1341
1342 $content.show();
1343 $wrapper.addClass('not-loaded').html('<div class="spinner is-active"></div>');
1344 $wrapper.css({'grid-template-columns': '1fr'});
1345 $loader.addClass('is-active');
1346
1347 jQuery.ajax({
1348 type: "POST",
1349 url: ajaxurl,
1350 data: {
1351 'img_id': img_id,
1352 'gallery_id': gallery_id,
1353 'override_attachments': $('#foogallery_attachments').val(),
1354 'current_tab': current_tab,
1355 'nonce': nonce,
1356 'action': 'foogallery_attachment_modal_open'
1357 },
1358 success: function(json) {
1359 jQuery('#foogallery-image-edit-modal .edit-media-header button.left')
1360 .attr('disabled', !json.prev_slide)
1361 .data('attachment', json.prev_img_id);
1362 jQuery('#foogallery-image-edit-modal .edit-media-header button.right')
1363 .attr('disabled', !json.next_slide)
1364 .data('attachment', json.next_img_id);
1365
1366 if ( json.override_thumbnail ) {
1367 jQuery('#foogallery-image-edit-modal .tab-panels .settings span.setting.override-thumbnail').addClass('is-override-thumbnail');
1368 jQuery('#foogallery-image-edit-modal .tab-panels .settings span.setting.override-thumbnail-preview').addClass('is-override-thumbnail');
1369 }
1370
1371 jQuery('#foogallery-image-edit-modal .media-modal-content .edit-attachment-frame .media-frame-content .attachment-details').html(json.html);
1372
1373 if ( json.current_tab ) {
1374 jQuery('.foogallery-img-modal-tab-wrapper[data-tab_id="' + json.current_tab + '"] input').trigger('click');
1375 }
1376
1377 $wrapper.removeClass('not-loaded');
1378 $wrapper.css({'grid-template-columns': '1fr 2fr'});
1379 $loader.removeClass('is-active');
1380 },
1381 });
1382 };
1383
1384 FOOGALLERY.initDropzone = function() {
1385 const $metaBox = $('#foogallery_items .inside');
1386
1387 if (!$metaBox.length || typeof wp === 'undefined' || !wp.Uploader) {
1388 return;
1389 }
1390
1391 if (FOOGALLERY.dropzoneInitialized) {
1392 return;
1393 }
1394
1395 let $dropZone = $metaBox.find('.foogallery-dropzone');
1396 if (!$dropZone.length) {
1397 const dropzoneMessage = (FOOGALLERY.il8n && FOOGALLERY.il8n.dropzone_message) || 'Drop images here to upload';
1398
1399 $dropZone = $(
1400 '<div class="foogallery-dropzone">' +
1401 '<p class="message"></p>' +
1402 '<div class="upload-progress"></div>' +
1403 '</div>'
1404 );
1405 $metaBox.prepend($dropZone);
1406 $dropZone.find('.message').text(dropzoneMessage);
1407 }
1408
1409 FOOGALLERY.dropzoneInitialized = true;
1410
1411 const $progress = $dropZone.find('.upload-progress').css('width', '0').hide();
1412 const $uploadButton = $metaBox.find('.foogallery-upload-direct');
1413
1414 let dragDepth = 0;
1415 let uploadActive = false;
1416
1417 const isFileDrag = function(event) {
1418 const dt = event && event.originalEvent && event.originalEvent.dataTransfer;
1419
1420 if (!dt || !dt.types) {
1421 return false;
1422 }
1423
1424 if (typeof dt.types.indexOf === 'function') {
1425 return dt.types.indexOf('Files') !== -1;
1426 }
1427
1428 if (typeof dt.types.contains === 'function') {
1429 return dt.types.contains('Files');
1430 }
1431
1432 return dt.types === 'Files';
1433 };
1434
1435 $(document)
1436 .on('dragenter.foogalleryDropzone', function(event) {
1437 if (!isFileDrag(event)) {
1438 return;
1439 }
1440
1441 dragDepth++;
1442 $dropZone.addClass('visible');
1443 })
1444 .on('dragleave.foogalleryDropzone', function(event) {
1445 if (!isFileDrag(event)) {
1446 return;
1447 }
1448
1449 dragDepth = Math.max(dragDepth - 1, 0);
1450 if (dragDepth === 0) {
1451 if (uploadActive) {
1452 $dropZone.removeClass('drag-over');
1453 } else {
1454 $dropZone.removeClass('visible drag-over');
1455 }
1456 }
1457 })
1458 .on('dragover.foogalleryDropzone', function(event) {
1459 if (!isFileDrag(event)) {
1460 return;
1461 }
1462
1463 event.preventDefault();
1464 })
1465 .on('drop.foogalleryDropzone', function(event) {
1466 if (!isFileDrag(event)) {
1467 return;
1468 }
1469
1470 event.preventDefault();
1471
1472 const isInsideDropZone = $dropZone.is(event.target) || $.contains($dropZone[0], event.target);
1473
1474 dragDepth = 0;
1475 $dropZone.removeClass('drag-over');
1476
1477 if (!isInsideDropZone) {
1478 uploadActive = false;
1479 $dropZone.removeClass('visible');
1480 } else {
1481 uploadActive = true;
1482 }
1483 });
1484
1485 $dropZone
1486 .on('dropzone:enter.foogalleryDropzone', function() {
1487 $dropZone.addClass('visible drag-over');
1488 })
1489 .on('dropzone:leave.foogalleryDropzone', function() {
1490 if (dragDepth === 0) {
1491 $dropZone.removeClass('visible drag-over');
1492 }
1493 });
1494
1495 const finalizeUI = function() {
1496 setTimeout(function() {
1497 $dropZone.removeClass('drag-over visible');
1498 dragDepth = 0;
1499 uploadActive = false;
1500
1501 $progress.fadeOut(200, function() {
1502 $(this).css('width', '0');
1503 });
1504 }, 400);
1505 };
1506
1507 try {
1508 const uploaderSettings = {
1509 container: $dropZone,
1510 dropzone: $dropZone,
1511 params: {
1512 post_id: $('#post_ID').val()
1513 },
1514 added: function() {
1515 uploadActive = true;
1516 $dropZone.addClass('visible');
1517 $progress.show().css('width', '0');
1518 },
1519 progress: function(attachment) {
1520 let percent = 0;
1521
1522 if (this.uploader && this.uploader.total && typeof this.uploader.total.percent === 'number') {
1523 percent = this.uploader.total.percent;
1524 } else if (attachment && attachment.get) {
1525 percent = attachment.get('percent') || 0;
1526 }
1527
1528 $progress.css('width', percent + '%');
1529 },
1530 success: function(attachment) {
1531 const data = attachment && attachment.toJSON ? attachment.toJSON() : attachment;
1532
1533 if (!data || !data.id) {
1534 return;
1535 }
1536
1537 const subtype = data.subtype || (data.mime ? data.mime.split('/')[1] : null);
1538
1539 FOOGALLERY.addAttachmentToGalleryList({
1540 id: data.id,
1541 src: (data.sizes && data.sizes.thumbnail && data.sizes.thumbnail.url) ||
1542 data.icon ||
1543 data.url ||
1544 '',
1545 subtype: subtype
1546 });
1547
1548 if (!wp.Uploader.queue || !wp.Uploader.queue.length) {
1549 finalizeUI();
1550 }
1551 },
1552 error: function(message, responseData, file) {
1553 console.error('FooGallery uploader error:', message || responseData, file);
1554
1555 if (!wp.Uploader.queue || !wp.Uploader.queue.length) {
1556 finalizeUI();
1557 }
1558 }
1559 };
1560
1561 if ($uploadButton.length) {
1562 uploaderSettings.browser = $uploadButton;
1563 }
1564
1565 const uploader = new wp.Uploader(uploaderSettings);
1566
1567 FOOGALLERY.dropzoneUploader = uploader;
1568 } catch (error) {
1569 console.error('FooGallery failed to initialize dropzone uploader', error);
1570 }
1571 };
1572
1573 // $(document).ready(function () {
1574
1575 // FOOGALLERY.initAttachments();
1576
1577 // FOOGALLERY.galleryTemplateChanged(false);
1578 // });
1579
1580 }(window.FOOGALLERY = window.FOOGALLERY || {}, jQuery));
1581
1582 FooGallery.utils.ready(function ($) {
1583 if ( $('#foogallery_attachments').length > 0 ) {
1584 FOOGALLERY.adminReady();
1585 }
1586 });
1587