[IMP] website: theme customization assets recomputing

Assets are now split in multiple files and there is not a route which
always leads to the last version of an asset. This commit adapts theme
customization (without reloading) to these new features.
This commit is contained in:
qsm-odoo
2015-10-27 16:32:35 +01:00
parent 7329a7fc55
commit 58be02b927
6 changed files with 38 additions and 80 deletions
+6 -1
View File
@@ -12,6 +12,7 @@ import urllib2
import werkzeug.wrappers
import openerp
from openerp.addons.base.ir.ir_qweb import AssetsBundle
from openerp.addons.web.controllers.main import WebClient
from openerp.addons.web_editor.controllers.main import Web_Editor
from openerp.addons.web import http
@@ -303,7 +304,7 @@ class Website(openerp.addons.web.controllers.main.Home):
return [enable, disable]
@http.route(['/website/theme_customize'], type='json', auth="public", website=True)
def theme_customize(self, enable, disable):
def theme_customize(self, enable, disable, get_bundle=False):
""" enable or Disable lists of ``xml_id`` of the inherit templates
"""
cr, uid, context, pool = request.cr, request.uid, request.context, request.registry
@@ -317,6 +318,10 @@ class Website(openerp.addons.web.controllers.main.Home):
set_active(disable, False)
set_active(enable, True)
if get_bundle:
bundle = AssetsBundle('website.assets_frontend', cr=http.request.cr, uid=http.request.uid, context={}, registry=http.request.registry)
return bundle.to_html()
return True
@http.route(['/website/theme_customize_reload'], type='http', auth="public", website=True)
-8
View File
@@ -655,11 +655,3 @@ ul.oe_menu_editor .disclose {
position: absolute;
z-index: 1;
}
/* ---- force the browse to re-compute the stylesheets ---- */
body.theme_customize_css_loading {
magin-top: 1px;
}
body.theme_customize_css_loading #wrapwrap {
magin-top: -1px;
}
@@ -565,11 +565,3 @@ $infobar_height: 20px
opacity: 0.3
position: absolute
z-index: 1
/* ---- force the browse to re-compute the stylesheets ---- */
body.theme_customize_css_loading
magin-top: 1px
#wrapwrap
magin-top: -1px
// }}}
@@ -253,11 +253,3 @@
position: absolute;
z-index: 1;
}
/* ---- force the browse to re-compute the stylesheets ---- */
body.theme_customize_css_loading {
magin-top: 1px;
}
body.theme_customize_css_loading #wrapwrap {
magin-top: -1px;
}
@@ -224,11 +224,3 @@ $infobar_height: 20px
opacity: 0.3
position: absolute
z-index: 1
/* ---- force the browse to re-compute the stylesheets ---- */
body.theme_customize_css_loading
magin-top: 1px
#wrapwrap
magin-top: -1px
// }}}
+32 -47
View File
@@ -3,6 +3,7 @@ odoo.define('website.theme', function (require) {
var ajax = require('web.ajax');
var core = require('web.core');
var session = require('web.session');
var Widget = require('web.Widget');
var base = require('web_editor.base');
var website = require('website.website');
@@ -121,57 +122,41 @@ var Theme = Widget.extend({
});
return xml_ids;
},
compute_stylesheets: function () {
var self = this;
self.has_error = false;
$('link[href*=".assets_"]').attr('data-loading', true);
function theme_customize_css_onload() {
if ($('link[data-loading]').size()) {
$('body').toggleClass('theme_customize_css_loading');
setTimeout(theme_customize_css_onload, 50);
} else {
$('body').removeClass('theme_customize_css_loading');
self.$el.removeClass("loading");
if (window.getComputedStyle($('button[data-toggle="collapse"]:first')[0]).getPropertyValue('position') === 'static' ||
window.getComputedStyle($('#theme_customize_modal')[0]).getPropertyValue('display') === 'none') {
if (self.has_error) {
window.location.hash = "theme=true";
window.location.reload();
} else {
self.has_error = true;
$('link[href*=".assets_"][data-error]').removeAttr('data-error').attr('data-loading', true);
self.update_stylesheets();
setTimeout(theme_customize_css_onload, 50);
}
}
}
}
theme_customize_css_onload();
},
update_stylesheets: function () {
$('link[href*=".assets_"]').each(function update () {
var $style = $(this);
var href = $style.attr("href").replace(/[^\/]+$/, new Date().getTime());
var $asset = $('<link rel="stylesheet" href="'+href+'"/>');
$asset.attr("onload", "$(this).prev().attr('disable', true).remove(); $(this).removeAttr('onload').removeAttr('onerror');");
$asset.attr("onerror", "$(this).prev().removeAttr('data-loading').attr('data-error','loading'); $(this).attr('disable', true).remove();");
$style.after($asset);
});
},
update_style: function (enable, disable, reload) {
var self = this;
if (this.$el.hasClass("loading")) return;
this.$el.addClass("loading");
if (this.$el.hasClass("loading")) {
return;
}
this.$el.addClass('loading');
if (!reload && $('link[href*=".assets_"]').size()) {
this.compute_stylesheets();
if (!reload && !session.debug) {
var self = this;
return ajax.jsonRpc('/website/theme_customize', 'call', {
'enable': enable,
'disable': disable
}).then(function () {
self.update_stylesheets();
enable: enable,
disable: disable,
get_bundle: true,
}).then(function (bundleHTML) {
var $links = $('link[href*=".assets_frontend"]');
var $newLinks = $(bundleHTML).filter('link');
var linksLoaded = $.Deferred();
var nbLoaded = 0;
$newLinks.on('load', function (e) {
if (++nbLoaded >= $newLinks.length) {
linksLoaded.resolve();
}
});
$newLinks.on('error', function (e) {
linksLoaded.reject();
window.location.hash = "theme=true";
window.location.reload();
});
$links.last().after($newLinks);
return linksLoaded.then(function () {
$links.remove();
self.$el.removeClass('loading');
});
});
} else {
var href = '/website/theme_customize_reload'+
'?href='+encodeURIComponent(window.location.href)+