Skip to content
Permalink
Browse files

all_website_save_snippet

  • Loading branch information
qsm-odoo committed Jan 14, 2020
1 parent f7db00e commit d523dc369c49619f031918e8d7c755c8e1516619
@@ -289,7 +289,7 @@ def _snippet_save_view_values_hook(self, app_name, options=None):
return {}

@api.model
def save_snippet(self, name, arch, template_key, snippet_class=None, thumbnail_url=None):
def save_snippet(self, name, arch, template_key, snippet_class=None, thumbnail_url=None, options=None):
"""
Save a new snippet arch so that it appears with the given name when
using the given snippets template.
@@ -310,6 +310,8 @@ def save_snippet(self, name, arch, template_key, snippet_class=None, thumbnail_u
thumbnail_url (str, default=None):
the url of the thumbnail to use when displaying the snippet to
save (default one: see '_get_default_snippet_thumbnail')
options (dict, default=None)
"""
if not thumbnail_url:
thumbnail_url = self._get_default_snippet_thumbnail(snippet_class)
@@ -327,7 +329,7 @@ def save_snippet(self, name, arch, template_key, snippet_class=None, thumbnail_u
'type': 'qweb',
'arch': xml_arch,
}
new_snippet_view_values.update(self._snippet_save_view_values_hook(app_name))
new_snippet_view_values.update(self._snippet_save_view_values_hook(app_name, options))
self.create(new_snippet_view_values)

custom_section = self.search([('key', '=', template_key)])
@@ -347,7 +349,7 @@ def save_snippet(self, name, arch, template_key, snippet_class=None, thumbnail_u
</data>
""" % (template_key, snippet_key, thumbnail_url),
}
snippet_addition_view_values.update(self._snippet_save_view_values_hook(app_name))
snippet_addition_view_values.update(self._snippet_save_view_values_hook(app_name, options))
self.create(snippet_addition_view_values)

@api.model
@@ -2999,9 +2999,7 @@ registry.SnippetSave = SnippetOptionWidget.extend({
const dialog = new Dialog(this, {
title: _t("Save Your Block"),
size: 'small',
$content: $(qweb.render('web_editor.dialog.save_snippet', {
currentSnippetName: _.str.sprintf(_t("Custom %s"), this.$target[0].dataset.name),
})),
$content: $(qweb.render('web_editor.dialog.save_snippet', this._getModalRenderingOptions())),
buttons: [{
text: _t("Save"),
classes: 'btn-primary',
@@ -3018,6 +3016,7 @@ registry.SnippetSave = SnippetOptionWidget.extend({
'arch': targetCopyEl.outerHTML,
'template_key': this.options.snippets,
'snippet_class': [...this.$target[0].classList].filter(x => /\bs_./g.test(x))[0],
'options': this._getFormExtraOptions(dialog.el),
},
});
this.trigger_up('reload_snippet_template');
@@ -3031,6 +3030,28 @@ registry.SnippetSave = SnippetOptionWidget.extend({
}).open();
});
},

//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------

/**
* @private
* @param {HTMLElement} formEl
* @returns {Object}
*/
_getFormExtraOptions: function (formEl) {
return {};
},
/**
* @private
* @returns {Object}
*/
_getModalRenderingOptions: function () {
return {
currentSnippetName: _.str.sprintf(_t("Custom %s"), this.$target[0].dataset.name),
};
},
});


@@ -488,9 +488,10 @@ def _get_default_snippet_thumbnail(self, snippet_class=None):
return super()._get_default_snippet_thumbnail()

@api.model
def _snippet_save_view_values_hook(self, app_name):
def _snippet_save_view_values_hook(self, app_name, options=None):
res = super()._snippet_save_view_values_hook(app_name)
website_id = self.env.context.get('website_id')
if website_id:
res['website_id'] = website_id
if not options or not options.get('all_websites'):
website_id = self.env.context.get('website_id')
if website_id:
res['website_id'] = website_id
return res
@@ -15,6 +15,19 @@ options.Class.include({
// Private
//--------------------------------------------------------------------------

/**
* @private
* @returns {Promise<number>}
*/
_fetchNbWebsites: async function () {
if (options.Class.nbWebsites === undefined) {
options.Class.nbWebsites = await this._rpc({
model: 'website',
method: 'search_count',
});
}
return options.Class.nbWebsites;
},
/**
* Refreshes all public widgets related to the given element.
*
@@ -113,6 +126,45 @@ options.registry.background.include({
},
});

options.registry.SnippetSave.include({
xmlDependencies: options.registry.SnippetSave.prototype.xmlDependencies
.concat(['/website/static/src/xml/website.editor.xml']),

/**
* @override
*/
saveSnippet: async function (previewMode, widgetValue, params) {
const _super = this._super.bind(this);
await this._fetchNbWebsites(); // Pre-fetch the # of websites
return _super(...arguments);
},

//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------

/**
* @override
*/
_getFormExtraOptions: function (formEl) {
let allWebsites = false;
if (options.Class.nbWebsites > 1) {
allWebsites = formEl.querySelector('#multi_website_save').checked;
}
return _.extend(this._super(...arguments), {
'all_websites': allWebsites,
});
},
/**
* @override
*/
_getModalRenderingOptions: function () {
return _.extend(this._super(...arguments), {
nbWebsites: options.Class.nbWebsites,
});
},
});

options.registry.menu_data = options.Class.extend({
xmlDependencies: ['/website/static/src/xml/website.editor.xml'],

@@ -133,4 +133,16 @@
</div>
</div>
</div>

<!-- Snippet saving -->
<t t-extend="web_editor.dialog.save_snippet">
<t t-jquery=".form-group:last" t-operation="after">
<div t-if="nbWebsites &gt; 1" class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="multi_website_save"/>
<label class="custom-control-label" for="multi_website_save">Save for all websites</label>
</div>
</div>
</t>
</t>
</templates>

0 comments on commit d523dc3

Please sign in to comment.
You can’t perform that action at this time.