Skip to content

Remove dependency from FM #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ For more on how Alexa skills work in general, [see here](https://developer.amazo

## Requirements

- [Fieldmanager](http://fieldmanager.org).
- SSL certificate.
- Minimum version of PHP: 5.3.
- Minimum version of PHP: 5.4.
- Minimum version of WordPress: 4.4.

## Installation

- Install and activate [Fieldmanager](https://github.com/alleyinteractive/wordpress-fieldmanager/archive/1.0.0.zip).
- Download the .zip file of this repo and upload to your WordPress site by going to WP Admin and navigating to **Plugins -> Add New**. Select the 'Upload Plugin' button near the top of the top of the screen to upload the .zip file.

## Features
Expand Down
49 changes: 49 additions & 0 deletions client/css/admin/settings.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
a.voicewp-delete {
color: #bc0b0b;
text-decoration: none;
}

.voicewp-group-wrapper {
border: 1px solid #ddd;
padding: 12px;
background-color: #fff;
margin-bottom: 12px;
border: solid 1px #dfdfdf;
-webkit-box-shadow: 0 1px 1px rgba(0,0,0,.04);
box-shadow: 0 1px 1px rgba(0,0,0,.04);
}

.voicewpjs-removable {
position: relative;
}

.voicewpjs-remove:before {
font-family: dashicons;
font-size: 15px;
line-height: 1;
color: #a0a5aa;
}

.voicewpjs-remove {
margin: 4px 0 0 5px;
display: block;
height: 15px;
width: 15px;
float: left;
text-decoration: none;
position: absolute;
top: 0;
right: 0;
}

.voicewpjs-remove:before {
content: "\f153";
}

.voicewpjs-remove:hover {
cursor: pointer;
}

.voicewpjs-remove:hover:before {
color: #d54e21;
}
107 changes: 107 additions & 0 deletions client/js/admin/settings-media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var voicewp_media_frame = [];
( function( $ ) {

$( document ).on( 'click', '.voicewp-media-remove', function(e) {
e.preventDefault();
$(this).parents( '.voicewp-wrapper' ).find( '.voicewp-media-id' ).val( 0 );
$(this).parents( '.voicewp-wrapper' ).find( '.media-wrapper' ).html( '' );
});

$( document ).on( 'click', '.media-wrapper a', function( event ){
event.preventDefault();
$(this).closest('.media-wrapper').siblings('.voicewp-media-button').click();
} );

$( document ).on( 'click', '.voicewp-media-button', function( event ) {
var $el = $(this),
library = {};
event.preventDefault();

// If the media frame already exists, reopen it.
if ( voicewp_media_frame[ $el.attr('id') ] ) {
voicewp_media_frame[ $el.attr('id') ].open();
return;
}

// If mime type has been restricted, make sure the library only shows that
// type.
if ( $el.data( 'mime-type' ) && 'all' !== $el.data( 'mime-type' ) ) {
library.type = $el.data( 'mime-type' );
}

// Create the media frame.
voicewp_media_frame[ $el.attr('id') ] = wp.media({
library: library,
});

// If mime type has been restricted, make sure the library doesn't autoselect
// an uploaded file if it's the wrong mime type
if ( $el.data( 'mime-type' ) && 'all' !== $el.data( 'mime-type' ) ) {
// This event is only fired when a file is uploaded.
// @see {wp.media.controller.Library:uploading()}
voicewp_media_frame[ $el.attr('id') ].on( 'library:selection:add', function() {
// This event gets fired for every frame that has ever been created on
// the current page, which causes errors. We only care about the visible
// frame. Also, FM can change the ID of buttons, which means some older
// frames may no longer be valid.
if ( 'undefined' === typeof voicewp_media_frame[ $el.attr('id') ] || ! voicewp_media_frame[ $el.attr('id') ].$el.is(':visible') ) {
return;
}

// Get the Selection object and the currently selected attachment.
var selection = voicewp_media_frame[ $el.attr('id') ].state().get('selection'),
attachment = selection.first();
// If the mime type is wrong, deselect the file.
if ( attachment.attributes.type !== $el.data( 'mime-type' ) ) {
selection.remove(attachment);
}
});
}

// When an image is selected, run a callback.
voicewp_media_frame[ $el.attr('id') ].on( 'select', function() {
// Grab the selected attachment.
var attachment = voicewp_media_frame[ $el.attr('id') ].state().get('selection').first().attributes;

props = { size: $el.data('preview-size') || 'thumbnail' };
props = wp.media.string.props( props, attachment );
props.align = 'none';
props.link = 'custom';
props.linkUrl = '#';
props.caption = '';
$el.parent().find('.voicewp-media-id').val( attachment.id );

if ( attachment.type == 'image' ) {
props.url = props.src;
var preview = 'Uploaded file:<br />';
preview += wp.media.string.image( props );
} else {
var preview = 'Uploaded file:&nbsp;';
preview += wp.media.string.link( props );
}

preview += '<br /><a class="voicewp-media-remove voicewp-delete" href="#">remove</a>';
var $wrapper = $el.parent().find( '.media-wrapper' );
$wrapper.html( preview );
});

// Select the attachment when the frame opens
voicewp_media_frame[ $el.attr('id') ].on( 'open', function() {
// Select the current attachment inside the frame
var selection = voicewp_media_frame[ $el.attr('id') ].state().get('selection'),
id = $el.parent().find('.voicewp-media-id').val(),
attachment;

// If there is a saved attachment, use it
if ( '' !== id && -1 !== id && typeof wp.media.attachment !== "undefined" ) {
attachment = wp.media.attachment( id );
attachment.fetch();
}

selection.reset( attachment ? [ attachment ] : [] );
} );

voicewp_media_frame[ $el.attr('id') ].open();
} );

} )( jQuery );
164 changes: 164 additions & 0 deletions client/js/admin/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
voicewp_add_another = function( $element ) {
const repeaterGroup = $element.parent().parent();
const cloneElement = repeaterGroup.find( '.voicewp-group-wrapper' ).last().clone();
const items = repeaterGroup.find('.voicewp-group-wrapper');

cloneElement.insertBefore( $element.parent() );

// Clear data.
const fields = cloneElement.find('.voicewp-item');
fields.each( function() {
jQuery( this ).val( '' );
jQuery( this ).siblings( '.media-wrapper' ).html( '' );
});

voicewp_renumber( $element.closest( '.voicewpjs-repeating-group' ) );
}

voicewp_remove = function( $element ) {
const removedEl = $element.parent().parent();
const wrapper = removedEl.closest( '.voicewpjs-repeating-group' );
removedEl.remove();
voicewp_renumber( wrapper );
}

voicewp_renumber = function( $element ) {
const repeaterName = $element.data( 'repeater-name' );
const items = $element.find( '.voicewp-group-wrapper' );

// Update name and clear data.
items.each( function( index, value ) {
const fields = jQuery( this ).find('.voicewp-item');

fields.each( function() {
let groupName =
repeaterName.replace( 'voicewp-index', Math.max( 0, index ) )
+ '[' + jQuery( this ).data( 'base-name' ) + ']';

jQuery( this ).attr( 'name', groupName );
jQuery( this ).attr( 'id', groupName );
});
});
}

jQuery( document ).ready( function ( $ ) {
// Handle adding another element.
$( document ).on( 'click', '.voicewp-add-another', function( e ) {
e.preventDefault();
voicewp_add_another( $( this ) );
} );

// Handle remove events
$( document ).on( 'click', '.voicewpjs-remove', function( e ) {
e.preventDefault();
voicewp_remove( $( this ) );
} );

// Initializes triggers to conditionally hide or show fields
voicewp_init_display_if = function() {
var val;
var src = $( this ).data( 'display-src' );
var values = voicewpGetCompareValues( this );
var wrapper = $( this ).closest('.voicewp-wrapper');
// Wrapper divs sometimes receive .voicewp-element, but don't use them as
// triggers. Also don't use autocomplete inputs or a checkbox's hidden
// sibling as triggers, because the value is in their sibling fields
// (which this still matches).
var trigger = wrapper.siblings( '.voicewp-' + src + '-wrapper' ).find( '.voicewp-element' ).not( 'div, .voicewp-autocomplete, .voicewp-checkbox-hidden' );

// Sanity check before calling `val()` or `split()`.
if ( 0 === trigger.length ) {
return;
}

if ( trigger.is( ':checkbox' ) ) {
if ( trigger.is( ':checked' ) ) {
val = true;
} else {
val = false;
}
} else if ( trigger.is( ':radio' ) ) {
if ( trigger.filter( ':checked' ).length ) {
val = trigger.filter( ':checked' ).val();
} else {
// On load, there might not be any selected radio, in which case call the value blank.
val = '';
}
} else {
val = trigger.val().split( ',' );
}
trigger.addClass( 'display-trigger' );
if ( ! voicewp_match_value( values, val ) ) {
wrapper.hide();
}
};
$( '.voicewp-display-if' ).each( voicewp_init_display_if );

// Controls the trigger to show or hide fields
voicewp_trigger_display_if = function() {
var val;
var $this = $( this );
var name = $this.attr( 'name' );
if ( $this.is( ':checkbox' ) ) {
if ( $this.is( ':checked' ) ) {
val = true;
} else {
val = false;
}
} else if ( $this.is( ':radio' ) ) {
val = $this.filter( ':checked' ).val();
} else {
val = $this.val().split( ',' );
}

$( this ).closest( '.voicewp-wrapper' ).siblings().each( function() {
var element = $( this ).find('.voicewp-element');

if ( element.hasClass( 'voicewp-display-if' ) ) {
if ( name && name.match( element.data( 'display-src' ) ) != null ) {
if ( voicewp_match_value( voicewpGetCompareValues( element ), val ) ) {
$( this ).show();
} else {
$( this ).hide();
}
}
}
} );
};
$( document ).on( 'change', '.display-trigger', voicewp_trigger_display_if );
} );

/**
* Get data attribute display-value(s).
*
* Accounts for jQuery converting string to number automatically.
*
* @param HTMLDivElement el Wrapper with the data attribute.
* @return string|number|array Single string or number, or array if data attr contains CSV.
*/
var voicewpGetCompareValues = function( el ) {
var values = jQuery( el ).data( 'display-value' );
try {
values = values.split( ',' );
} catch( e ) {
// If jQuery already converted string to number.
values = [ values ];
}
return values;
};

/**
* Matches the display if value.
*
* @param {array} values The list of values to match.
* @param {string} match_string The match string.
* @return {bool} True of false.
*/
var voicewp_match_value = function( values, match_string ) {
for ( var index in values ) {
if ( values[index] == match_string ) {
return true;
}
}
return false;
}
2 changes: 1 addition & 1 deletion inc/class-voicewp-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Voicewp_Setup {
* @var string
* @access public
*/
public static $version = '1.0.0';
public static $version = '1.1.0';

protected static $instance;

Expand Down
Loading