1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
odoo.define('website.editor.menu', function (require) {
'use strict';
var Dialog = require('web.Dialog');
var dom = require('web.dom');
var Widget = require('web.Widget');
var core = require('web.core');
var Wysiwyg = require('web_editor.wysiwyg.root');
var _t = core._t;
var WysiwygMultizone = Wysiwyg.extend({
assetLibs: Wysiwyg.prototype.assetLibs.concat(['website.compiled_assets_wysiwyg']),
_getWysiwygContructor: function () {
return odoo.__DEBUG__.services['web_editor.wysiwyg.multizone'];
}
});
var EditorMenu = Widget.extend({
template: 'website.editorbar',
xmlDependencies: ['/website/static/src/xml/website.editor.xml'],
events: {
'click button[data-action=undo]': '_onUndoClick',
'click button[data-action=redo]': '_onRedoClick',
'click button[data-action=save]': '_onSaveClick',
'click button[data-action=cancel]': '_onCancelClick',
},
custom_events: {
request_save: '_onSnippetRequestSave',
get_clean_html: '_onGetCleanHTML',
},
/**
* @override
*/
willStart: function () {
var self = this;
this.$el = null; // temporary null to avoid hidden error (@see start)
return this._super()
.then(function () {
var $wrapwrap = $('#wrapwrap');
$wrapwrap.removeClass('o_editable'); // clean the dom before edition
self.editable($wrapwrap).addClass('o_editable');
self.wysiwyg = self._wysiwygInstance();
});
},
/**
* @override
*/
start: function () {
var self = this;
this.$el.css({width: '100%'});
return this.wysiwyg.attachTo($('#wrapwrap')).then(function () {
self.trigger_up('edit_mode');
self.$el.css({width: ''});
});
},
/**
* @override
*/
destroy: function () {
this.trigger_up('readonly_mode');
this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Asks the user if they really wants to discard their changes (if any),
* then simply reloads the page if they want to.
*
* @param {boolean} [reload=true]
* true if the page has to be reloaded when the user answers yes
* (do nothing otherwise but add this to allow class extension)
* @returns {Deferred}
*/
cancel: function (reload) {
var self = this;
var def = new Promise(function (resolve, reject) {
if (!self.wysiwyg.isDirty()) {
resolve();
} else {
var confirm = Dialog.confirm(self, _t("If you discard the current edits, all unsaved changes will be lost. You can cancel to return to edit mode."), {
confirm_callback: resolve,
});
confirm.on('closed', self, reject);
}
});
return def.then(function () {
self.trigger_up('edition_will_stopped');
var $wrapwrap = $('#wrapwrap');
self.editable($wrapwrap).removeClass('o_editable');
if (reload !== false) {
window.onbeforeunload = null;
self.wysiwyg.destroy();
return self._reload();
} else {
self.wysiwyg.destroy();
self.trigger_up('readonly_mode');
self.trigger_up('edition_was_stopped');
self.destroy();
}
});
},
/**
* Asks the snippets to clean themself, then saves the page, then reloads it
* if asked to.
*
* @param {boolean} [reload=true]
* true if the page has to be reloaded after the save
* @returns {Promise}
*/
save: async function (reload) {
if (this._saving) {
return false;
}
var self = this;
this._saving = true;
this.trigger_up('edition_will_stopped');
return this.wysiwyg.save(false).then(function (result) {
var $wrapwrap = $('#wrapwrap');
self.editable($wrapwrap).removeClass('o_editable');
if (result.isDirty && reload !== false) {
// remove top padding because the connected bar is not visible
$('body').removeClass('o_connected_user');
return self._reload();
} else {
self.wysiwyg.destroy();
self.trigger_up('edition_was_stopped');
self.destroy();
}
return true;
}).guardedCatch(() => {
this._saving = false;
});
},
/**
* Returns the editable areas on the page.
*
* @param {DOM} $wrapwrap
* @returns {jQuery}
*/
editable: function ($wrapwrap) {
return $wrapwrap.find('[data-oe-model]')
.not('.o_not_editable')
.filter(function () {
var $parent = $(this).closest('.o_editable, .o_not_editable');
return !$parent.length || $parent.hasClass('o_editable');
})
.not('link, script')
.not('[data-oe-readonly]')
.not('img[data-oe-field="arch"], br[data-oe-field="arch"], input[data-oe-field="arch"]')
.not('.oe_snippet_editor')
.not('hr, br, input, textarea')
.add('.o_editable');
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @private
*/
_wysiwygInstance: function () {
var context;
this.trigger_up('context_get', {
callback: function (ctx) {
context = ctx;
},
});
return new WysiwygMultizone(this, {
snippets: 'website.snippets',
recordInfo: {
context: context,
data_res_model: 'website',
data_res_id: context.website_id,
}
});
},
/**
* Reloads the page in non-editable mode, with the right scrolling.
*
* @private
* @returns {Deferred} (never resolved, the page is reloading anyway)
*/
_reload: function () {
$('body').addClass('o_wait_reload');
this.wysiwyg.destroy();
this.$el.hide();
window.location.hash = 'scrollTop=' + window.document.body.scrollTop;
window.location.reload(true);
return new Promise(function () {});
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* Called when the "Discard" button is clicked -> discards the changes.
*
* @private
*/
_onCancelClick: function () {
this.cancel(true);
},
/**
* Get the cleaned value of the editable element.
*
* @private
* @param {OdooEvent} ev
*/
_onGetCleanHTML: function (ev) {
ev.data.callback(this.wysiwyg.getValue({$layout: ev.data.$layout}));
},
/**
* Snippet (menu_data) can request to save the document to leave the page
*
* @private
* @param {OdooEvent} ev
* @param {object} ev.data
* @param {function} ev.data.onSuccess
* @param {function} ev.data.onFailure
*/
_onSnippetRequestSave: function (ev) {
this.save(false).then(ev.data.onSuccess, ev.data.onFailure);
},
/**
* Called when the "Save" button is clicked -> saves the changes.
*
* @private
*/
_onSaveClick: function (ev) {
const restore = dom.addButtonLoadingEffect(ev.currentTarget);
this.save().then(restore).guardedCatch(restore);
},
/**
* @private
*/
_onUndoClick() {
$('.note-history [data-event=undo]').first().click();
},
/**
* @private
*/
_onRedoClick() {
$('.note-history [data-event=redo]').first().click();
},
});
return EditorMenu;
});
|