Skip to content
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

Add JS Widgets as Shortcodes (Post Elements) with Shortcode UI (via Shortcake) #32

Merged
merged 22 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c02bdd6
Add widgets initially as Shortcake Post Elements (without Form integr…
westonruter Jan 12, 2017
da55eef
Allow shortcode attributes for the widget's instance properties
westonruter Jan 12, 2017
0f9b8ed
Supply proper args to render method via first-registered sidebar
westonruter Jan 12, 2017
2b88531
Re-use the dashicon associated with a given widget in the customizer …
Jan 12, 2017
9dc0700
Define icon_name on base WP_JS_Widget class
westonruter Jan 12, 2017
2b35e80
Add skeleton for widget_form shortcake field type
westonruter Jan 12, 2017
4c32ff3
Add skeleton for shortcode UI js-hooks integration
westonruter Jan 12, 2017
6e581f6
Add missing js-widget dependnecies for shortcode-ui
westonruter Jan 12, 2017
8057915
Embed widget form in shortcake form
westonruter Jan 13, 2017
166751b
Include widget base styles in `widget-form.css` so that forms look as…
Jan 13, 2017
f800691
Override display:block style for labels in shortcake widget forms
westonruter Jan 18, 2017
0aa05a5
Hide Edit Menu button when no menu is selected
westonruter Jan 18, 2017
3c33e2b
Prevent enqueueing trac-39389-preview script when user cannot manage …
westonruter Jan 22, 2017
267498c
Extend editAttributeField View instead of hacking hidden input to inj…
westonruter Feb 14, 2017
09ee307
Merge branch 'develop' of https://github.com/xwp/wp-js-widgets into f…
westonruter Feb 16, 2017
192f7b5
Fix excluding pages in the Pages widget
westonruter Feb 16, 2017
3a64d1a
Ensure attributes passed to shortcodes get validated and sanitized
westonruter Feb 16, 2017
d703fd2
Separate out shortcode UI logic into separate class
westonruter Feb 17, 2017
69f68a0
Fix phpcs issues
westonruter Feb 17, 2017
fda18fe
Remove leftover id_base exporting
westonruter Feb 17, 2017
a4c06ae
Ensure frontend assets are enqueued for widgets as shortcodes
westonruter Feb 17, 2017
ae6ba78
Ensure dashicon related to a JS Widget gets applied in the available …
westonruter Feb 17, 2017
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: 4 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"preset": "wordpress",
"requireCamelCaseOrUpperCaseIdentifiers": {
"ignoreProperties": true,
"allExcept": [ "Shortcode_UI" ]
},
"excludeFiles": [
"**/*.min.js",
"**/*.jsx",
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/archives/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Archives extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-archive';

/**
* WP_JS_Widget_Archives constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/calendar/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Calendar extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-calendar';

/**
* WP_JS_Widget_Calendar constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/categories/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Categories extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-category';

/**
* WP_JS_Widget_Categories constructor.
*
Expand Down
8 changes: 7 additions & 1 deletion core-adapter-widgets/meta/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Meta extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-wordpress';

/**
* WP_JS_Widget_Meta constructor.
*
Expand All @@ -22,7 +29,6 @@ public function __construct( JS_Widgets_Plugin $plugin, WP_Widget_Meta $adapted_
parent::__construct( $plugin, $adapted_widget );
}


/**
* Get instance schema properties.
*
Expand Down
9 changes: 8 additions & 1 deletion core-adapter-widgets/nav_menu/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
class WP_JS_Widget_Nav_Menu extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-menu';

/**
* WP_JS_Widget_Nav_Menu constructor.
*
Expand Down Expand Up @@ -101,7 +108,7 @@ public function render_form_template() {
),
) );
?>
<p>
<p class="edit-menu" hidden>
<button type="button" class="button edit"><?php esc_html_e( 'Edit Menu', 'default' ) ?></button>
</p>
</div>
Expand Down
16 changes: 15 additions & 1 deletion core-adapter-widgets/nav_menu/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ wp.widgets.formConstructor.nav_menu = (function( api, $ ) {
initialize: function initialize( properties ) {
var form = this;
wp.widgets.Form.prototype.initialize.call( form, properties );
_.bindAll( form, 'updateForm', 'handleEditButtonClick' );
_.bindAll( form, 'updateForm', 'handleEditButtonClick', 'updateEditButtonVisibility' );

if ( _.isObject( form.config.nav_menus ) && 0 === classProps.navMenuCollection.length ) {
_.each( form.config.nav_menus, function( name, id ) {
Expand All @@ -120,10 +120,12 @@ wp.widgets.formConstructor.nav_menu = (function( api, $ ) {
var form = this;
wp.widgets.Form.prototype.render.call( form );
NavMenuWidgetForm.navMenuCollection.on( 'update change', form.updateForm );
form.model.bind( form.updateEditButtonVisibility );
form.container.find( 'button.edit' ).on( 'click', form.handleEditButtonClick );
form.noMenusMessage = form.container.find( '.no-menus-message' );
form.menuSelection = form.container.find( '.menu-selection' );
form.updateForm();
form.updateEditButtonVisibility();
},

/**
Expand All @@ -135,6 +137,7 @@ wp.widgets.formConstructor.nav_menu = (function( api, $ ) {
var form = this;
form.container.find( 'button.edit' ).off( 'click', form.handleEditButtonClick );
NavMenuWidgetForm.navMenuCollection.off( 'update change', form.updateForm );
form.model.unbind( form.updateEditButtonVisibility );
form.noMenusMessage = null;
form.menuSelection = null;
wp.widgets.Form.prototype.destruct.call( form );
Expand Down Expand Up @@ -182,6 +185,17 @@ wp.widgets.formConstructor.nav_menu = (function( api, $ ) {
select.val( NavMenuWidgetForm.navMenuCollection.has( currentValue.nav_menu ) ? currentValue.nav_menu : 0 );
form.noMenusMessage.toggle( 0 === NavMenuWidgetForm.navMenuCollection.length );
form.menuSelection.toggle( 0 !== NavMenuWidgetForm.navMenuCollection.length );
},

/**
* Update the visibility of the edit button based on whether a menu is selected.
*
* @returns {void}
*/
updateEditButtonVisibility: function updateEditButtonVisibility() {
var form = this, button;
button = form.container.find( '.edit-menu' );
button.toggle( NavMenuWidgetForm.navMenuCollection.length > 0 && form.getValue().nav_menu > 0 );
}

}, classProps );
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/pages/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class WP_JS_Widget_Pages extends WP_Adapter_JS_Widget {

const ID_LIST_PATTERN = '\d+(,\s*\d+)*';

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-admin-page';

/**
* WP_JS_Widget_Pages constructor.
*
Expand Down
2 changes: 1 addition & 1 deletion core-adapter-widgets/pages/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ wp.widgets.formConstructor.pages = (function( api ) {
excludeIds.push( id );
}
} );
form.model._value.exclude = excludeIds;
}
form.model._value.exclude = excludeIds;

form.syncedProperties.exclude = form.createSyncedPropertyValue( form.model, 'exclude' );
}
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/recent-comments/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Recent_Comments extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-admin-comments';

/**
* WP_JS_Widget_Recent_Comments constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/recent-posts/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Recent_Posts extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-admin-post';

/**
* WP_JS_Widget_Recent_Posts constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/rss/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_RSS extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-rss';

/**
* WP_JS_Widget_RSS constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/search/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Search extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-search';

/**
* WP_JS_Widget_Search constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/tag_cloud/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class WP_JS_Widget_Tag_Cloud extends WP_Adapter_JS_Widget {

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-tagcloud';

/**
* WP_JS_Widget_Tag_Cloud constructor.
*
Expand Down
7 changes: 7 additions & 0 deletions core-adapter-widgets/text/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class WP_JS_Widget_Text extends WP_Adapter_JS_Widget {
*/
public $adapted_widget;

/**
* Icon name.
*
* @var string
*/
public $icon_name = 'dashicons-text';

/**
* Get instance schema properties.
*
Expand Down
51 changes: 51 additions & 0 deletions css/widget-form.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,54 @@
.js-widget-form-notifications-container .notice-warning.notice-alt {
background-color: #fff8e5;
}

.js-widget-form-shortcode-ui input[type="text"],
.js-widget-form-shortcode-ui input[type="password"],
.js-widget-form-shortcode-ui input[type="color"],
.js-widget-form-shortcode-ui input[type="date"],
.js-widget-form-shortcode-ui input[type="datetime"],
.js-widget-form-shortcode-ui input[type="datetime-local"],
.js-widget-form-shortcode-ui input[type="email"],
.js-widget-form-shortcode-ui input[type="month"],
.js-widget-form-shortcode-ui input[type="number"],
.js-widget-form-shortcode-ui input[type="search"],
.js-widget-form-shortcode-ui input[type="tel"],
.js-widget-form-shortcode-ui input[type="text"],
.js-widget-form-shortcode-ui input[type="time"],
.js-widget-form-shortcode-ui input[type="url"],
.js-widget-form-shortcode-ui input[type="week"],
.js-widget-form-shortcode-ui select,
.js-widget-form-shortcode-ui textarea {
width: 25em;
padding: 3px 5px;
font-size: 14px;
}

.js-widget-form-shortcode-ui input[type="checkbox"],
.js-widget-form-shortcode-ui input[type="radio"] {
border: 1px solid #b4b9be;
color: #555;
}
.js-widget-form-shortcode-ui input[type="checkbox"]:focus,
.js-widget-form-shortcode-ui input[type="radio"]:focus {
border-color: #5b9dd9;
}
.js-widget-form-shortcode-ui input[type="checkbox"] + label,
.js-widget-form-shortcode-ui input[type="radio"] + label {
display: inline;
clear: none;
}

.js-widget-form-shortcode-ui label {
color: #555d66;
}

.js-widget-form-shortcode-ui .select2-container {
max-width: 26.92em; /* 25em * 14px = 350px => 350px / 13px = 26.92em */
}

