Add auto-refresh of Posts block contents on option change

This commit is contained in:
Tautvidas Sipavičius
2015-11-17 14:40:39 +02:00
parent ae9b3df92d
commit c9a42ebb76
4 changed files with 53 additions and 29 deletions

View File

@@ -1,15 +1,12 @@
.mailpoet_posts_block .mailpoet_posts_block
box-shadow(none) padding-left: 0
padding-right: 0
& > .mailpoet_content .mailpoet_posts_block_posts
font-size: 1em overflow: auto
text-align: center
background-color: $primary-active-color & > .mailpoet_block
margin: 20px 0 width: 100%
padding: 15px
box-shadow(inset 1px 2px 1px $primary-inset-shadow-color)
color: $white-color
border-radius(3px)
.mailpoet_post_selection_filter_row .mailpoet_post_selection_filter_row
margin-top: 5px margin-top: 5px

View File

@@ -31,7 +31,7 @@ define([
base = BaseBlock; base = BaseBlock;
Module.PostsBlockModel = base.BlockModel.extend({ Module.PostsBlockModel = base.BlockModel.extend({
stale: ['_selectedPosts', '_availablePosts'], stale: ['_selectedPosts', '_availablePosts', '_transformedPosts'],
defaults: function() { defaults: function() {
return this._getDefaults({ return this._getDefaults({
type: 'posts', type: 'posts',
@@ -63,6 +63,7 @@ define([
divider: {}, divider: {},
_selectedPosts: [], _selectedPosts: [],
_availablePosts: [], _availablePosts: [],
_transformedPosts: new (App.getBlockTypeModel('container'))(),
}, App.getConfig().get('blockDefaults.posts')); }, App.getConfig().get('blockDefaults.posts'));
}, },
relations: function() { relations: function() {
@@ -71,15 +72,26 @@ define([
divider: App.getBlockTypeModel('divider'), divider: App.getBlockTypeModel('divider'),
_selectedPosts: Backbone.Collection, _selectedPosts: Backbone.Collection,
_availablePosts: Backbone.Collection, _availablePosts: Backbone.Collection,
_transformedPosts: App.getBlockTypeModel('container'),
}; };
}, },
initialize: function() { initialize: function() {
var that = this; var that = this,
POST_REFRESH_DELAY_MS = 500,
refreshAvailablePosts = _.debounce(this.fetchAvailablePosts.bind(this), POST_REFRESH_DELAY_MS),
refreshTransformedPosts = _.debounce(this._refreshTransformedPosts.bind(this), POST_REFRESH_DELAY_MS);
// Attach Radio.Requests API primarily for highlighting // Attach Radio.Requests API primarily for highlighting
_.extend(this, Radio.Requests); _.extend(this, Radio.Requests);
this.fetchAvailablePosts(); this.fetchAvailablePosts();
this.on('change:amount change:contentType change:terms change:inclusionType change:postStatus change:search change:sortBy', this._scheduleFetchAvailablePosts, this); this.on('change:amount change:contentType change:terms change:inclusionType change:postStatus change:search change:sortBy', refreshAvailablePosts);
this.listenTo(this.get('_selectedPosts'), 'add remove reset', refreshTransformedPosts);
this.on('change:displayType change:titleFormat change:titlePosition change:titleAlignment change:titleIsLink change:imagePadded change:showAuthor change:authorPrecededBy change:showCategories change:categoriesPrecededBy change:readMoreType change:readMoreText change:showDivider', refreshTransformedPosts);
this.listenTo(this.get('readMoreButton'), 'change', refreshTransformedPosts);
this.listenTo(this.get('divider'), 'change', refreshTransformedPosts);
this.on('insertSelectedPosts', this._insertSelectedPosts, this); this.on('insertSelectedPosts', this._insertSelectedPosts, this);
}, },
fetchAvailablePosts: function() { fetchAvailablePosts: function() {
@@ -93,20 +105,25 @@ define([
console.log('Posts fetchPosts error', arguments); console.log('Posts fetchPosts error', arguments);
}); });
}, },
/** _refreshTransformedPosts: function() {
* Batch more changes during a specific time, instead of fetching var that = this,
* ALC posts on each model change data = this.toJSON(),
*/ index = this.collection.indexOf(this),
_scheduleFetchAvailablePosts: function() { collection = this.get('_transformedPosts');
var timeout = 500,
that = this; data.posts = this.get('_selectedPosts').pluck('ID');
if (this._fetchPostsTimer !== undefined) {
clearTimeout(this._fetchPostsTimer); if (data.posts.length === 0) {
that.get('_transformedPosts').get('blocks').reset();
return;
} }
this._fetchPostsTimer = setTimeout(function() {
that.fetchAvailablePosts(); WordpressComponent.getTransformedPosts(data).done(function(posts) {
that._fetchPostsTimer = undefined; console.log('Available posts fetched', arguments);
}, timeout); that.get('_transformedPosts').get('blocks').reset(posts, {parse: true});
}).fail(function() {
console.log('Posts fetchPosts error', arguments);
});
}, },
_insertSelectedPosts: function() { _insertSelectedPosts: function() {
var that = this, var that = this,
@@ -131,6 +148,9 @@ define([
className: "mailpoet_block mailpoet_posts_block mailpoet_droppable_block", className: "mailpoet_block mailpoet_posts_block mailpoet_droppable_block",
getTemplate: function() { return templates.postsBlock; }, getTemplate: function() { return templates.postsBlock; },
modelEvents: {}, modelEvents: {},
regions: _.extend({
postsRegion: '.mailpoet_posts_block_posts',
}, base.BlockView.prototype.regions),
onDragSubstituteBy: function() { return Module.PostsWidgetView; }, onDragSubstituteBy: function() { return Module.PostsWidgetView; },
initialize: function() { initialize: function() {
base.BlockView.prototype.initialize.apply(this, arguments); base.BlockView.prototype.initialize.apply(this, arguments);
@@ -142,6 +162,13 @@ define([
this.toolsRegion.show(this.toolsView); this.toolsRegion.show(this.toolsView);
} }
this.trigger('showSettings'); this.trigger('showSettings');
var ContainerView = App.getBlockTypeView('container'),
renderOptions = {
disableTextEditor: true,
disableDragAndDrop: true,
};
this.postsRegion.show(new ContainerView({ model: this.model.get('_transformedPosts'), renderOptions: renderOptions }));
}, },
notifyAboutSelf: function() { notifyAboutSelf: function() {
return this; return this;

View File

@@ -1,6 +1,6 @@
<div class="mailpoet_tools"></div> <div class="mailpoet_tools"></div>
<div class="mailpoet_content"> <div class="mailpoet_content">
<div class="mailpoet_automated_latest_content_block_overlay"></div> <div class="mailpoet_automated_latest_content_block_overlay"></div>
<div class="mailpoet_automated_latest_content_block_posts"></div> <div class="mailpoet_automated_latest_content_block_posts"></div>
</div> </div>
<div class="mailpoet_block_highlight"></div> <div class="mailpoet_block_highlight"></div>

View File

@@ -1,5 +1,5 @@
<div class="mailpoet_tools"></div> <div class="mailpoet_tools"></div>
<div class="mailpoet_content"> <div class="mailpoet_content">
<%= __('Your posts will be inserted here') %> <div class="mailpoet_posts_block_posts"></div>
</div> </div>
<div class="mailpoet_block_highlight"></div> <div class="mailpoet_block_highlight"></div>