odoo.define('website.mobile', function (require) { 'use strict'; var Widget = require('web.Widget'); var website = require('website.website'); website.add_template_file('/website/static/src/xml/website.xml'); var MobilePreview = Widget.extend({ template: 'website.mobile_preview', events: { 'hidden.bs.modal': 'destroy' }, start: function() { if (!window.location.origin) { // fix for ie9 window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: ''); } document.getElementById("mobile-viewport").src = window.location.origin + window.location.pathname + window.location.search + "#mobile-preview"; this.$el.modal(); }, destroy: function() { $('.modal-backdrop').remove(); this._super(); }, }); website.ready().done(function() { $(document.body).on('click', 'a[data-action=show-mobile-preview]', function() { new MobilePreview().appendTo($(document.body)); }); }); });
}).then(function (forum_name, field, $dialog) { var add_menu = ($dialog.find('input[type="checkbox"]').is(':checked')); website.form('/forum/new', 'POST', { forum_name: forum_name, add_menu: add_menu || "" }); });
loadView: function (id) { var viewId = parseInt(id, 10); var self = this; ajax.jsonRpc('/web/dataset/call', 'call', { model: 'ir.ui.view', method: 'read', args: [[viewId], ['arch'], website.get_context()], }).then(function(result) { var editingSession = self.buffers[viewId] = new ace.EditSession(result[0].arch); editingSession.setMode("ace/mode/xml"); editingSession.setUndoManager(new ace.UndoManager()); editingSession.on("change", function () { setTimeout(function () { var $option = self.$('#ace-view-list').find('[value='+viewId+']'); var bufferName = $option.text(); var dirtyMarker = " (unsaved changes)"; var isDirty = editingSession.getUndoManager().hasUndo(); if (isDirty && bufferName.indexOf(dirtyMarker) < 0) { $option.text(bufferName + dirtyMarker); } else if (!isDirty && bufferName.indexOf(dirtyMarker) > 0) { $option.text(bufferName.substring(0, bufferName.indexOf(dirtyMarker))); } }, 1); }); if (viewId === self.selectedViewId()) { self.displayView.call(self, viewId); } }); },
new_forum: function() { website.prompt({ id: "editor_new_forum", window_title: _t("New Forum"), input: "Forum Name",init: function () { var $group = this.$dialog.find("div.form-group"); $group.removeClass("mb0"); var $add = $( '<div class="form-group mb0">'+ '<label class="col-sm-offset-3 col-sm-9 text-left">'+ ' <input type="checkbox" required="required"/> '+ '</label>'+ '</div>'); $add.find('label').append(_t("Add page in menu")); $group.after($add); } }).then(function (forum_name, field, $dialog) { var add_menu = ($dialog.find('input[type="checkbox"]').is(':checked')); website.form('/forum/new', 'POST', { forum_name: forum_name, add_menu: add_menu || "" }); }); },
rename_page: function() { var self = this; var context = website.get_context(); self.mo_id = self.getMainObject().id; openerp.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website', method: 'page_search_dependencies', args: [self.mo_id], kwargs: { context: context }, }).then(function (deps) { website.prompt({ id: "editor_rename_page", window_title: _t("Rename Page"), dependencies: deps, }, 'website.rename_page').then(function (val, field, $dialog) { openerp.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website', method: 'rename_page', args: [ self.mo_id, val, ], kwargs: { context: context }, }).then(function (new_name) { window.location = "/page/" + encodeURIComponent(new_name); }); }); }); },
new_page: function() { website.prompt({ id: "editor_new_page", window_title: _t("New Page"), input: _t("Page Title"), init: function () { var $group = this.$dialog.find("div.form-group"); $group.removeClass("mb0"); var $add = $( '<div class="form-group mb0">'+ '<label class="col-sm-offset-3 col-sm-9 text-left">'+ ' <input type="checkbox" checked="checked" required="required"/> '+ '</label>'+ '</div>'); $add.find('label').append(_t("Add page in menu")); $group.after($add); } }).then(function (val, field, $dialog) { if (val) { var url = '/website/add/' + encodeURIComponent(val); if ($dialog.find('input[type="checkbox"]').is(':checked')) url +="?add_menu=1"; document.location = url; } }); },
edit_menu: function() { var context = website.get_context(); var def = $.Deferred(); if ($("[data-content_menu_id]").length) { var select = new SelectEditMenuDialog(); select.appendTo(document.body); select.on('save', this, function (root) { def.resolve(root); }); } else { def.resolve(null); } def.then(function (root_id) { ajax.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website.menu', method: 'get_tree', args: [context.website_id, root_id], kwargs: { context: context }, }).then(function (menu) { var result = new EditMenuDialog(menu).appendTo(document.body); return result; }); }); },
save: function () { var self = this; var new_menu = this.$('.oe_menu_editor').nestedSortable('toArray', {startDepthCount: 0}); var levels = []; var data = []; var context = website.get_context(); // Resequence, re-tree and remove useless data new_menu.forEach(function (menu) { if (menu.item_id) { levels[menu.depth] = (levels[menu.depth] || 0) + 1; var mobj = self.flat[menu.item_id]; mobj.sequence = levels[menu.depth]; mobj.parent_id = (menu.parent_id|0) || menu.parent_id || self.root_menu_id; delete(mobj.children); data.push(mobj); } }); ajax.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website.menu', method: 'save', args: [[context.website_id], { data: data, to_delete: self.to_delete }], kwargs: { context: context }, }).then(function (menu) { self.close(); website.reload(); }); },
delete_page: function() { var self = this; var context = website.get_context(); self.mo_id = self.getMainObject().id; ajax.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website', method: 'page_search_dependencies', args: [self.mo_id], kwargs: { context: context }, }).then(function (deps) { website.prompt({ id: "editor_delete_page", window_title: _t("Delete Page"), dependencies: deps, init: function() { $('.btn-continue').prop("disabled", true)}, }, 'website.delete_page').then(function (val, field, $dialog) { if ($dialog.find('input[type="checkbox"]').is(':checked')){ ajax.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website', method: 'delete_page', args: [self.mo_id], kwargs: { context: context }, }).then(function () { window.location = "/"; }); } }); }); },
trackPage: function(val) { var obj = seo.Configurator.prototype.getMainObject(); if (!obj) { return $.Deferred().reject(); } else { return new Model(obj.model).call('write', [[obj.id], { track: val }, website.get_context()]); } },
is_tracked: function(val) { var obj = seo.Configurator.prototype.getMainObject(); if (!obj) { return $.Deferred().reject(); } else { return new Model(obj.model).call('read', [[obj.id], ['track'], website.get_context()]); } },
$(".odoo-tw-view-tweet-delete").on('click', function() { var tweet = $(this).parent().parent(".odoo-tw-tweet"); new Model("twitter.tweet").call("unlink", [[tweet.data("tweet-id")]], { context: website.get_context() }).then(function(res) { if(res) tweet.slideUp(500); }); });
saveMetaData: function (data) { var obj = this.getMainObject(); if (!obj) { return $.Deferred().reject(); } else { var model = new Model(obj.model); return model.call('write', [[obj.id], data, website.get_context()]); } },
function themeError(message) { var _t = core._t; if (message.indexOf('lessc')) { message = '<span class="text-muted">' + message + "</span><br/><br/>" + _t("Please install or update node-less"); } website.error(_t("Theme Error"), message); }
$(document).ready(function () { load_called_template(); // if asset is placed into head, move this call into $(document).ready if ($(".o_gallery:not(.oe_slideshow)").size()) { // load gallery modal template website.add_template_file('/website/static/src/xml/website.gallery.xml'); } start_animation(); });
$("button.odoo-tw-wall-archive").on('click', function() { var self = this; new Model("twitter.agent").call("write", [[$(this).data("wall-id")], {'state': 'archive'}, website.get_context()] ).then(function(res) { if(res) { $(self).hide(); $(self).siblings("label.odoo-tw-wall-archive").show(); } }); });
new_event: function() { website.prompt({ id: "editor_new_event", window_title: _t("New Event"), input: "Event Name", }).then(function (event_name) { website.form('/event/add_event', 'POST', { event_name: event_name }); }); },
this.$("input[name='tweetus_hashtag']").select2(this.select2_wrapper(_t('Hashtag'), true, function () { return ajax.jsonRpc("/web/dataset/call_kw", 'call', { model: 'twitter.hashtag', method: 'search_read', args: [], kwargs: { fields: ['name'], context: website.get_context() } }); }));
new_product: function() { website.prompt({ id: "editor_new_product", window_title: _t("New Product"), input: "Product Name", }).then(function (name) { website.form('/shop/add_product', 'POST', { name: name }); }); },
select_mailing_list: function (type, value) { var self = this; if (type !== "click") return; return website.prompt({ id: "editor_new_subscribe_button", window_title: _t("Add a Subscribe Button"), select: _t("Discussion List"), init: function (field) { return new Model('mail.channel') .call('name_search', ['', [['public','=','public']]], { context: base.get_context() }); }, }).then(function (mail_channel_id) { self.$target.attr("data-id", mail_channel_id); }); },
displayError: function (source, session, error) { var self = this; var line, test; // format error message var message = _.isString(error) ? error : (error && error.data && error.data.arguments && error.data.arguments[0] === "Access Denied") ? "Access denied: please sign in" : (error && error.data && error.data.message) ? error.data.message : (error && error.message) ? error.message : "Unexpected error"; if (source == "server") { message = eval(message.replace(/^\(/g, '([') .replace(/\)$/g, '])') .replace(/u'/g, "'") .replace(/<([^>]+)>/g, '<b style="color:#661100;"><\$1></b>'))[1]; line = -1; } else { line = message.match(/line ([0-9]+)/i); line = line ? parseInt(line[1],10) : -1; test = new RegExp("^\\s*"+line+"\\s*$"); } function gotoline() { self.aceEditor.gotoLine(line); setTimeout(function () { var $lines = self.$(".ace_editor .ace_gutter .ace_gutter-cell"); var index = $lines.filter(function () { return test.test($(this).text()); }).index(); if (index>0) { self.$(".ace_layer.ace_text-layer .ace_line:eq(" + index + ")").css("background", "#661100"); } },100); } function onchangeSession () { self.aceEditor.off('changeSession', onchangeSession); gotoline(); } var $list = this.$("#ace-view-list"); if (+$list.val() == session.id) { if (line>-1) gotoline(); } else { if (line) self.aceEditor.on('changeSession', onchangeSession); this.$("#ace-view-list").val(session.id).change(); } website.error(session.text.match(/\s+name=['"]([^'"]+)['"]/i)[1], "<b>Malformed XML document</b>:<br/>" + message); },
$modal.on('click', '.a-submit', function () { var $a = $(this); $form.ajaxSubmit({ url: '/shop/cart/update_option', data: {lang: website.get_context().lang}, success: function (quantity) { if (!$a.hasClass('js_goto_shop')) { window.location.href = window.location.href.replace(/shop([\/?].*)?$/, "shop/cart"); } var $q = $(".my_cart_quantity"); $q.parent().parent().removeClass("hidden", !quantity); $q.html(quantity).hide().fadeIn(600); } }); $modal.modal('hide'); });
select_mailing_list: function (type, value) { var self = this; if (type !== "click") return; var def = website.prompt({ 'id': this.popup_template_id, 'window_title': this.popup_title, 'select': _t("Newsletter"), 'init': function (field) { return new Model('mail.mass_mailing.list').call('name_search', ['', []], { context: base.get_context() }); }, }); def.then(function (mailing_list_id) { self.$target.attr("data-list-id", mailing_list_id); }); return def; },
model.call('name_search', [], { context: base.get_context() }).then(function (blog_ids) { if (blog_ids.length == 1) { document.location = '/blog/' + blog_ids[0][0] + '/post/new'; } else if (blog_ids.length > 1) { website.prompt({ id: "editor_new_blog", window_title: _t("New Blog Post"), select: "Select Blog", init: function (field) { return blog_ids; }, }).then(function (blog_id) { document.location = '/blog/' + blog_id + '/post/new'; }); } });
saveView: function (session) { var xml = new XmlDocument(session.text); var isWellFormed = xml.isWellFormed(); var def = $.Deferred(); if (isWellFormed === true) { ajax.jsonRpc('/web/dataset/call', 'call', { model: 'ir.ui.view', method: 'write', args: [[session.id], { 'arch': xml.xml }, website.get_context()], }).then(function () { def.resolve(); }).fail(function (source, error) { def.reject("server", session, error); }); } else { def.reject(null, session, isWellFormed); } return def; },
ajax.jsonRpc( '/website_version/set_google_access', 'call', {'ga_key':ga_key, 'view_id':view_id, 'client_id':client_id, 'client_secret':client_secret}).then(function (result) { var context = website.get_context(); ajax.jsonRpc( '/website_version/google_access', 'call', { fromurl: window.location.href, local_context: context }).done(function(o) { if (o.status === "need_auth") { var wizardC = $(QWeb.render("website_version.message",{message:_t("You will be redirected to Google to authorize access to your Analytics Account!")})); wizardC.appendTo($('body')).modal({"keyboard" :true}); wizardC.on('click','.o_confirm', function(){ window.location.href = o.url; }); wizardC.on('hidden.bs.modal', function () {$(this).remove();}); } else if (o.status === "need_config_from_admin"){ if (confirm(_t("The Google Management API key needs to be configured before you can use it, do you want to do it now?"))) { window.location.href = o.action; } } }).always(function() { $('button.GoogleAccess').prop('disabled', false); }); });
}).then(function (deps) { website.prompt({ id: "editor_rename_page", window_title: _t("Rename This Page"), dependencies: deps, }, 'website.rename_page').then(function (val, field, $dialog) { ajax.jsonRpc('/web/dataset/call_kw', 'call', { model: 'website', method: 'rename_page', args: [ self.mo_id, val, ], kwargs: { context: context }, }).then(function (new_name) { window.location = "/page/" + encodeURIComponent(new_name); }); }); });
new_page: function () { website.prompt({ id: "editor_new_page", window_title: _t("New Page"), input: _t("Page Title"), init: function () { var $group = this.$dialog.find("div.form-group"); $group.removeClass("mb0"); var $add = $('<div/>', {'class': 'form-group mb0'}) .append($('<span/>', {'class': 'col-sm-offset-3 col-sm-9 text-left'}) .append(qweb.render('web_editor.components.switch', {id: 'switch_addTo_menu', label: _t("Add page in menu")}))); $add.find('input').prop('checked', true); $group.after($add); } }).then(function (val, field, $dialog) { if (val) { var url = '/website/add/' + encodeURIComponent(val); if ($dialog.find('input[type="checkbox"]').is(':checked')) url +="?add_menu=1"; document.location = url; } }); },
loadMetaData: function () { var self = this; var obj = this.getMainObject(); var def = $.Deferred(); if (!obj) { // return $.Deferred().reject(new Error("No main_object was found.")); def.resolve(null); } else { var fields = ['website_meta_title', 'website_meta_description', 'website_meta_keywords']; var model = new Model(obj.model); model.call('read', [[obj.id], fields, website.get_context()]).then(function (data) { if (data.length) { var meta = data[0]; meta.model = obj.model; def.resolve(meta); } else { def.resolve(null); } }).fail(function () { def.reject(); }); } return def; },
}).then(function (name) { website.form('/shop/add_product', 'POST', { name: name }); });