/
email-variable-view.js
312 lines (279 loc) · 10.1 KB
/
email-variable-view.js
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
define(function(require) {
'use strict';
const document = window.document;
const $ = require('jquery');
const _ = require('underscore');
const __ = require('orotranslation/js/translator');
const mediator = require('oroui/js/mediator');
const BaseView = require('oroui/js/app/views/base/view');
const tinyMCE = require('tinymce/tinymce');
/**
* @export oroemail/js/app/views/email-variable-view
*/
const EmailVariableView = BaseView.extend({
options: {
templateSelector: null,
sectionTemplateSelector: null,
sectionContentSelector: null,
sectionTabSelector: null,
fieldsSelectors: ['input[type="text"][name*="subject"]', 'textarea[name*="content"]'],
defaultFieldIndex: 1 // index in fieldsSelectors
},
events: {
'click a.variable': '_handleVariableClick',
'click a.reference': '_handleReferenceClick'
},
sections: {
system: 'system',
entity: 'entity'
},
/**
* @property {jQuery}
*/
lastElement: null,
/**
* @inheritDoc
*/
constructor: function EmailVariableView(options) {
EmailVariableView.__super__.constructor.call(this, options);
},
/**
* Constructor
*
* @param {Object} options
*/
initialize: function(options) {
this.options = _.defaults(options || {}, this.options);
this.template = _.template($(this.options.templateSelector).html());
this.systemTemplate = _.template(
$(this._getSectionSelector(this.options.sectionTemplateSelector, this.sections.system)).html()
);
this.entityTemplate = _.template(
$(this._getSectionSelector(this.options.sectionTemplateSelector, this.sections.entity)).html()
);
this.listenTo(this.model, 'change:entity', this._renderEntityVariables);
this.fields = $(this.options.fieldsSelectors.join(','));
this.fields.on('blur', _.bind(this._updateElementsMetaData, this));
this.lastElement = $(this.options.fieldsSelectors[this.options.defaultFieldIndex]);
},
/**
* Renders the view
*
* @returns {*}
*/
render: function() {
const vars = {system: this._getSystemVariablesHtml(this.model.getSystemVariables())};
this.$el.empty();
this.$el.html(this.template({variables: vars}));
this._applyDraggable(this._getSectionContent(this.sections.system));
this._renderEntityVariables();
return this;
},
/**
* Checks if fields has any text
*
* @returns {boolean}
*/
isEmpty: function() {
let result = true;
_.each(this.fields, function(el) {
if (el.value) {
result = false;
}
});
return result;
},
/**
* Sets empty string as a value for all fields
*/
clear: function() {
_.each(this.fields, function(el) {
const editor = tinyMCE.get(el.id);
if (editor) {
editor.setContent('');
} else if (el.value) {
el.value = '';
}
});
},
/**
* Renders section contains entity related variables
*
* @private
*/
_renderEntityVariables: function() {
const $el = this._getSectionContent(this.sections.entity);
const $tabEl = this._getSectionTab(this.sections.entity);
const entityVars = this.model.getEntityVariables();
const entityLabel = this.model.getEntityLabel();
const path = this.model.getPath();
const pathLabels = this.model.getPathLabels();
// remove old content
$el.empty();
if (_.isEmpty(entityVars) && !path) {
// hide section and switch to 'system' section if 'entity' section is active
if (this._isVisible($tabEl)) {
if ($tabEl.hasClass('active')) {
$tabEl.removeClass('active');
this._getSectionTab(this.sections.system).addClass('active');
}
$tabEl.hide();
}
if (this._isVisible($el)) {
if ($el.hasClass('active')) {
$el.removeClass('active');
this._getSectionContent(this.sections.system).addClass('active');
}
$el.hide();
}
} else {
// show 'entity' section if it is invisible
// we cannot use 'show' method here because it adds 'display: block' inline style
// and as result both 'system' and 'entity' variables are visible in the 'system' section
if (!this._isVisible($tabEl)) {
$tabEl.removeAttr('style');
}
if (!this._isVisible($el)) {
$el.removeAttr('style');
}
// add new content
$el.html(this._getEntityVariablesHtml(entityVars, entityLabel, path, pathLabels));
this._applyDraggable($el);
}
},
/**
* Checks whether the given element is visible or not
*
* @param {jQuery} $el
* @returns {boolean}
* @private
*/
_isVisible: function($el) {
// $el.is(':visible') cannot be used here because it is possible that this method
// is called when the element is temporary not visible
// for example this view is rendered when 'Loading ...' mask is not hidden yet
return $el.css('display') !== 'none';
},
/**
* @param {Array} variables
* @returns {string}
* @private
*/
_getSystemVariablesHtml: function(variables) {
return this.systemTemplate({
variables: variables,
root: this.sections.system
});
},
/**
* @param {Array} variables
* @param {string} entityLabel
* @param {string} path
* @param {Array} pathLabels
* @returns {string}
* @private
*/
_getEntityVariablesHtml: function(variables, entityLabel, path, pathLabels) {
const fields = {};
const relations = {};
_.each(variables, function(variable, varName) {
if (_.has(variable, 'related_entity_name')) {
relations[varName] = variable;
fields[varName] = variable;
} else {
fields[varName] = variable;
}
});
return this.entityTemplate({
fields: fields,
relations: relations,
entityLabel: entityLabel,
path: path,
pathLabels: pathLabels,
root: this.sections.entity
});
},
/**
* @param {jQuery} $el
* @private
*/
_applyDraggable: function($el) {
$el.find('a.variable').on('dragstart', function(e) {
const dt = e.originalEvent.dataTransfer;
for (let i = 0; i < dt.types.length; i++) {
const type = dt.types[i];
dt.clearData(type);
}
dt.setData('text', $(e.currentTarget).text());
});
},
/**
* @param {string} selectorTemplate
* @param {string} sectionName
* @returns {string}
* @private
*/
_getSectionSelector: function(selectorTemplate, sectionName) {
return selectorTemplate.replace(/\{sectionName\}/g, sectionName);
},
/**
* @param {string} sectionName
* @returns {jQuery}
* @private
*/
_getSectionTab: function(sectionName) {
return this.$el.find(this._getSectionSelector(this.options.sectionTabSelector, sectionName));
},
/**
* @param {string} sectionName
* @returns {jQuery}
* @private
*/
_getSectionContent: function(sectionName) {
return this.$el.find(this._getSectionSelector(this.options.sectionContentSelector, sectionName));
},
/**
* Handle onClick event for 'variable' links
* This method adds a variable to the last element
*
* @param {Event} e
*/
_handleVariableClick: function(e) {
let field = this.fields.filter(document.activeElement);
const variable = $(e.currentTarget).html();
e.preventDefault();
if (!field.length && this.lastElement && this.lastElement.is(':visible, [data-focusable]')) {
field = this.lastElement;
}
if (field.length) {
field.insertAtCursor(variable).focus();
mediator.trigger('email-variable-view:click-variable', field, variable);
} else {
mediator.execute('showFlashMessage', 'error',
__('oro.email.emailtemplate.cannot_insert_variable'));
}
},
/**
* Handle onClick event for 'reference' links
* This method change the current entity path in the model
*
* @param {Event} e
* @private
*/
_handleReferenceClick: function(e) {
const $el = $(e.currentTarget);
const path = $el.data('path');
this.model.setPath(path);
},
/**
* Update elements metadata
*
* @param {Event} e
* @private
*/
_updateElementsMetaData: function(e) {
this.lastElement = $(e.currentTarget);
}
});
return EmailVariableView;
});