2013-08-20 21:34:50 +00:00
|
|
|
(function() {
|
|
|
|
"use strict";
|
2013-08-19 14:11:08 +00:00
|
|
|
|
2013-08-20 21:34:50 +00:00
|
|
|
var website = {};
|
|
|
|
// The following line can be removed in 2017
|
|
|
|
openerp.website = website;
|
2013-08-05 08:26:38 +00:00
|
|
|
|
2013-09-11 16:18:54 +00:00
|
|
|
website.get_context = function (dict) {
|
2013-09-12 11:30:28 +00:00
|
|
|
var html = document.documentElement;
|
2013-09-11 16:18:54 +00:00
|
|
|
return _.extend({
|
2013-10-25 07:43:43 +00:00
|
|
|
lang: html.getAttribute('lang').replace('-', '_'),
|
|
|
|
website_id: html.getAttribute('data-website-id')|0
|
2013-09-11 16:18:54 +00:00
|
|
|
}, dict);
|
|
|
|
};
|
|
|
|
|
2013-08-19 14:11:08 +00:00
|
|
|
/* ----- TEMPLATE LOADING ---- */
|
2013-10-29 16:11:28 +00:00
|
|
|
var templates_def = $.Deferred().resolve();
|
|
|
|
website.add_template_file = function(template) {
|
|
|
|
templates_def = templates_def.then(function() {
|
|
|
|
var def = $.Deferred();
|
|
|
|
openerp.qweb.add_template(template, function(err) {
|
|
|
|
if (err) {
|
|
|
|
def.reject(err);
|
|
|
|
} else {
|
|
|
|
def.resolve();
|
|
|
|
}
|
2013-07-31 14:06:31 +00:00
|
|
|
});
|
2013-10-29 16:11:28 +00:00
|
|
|
return def;
|
2013-07-31 14:06:31 +00:00
|
|
|
});
|
2013-08-20 21:34:50 +00:00
|
|
|
};
|
2013-10-29 16:11:28 +00:00
|
|
|
website.add_template_file('/website/static/src/xml/website.xml');
|
2013-09-23 11:50:54 +00:00
|
|
|
website.reload = function () {
|
2013-10-16 10:45:45 +00:00
|
|
|
location.hash = "scrollTop=" + window.document.body.scrollTop;
|
2013-10-23 09:11:52 +00:00
|
|
|
if (location.search.indexOf("enable_editor") > -1) {
|
|
|
|
window.location.href = window.location.href.replace(/enable_editor(=[^&]*)?/g, '');
|
2013-10-16 10:45:45 +00:00
|
|
|
} else {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
2013-09-23 11:50:54 +00:00
|
|
|
};
|
2013-08-23 08:27:12 +00:00
|
|
|
|
2013-08-20 21:34:50 +00:00
|
|
|
var all_ready = null;
|
2013-08-29 10:29:56 +00:00
|
|
|
var dom_ready = website.dom_ready = $.Deferred();
|
2013-11-13 14:30:58 +00:00
|
|
|
$(document).ready(function () {
|
|
|
|
website.is_editable = $('html').data('editable');
|
|
|
|
dom_ready.resolve();
|
|
|
|
});
|
2013-08-20 21:34:50 +00:00
|
|
|
|
2013-08-22 09:18:12 +00:00
|
|
|
website.init_kanban = function ($kanban) {
|
|
|
|
$('.js_kanban_col', $kanban).each(function () {
|
|
|
|
var $col = $(this);
|
|
|
|
var $pagination = $('.pagination', $col);
|
|
|
|
if(!$pagination.size()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-22 12:40:13 +00:00
|
|
|
|
2013-08-22 09:18:12 +00:00
|
|
|
var page_count = $col.data('page_count');
|
2013-08-22 09:25:36 +00:00
|
|
|
var scope = $pagination.last().find("li").size()-2;
|
2013-08-22 09:18:12 +00:00
|
|
|
var kanban_url_col = $pagination.find("li a:first").attr("href").replace(/[0-9]+$/, '');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
'domain': $col.data('domain'),
|
|
|
|
'model': $col.data('model'),
|
|
|
|
'template': $col.data('template'),
|
|
|
|
'step': $col.data('step'),
|
|
|
|
'orderby': $col.data('orderby')
|
|
|
|
};
|
|
|
|
|
|
|
|
$pagination.on('click', 'a', function (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
var $a = $(ev.target);
|
2013-08-22 09:24:16 +00:00
|
|
|
if($a.parent().hasClass('active')) {
|
2013-08-22 09:18:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var page = +$a.attr("href").split(",").pop().split('-')[1];
|
|
|
|
data['page'] = page;
|
|
|
|
|
|
|
|
$.post('/website/kanban/', data, function (col) {
|
2013-08-22 09:24:16 +00:00
|
|
|
$col.find("> .thumbnail").remove();
|
2013-08-22 09:30:49 +00:00
|
|
|
$pagination.last().before(col);
|
2013-08-22 09:18:12 +00:00
|
|
|
});
|
|
|
|
|
2013-09-26 13:52:16 +00:00
|
|
|
var page_start = page - parseInt(Math.floor((scope-1)/2), 10);
|
2013-08-22 09:24:16 +00:00
|
|
|
if (page_start < 1 ) page_start = 1;
|
2013-08-22 09:18:12 +00:00
|
|
|
var page_end = page_start + (scope-1);
|
2013-08-22 09:24:16 +00:00
|
|
|
if (page_end > page_count ) page_end = page_count;
|
2013-08-22 09:18:12 +00:00
|
|
|
|
2013-08-22 09:24:16 +00:00
|
|
|
if (page_end - page_start < scope) {
|
|
|
|
page_start = page_end - scope > 0 ? page_end - scope : 1;
|
2013-08-22 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$pagination.find('li.prev a').attr("href", kanban_url_col+(page-1 > 0 ? page-1 : 1));
|
2013-08-22 09:24:16 +00:00
|
|
|
$pagination.find('li.next a').attr("href", kanban_url_col+(page < page_end ? page+1 : page_end));
|
|
|
|
for(var i=0; i < scope; i++) {
|
2013-08-22 09:18:12 +00:00
|
|
|
$pagination.find('li:not(.prev):not(.next):eq('+i+') a').attr("href", kanban_url_col+(page_start+i)).html(page_start+i);
|
|
|
|
}
|
|
|
|
$pagination.find('li.active').removeClass('active');
|
|
|
|
$pagination.find('li:has(a[href="'+kanban_url_col+page+'"])').addClass('active');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-20 21:34:50 +00:00
|
|
|
/**
|
|
|
|
* Returns a deferred resolved when the templates are loaded
|
|
|
|
* and the Widgets can be instanciated.
|
|
|
|
*/
|
|
|
|
website.ready = function() {
|
|
|
|
if (!all_ready) {
|
2013-11-13 14:30:58 +00:00
|
|
|
all_ready = dom_ready.then(function () {
|
|
|
|
return templates_def;
|
|
|
|
}).then(function () {
|
|
|
|
if (website.is_editable) {
|
2013-10-25 15:24:46 +00:00
|
|
|
website.id = $('html').data('website-id');
|
2013-10-10 13:29:07 +00:00
|
|
|
website.session = new openerp.Session();
|
|
|
|
var modules = ['website'];
|
|
|
|
return openerp._t.database.load_translations(website.session, modules, website.get_context().lang);
|
|
|
|
}
|
2013-10-29 16:11:28 +00:00
|
|
|
}).promise();
|
2013-08-20 21:34:50 +00:00
|
|
|
}
|
|
|
|
return all_ready;
|
|
|
|
};
|
|
|
|
|
2013-11-04 15:52:30 +00:00
|
|
|
website.error = function(data, url) {
|
|
|
|
var $error = $(openerp.qweb.render('website.error_dialog', {
|
|
|
|
'title': data.data ? data.data.arguments[0] : data.statusText,
|
|
|
|
'message': data.data ? data.data.arguments[1] : "",
|
|
|
|
'backend_url': url
|
|
|
|
}));
|
|
|
|
$error.appendTo("body");
|
|
|
|
$error.modal('show');
|
|
|
|
};
|
|
|
|
|
2013-11-13 17:14:38 +00:00
|
|
|
website.prompt = function (window_title, field_name, field_type, on_init) {
|
|
|
|
// A bootstraped version of javascript prompt albeit asynchronous
|
|
|
|
// Built to handle only one field, for anything more complex, use editor.Dialog class
|
|
|
|
// TODO: use option dict for parameters
|
|
|
|
field_type = field_type || 'input';
|
|
|
|
on_init = on_init || function() {};
|
|
|
|
var def = $.Deferred();
|
|
|
|
var dialog = $(openerp.qweb.render('website.prompt', {
|
|
|
|
field_type: field_type,
|
|
|
|
field_name: field_name,
|
|
|
|
window_title: window_title,
|
|
|
|
})).appendTo("body");
|
|
|
|
var input = dialog.find(field_type).first();
|
|
|
|
var init = on_init(input);
|
|
|
|
$.when(init).then(function () {
|
|
|
|
dialog.modal('show');
|
|
|
|
input.focus();
|
|
|
|
dialog.on('click', '.btn-primary', function () {
|
|
|
|
def.resolve(input.val(), input);
|
|
|
|
dialog.remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
dialog.on('hidden.bs.modal', function () {
|
|
|
|
def.reject();
|
|
|
|
dialog.remove();
|
|
|
|
});
|
|
|
|
if (input.is('input[type="text"]')) {
|
|
|
|
input.keypress(function (e) {
|
|
|
|
if (e.which == 13) {
|
|
|
|
dialog.find('.btn-primary').trigger('click');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
};
|
|
|
|
|
2013-08-20 21:34:50 +00:00
|
|
|
dom_ready.then(function () {
|
2013-09-30 15:45:11 +00:00
|
|
|
|
2013-11-04 17:47:20 +00:00
|
|
|
/* ----- BOOTSTRAP STUFF ---- */
|
|
|
|
$('.js_tooltip').bstooltip();
|
|
|
|
|
2013-08-20 21:34:50 +00:00
|
|
|
/* ----- PUBLISHING STUFF ---- */
|
2013-09-30 15:45:11 +00:00
|
|
|
$('[data-publish]:has(.js_publish)').each(function () {
|
|
|
|
var $pub = $("[data-publish]", this);
|
|
|
|
if($pub.size())
|
|
|
|
$(this).attr("data-publish", $pub.attr("data-publish"));
|
|
|
|
else
|
|
|
|
$(this).removeAttr("data-publish");
|
|
|
|
});
|
|
|
|
|
2013-09-26 11:18:54 +00:00
|
|
|
$('[data-publish]:has(.js_publish_management)').each(function () {
|
|
|
|
$(this).attr("data-publish", $(".js_publish_management .btn-success", this).size() ? "on" : 'off');
|
2013-09-30 15:45:11 +00:00
|
|
|
$(this).attr("data-publish", $(".js_publish_management .btn-success", this).size() ? "on" : 'off');
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', '.js_publish', function (e) {
|
|
|
|
e.preventDefault();
|
2013-11-04 15:52:30 +00:00
|
|
|
var $a = $(this);
|
|
|
|
var $data = $a.find(":first").parents("[data-publish]");
|
|
|
|
openerp.jsonRpc($a.data('controller') || '/website/publish', 'call', {'id': +$a.data('id'), 'object': $a.data('object')})
|
2013-09-30 15:45:11 +00:00
|
|
|
.then(function (result) {
|
|
|
|
$data.attr("data-publish", +result ? 'on' : 'off');
|
2013-11-04 15:52:30 +00:00
|
|
|
}).fail(function (err, data) {
|
|
|
|
website.error(data, '/web#model='+$a.data('object')+'&id='+$a.data('id'));
|
2013-09-30 15:45:11 +00:00
|
|
|
});
|
2013-11-05 16:06:17 +00:00
|
|
|
return false;
|
2013-09-03 10:41:15 +00:00
|
|
|
});
|
|
|
|
|
2013-10-22 15:11:44 +00:00
|
|
|
$(document).on('click', '.js_publish_management .js_publish_btn', function () {
|
2013-09-26 11:18:54 +00:00
|
|
|
var $data = $(this).parents(".js_publish_management:first");
|
|
|
|
var $btn = $data.find('.btn:first');
|
|
|
|
var publish = $btn.hasClass("btn-success");
|
|
|
|
|
|
|
|
$data.toggleClass("css_unpublish css_publish");
|
|
|
|
$btn.removeClass("btn-default btn-success");
|
|
|
|
|
2013-11-04 15:52:30 +00:00
|
|
|
openerp.jsonRpc($data.data('controller') || '/website/publish', 'call', {'id': +$data.data('id'), 'object': $data.data('object')})
|
2013-09-26 11:18:54 +00:00
|
|
|
.then(function (result) {
|
2013-09-26 13:09:54 +00:00
|
|
|
$btn.toggleClass("btn-default", !result).toggleClass("btn-success", result);
|
|
|
|
$data.toggleClass("css_unpublish", !result).toggleClass("css_publish", result);
|
2013-09-26 11:18:54 +00:00
|
|
|
$data.parents("[data-publish]").attr("data-publish", +result ? 'on' : 'off');
|
2013-11-04 15:52:30 +00:00
|
|
|
}).fail(function (err, data) {
|
|
|
|
website.error(data, '/web#model='+$data.data('object')+'&id='+$data.data('id'));
|
2013-09-26 11:18:54 +00:00
|
|
|
});
|
2013-08-20 21:34:50 +00:00
|
|
|
});
|
|
|
|
|
2013-08-22 09:18:12 +00:00
|
|
|
/* ----- KANBAN WEBSITE ---- */
|
2013-08-22 09:24:16 +00:00
|
|
|
$('.js_kanban').each(function () {
|
2013-08-22 09:18:12 +00:00
|
|
|
website.init_kanban(this);
|
|
|
|
});
|
|
|
|
|
2013-10-16 10:45:45 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
if (window.location.hash.indexOf("scrollTop=") > -1) {
|
|
|
|
window.document.body.scrollTop = +location.hash.match(/scrollTop=([0-9]+)/)[1];
|
|
|
|
}
|
|
|
|
},0);
|
2013-08-20 21:34:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return website;
|
|
|
|
})();
|