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-14 09:32:14 +00:00
|
|
|
website.prompt = function (options) {
|
|
|
|
/**
|
|
|
|
* A bootstrapped version of prompt() albeit asynchronous
|
|
|
|
* This was built to quickly prompt the user with a single field.
|
|
|
|
* For anything more complex, please use editor.Dialog class
|
|
|
|
*
|
|
|
|
* Usage Ex:
|
|
|
|
*
|
|
|
|
* website.prompt("What... is your quest ?").then(function (answer) {
|
2013-11-14 10:04:19 +00:00
|
|
|
* arthur.reply(answer || "To seek the Holy Grail.");
|
2013-11-14 09:32:14 +00:00
|
|
|
* });
|
|
|
|
*
|
|
|
|
* website.prompt({
|
|
|
|
* select: "Please choose your destiny",
|
|
|
|
* init: function() {
|
|
|
|
* return [ [0, "Sub-Zero"], [1, "Robo-Ky"] ];
|
|
|
|
* }
|
|
|
|
* }).then(function (answer) {
|
|
|
|
* mame_station.loadCharacter(answer);
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param {Object|String} options A set of options used to configure the prompt or the text field name if string
|
|
|
|
* @param {String} [options.window_title=''] title of the prompt modal
|
|
|
|
* @param {String} [options.input] tell the modal to use an input text field, the given value will be the field title
|
|
|
|
* @param {String} [options.textarea] tell the modal to use a textarea field, the given value will be the field title
|
|
|
|
* @param {String} [options.select] tell the modal to use a select box, the given value will be the field title
|
|
|
|
* @param {Object} [options.default=''] default value of the field
|
|
|
|
* @param {Function} [options.init] optional function that takes the `field` (enhanced with a fillWith() method) and the `dialog` as parameters [can return a deferred]
|
|
|
|
*/
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = {
|
|
|
|
text: options
|
|
|
|
};
|
|
|
|
}
|
|
|
|
options = _.extend({
|
|
|
|
window_title: '',
|
|
|
|
field_name: '',
|
|
|
|
default: '',
|
|
|
|
init: function() {}
|
|
|
|
}, options || {});
|
|
|
|
|
|
|
|
var type = _.intersect(Object.keys(options), ['input', 'textarea', 'select']);
|
|
|
|
type = type.length ? type[0] : 'text';
|
|
|
|
options.field_type = type;
|
|
|
|
options.field_name = options.field_name || options[type];
|
|
|
|
|
2013-11-13 17:14:38 +00:00
|
|
|
var def = $.Deferred();
|
2013-11-14 09:32:14 +00:00
|
|
|
var dialog = $(openerp.qweb.render('website.prompt', options)).appendTo("body");
|
|
|
|
var field = dialog.find(options.field_type).first();
|
|
|
|
field.val(options.default);
|
|
|
|
field.fillWith = function (data) {
|
|
|
|
if (field.is('select')) {
|
|
|
|
var select = field[0];
|
|
|
|
data.forEach(function (item) {
|
|
|
|
select.options[select.options.length] = new Option(item[1], item[0]);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
field.val(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var init = options.init(field, dialog);
|
|
|
|
$.when(init).then(function (fill) {
|
|
|
|
if (fill) {
|
|
|
|
field.fillWith(fill);
|
|
|
|
}
|
2013-11-13 17:14:38 +00:00
|
|
|
dialog.modal('show');
|
2013-11-14 09:32:14 +00:00
|
|
|
field.focus();
|
2013-11-13 17:14:38 +00:00
|
|
|
dialog.on('click', '.btn-primary', function () {
|
2013-11-14 09:32:14 +00:00
|
|
|
def.resolve(field.val(), field);
|
2013-11-13 17:14:38 +00:00
|
|
|
dialog.remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
dialog.on('hidden.bs.modal', function () {
|
|
|
|
def.reject();
|
|
|
|
dialog.remove();
|
|
|
|
});
|
2013-11-14 09:32:14 +00:00
|
|
|
if (field.is('input[type="text"], select')) {
|
|
|
|
field.keypress(function (e) {
|
2013-11-13 17:14:38 +00:00
|
|
|
if (e.which == 13) {
|
2013-11-19 10:46:41 +00:00
|
|
|
e.preventDefault();
|
2013-11-13 17:14:38 +00:00
|
|
|
dialog.find('.btn-primary').trigger('click');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
};
|
|
|
|
|
2013-11-19 10:59:14 +00:00
|
|
|
website.form = function (url, method, params) {
|
|
|
|
var form = document.createElement('form');
|
|
|
|
form.action = url;
|
|
|
|
form.method = method;
|
|
|
|
_.each(params, function (v, k) {
|
|
|
|
var param = document.createElement('input');
|
|
|
|
param.type = 'hidden';
|
|
|
|
param.name = k;
|
|
|
|
param.value = v;
|
|
|
|
form.appendChild(param);
|
|
|
|
});
|
|
|
|
document.body.appendChild(form);
|
|
|
|
form.submit();
|
|
|
|
};
|
|
|
|
|
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-10-22 15:11:44 +00:00
|
|
|
$(document).on('click', '.js_publish_management .js_publish_btn', function () {
|
2013-11-16 15:09:27 +00:00
|
|
|
var $data = $(this).parent(".js_publish_management");
|
2013-11-18 12:49:58 +00:00
|
|
|
var self=this;
|
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-11-16 15:09:27 +00:00
|
|
|
$data.toggleClass("css_unpublished css_published");
|
|
|
|
$(self).toggleClass("btn-success btn-danger");
|
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) {
|
2013-11-18 12:49:58 +00:00
|
|
|
website.error(data, '/web#return_label=Website&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;
|
|
|
|
})();
|