5 // The following line can be removed in 2017
6 openerp.website = website;
8 website.get_context = function (dict) {
9 var html = document.documentElement;
11 lang: html.getAttribute('lang').replace('-', '_'),
12 website_id: html.getAttribute('data-website-id')|0
16 website.parseQS = function (qs) {
19 pl = /\+/g, // Regex for replacing addition symbol with a space
20 search = /([^&=]+)=?([^&]*)/g;
22 while ((match = search.exec(qs))) {
23 var name = decodeURIComponent(match[1].replace(pl, " "));
24 var value = decodeURIComponent(match[2].replace(pl, " "));
31 website.parseSearch = function () {
33 parsedSearch = website.parseQS(window.location.search.substring(1));
37 website.parseHash = function () {
38 return website.parseQS(window.location.hash.substring(1));
41 /* ----- TEMPLATE LOADING ---- */
42 var templates_def = $.Deferred().resolve();
43 website.add_template_file = function(template) {
44 templates_def = templates_def.then(function() {
45 var def = $.Deferred();
46 openerp.qweb.add_template(template, function(err) {
56 website.add_template_file('/website/static/src/xml/website.xml');
57 website.reload = function () {
58 location.hash = "scrollTop=" + window.document.body.scrollTop;
59 if (location.search.indexOf("enable_editor") > -1) {
60 window.location.href = window.location.href.replace(/enable_editor(=[^&]*)?/g, '');
62 window.location.reload();
67 var dom_ready = website.dom_ready = $.Deferred();
68 $(document).ready(function () {
69 website.is_editable = $('html').data('editable');
73 website.init_kanban = function ($kanban) {
74 $('.js_kanban_col', $kanban).each(function () {
76 var $pagination = $('.pagination', $col);
77 if(!$pagination.size()) {
81 var page_count = $col.data('page_count');
82 var scope = $pagination.last().find("li").size()-2;
83 var kanban_url_col = $pagination.find("li a:first").attr("href").replace(/[0-9]+$/, '');
86 'domain': $col.data('domain'),
87 'model': $col.data('model'),
88 'template': $col.data('template'),
89 'step': $col.data('step'),
90 'orderby': $col.data('orderby')
93 $pagination.on('click', 'a', function (ev) {
95 var $a = $(ev.target);
96 if($a.parent().hasClass('active')) {
100 var page = +$a.attr("href").split(",").pop().split('-')[1];
103 $.post('/website/kanban/', data, function (col) {
104 $col.find("> .thumbnail").remove();
105 $pagination.last().before(col);
108 var page_start = page - parseInt(Math.floor((scope-1)/2), 10);
109 if (page_start < 1 ) page_start = 1;
110 var page_end = page_start + (scope-1);
111 if (page_end > page_count ) page_end = page_count;
113 if (page_end - page_start < scope) {
114 page_start = page_end - scope > 0 ? page_end - scope : 1;
117 $pagination.find('li.prev a').attr("href", kanban_url_col+(page-1 > 0 ? page-1 : 1));
118 $pagination.find('li.next a').attr("href", kanban_url_col+(page < page_end ? page+1 : page_end));
119 for(var i=0; i < scope; i++) {
120 $pagination.find('li:not(.prev):not(.next):eq('+i+') a').attr("href", kanban_url_col+(page_start+i)).html(page_start+i);
122 $pagination.find('li.active').removeClass('active');
123 $pagination.find('li:has(a[href="'+kanban_url_col+page+'"])').addClass('active');
131 * Returns a deferred resolved when the templates are loaded
132 * and the Widgets can be instanciated.
134 website.ready = function() {
136 all_ready = dom_ready.then(function () {
137 return templates_def;
138 }).then(function () {
139 if (website.is_editable) {
140 website.id = $('html').data('website-id');
141 website.session = new openerp.Session();
142 var modules = ['website'];
143 return openerp._t.database.load_translations(website.session, modules, website.get_context().lang);
150 website.error = function(data, url) {
151 var $error = $(openerp.qweb.render('website.error_dialog', {
152 'title': data.data ? data.data.arguments[0] : data.statusText,
153 'message': data.data ? data.data.arguments[1] : "",
156 $error.appendTo("body");
157 $error.modal('show');
160 website.prompt = function (options) {
162 * A bootstrapped version of prompt() albeit asynchronous
163 * This was built to quickly prompt the user with a single field.
164 * For anything more complex, please use editor.Dialog class
168 * website.prompt("What... is your quest ?").then(function (answer) {
169 * arthur.reply(answer || "To seek the Holy Grail.");
173 * select: "Please choose your destiny",
175 * return [ [0, "Sub-Zero"], [1, "Robo-Ky"] ];
177 * }).then(function (answer) {
178 * mame_station.loadCharacter(answer);
181 * @param {Object|String} options A set of options used to configure the prompt or the text field name if string
182 * @param {String} [options.window_title=''] title of the prompt modal
183 * @param {String} [options.input] tell the modal to use an input text field, the given value will be the field title
184 * @param {String} [options.textarea] tell the modal to use a textarea field, the given value will be the field title
185 * @param {String} [options.select] tell the modal to use a select box, the given value will be the field title
186 * @param {Object} [options.default=''] default value of the field
187 * @param {Function} [options.init] optional function that takes the `field` (enhanced with a fillWith() method) and the `dialog` as parameters [can return a deferred]
189 if (typeof options === 'string') {
201 var type = _.intersect(Object.keys(options), ['input', 'textarea', 'select']);
202 type = type.length ? type[0] : 'text';
203 options.field_type = type;
204 options.field_name = options.field_name || options[type];
206 var def = $.Deferred();
207 var dialog = $(openerp.qweb.render('website.prompt', options)).appendTo("body");
208 var field = dialog.find(options.field_type).first();
209 field.val(options.default);
210 field.fillWith = function (data) {
211 if (field.is('select')) {
212 var select = field[0];
213 data.forEach(function (item) {
214 select.options[select.options.length] = new Option(item[1], item[0]);
220 var init = options.init(field, dialog);
221 $.when(init).then(function (fill) {
223 field.fillWith(fill);
225 dialog.modal('show');
227 dialog.on('click', '.btn-primary', function () {
228 def.resolve(field.val(), field);
232 dialog.on('hidden.bs.modal', function () {
236 if (field.is('input[type="text"], select')) {
237 field.keypress(function (e) {
240 dialog.find('.btn-primary').trigger('click');
247 website.form = function (url, method, params) {
248 var form = document.createElement('form');
250 form.method = method;
251 _.each(params, function (v, k) {
252 var param = document.createElement('input');
253 param.type = 'hidden';
256 form.appendChild(param);
258 document.body.appendChild(form);
262 dom_ready.then(function () {
264 /* ----- BOOTSTRAP STUFF ---- */
265 $('.js_tooltip').bstooltip();
267 /* ----- PUBLISHING STUFF ---- */
268 $(document).on('click', '.js_publish_management .js_publish_btn', function () {
269 var $data = $(this).parent(".js_publish_management");
271 openerp.jsonRpc($data.data('controller') || '/website/publish', 'call', {'id': +$data.data('id'), 'object': $data.data('object')})
272 .then(function (result) {
273 $data.toggleClass("css_unpublished css_published");
274 $(self).toggleClass("btn-success btn-danger");
275 $data.parents("[data-publish]").attr("data-publish", +result ? 'on' : 'off');
276 }).fail(function (err, data) {
277 website.error(data, '/web#return_label=Website&model='+$data.data('object')+'&id='+$data.data('id'));
281 /* ----- KANBAN WEBSITE ---- */
282 $('.js_kanban').each(function () {
283 website.init_kanban(this);
286 setTimeout(function () {
287 if (window.location.hash.indexOf("scrollTop=") > -1) {
288 window.document.body.scrollTop = +location.hash.match(/scrollTop=([0-9]+)/)[1];