summaryrefslogtreecommitdiff
path: root/addons/web_editor/static/src/js/editor.js
diff options
context:
space:
mode:
Diffstat (limited to 'addons/web_editor/static/src/js/editor.js')
-rw-r--r--addons/web_editor/static/src/js/editor.js164
1 files changed, 164 insertions, 0 deletions
diff --git a/addons/web_editor/static/src/js/editor.js b/addons/web_editor/static/src/js/editor.js
new file mode 100644
index 00000000000..b09a69e3818
--- /dev/null
+++ b/addons/web_editor/static/src/js/editor.js
@@ -0,0 +1,164 @@
+odoo.define('web_editor.editor', function (require) {
+"use strict";
+
+var ajax = require('web.ajax');
+var Widget = require('web.Widget');
+var core = require('web.core');
+var base = require('web_editor.base');
+var rte = require('web_editor.rte');
+
+var qweb = core.qweb;
+var _t = core._t;
+var editor = {};
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+editor.dummy = function () {return true;}; // used for snippets, options...
+
+editor.editable = !!$('html').data('editable');
+
+ajax.loadXML('/web_editor/static/src/xml/editor.xml', qweb);
+
+base.dom_ready.done(function () {
+ $(document).on('click', '.note-editable', function (ev) {
+ ev.preventDefault();
+ });
+
+ $(document).on('submit', '.note-editable form .btn', function (ev) {
+ // Disable form submition in editable mode
+ ev.preventDefault();
+ });
+
+ $(document).on('hide.bs.dropdown', '.dropdown', function (ev) {
+ // Prevent dropdown closing when a contenteditable children is focused
+ if (ev.originalEvent
+ && $(ev.target).has(ev.originalEvent.target).length
+ && $(ev.originalEvent.target).is('[contenteditable]')) {
+ ev.preventDefault();
+ }
+ });
+});
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+editor.reload = function () {
+ location.hash = "scrollTop=" + window.document.body.scrollTop;
+ if (location.search.indexOf("enable_editor") > -1) {
+ window.location.href = window.location.href.replace(/&?enable_editor(=[^&]*)?/g, '');
+ } else {
+ window.location.reload();
+ }
+};
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/* ----- TOP EDITOR BAR FOR ADMIN ---- */
+
+base.ready().then(function () {
+ if (editor.editable && location.search.indexOf("enable_editor") >= 0 && !editor.no_editor) {
+ editor.editor_bar = new editor.Class();
+ editor.editor_bar.prependTo(document.body);
+ }
+});
+
+editor.Class = Widget.extend({
+ template: 'web_editor.editorbar',
+ events: {
+ 'click button[data-action=save]': 'save',
+ 'click a[data-action=cancel]': 'cancel',
+ },
+ init: function(parent) {
+ var self = this;
+ var res = this._super.apply(this, arguments);
+ this.parent = parent;
+ this.rte = new rte.Class(this);
+ this.rte.on('rte:start', this, function () {
+ self.trigger('rte:start');
+ });
+ return res;
+ },
+ start: function() {
+ var self = this;
+
+ this.$('button[data-action=save]').prop('disabled', true);
+ $('.dropdown-toggle').dropdown();
+
+ this.display_placeholder();
+
+ this.rte.on('change', this, this.proxy('rte_changed'));
+ this.rte.start();
+
+ var flag = false;
+ window.onbeforeunload = function(event) {
+ if ($('.o_editable.o_dirty').length && !flag) {
+ flag = true;
+ setTimeout(function () {flag=false;},0);
+ return _t('This document is not saved!');
+ }
+ };
+ return this._super();
+ },
+ display_placeholder: function () {
+ var $area = $("#wrapwrap").find("[data-oe-model] .oe_structure.oe_empty, [data-oe-model].oe_structure.oe_empty, [data-oe-type=html]")
+ .addClass("oe_empty")
+ .attr("data-oe-placeholder", _t("Press The Top-Left Edit Button"));
+
+ this.on('rte:start', this, function () {
+ $area.attr("data-oe-placeholder", _t("Write Your Text Here"));
+ });
+ this.on("snippets:ready", this, function () {
+ if ($("body").hasClass("editor_has_snippets")) {
+ $area.attr("data-oe-placeholder", _t("Write Your Text or Drag a Block Here"));
+ }
+ });
+
+ $(document).on("keyup", function (event) {
+ if((event.keyCode === 8 || event.keyCode === 46)) {
+ var $target = $(event.target).closest(".o_editable");
+ if(!$target.is(":has(*:not(p):not(br))") && !$target.text().match(/\S/)) {
+ $target.empty();
+ }
+ }
+ });
+ },
+ rte_changed: function () {
+ // todo: use !rte.history.getEditableHasUndo().length (but phantomjs unknow error)
+ this.$('button[data-action=save]').prop('disabled', !$('.o_dirty').length);
+ },
+ save: function () {
+ return this.rte.save().then(function () {
+ editor.reload();
+ });
+ },
+ /**
+ * Saves an RTE content, which always corresponds to a view section (?).
+ */
+ save_without_reload: function () {
+ return this.rte.save();
+ },
+ cancel: function () {
+ new $.Deferred(function (d) {
+ if (!rte.history.getEditableHasUndo().length) {
+ return d.resolve();
+ }
+ var $dialog = $(qweb.render('web_editor.discard')).appendTo(document.body);
+ $dialog.on('click', '.btn-danger', function () {
+ d.resolve();
+ }).on('hidden.bs.modal', function () {
+ d.reject();
+ });
+ d.always(function () {
+ $dialog.remove();
+ });
+ $dialog.modal('show');
+ }).then(function () {
+ window.onbeforeunload = null;
+ editor.reload();
+ });
+ },
+});
+
+return editor;
+
+});
+