forked from alohaeditor/Aloha-Editor
/
port-helper-attribute-field.js
507 lines (447 loc) · 13 KB
/
port-helper-attribute-field.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/**
* This is a helper module for porting plugins from the old
* ui-attributefield.js in the aloha core to the new ui-plugin.
* This interface is obsolete and must not be used for new implementations.
*/
define([
'jquery',
'ui/ui',
'ui/component',
'ui/scopes',
'ui/context',
'ui/utils',
'aloha/repositorymanager',
'aloha/selection',
'aloha/console',
'ui/vendor/jquery-ui-autocomplete-html'
], function (
$,
Ui,
Component,
Scopes,
Context,
Utils,
RepositoryManager,
Selection,
console
) {
'use strict';
// Main responsibilities implemented by the attribute-field are
//
// * setting a target object and attribute and the subsequent change of
// that target attribute (example link plugin, table plugin)
// * background color highlighting of the target object (example link plugin)
// * a placeholder in the attribute field with a grey foreground color
// (example link plugin)
// * maintain a current repository item to distinguish link plugin
// repository items from literal values typed in the attribute field
// (example link plugin)
// * repository manager markObject on the target object if a repository
// item was selected (example link plugin)
/**
* Creates a new attribute field.
*
* @param {!Object} props
* A map containing the following properties
* name -
* label - some text that will be displayed alongside
* the attribute field,
* labelClass - a class to identify the label element,
* valueField -
* displayField -
* objectTypeFilter -
* placeholder -
* noTargetHighlight -
* targetHighlightCass - a class to be identify focused element
* cls -
* width -
* scope -
* element - the <input> element to use.
* If not supplied, a new one will be created.
*/
var AttributeField = function (props) {
var valueField = props.valueField || 'id',
displayField = props.displayField || 'name',
objectTypeFilter = props.objectTypeFilter || ['all'],
placeholder = props.placeholder,
noTargetHighlight = !!props.noTargetHighlight,
targetHighlightClass = props.targetHighlightClass,
element = props.element ? $(props.element) : $('<input id="aloha-attribute-field-' + props.name + '">'),
component,
template,
resourceItem,
resourceValue,
targetObject,
targetAttribute,
lastAttributeValue,
additionalTargetObjects = [];
if (props.cls) {
element.addClass(props.cls);
}
if (props.width) {
element.width(props.width);
}
component = Ui.adopt(props.name, Component, {
scope: props.scope,
init: function () {
if (props.element) {
this.element = element;
} else {
if (props.label) {
this.element = Utils.wrapWithLabel(props.label, element);
if (props.labelClass) {
this.element.addClass(props.labelClass);
}
} else {
// Why do we have to wrap the element in a span? It
// doesn't seem to work otherwise.
this.element = $('<span>').append(element);
}
}
element.autocomplete({
'html': true,
'appendTo': Context.selector,
'source': function (req, res) {
RepositoryManager.query({
queryString: req.term,
objectTypeFilter: objectTypeFilter
}, function (data) {
res($.map(data.items, function (item) {
return {
label: parse(template, item),
value: item.name,
obj: item
};
}));
});
},
"open": props.open,
"select": onSelect
});
}
});
element
.bind("focus", onFocus)
.bind("blur", onBlur)
.bind("keydown", onKeyDown)
.bind("keyup", onKeyup)
.bind("change", onChange);
setPlaceholder();
// Because IE7 doesn't give us the blur event when the editable
// is deactivated and the toolbar disappears (other browsers do).
// TODO unbind, otherwise mermory leak
Aloha.bind('aloha-editable-deactivated', onBlur);
/**
* Update the attribute in the target element
*/
function updateTarget() {
// If this attribute field currently refers to a repository
// item, and the user edits the contents of the input field,
// this attribute field seizes to refer to the repository item.
if (resourceItem && resourceValue !== getValue()) {
resourceItem = null;
resourceValue = null;
}
// This handles attribute updates for non-repository, literal urls typed into the input field.
// Input values that refer to a repository item are handled via setItem().
if ( ! resourceItem ) {
setAttribute(targetAttribute, getValue());
}
}
function onSelect(event, ui) {
if (ui.item) {
setItem(ui.item.obj);
}
finishEditing();
}
function onBlur() {
finishEditing();
}
function onFocus(event, ui) {
if (!$(event.target).is(':visible')) {
// The check for visible fixes the bug that the background
// color of the target element is not restored.
// Rationale: it's possible for the input to receive the focus event,
// for example if it was triggered programatically, even if
// it isn't visible. Problem is, if it's not visible, then
// it will not really get focused and consequently, there
// will be no blur event either. However, we must be able to
// assume that the blur event will be fired so that we can
// clean up the background color.
return;
}
changeTargetBackground();
// Remove placeholder
if (getValue() === placeholder) {
setValue('');
}
}
function onKeyDown(event) {
// on ENTER or ESC leave the editing
if (event.keyCode == 13 || event.keyCode == 27) {
event.preventDefault();
}
}
function onKeyup(event) {
updateTarget();
if ( ( event.keyCode == 13 || event.keyCode == 27 ) ) {
// Set focus to link element and select the object
Selection.getRangeObject().select();
finishEditing();
}
}
function onChange(event) {
updateTarget();
}
function finishEditing() {
restoreTargetBackground();
if (!targetObject || lastAttributeValue === $(targetObject).attr(targetAttribute)) {
return;
}
// when no resource item was selected, remove any marking of the target object
if (!resourceItem) {
RepositoryManager.markObject( targetObject );
}
if (getValue() === '') {
setPlaceholder();
}
}
/**
* Execute a function for every targets this attribute
*
* fields is pointing to.
* @param {Function} fn Function to execute for each target
*/
function executeForTargets(fn) {
var target = $(targetObject);
fn(target);
for (var i = 0, len = additionalTargetObjects.length; i < len ; i++) {
fn($(additionalTargetObjects[i]));
}
}
/**
* Change target background so the targets are
* highlighted
*/
function changeTargetBackground() {
var target = $(targetObject);
if (targetHighlightClass) {
executeForTargets(function (target) {
target.addClass(targetHighlightClass);
});
}
if (noTargetHighlight) {
return;
}
// Make sure that multiple invokations of
// changeTargetBackground don't set an incorrect
// data-original-background-color.
restoreTargetBackground();
// set background color to give visual feedback which link is modified
if (target.context && target.context.style &&
target.context.style['background-color']) {
executeForTargets(function (target) {
target.attr('data-original-background-color',
target.context.style['background-color']);
});
}
executeForTargets(function (target) {
target.css('background-color', '#80B5F2');
});
}
function restoreTargetBackground() {
var target = $(targetObject);
if (targetHighlightClass) {
executeForTargets(function (target) {
target.removeClass(targetHighlightClass);
});
}
if (noTargetHighlight) {
return;
}
// Remove the highlighting and restore original color if was set before
var color = target.attr('data-original-background-color');
executeForTargets(function (target) {
target.css('background-color', color || '');
});
if (!target.attr('style')) {
executeForTargets(function (target) {
target.removeAttr('style');
});
}
executeForTargets(function (target) {
target.removeAttr('data-original-background-color');
});
}
function parse(template, item) {
return template.replace(/\{([^}]+)\}/g, function (_, name) {
return name in item ? item[name] : "";
});
}
function setPlaceholder() {
if (null === placeholder) {
return;
}
element.css('color', '#AAA');
element.val(placeholder);
}
function setTemplate(tmpl) {
template = tmpl;
}
/**
* When at least on objectType is set the value in the Attribute field does a query to all registered repositories.
* @param {Array} objectTypeFilter The array of objectTypeFilter to be searched for.
* @void
*/
function setObjectTypeFilter(objTypeFilter) {
objectTypeFilter = objTypeFilter;
}
/**
* Adding a listener to the field
* @param {String} eventname The name of the event. Ex. 'keyup'
* @param {function} handler The function that should be called when the event happens.
*/
function addListener(eventName, handler) {
element.bind(eventName, $.proxy(handler, attrField));
}
function getValue() {
return element.val();
}
function setValue(value) {
element.val(value);
element.css('color', 'black');
}
function setItem(item) {
resourceItem = item;
if (item) {
// TODO split display field by '.' and get corresponding attribute, because it could be a properties attribute.
var v = item[displayField];
// set the value into the field
setValue(v);
// store the value to be the "reference" value for the currently selected resource item
resourceValue = v;
setAttribute(targetAttribute, item[valueField]);
RepositoryManager.markObject(targetObject, item);
element.trigger('item-change');
} else {
resourceValue = null;
}
}
function getItem() {
return resourceItem;
}
/**
* Sets an attribute optionally based on a regex on reference
* @param {String} attr The Attribute name which should be set. Ex. "lang"
* @param {String} value The value to set. Ex. "de-AT"
* @param {String} regex The regex when the attribute should be set. The regex is applied to the value of refernece.
* @param {String} reference The value for the regex.
*/
function setAttribute(attr, value, regex, reference) {
if (targetObject) {
// check if a reference value is submitted to check against with a regex
if (typeof reference !== 'undefined' && !reference.match(new RegExp(regex))) {
$(targetObject).removeAttr(attr);
} else {
executeForTargets(function (target) {
target.attr(attr, value);
});
}
}
}
/**
* Sets the target Object of which the Attribute should be modified
* @param {jQuery} obj the target object
* @param {String} attr Attribute to be modified ex. "href" of a link
* @void
*/
function setTargetObject(obj, attr) {
targetObject = obj;
targetAttribute = attr;
additionalTargetObjects = [];
setItem(null);
if (obj && attr) {
lastAttributeValue = $(obj).attr(attr);
setValue($(targetObject).attr(targetAttribute));
} else {
setValue('');
return;
}
// check whether a repository item is linked to the object
RepositoryManager.getObject( obj, function ( items ) {
if (items && items.length > 0) {
setItem(items[0]);
}
} );
}
function addAdditionalTargetObject(targetObj) {
additionalTargetObjects.push(targetObj);
}
function getTargetObject() {
return targetObject;
}
function focus() {
component.focus();
element.focus();
}
function foreground() {
component.foreground();
}
function show() {
element.show();
}
function hide() {
element.hide();
}
/**
* Disables input text, so the text can not be edit.
*/
function disableInput() {
element.attr('disabled','disabled');
}
/**
* Enables input text, so the text can be edit.
*/
function enableInput() {
element.removeAttr('disabled');
}
function getInputId(){
return element.attr("id");
}
function hasInputElem() {
return true;
}
function getInputElem() {
return element[0];
}
function getInputJQuery() {
return element;
}
var attrField = {
getInputElem: getInputElem,
hasInputElem: hasInputElem,
getInputId: getInputId,
hide: hide,
show: show,
foreground: foreground,
focus: focus,
getTargetObject: getTargetObject,
setTargetObject: setTargetObject,
addAdditionalTargetObject: addAdditionalTargetObject,
setAttribute: setAttribute,
getItem: getItem,
setItem: setItem,
setValue: setValue,
getValue: getValue,
addListener: addListener,
setObjectTypeFilter: setObjectTypeFilter,
setTemplate: setTemplate,
setPlaceholder: setPlaceholder,
getInputJQuery: getInputJQuery,
enableInput: enableInput,
disableInput: disableInput
};
return attrField;
};
return AttributeField;
});