This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from xwp/feature/25-featured_block
[Gutenberg] Add "Featured Destinations" homepage block
- Loading branch information
Showing
5 changed files
with
699 additions
and
144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* global wp */ | ||
|
||
/** | ||
* JS functions for terms in admin UI. | ||
*/ | ||
|
||
( function( $ ) { | ||
'use strict'; | ||
|
||
let component = {}; | ||
|
||
/** | ||
* Init. | ||
*/ | ||
component.init = function init() { | ||
_.extend( component, { | ||
fileFrame: [], | ||
uploadButton: $( '#location-cover-image' ), | ||
removeButton: $( '#location-cover-image-remove' ), | ||
imgContainer: $( '#location-cover-image-preview' ), | ||
imgIdInput: $( '#location-cover-image-value' ), | ||
saveNewTermButton: $( '#addtag #submit' ) | ||
} ); | ||
|
||
component.setupLocationMediaUploader(); | ||
component.setupAddTerm(); | ||
}; | ||
|
||
/** | ||
* Add media uploader for location cover image. | ||
*/ | ||
component.setupLocationMediaUploader = function() { | ||
component.uploadButton.on( 'click', function ( event ) { | ||
|
||
event.preventDefault(); | ||
|
||
let self = $( this ), | ||
id = self.attr( 'id' ); | ||
|
||
// If the media frame already exists, reopen it. | ||
if ( component.fileFrame[ id ] ) { | ||
component.fileFrame[ id ].open(); | ||
|
||
return; | ||
} | ||
|
||
// Create the media frame. | ||
component.fileFrame[ id ] = wp.media.frames.fileFrame = wp.media( { | ||
title: wp.i18n.__( 'Select Location Cover Image' ), | ||
button: { | ||
text: wp.i18n.__( 'Select Image' ) | ||
}, | ||
multiple: false | ||
} ); | ||
|
||
// When an image is selected, run a callback. | ||
component.fileFrame[ id ].on( 'select', function() { | ||
let attachment = component.fileFrame[ id ].state().get( 'selection' ).first().toJSON(); | ||
|
||
// Set input value. | ||
component.imgIdInput.val( attachment.id ); | ||
component.imgContainer.html( '<img src="' + attachment.url + '" style="max-width: 100%;" />' ); | ||
component.removeButton.removeClass( 'hidden' ); | ||
|
||
} ); | ||
|
||
// Finally, open the modal | ||
component.fileFrame[ id ].open(); | ||
|
||
} ); | ||
|
||
component.removeButton.on( 'click', function( event ) { | ||
event.preventDefault(); | ||
component.imgIdInput.val( '' ); | ||
component.imgContainer.html( '' ); | ||
component.removeButton.addClass( 'hidden' ); | ||
|
||
} ); | ||
}; | ||
|
||
/** | ||
* Set up events when adding new term. | ||
*/ | ||
component.setupAddTerm = function() { | ||
component.saveNewTermButton.on( 'click', function() { | ||
|
||
if ( ! validateForm( $( this ).parents( 'form' ) ) ) { | ||
return; | ||
} | ||
|
||
// Wait for ajax save stopping. | ||
$( document ).ajaxStop( function() { | ||
if ( 0 === $( '#ajax-response .error' ).length ) { | ||
component.imgIdInput.val( '' ); | ||
component.imgContainer.html( '' ); | ||
component.removeButton.addClass( 'hidden' ); | ||
$( '.location-is-featured-wrap input[type="checkbox"]' ).prop( 'checked', false ); | ||
} | ||
} ); | ||
} ); | ||
}; | ||
|
||
$( document ).ready( function() { | ||
component.init(); | ||
} ); | ||
|
||
})( jQuery ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.