1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
odoo.define('website_blog.s_latest_posts_frontend', function (require) {
'use strict';
var core = require('web.core');
var wUtils = require('website.utils');
var publicWidget = require('web.public.widget');
var _t = core._t;
publicWidget.registry.js_get_posts = publicWidget.Widget.extend({
selector: '.js_get_posts',
disabledInEditableMode: false,
/**
* @override
*/
start: function () {
var self = this;
const data = self.$target[0].dataset;
const limit = parseInt(data.postsLimit) || 4;
const blogID = parseInt(data.filterByBlogId);
// Compatibility with old template xml id
if (data.template && data.template.endsWith('.s_latest_posts_big_orizontal_template')) {
data.template = 'website_blog.s_latest_posts_horizontal_template';
}
const template = data.template || 'website_blog.s_latest_posts_list_template';
const loading = data.loading === 'true';
const order = data.order || 'published_date desc';
this.$target.empty(); // Compatibility with db that saved content inside by mistake
this.$target.attr('contenteditable', 'False'); // Prevent user edition
var domain = [];
if (blogID) {
domain.push(['blog_id', '=', blogID]);
}
if (order.includes('visits')) {
domain.push(['visits', '!=', false]);
}
var prom = new Promise(function (resolve) {
self._rpc({
route: '/blog/render_latest_posts',
params: {
template: template,
domain: domain,
limit: limit,
order: order,
},
}).then(function (posts) {
var $posts = $(posts).filter('.s_latest_posts_post');
if (!$posts.length) {
self.$target.append($('<div/>', {class: 'col-md-6 offset-md-3'})
.append($('<div/>', {
class: 'alert alert-warning alert-dismissible text-center',
text: _t("No blog post was found. Make sure your posts are published."),
})));
resolve();
}
if (loading) {
// Perform an intro animation
self._showLoading($posts);
} else {
self.$target.html($posts);
}
resolve();
}).guardedCatch(function () {
if (self.editableMode) {
self.$target.append($('<p/>', {
class: 'text-danger',
text: _t("An error occured with this latest posts block. If the problem persists, please consider deleting it and adding a new one"),
}));
}
resolve();
});
});
return Promise.all([this._super.apply(this, arguments), prom]);
},
/**
* @override
*/
destroy: function () {
this.$target.empty();
this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
*/
_showLoading: function ($posts) {
var self = this;
_.each($posts, function (post, i) {
var $post = $(post);
var $progress = $post.find('.s_latest_posts_loader');
var bgUrl = $post.find('.o_record_cover_image').css('background-image').replace('url(','').replace(')','').replace(/\"/gi, "") || 'none';
// Append $post to the snippet, regardless by the loading state.
$post.appendTo(self.$target);
// No cover-image found. Add a 'flag' class and exit.
if (bgUrl === 'none') {
$post.addClass('s_latest_posts_loader_no_cover');
$progress.remove();
return;
}
// Cover image found. Show the spinning icon.
$progress.find('> div').removeClass('d-none').css('animation-delay', i * 200 + 'ms');
var $dummyImg = $('<img/>', {src: bgUrl});
// If the image is not loaded in 10 sec, remove loader and provide a fallback bg-color to the container.
// Hopefully one day the image will load, covering the bg-color...
var timer = setTimeout(function () {
$post.find('.o_record_cover_image').addClass('bg-200');
$progress.remove();
}, 10000);
wUtils.onceAllImagesLoaded($dummyImg).then(function () {
$progress.fadeOut(500, function () {
$progress.removeClass('d-flex');
});
$dummyImg.remove();
clearTimeout(timer);
});
});
},
});
});
|