/* Shortcake style fixes */
.edit-shortcode-form .js-widget-form-notifications-container label {
clear: none;
display: inline;
}
47 changes: 47 additions & 0 deletions js/shortcode-ui-view-widget-form-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* global wp, JSON, Shortcode_UI */
/* eslint-disable strict */
/* eslint consistent-this: [ "error", "view" ] */
/* eslint-disable complexity */

Shortcode_UI.views.widgetFormField = (function( sui ) {
'use strict';

/**
* Widget form.
*
* @class
*/
return sui.views.editAttributeField.extend( {

events: {},

/**
* Render.
*
* @return {sui.views.editAttributeField} View.
*/
render: function() {
var view = this, FormConstructor, instanceValue, form;

if ( ! view.shortcode.get( 'widgetType' ) || ! wp.widgets.formConstructor[ view.shortcode.get( 'widgetType' ) ] ) {
throw new Error( 'Unable to determine the widget type.' );
}

view.$el.addClass( 'js-widget-form-shortcode-ui' );
instanceValue = new wp.customize.Value( view.getValue() ? JSON.parse( view.getValue() ) : {} );
FormConstructor = wp.widgets.formConstructor[ view.shortcode.get( 'widgetType' ) ];
form = new FormConstructor( {
model: instanceValue,
container: view.$el
} );
instanceValue.bind( function( instanceData ) {
view.setValue( JSON.stringify( instanceData ) );
} );
form.render();

view.triggerCallbacks();
return view;
}
} );

})( Shortcode_UI );
Loading