biso24-editor 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/CKEditor4/.eslintrc.json +4 -0
  2. package/CKEditor4/CHANGES.md +2182 -0
  3. package/CKEditor4/LICENSE.md +1436 -0
  4. package/CKEditor4/README.md +39 -0
  5. package/CKEditor4/SECURITY.md +10 -0
  6. package/CKEditor4/adapters/jquery.js +158 -0
  7. package/CKEditor4/bender-runner.config.json +16 -0
  8. package/CKEditor4/build-config.js +100 -0
  9. package/CKEditor4/ckeditor.js +30065 -0
  10. package/CKEditor4/config.js +39 -0
  11. package/CKEditor4/contents.css +161 -0
  12. package/CKEditor4/lang/vi.js +475 -0
  13. package/CKEditor4/plugins/a11yhelp/dialogs/a11yhelp.js +143 -0
  14. package/CKEditor4/plugins/a11yhelp/dialogs/lang/vi.js +122 -0
  15. package/CKEditor4/plugins/ajax/plugin.js +241 -0
  16. package/CKEditor4/plugins/autocomplete/plugin.js +1655 -0
  17. package/CKEditor4/plugins/autocomplete/skins/default.css +38 -0
  18. package/CKEditor4/plugins/custompaste/plugin.js +57 -0
  19. package/CKEditor4/plugins/dialog/dialogDefinition.js +4 -0
  20. package/CKEditor4/plugins/dialog/styles/dialog.css +18 -0
  21. package/CKEditor4/plugins/font/lang/vi.js +14 -0
  22. package/CKEditor4/plugins/font/plugin.js +535 -0
  23. package/CKEditor4/plugins/icons.png +0 -0
  24. package/CKEditor4/plugins/icons_hidpi.png +0 -0
  25. package/CKEditor4/plugins/image2/dialogs/image2.js +558 -0
  26. package/CKEditor4/plugins/image2/icons/hidpi/image.png +0 -0
  27. package/CKEditor4/plugins/image2/icons/image.png +0 -0
  28. package/CKEditor4/plugins/image2/lang/vi.js +21 -0
  29. package/CKEditor4/plugins/image2/plugin.js +1708 -0
  30. package/CKEditor4/plugins/justify/icons/hidpi/justifyblock.png +0 -0
  31. package/CKEditor4/plugins/justify/icons/hidpi/justifycenter.png +0 -0
  32. package/CKEditor4/plugins/justify/icons/hidpi/justifyleft.png +0 -0
  33. package/CKEditor4/plugins/justify/icons/hidpi/justifyright.png +0 -0
  34. package/CKEditor4/plugins/justify/icons/justifyblock.png +0 -0
  35. package/CKEditor4/plugins/justify/icons/justifycenter.png +0 -0
  36. package/CKEditor4/plugins/justify/icons/justifyleft.png +0 -0
  37. package/CKEditor4/plugins/justify/icons/justifyright.png +0 -0
  38. package/CKEditor4/plugins/justify/plugin.js +266 -0
  39. package/CKEditor4/plugins/justify_group/icons/hidpi/justifycenter.png +0 -0
  40. package/CKEditor4/plugins/justify_group/icons/justifycenter.png +0 -0
  41. package/CKEditor4/plugins/justify_group/plugin.js +64 -0
  42. package/CKEditor4/plugins/lineheight/LICENSE +22 -0
  43. package/CKEditor4/plugins/lineheight/README.md +2 -0
  44. package/CKEditor4/plugins/lineheight/lang/vi.js +3 -0
  45. package/CKEditor4/plugins/lineheight/plugin.js +99 -0
  46. package/CKEditor4/plugins/lineheight/readme.txt +30 -0
  47. package/CKEditor4/plugins/link/dialogs/anchor.js +82 -0
  48. package/CKEditor4/plugins/link/dialogs/link.js +777 -0
  49. package/CKEditor4/plugins/link/images/anchor.png +0 -0
  50. package/CKEditor4/plugins/link/images/hidpi/anchor.png +0 -0
  51. package/CKEditor4/plugins/magicline/images/hidpi/icon-rtl.png +0 -0
  52. package/CKEditor4/plugins/magicline/images/hidpi/icon.png +0 -0
  53. package/CKEditor4/plugins/magicline/images/icon-rtl.png +0 -0
  54. package/CKEditor4/plugins/magicline/images/icon.png +0 -0
  55. package/CKEditor4/plugins/mentions/plugin.js +471 -0
  56. package/CKEditor4/plugins/menubutton/plugin.js +99 -0
  57. package/CKEditor4/plugins/pastefromword/filter/default.js +849 -0
  58. package/CKEditor4/plugins/pastetools/filter/common.js +445 -0
  59. package/CKEditor4/plugins/pastetools/filter/image.js +163 -0
  60. package/CKEditor4/plugins/pdffile/icons/exporttopdf.png +0 -0
  61. package/CKEditor4/plugins/pdffile/icons/hidpi/exporttopdf.png +0 -0
  62. package/CKEditor4/plugins/pdffile/plugin.js +36 -0
  63. package/CKEditor4/plugins/placeholder_button/plugin.js +31 -0
  64. package/CKEditor4/plugins/sourcedialog/dialogs/sourcedialog.js +88 -0
  65. package/CKEditor4/plugins/sourcedialog/icons/hidpi/sourcedialog-rtl.png +0 -0
  66. package/CKEditor4/plugins/sourcedialog/icons/hidpi/sourcedialog.png +0 -0
  67. package/CKEditor4/plugins/sourcedialog/icons/sourcedialog-rtl.png +0 -0
  68. package/CKEditor4/plugins/sourcedialog/icons/sourcedialog.png +0 -0
  69. package/CKEditor4/plugins/sourcedialog/lang/vi.js +9 -0
  70. package/CKEditor4/plugins/sourcedialog/plugin.js +30 -0
  71. package/CKEditor4/plugins/textmatch/plugin.js +331 -0
  72. package/CKEditor4/plugins/textwatcher/plugin.js +295 -0
  73. package/CKEditor4/plugins/widget/images/handle.png +0 -0
  74. package/CKEditor4/plugins/wordfile/icons/exporttoword.png +0 -0
  75. package/CKEditor4/plugins/wordfile/icons/hidpi/exporttoword.png +0 -0
  76. package/CKEditor4/plugins/wordfile/icons/hidpi/importfromword.png +0 -0
  77. package/CKEditor4/plugins/wordfile/icons/importfromword.png +0 -0
  78. package/CKEditor4/plugins/wordfile/plugin.js +161 -0
  79. package/CKEditor4/skins/moono-lisa/dialog.css +685 -0
  80. package/CKEditor4/skins/moono-lisa/dialog_ie.css +721 -0
  81. package/CKEditor4/skins/moono-lisa/dialog_ie8.css +746 -0
  82. package/CKEditor4/skins/moono-lisa/dialog_iequirks.css +724 -0
  83. package/CKEditor4/skins/moono-lisa/editor.css +1551 -0
  84. package/CKEditor4/skins/moono-lisa/editor_gecko.css +1556 -0
  85. package/CKEditor4/skins/moono-lisa/editor_ie.css +1588 -0
  86. package/CKEditor4/skins/moono-lisa/editor_ie8.css +1639 -0
  87. package/CKEditor4/skins/moono-lisa/editor_iequirks.css +1632 -0
  88. package/CKEditor4/skins/moono-lisa/icons.png +0 -0
  89. package/CKEditor4/skins/moono-lisa/icons_hidpi.png +0 -0
  90. package/CKEditor4/skins/moono-lisa/images/arrow.png +0 -0
  91. package/CKEditor4/skins/moono-lisa/images/close.png +0 -0
  92. package/CKEditor4/skins/moono-lisa/images/hidpi/close.png +0 -0
  93. package/CKEditor4/skins/moono-lisa/images/hidpi/lock-open.png +0 -0
  94. package/CKEditor4/skins/moono-lisa/images/hidpi/lock.png +0 -0
  95. package/CKEditor4/skins/moono-lisa/images/hidpi/refresh.png +0 -0
  96. package/CKEditor4/skins/moono-lisa/images/lock-open.png +0 -0
  97. package/CKEditor4/skins/moono-lisa/images/lock.png +0 -0
  98. package/CKEditor4/skins/moono-lisa/images/refresh.png +0 -0
  99. package/CKEditor4/skins/moono-lisa/images/spinner.gif +0 -0
  100. package/CKEditor4/skins/moono-lisa/readme.md +46 -0
  101. package/CKEditor4/styles.js +136 -0
  102. package/CKEditor4/vendor/promise.js +385 -0
  103. package/README.md +3 -0
  104. package/package.json +9 -0
@@ -0,0 +1,1655 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ 'use strict';
7
+
8
+ ( function() {
9
+
10
+ CKEDITOR.plugins.add( 'autocomplete', {
11
+ requires: 'textwatcher',
12
+ onLoad: function() {
13
+ CKEDITOR.document.appendStyleSheet( this.path + 'skins/default.css' );
14
+ },
15
+ isSupportedEnvironment: function() {
16
+ return !CKEDITOR.env.ie || CKEDITOR.env.version > 8;
17
+ }
18
+ } );
19
+
20
+ /**
21
+ * The main class implementing a generic [Autocomplete](https://ckeditor.com/cke4/addon/autocomplete) feature in the editor.
22
+ * It acts as a controller that works with the {@link CKEDITOR.plugins.autocomplete.model model} and
23
+ * {@link CKEDITOR.plugins.autocomplete.view view} classes.
24
+ *
25
+ * It is possible to maintain multiple autocomplete instances for a single editor at a time.
26
+ * In order to create an autocomplete instance use its {@link #constructor constructor}.
27
+ *
28
+ * @class CKEDITOR.plugins.autocomplete
29
+ * @since 4.10.0
30
+ * @constructor Creates a new instance of autocomplete and attaches it to the editor.
31
+ *
32
+ * In order to initialize the autocomplete feature it is enough to instantiate this class with
33
+ * two required callbacks:
34
+ *
35
+ * * {@link CKEDITOR.plugins.autocomplete.configDefinition#textTestCallback config.textTestCallback} – A function being called by
36
+ * the {@link CKEDITOR.plugins.textWatcher text watcher} plugin, as new text is being inserted.
37
+ * Its purpose is to determine whether a given range should be matched or not.
38
+ * See {@link CKEDITOR.plugins.textWatcher#constructor} for more details.
39
+ * There is also {@link CKEDITOR.plugins.textMatch#match} which is a handy helper for that purpose.
40
+ * * {@link CKEDITOR.plugins.autocomplete.configDefinition#dataCallback config.dataCallback} – A function that should return
41
+ * (through its callback) suggestion data for the current query string.
42
+ *
43
+ * # Creating an autocomplete instance
44
+ *
45
+ * Depending on your use case, put this code in the {@link CKEDITOR.pluginDefinition#init} callback of your
46
+ * plugin or, for example, in the {@link CKEDITOR.editor#instanceReady} event listener. Ensure that you loaded the
47
+ * {@link CKEDITOR.plugins.textMatch Text Match} plugin.
48
+ *
49
+ * ```javascript
50
+ * var itemsArray = [ { id: 1, name: '@Andrew' }, { id: 2, name: '@Kate' } ];
51
+ *
52
+ * // Called when the user types in the editor or moves the caret.
53
+ * // The range represents the caret position.
54
+ * function textTestCallback( range ) {
55
+ * // You do not want to autocomplete a non-empty selection.
56
+ * if ( !range.collapsed ) {
57
+ * return null;
58
+ * }
59
+ *
60
+ * // Use the text match plugin which does the tricky job of doing
61
+ * // a text search in the DOM. The matchCallback function should return
62
+ * // a matching fragment of the text.
63
+ * return CKEDITOR.plugins.textMatch.match( range, matchCallback );
64
+ * }
65
+ *
66
+ * // Returns the position of the matching text.
67
+ * // It matches with a word starting from the '@' character
68
+ * // up to the caret position.
69
+ * function matchCallback( text, offset ) {
70
+ * // Get the text before the caret.
71
+ * var left = text.slice( 0, offset ),
72
+ * // Will look for an '@' character followed by word characters.
73
+ * match = left.match( /@\w*$/ );
74
+ *
75
+ * if ( !match ) {
76
+ * return null;
77
+ * }
78
+ *
79
+ * return { start: match.index, end: offset };
80
+ * }
81
+ *
82
+ * // Returns (through its callback) the suggestions for the current query.
83
+ * function dataCallback( matchInfo, callback ) {
84
+ * // Simple search.
85
+ * // Filter the entire items array so only the items that start
86
+ * // with the query remain.
87
+ * var suggestions = itemsArray.filter( function( item ) {
88
+ * return item.name.toLowerCase().indexOf( matchInfo.query.toLowerCase() ) == 0;
89
+ * } );
90
+ *
91
+ * // Note: The callback function can also be executed asynchronously
92
+ * // so dataCallback can do XHR requests or use any other asynchronous API.
93
+ * callback( suggestions );
94
+ * }
95
+ *
96
+ * // Finally, instantiate the autocomplete class.
97
+ * new CKEDITOR.plugins.autocomplete( editor, {
98
+ * textTestCallback: textTestCallback,
99
+ * dataCallback: dataCallback
100
+ * } );
101
+ * ```
102
+ *
103
+ * # Changing the behavior of the autocomplete class by subclassing it
104
+ *
105
+ * This plugin will expose a `CKEDITOR.plugins.customAutocomplete` class which uses
106
+ * a custom view that positions the panel relative to the {@link CKEDITOR.editor#container}.
107
+ *
108
+ * ```javascript
109
+ * CKEDITOR.plugins.add( 'customautocomplete', {
110
+ * requires: 'autocomplete',
111
+ *
112
+ * onLoad: function() {
113
+ * var View = CKEDITOR.plugins.autocomplete.view,
114
+ * Autocomplete = CKEDITOR.plugins.autocomplete;
115
+ *
116
+ * function CustomView( editor ) {
117
+ * // Call the parent class constructor.
118
+ * View.call( this, editor );
119
+ * }
120
+ * // Inherit the view methods.
121
+ * CustomView.prototype = CKEDITOR.tools.prototypedCopy( View.prototype );
122
+ *
123
+ * // Change the positioning of the panel, so it is stretched
124
+ * // to 100% of the editor container width and is positioned
125
+ * // relative to the editor container.
126
+ * CustomView.prototype.updatePosition = function( range ) {
127
+ * var caretRect = this.getViewPosition( range ),
128
+ * container = this.editor.container;
129
+ *
130
+ * this.setPosition( {
131
+ * // Position the panel relative to the editor container.
132
+ * left: container.$.offsetLeft,
133
+ * top: caretRect.top,
134
+ * bottom: caretRect.bottom
135
+ * } );
136
+ * // Stretch the panel to 100% of the editor container width.
137
+ * this.element.setStyle( 'width', container.getSize( 'width' ) + 'px' );
138
+ * };
139
+ *
140
+ * function CustomAutocomplete( editor, configDefinition ) {
141
+ * // Call the parent class constructor.
142
+ * Autocomplete.call( this, editor, configDefinition );
143
+ * }
144
+ * // Inherit the autocomplete methods.
145
+ * CustomAutocomplete.prototype = CKEDITOR.tools.prototypedCopy( Autocomplete.prototype );
146
+ *
147
+ * CustomAutocomplete.prototype.getView = function() {
148
+ * return new CustomView( this.editor );
149
+ * }
150
+ *
151
+ * // Expose the custom autocomplete so it can be used later.
152
+ * CKEDITOR.plugins.customAutocomplete = CustomAutocomplete;
153
+ * }
154
+ * } );
155
+ * ```
156
+ * @param {CKEDITOR.editor} editor The editor to watch.
157
+ * @param {CKEDITOR.plugins.autocomplete.configDefinition} config Configuration object for this autocomplete instance.
158
+ */
159
+ function Autocomplete( editor, config ) {
160
+ var configKeystrokes = editor.config.autocomplete_commitKeystrokes || CKEDITOR.config.autocomplete_commitKeystrokes;
161
+
162
+ /**
163
+ * The editor instance that autocomplete is attached to.
164
+ *
165
+ * @readonly
166
+ * @property {CKEDITOR.editor}
167
+ */
168
+ this.editor = editor;
169
+
170
+ /**
171
+ * Indicates throttle threshold expressed in milliseconds, reducing text checks frequency.
172
+ *
173
+ * @property {Number} [throttle=20]
174
+ */
175
+ this.throttle = config.throttle !== undefined ? config.throttle : 20;
176
+
177
+ /**
178
+ * The autocomplete view instance.
179
+ *
180
+ * @readonly
181
+ * @property {CKEDITOR.plugins.autocomplete.view}
182
+ */
183
+ this.view = this.getView();
184
+
185
+ /**
186
+ * The autocomplete model instance.
187
+ *
188
+ * @readonly
189
+ * @property {CKEDITOR.plugins.autocomplete.model}
190
+ */
191
+ this.model = this.getModel( config.dataCallback );
192
+ this.model.itemsLimit = config.itemsLimit;
193
+
194
+ /**
195
+ * The autocomplete text watcher instance.
196
+ *
197
+ * @readonly
198
+ * @property {CKEDITOR.plugins.textWatcher}
199
+ */
200
+ this.textWatcher = this.getTextWatcher( config.textTestCallback );
201
+
202
+ /**
203
+ * The autocomplete keystrokes used to finish autocompletion with the selected view item.
204
+ * The property is using the {@link CKEDITOR.config#autocomplete_commitKeystrokes} configuration option as default keystrokes.
205
+ * You can change this property to set individual keystrokes for the plugin instance.
206
+ *
207
+ * @property {Number[]}
208
+ * @readonly
209
+ */
210
+ this.commitKeystrokes = CKEDITOR.tools.array.isArray( configKeystrokes ) ? configKeystrokes.slice() : [ configKeystrokes ];
211
+
212
+ /**
213
+ * Listeners registered by this autocomplete instance.
214
+ *
215
+ * @private
216
+ */
217
+ this._listeners = [];
218
+
219
+ /**
220
+ * Template of markup to be inserted as the autocomplete item gets committed.
221
+ *
222
+ * You can use {@link CKEDITOR.plugins.autocomplete.model.item item} properties to customize the template.
223
+ *
224
+ * ```javascript
225
+ * var outputTemplate = `<a href="/tracker/{ticket}">#{ticket} ({name})</a>`;
226
+ * ```
227
+ *
228
+ * @readonly
229
+ * @property {CKEDITOR.template} [outputTemplate=null]
230
+ */
231
+ this.outputTemplate = config.outputTemplate !== undefined ? new CKEDITOR.template( config.outputTemplate ) : null;
232
+
233
+ if ( config.itemTemplate ) {
234
+ this.view.itemTemplate = new CKEDITOR.template( config.itemTemplate );
235
+ }
236
+
237
+ // Attach autocomplete when editor instance is ready (#2114).
238
+ if ( this.editor.status === 'ready' ) {
239
+ this.attach();
240
+ } else {
241
+ this.editor.on( 'instanceReady', function() {
242
+ this.attach();
243
+ }, this );
244
+ }
245
+
246
+ editor.on( 'destroy', function() {
247
+ this.destroy();
248
+ }, this );
249
+ }
250
+
251
+ Autocomplete.prototype = {
252
+ /**
253
+ * Attaches the autocomplete to the {@link #editor}.
254
+ *
255
+ * * The view is appended to the DOM and the listeners are attached.
256
+ * * The {@link #textWatcher text watcher} is attached to the editor.
257
+ * * The listeners on the {@link #model} and {@link #view} events are added.
258
+ */
259
+ attach: function() {
260
+ var editor = this.editor,
261
+ win = CKEDITOR.document.getWindow(),
262
+ editable = editor.editable(),
263
+ editorScrollableElement = editable.isInline() ? editable : editable.getDocument();
264
+
265
+ // iOS classic editor listens on frame parent element for editor `scroll` event (#1910).
266
+ if ( CKEDITOR.env.iOS && !editable.isInline() ) {
267
+ editorScrollableElement = iOSViewportElement( editor );
268
+ }
269
+
270
+ this.view.append();
271
+ this.view.attach();
272
+ this.textWatcher.attach();
273
+
274
+ this._listeners.push( this.textWatcher.on( 'matched', this.onTextMatched, this ) );
275
+ this._listeners.push( this.textWatcher.on( 'unmatched', this.onTextUnmatched, this ) );
276
+ this._listeners.push( this.model.on( 'change-data', this.modelChangeListener, this ) );
277
+ this._listeners.push( this.model.on( 'change-selectedItemId', this.onSelectedItemId, this ) );
278
+ this._listeners.push( this.view.on( 'change-selectedItemId', this.onSelectedItemId, this ) );
279
+ this._listeners.push( this.view.on( 'click-item', this.onItemClick, this ) );
280
+
281
+ // (#4617)
282
+ this._listeners.push( this.model.on( 'change-isActive', this.updateAriaAttributesOnEditable, this ) );
283
+
284
+ // Update view position on viewport change.
285
+ // Note: CKEditor's event system has a limitation that one function
286
+ // cannot be used as listener for the same event more than once. Hence, wrapper functions.
287
+ this._listeners.push( win.on( 'scroll', function() {
288
+ this.viewRepositionListener();
289
+ }, this ) );
290
+ this._listeners.push( editorScrollableElement.on( 'scroll', function() {
291
+ this.viewRepositionListener();
292
+ }, this ) );
293
+
294
+ // Don't let browser to focus dropdown element (#2107).
295
+ this._listeners.push( this.view.element.on( 'mousedown', function( e ) {
296
+ e.data.preventDefault();
297
+ }, null, null, 9999 ) );
298
+
299
+ // Register keybindings and add ARIA attributes to the editable right away
300
+ // if editor is already initialized.
301
+ if ( editable ) {
302
+ this.registerPanelNavigation();
303
+ this.addAriaAttributesToEditable();
304
+ }
305
+
306
+ // Note: CKEditor's event system has a limitation that one function
307
+ // cannot be used as listener for the same event more than once. Hence, wrapper function.
308
+ // (#4107)
309
+ editor.on( 'contentDom', function() {
310
+ this.registerPanelNavigation();
311
+ this.addAriaAttributesToEditable();
312
+ }, this );
313
+ },
314
+
315
+ registerPanelNavigation: function() {
316
+ var editable = this.editor.editable();
317
+
318
+ // Priority 5 to get before the enterkey.
319
+ // Note: CKEditor's event system has a limitation that one function (in this case this.onKeyDown)
320
+ // cannot be used as listener for the same event more than once. Hence, wrapper function.
321
+ this._listeners.push( editable.attachListener( editable, 'keydown', function( evt ) {
322
+ this.onKeyDown( evt );
323
+ }, this, null, 5 ) );
324
+ },
325
+
326
+ /**
327
+ * @since 4.16.1
328
+ */
329
+ addAriaAttributesToEditable: function() {
330
+ var editable = this.editor.editable(),
331
+ autocompleteId = this.view.element.getAttribute( 'id' );
332
+
333
+ if ( !editable.isInline() ) {
334
+ return;
335
+ }
336
+
337
+ editable.setAttribute( 'aria-controls', autocompleteId );
338
+ editable.setAttribute( 'aria-activedescendant', '' );
339
+ editable.setAttribute( 'aria-autocomplete', 'list' );
340
+ editable.setAttribute( 'aria-expanded', 'false' );
341
+ },
342
+
343
+ /**
344
+ * @since 4.16.1
345
+ */
346
+ updateAriaAttributesOnEditable: function( evt ) {
347
+ var editable = this.editor.editable(),
348
+ isActive = evt.data;
349
+
350
+ if ( !editable || !editable.isInline() ) {
351
+ return;
352
+ }
353
+
354
+ editable.setAttribute( 'aria-expanded', isActive ? 'true' : 'false' );
355
+
356
+ if ( !isActive ) {
357
+ editable.setAttribute( 'aria-activedescendant', '' );
358
+ }
359
+ },
360
+
361
+ /**
362
+ * @since 4.16.1
363
+ */
364
+ updateAriaActiveDescendantAttributeOnEditable: function( id ) {
365
+ var editable = this.editor.editable();
366
+
367
+ if ( !editable.isInline() ) {
368
+ return;
369
+ }
370
+
371
+ editable.setAttribute( 'aria-activedescendant', id );
372
+ },
373
+
374
+ /**
375
+ * @since 4.16.1
376
+ */
377
+ removeAriaAttributesFromEditable: function() {
378
+ var editable = this.editor.editable();
379
+
380
+ if ( !editable || !editable.isInline() ) {
381
+ return;
382
+ }
383
+
384
+ editable.removeAttributes( [
385
+ 'aria-controls',
386
+ 'aria-expanded',
387
+ 'aria-activedescendant'
388
+ ] );
389
+
390
+ editable.setAttribute( 'aria-autocomplete', 'none' );
391
+ },
392
+
393
+ /**
394
+ * Closes the view and sets its {@link CKEDITOR.plugins.autocomplete.model#isActive state} to inactive.
395
+ */
396
+ close: function() {
397
+ this.model.setActive( false );
398
+ this.view.close();
399
+ },
400
+
401
+ /**
402
+ * Commits the currently chosen or given item. HTML is generated for this item using the
403
+ * {@link #getHtmlToInsert} method and then it is inserted into the editor. The item is inserted
404
+ * into the {@link CKEDITOR.plugins.autocomplete.model#range query's range}, so the query text is
405
+ * replaced by the inserted HTML.
406
+ *
407
+ * @param {Number/String} [itemId] If given, then the specified item will be inserted into the editor
408
+ * instead of the currently chosen one.
409
+ */
410
+ commit: function( itemId ) {
411
+ if ( !this.model.isActive ) {
412
+ return;
413
+ }
414
+
415
+ this.close();
416
+
417
+ if ( itemId == null ) {
418
+ itemId = this.model.selectedItemId;
419
+
420
+ // If non item is selected abort commit.
421
+ if ( itemId == null ) {
422
+ return;
423
+ }
424
+ }
425
+
426
+ var item = this.model.getItemById( itemId ),
427
+ editor = this.editor;
428
+
429
+ editor.fire( 'saveSnapshot' );
430
+ editor.getSelection().selectRanges( [ this.model.range ] );
431
+ editor.insertHtml( this.getHtmlToInsert( item ), 'text' );
432
+ editor.fire( 'saveSnapshot' );
433
+ },
434
+
435
+ /**
436
+ * Destroys the autocomplete instance.
437
+ * View element and event listeners will be removed from the DOM.
438
+ */
439
+ destroy: function() {
440
+ CKEDITOR.tools.array.forEach( this._listeners, function( obj ) {
441
+ obj.removeListener();
442
+ } );
443
+
444
+ this._listeners = [];
445
+
446
+ this.view.element && this.view.element.remove();
447
+ this.removeAriaAttributesFromEditable();
448
+ },
449
+
450
+ /**
451
+ * Returns HTML that should be inserted into the editor when the item is committed.
452
+ *
453
+ * See also the {@link #commit} method.
454
+ *
455
+ * @param {CKEDITOR.plugins.autocomplete.model.item} item
456
+ * @returns {String} The HTML to insert.
457
+ */
458
+ getHtmlToInsert: function( item ) {
459
+ var encodedItem = encodeItem( item );
460
+ return this.outputTemplate ? this.outputTemplate.output( encodedItem ) : encodedItem.name;
461
+ },
462
+
463
+ /**
464
+ * Creates and returns the model instance. This method is used when
465
+ * initializing the autocomplete and can be overwritten in order to
466
+ * return an instance of a different class than the default model.
467
+ *
468
+ * @param {Function} dataCallback See {@link CKEDITOR.plugins.autocomplete.configDefinition#dataCallback configDefinition.dataCallback}.
469
+ * @returns {CKEDITOR.plugins.autocomplete.model} The model instance.
470
+ */
471
+ getModel: function( dataCallback ) {
472
+ var that = this;
473
+
474
+ return new Model( function( matchInfo, callback ) {
475
+ return dataCallback.call( this, CKEDITOR.tools.extend( {
476
+ // Make sure autocomplete instance is available in the callback (#2108).
477
+ autocomplete: that
478
+ }, matchInfo ), callback );
479
+ } );
480
+ },
481
+
482
+ /**
483
+ * Creates and returns the text watcher instance. This method is used while
484
+ * initializing the autocomplete and can be overwritten in order to
485
+ * return an instance of a different class than the default text watcher.
486
+ *
487
+ * @param {Function} textTestCallback See the {@link CKEDITOR.plugins.autocomplete} arguments.
488
+ * @returns {CKEDITOR.plugins.textWatcher} The text watcher instance.
489
+ */
490
+ getTextWatcher: function( textTestCallback ) {
491
+ return new CKEDITOR.plugins.textWatcher( this.editor, textTestCallback, this.throttle );
492
+ },
493
+
494
+ /**
495
+ * Creates and returns the view instance. This method is used while
496
+ * initializing the autocomplete and can be overwritten in order to
497
+ * return an instance of a different class than the default view.
498
+ *
499
+ * @returns {CKEDITOR.plugins.autocomplete.view} The view instance.
500
+ */
501
+ getView: function() {
502
+ return new View( this.editor );
503
+ },
504
+
505
+ /**
506
+ * Opens the panel if {@link CKEDITOR.plugins.autocomplete.model#hasData there is any data available}.
507
+ */
508
+ open: function() {
509
+ if ( this.model.hasData() ) {
510
+ this.model.setActive( true );
511
+ this.view.open();
512
+ this.model.selectFirst();
513
+ this.view.updatePosition( this.model.range );
514
+ }
515
+ },
516
+
517
+ // LISTENERS ------------------
518
+
519
+ /**
520
+ * The function that should be called when the view has to be repositioned, e.g on scroll.
521
+ *
522
+ * @private
523
+ */
524
+ viewRepositionListener: function() {
525
+ if ( this.model.isActive ) {
526
+ this.view.updatePosition( this.model.range );
527
+ }
528
+ },
529
+
530
+ /**
531
+ * The function that should be called once the model data has changed.
532
+ *
533
+ * @param {CKEDITOR.eventInfo} evt
534
+ * @private
535
+ */
536
+ modelChangeListener: function( evt ) {
537
+ if ( this.model.hasData() ) {
538
+ this.view.updateItems( evt.data );
539
+ this.open();
540
+ } else {
541
+ this.close();
542
+ }
543
+ },
544
+
545
+ /**
546
+ * The function that should be called once a view item was clicked.
547
+ *
548
+ * @param {CKEDITOR.eventInfo} evt
549
+ * @private
550
+ */
551
+ onItemClick: function( evt ) {
552
+ this.commit( evt.data );
553
+ },
554
+
555
+ /**
556
+ * The function that should be called on every `keydown` event occurred within the {@link CKEDITOR.editable editable} element.
557
+ *
558
+ * @param {CKEDITOR.dom.event} evt
559
+ * @private
560
+ */
561
+ onKeyDown: function( evt ) {
562
+ if ( !this.model.isActive ) {
563
+ return;
564
+ }
565
+
566
+ var keyCode = evt.data.getKey(),
567
+ handled = false;
568
+
569
+ // Esc key.
570
+ if ( keyCode == 27 ) {
571
+ this.close();
572
+ this.textWatcher.unmatch();
573
+ handled = true;
574
+ // Down Arrow.
575
+ } else if ( keyCode == 40 ) {
576
+ this.model.selectNext();
577
+ handled = true;
578
+ // Up Arrow.
579
+ } else if ( keyCode == 38 ) {
580
+ this.model.selectPrevious();
581
+ handled = true;
582
+ // Completion keys.
583
+ } else if ( CKEDITOR.tools.indexOf( this.commitKeystrokes, keyCode ) != -1 ) {
584
+ this.commit();
585
+ this.textWatcher.unmatch();
586
+ handled = true;
587
+ }
588
+
589
+ if ( handled ) {
590
+ evt.cancel();
591
+ evt.data.preventDefault();
592
+ this.textWatcher.consumeNext();
593
+ }
594
+ },
595
+
596
+ /**
597
+ * The function that should be called once an item was selected.
598
+ *
599
+ * @param {CKEDITOR.eventInfo} evt
600
+ * @private
601
+ */
602
+ onSelectedItemId: function( evt ) {
603
+ var itemId = evt.data,
604
+ selectedItem = this.view.getItemById( itemId );
605
+
606
+ this.model.setItem( itemId );
607
+ this.view.selectItem( itemId );
608
+
609
+ this.updateAriaActiveDescendantAttributeOnEditable( selectedItem.getAttribute( 'id' ) );
610
+ },
611
+
612
+ /**
613
+ * The function that should be called once a text was matched by the {@link CKEDITOR.plugins.textWatcher text watcher}
614
+ * component.
615
+ *
616
+ * @param {CKEDITOR.eventInfo} evt
617
+ * @private
618
+ */
619
+ onTextMatched: function( evt ) {
620
+ this.model.setActive( false );
621
+ this.model.setQuery( evt.data.text, evt.data.range );
622
+ },
623
+
624
+ /**
625
+ * The function that should be called once a text was unmatched by the {@link CKEDITOR.plugins.textWatcher text watcher}
626
+ * component.
627
+ *
628
+ * @param {CKEDITOR.eventInfo} evt
629
+ * @private
630
+ */
631
+ onTextUnmatched: function() {
632
+ // Remove query and request ID to avoid opening view for invalid callback (#1984).
633
+ this.model.query = null;
634
+ this.model.lastRequestId = null;
635
+
636
+ this.close();
637
+ }
638
+ };
639
+
640
+ /**
641
+ * Class representing the autocomplete view.
642
+ *
643
+ * In order to use a different view, implement a new view class and override
644
+ * the {@link CKEDITOR.plugins.autocomplete#getView} method.
645
+ *
646
+ * ```javascript
647
+ * myAutocomplete.prototype.getView = function() {
648
+ * return new myView( this.editor );
649
+ * };
650
+ * ```
651
+ *
652
+ * You can also modify this autocomplete instance on the fly.
653
+ *
654
+ * ```javascript
655
+ * myAutocomplete.prototype.getView = function() {
656
+ * // Call the original getView method.
657
+ * var view = CKEDITOR.plugins.autocomplete.prototype.getView.call( this );
658
+ *
659
+ * // Override one property.
660
+ * view.itemTemplate = new CKEDITOR.template( '<li data-id={id}><img src="{iconSrc}" alt="..."> {name}</li>' );
661
+ *
662
+ * return view;
663
+ * };
664
+ * ```
665
+ *
666
+ * **Note:** This class is marked as private, which means that its API might be subject to change in order to
667
+ * provide further enhancements.
668
+ *
669
+ * @class CKEDITOR.plugins.autocomplete.view
670
+ * @since 4.10.0
671
+ * @private
672
+ * @mixins CKEDITOR.event
673
+ * @constructor Creates the autocomplete view instance.
674
+ * @param {CKEDITOR.editor} editor The editor instance.
675
+ */
676
+ function View( editor ) {
677
+ /**
678
+ * The panel's item template used to render matches in the dropdown.
679
+ *
680
+ * You can use {@link CKEDITOR.plugins.autocomplete.model#data data item} properties to customize the template.
681
+ *
682
+ * A minimal template must be wrapped with a HTML `li` element containing the `data-id="{id}"` attribute.
683
+ *
684
+ * ```javascript
685
+ * var itemTemplate = '<li data-id="{id}"><img src="{iconSrc}" alt="{name}">{name}</li>';
686
+ * ```
687
+ *
688
+ * @readonly
689
+ * @property {CKEDITOR.template}
690
+ */
691
+ this.itemTemplate = new CKEDITOR.template( '<li data-id="{id}">{name}</li>' );
692
+
693
+ /**
694
+ * The editor instance.
695
+ *
696
+ * @readonly
697
+ * @property {CKEDITOR.editor}
698
+ */
699
+ this.editor = editor;
700
+
701
+ /**
702
+ * The ID of the selected item.
703
+ *
704
+ * @readonly
705
+ * @property {Number/String} selectedItemId
706
+ */
707
+
708
+ /**
709
+ * The document to which the view is attached. It is set by the {@link #append} method.
710
+ *
711
+ * @readonly
712
+ * @property {CKEDITOR.dom.document} document
713
+ */
714
+
715
+ /**
716
+ * The view's main element. It is set by the {@link #append} method.
717
+ *
718
+ * @readonly
719
+ * @property {CKEDITOR.dom.element} element
720
+ */
721
+
722
+ /**
723
+ * Event fired when an item in the panel is clicked.
724
+ *
725
+ * @event click-item
726
+ * @param {String} The clicked item {@link CKEDITOR.plugins.autocomplete.model.item#id}. Note: the ID
727
+ * is stringified due to the way how it is stored in the DOM.
728
+ */
729
+
730
+ /**
731
+ * Event fired when the {@link #selectedItemId} property changes.
732
+ *
733
+ * @event change-selectedItemId
734
+ * @param {Number/String} data The new value.
735
+ */
736
+ }
737
+
738
+ View.prototype = {
739
+ /**
740
+ * Appends the {@link #element main element} to the DOM.
741
+ */
742
+ append: function() {
743
+ this.document = CKEDITOR.document;
744
+ this.element = this.createElement();
745
+
746
+ this.document.getBody().append( this.element );
747
+ },
748
+
749
+ /**
750
+ * Removes existing items and appends given items to the {@link #element}.
751
+ *
752
+ * @param {CKEDITOR.dom.documentFragment} itemsFragment The document fragment with item elements.
753
+ */
754
+ appendItems: function( itemsFragment ) {
755
+ this.element.setHtml( '' );
756
+ this.element.append( itemsFragment );
757
+ },
758
+
759
+ /**
760
+ * Attaches the view's listeners to the DOM elements.
761
+ */
762
+ attach: function() {
763
+ this.element.on( 'click', function( evt ) {
764
+ var target = evt.data.getTarget(),
765
+ itemElement = target.getAscendant( this.isItemElement, true );
766
+
767
+ if ( itemElement ) {
768
+ this.fire( 'click-item', itemElement.data( 'id' ) );
769
+ }
770
+ }, this );
771
+
772
+ this.element.on( 'mouseover', function( evt ) {
773
+ var target = evt.data.getTarget();
774
+
775
+ if ( this.element.contains( target ) ) {
776
+
777
+ // Find node containing data-id attribute inside target node tree (#2187).
778
+ target = target.getAscendant( function( element ) {
779
+ return element.hasAttribute( 'data-id' );
780
+ }, true );
781
+
782
+ if ( !target ) {
783
+ return;
784
+ }
785
+
786
+ var itemId = target.data( 'id' );
787
+
788
+ this.fire( 'change-selectedItemId', itemId );
789
+ }
790
+
791
+ }, this );
792
+ },
793
+
794
+ /**
795
+ * Closes the panel.
796
+ */
797
+ close: function() {
798
+ this.element.removeClass( 'cke_autocomplete_opened' );
799
+ },
800
+
801
+ /**
802
+ * Creates and returns the view's main element.
803
+ *
804
+ * @private
805
+ * @returns {CKEDITOR.dom.element}
806
+ */
807
+ createElement: function() {
808
+ var el = new CKEDITOR.dom.element( 'ul', this.document ),
809
+ id = CKEDITOR.tools.getNextId();
810
+
811
+ // Id is needed to correctly bind autocomplete with the editable (#4617).
812
+ el.setAttribute( 'id', id );
813
+ el.addClass( 'cke_autocomplete_panel' );
814
+ // Below float panels and context menu, but above maximized editor (-5).
815
+ el.setStyle( 'z-index', this.editor.config.baseFloatZIndex - 3 );
816
+ // Add also appropriate role (#4617).
817
+ el.setAttribute( 'role', 'listbox' );
818
+
819
+ return el;
820
+ },
821
+
822
+ /**
823
+ * Creates the item element based on the {@link #itemTemplate}.
824
+ *
825
+ * @param {CKEDITOR.plugins.autocomplete.model.item} item The item for which an element will be created.
826
+ * @returns {CKEDITOR.dom.element}
827
+ */
828
+ createItem: function( item ) {
829
+ var encodedItem = encodeItem( item ),
830
+ itemElement = CKEDITOR.dom.element.createFromHtml( this.itemTemplate.output( encodedItem ), this.document ),
831
+ id = CKEDITOR.tools.getNextId();
832
+
833
+ // Add attributes needed for a11y support (#4617).
834
+ itemElement.setAttribute( 'id', id );
835
+ itemElement.setAttribute( 'role', 'option' );
836
+
837
+ return itemElement;
838
+ },
839
+
840
+ /**
841
+ * Returns the view position based on a given `range`.
842
+ *
843
+ * Indicates the start position of the autocomplete dropdown.
844
+ * The value returned by this function is passed to the {@link #setPosition} method
845
+ * by the {@link #updatePosition} method.
846
+ *
847
+ * @param {CKEDITOR.dom.range} range The range of the text match.
848
+ * @returns {Object} Represents the position of the caret. The value is relative to the panel's offset parent.
849
+ * @returns {Number} rect.left
850
+ * @returns {Number} rect.top
851
+ * @returns {Number} rect.bottom
852
+ */
853
+ getViewPosition: function( range ) {
854
+ // Use the last rect so the view will be
855
+ // correctly positioned with a word split into few lines.
856
+ var rects = range.getClientRects(),
857
+ viewPositionRect = rects[ rects.length - 1 ],
858
+ offset,
859
+ editable = this.editor.editable();
860
+
861
+ if ( editable.isInline() ) {
862
+ offset = CKEDITOR.document.getWindow().getScrollPosition();
863
+ } else {
864
+ offset = editable.getParent().getDocumentPosition( CKEDITOR.document );
865
+ }
866
+
867
+ // Consider that offset host might be repositioned on its own.
868
+ // Similar to #1048. See https://github.com/ckeditor/ckeditor4/pull/1732#discussion_r182790235.
869
+ var hostElement = CKEDITOR.document.getBody();
870
+ if ( hostElement.getComputedStyle( 'position' ) === 'static' ) {
871
+ hostElement = hostElement.getParent();
872
+ }
873
+
874
+ var offsetCorrection = hostElement.getDocumentPosition();
875
+
876
+ offset.x -= offsetCorrection.x;
877
+ offset.y -= offsetCorrection.y;
878
+
879
+ return {
880
+ top: ( viewPositionRect.top + offset.y ),
881
+ bottom: ( viewPositionRect.top + viewPositionRect.height + offset.y ),
882
+ left: ( viewPositionRect.left + offset.x )
883
+ };
884
+ },
885
+
886
+ /**
887
+ * Gets the item element by the item ID.
888
+ *
889
+ * @param {Number/String} itemId
890
+ * @returns {CKEDITOR.dom.element} The item element.
891
+ */
892
+ getItemById: function( itemId ) {
893
+ return this.element.findOne( 'li[data-id="' + itemId + '"]' );
894
+ },
895
+
896
+ /**
897
+ * Checks whether a given node is the item element.
898
+ *
899
+ * @param {CKEDITOR.dom.node} node
900
+ * @returns {Boolean}
901
+ */
902
+ isItemElement: function( node ) {
903
+ return node.type == CKEDITOR.NODE_ELEMENT &&
904
+ Boolean( node.data( 'id' ) );
905
+ },
906
+
907
+ /**
908
+ * Opens the panel.
909
+ */
910
+ open: function() {
911
+ this.element.addClass( 'cke_autocomplete_opened' );
912
+ },
913
+
914
+ /**
915
+ * Selects the item in the panel and scrolls the list to show it if needed.
916
+ * The {@link #selectedItemId currently selected item} is deselected first.
917
+ *
918
+ * @param {Number/String} itemId The ID of the item that should be selected.
919
+ */
920
+ selectItem: function( itemId ) {
921
+ if ( this.selectedItemId != null ) {
922
+ this.getItemById( this.selectedItemId ).removeClass( 'cke_autocomplete_selected' );
923
+ }
924
+
925
+ var itemElement = this.getItemById( itemId );
926
+ itemElement.addClass( 'cke_autocomplete_selected' );
927
+ this.selectedItemId = itemId;
928
+
929
+ this.scrollElementTo( itemElement );
930
+ },
931
+
932
+ /**
933
+ * Sets the position of the panel. This method only performs the check
934
+ * for the available space below and above the specified `rect` and
935
+ * positions the panel in the best place.
936
+ *
937
+ * This method is used by the {@link #updatePosition} method which
938
+ * controls how the panel should be positioned on the screen, for example
939
+ * based on the caret position and/or the editor position.
940
+ *
941
+ * @param {Object} rect Represents the position of a vertical (e.g. a caret) line relative to which
942
+ * the panel should be positioned.
943
+ * @param {Number} rect.left The position relative to the panel's offset parent in pixels.
944
+ * For example, the position of the caret.
945
+ * @param {Number} rect.top The position relative to the panel's offset parent in pixels.
946
+ * For example, the position of the upper end of the caret.
947
+ * @param {Number} rect.bottom The position relative to the panel's offset parent in pixels.
948
+ * For example, the position of the bottom end of the caret.
949
+ */
950
+ setPosition: function( rect ) {
951
+ var documentWindow = this.element.getWindow(),
952
+ windowRect = documentWindow.getViewPaneSize(),
953
+ top = getVerticalPosition( {
954
+ editorViewportRect: getEditorViewportRect( this.editor ),
955
+ caretRect: rect,
956
+ viewHeight: this.element.getSize( 'height' ),
957
+ scrollPositionY: documentWindow.getScrollPosition().y,
958
+ windowHeight: windowRect.height
959
+ } ),
960
+ left = getHorizontalPosition( {
961
+ leftPosition: rect.left,
962
+ viewWidth: this.element.getSize( 'width' ),
963
+ windowWidth: windowRect.width
964
+ } );
965
+
966
+ this.element.setStyles( {
967
+ left: left + 'px',
968
+ top: top + 'px'
969
+ } );
970
+
971
+ function getVerticalPosition( options ) {
972
+ var editorViewportRect = options.editorViewportRect,
973
+ caretRect = options.caretRect,
974
+ viewHeight = options.viewHeight,
975
+ scrollPositionY = options.scrollPositionY,
976
+ windowHeight = options.windowHeight;
977
+
978
+ // If the caret position is below the view - keep it at the bottom edge.
979
+ // +---------------------------------------------+
980
+ // | editor viewport |
981
+ // | |
982
+ // | +--------------+ |
983
+ // | | | |
984
+ // | | view | |
985
+ // | | | |
986
+ // +-----+==============+------------------------+
987
+ // | |
988
+ // | █ - caret position |
989
+ // | |
990
+ // +---------------------------------------------+
991
+ if ( editorViewportRect.bottom < caretRect.bottom ) {
992
+ return Math.min( caretRect.top, editorViewportRect.bottom ) - viewHeight;
993
+ }
994
+
995
+ // If the view doesn't fit below the caret position and fits above, set it there.
996
+ // This means that the position below the caret is preferred.
997
+ // +---------------------------------------------+
998
+ // | |
999
+ // | editor viewport |
1000
+ // | +--------------+ |
1001
+ // | | | |
1002
+ // | | view | |
1003
+ // | | | |
1004
+ // | +--------------+ |
1005
+ // | █ - caret position |
1006
+ // | |
1007
+ // | |
1008
+ // +---------------------------------------------+
1009
+ // How much space is there for the view above and below the specified rect.
1010
+ var spaceAbove = caretRect.top - editorViewportRect.top,
1011
+ spaceBelow = editorViewportRect.bottom - caretRect.bottom,
1012
+ viewExceedsTopViewport = ( caretRect.top - viewHeight ) < scrollPositionY;
1013
+
1014
+ if ( viewHeight > spaceBelow && viewHeight < spaceAbove && !viewExceedsTopViewport ) {
1015
+ return caretRect.top - viewHeight;
1016
+ }
1017
+
1018
+ // If the caret position is above the view - keep it at the top edge.
1019
+ // +---------------------------------------------+
1020
+ // | |
1021
+ // | █ - caret position |
1022
+ // | |
1023
+ // +-----+==============+------------------------+
1024
+ // | | | |
1025
+ // | | view | |
1026
+ // | | | |
1027
+ // | +--------------+ |
1028
+ // | |
1029
+ // | editor viewport |
1030
+ // +---------------------------------------------+
1031
+ if ( editorViewportRect.top > caretRect.top ) {
1032
+ return Math.max( caretRect.bottom, editorViewportRect.top );
1033
+ }
1034
+
1035
+ // (#3582)
1036
+ // If the view goes beyond bottom window border - reverse view position, even if it fits editor viewport.
1037
+ // +---------------------------------------------+
1038
+ // | editor viewport |
1039
+ // | |
1040
+ // | |
1041
+ // | +--------------+ |
1042
+ // | | view | |
1043
+ // | +--------------+ |
1044
+ // | caret position - █ |
1045
+ // | |
1046
+ // =============================================== - bottom window border
1047
+ // | |
1048
+ // | |
1049
+ // +---------------------------------------------+
1050
+ var viewExceedsBottomViewport = ( caretRect.bottom + viewHeight ) > ( windowHeight + scrollPositionY );
1051
+
1052
+ if ( !( viewHeight > spaceBelow && viewHeight < spaceAbove ) && viewExceedsBottomViewport ) {
1053
+ return caretRect.top - viewHeight;
1054
+ }
1055
+
1056
+ // As a default, keep the view inside the editor viewport.
1057
+ // +---------------------------------------------+
1058
+ // | editor viewport |
1059
+ // | |
1060
+ // | |
1061
+ // | |
1062
+ // | █ - caret position |
1063
+ // | +--------------+ |
1064
+ // | | view | |
1065
+ // | +--------------+ |
1066
+ // | |
1067
+ // | |
1068
+ // +---------------------------------------------+
1069
+ return Math.min( editorViewportRect.bottom, caretRect.bottom );
1070
+ }
1071
+
1072
+ function getHorizontalPosition( options ) {
1073
+ var caretLeftPosition = options.leftPosition,
1074
+ viewWidth = options.viewWidth,
1075
+ windowWidth = options.windowWidth;
1076
+
1077
+ // (#3582)
1078
+ // If the view goes beyond right window border - stick it to the edge of the available viewport.
1079
+ // +---------------------------------------------+ ||
1080
+ // | editor viewport | ||
1081
+ // | | ||
1082
+ // | | ||
1083
+ // | caret position - █ | || - right window border
1084
+ // | +--------------+||
1085
+ // | | |||
1086
+ // | | view |||
1087
+ // | | |||
1088
+ // | +--------------+||
1089
+ // | | ||
1090
+ // +---------------------------------------------+ ||
1091
+ if ( caretLeftPosition + viewWidth > windowWidth ) {
1092
+ return windowWidth - viewWidth;
1093
+ }
1094
+
1095
+ // Otherwise inherit the horizontal position from caret.
1096
+ return caretLeftPosition;
1097
+ }
1098
+
1099
+ // Bounding rect where the view should fit (visible editor viewport).
1100
+ function getEditorViewportRect( editor ) {
1101
+ var editable = editor.editable();
1102
+
1103
+ // iOS classic editor has different viewport element (#1910).
1104
+ if ( CKEDITOR.env.iOS && !editable.isInline() ) {
1105
+ return iOSViewportElement( editor ).getClientRect( true );
1106
+ } else {
1107
+ return editable.isInline() ? editable.getClientRect( true ) : editor.window.getFrame().getClientRect( true );
1108
+ }
1109
+ }
1110
+ },
1111
+
1112
+ /**
1113
+ * Scrolls the list so the item element is visible in it.
1114
+ *
1115
+ * @param {CKEDITOR.dom.element} itemElement
1116
+ */
1117
+ scrollElementTo: function( itemElement ) {
1118
+ itemElement.scrollIntoParent( this.element );
1119
+ },
1120
+
1121
+ /**
1122
+ * Updates the list of items in the panel.
1123
+ *
1124
+ * @param {CKEDITOR.plugins.autocomplete.model.item[]} items.
1125
+ */
1126
+ updateItems: function( items ) {
1127
+ var i,
1128
+ frag = new CKEDITOR.dom.documentFragment( this.document );
1129
+
1130
+ for ( i = 0; i < items.length; ++i ) {
1131
+ frag.append( this.createItem( items[ i ] ) );
1132
+ }
1133
+
1134
+ this.appendItems( frag );
1135
+ this.selectedItemId = null;
1136
+ },
1137
+
1138
+ /**
1139
+ * Updates the position of the panel.
1140
+ *
1141
+ * By default this method finds the position of the caret and uses
1142
+ * {@link #setPosition} to move the panel to the best position close
1143
+ * to the caret.
1144
+ *
1145
+ * @param {CKEDITOR.dom.range} range The range of the text match.
1146
+ */
1147
+ updatePosition: function( range ) {
1148
+ this.setPosition( this.getViewPosition( range ) );
1149
+ }
1150
+ };
1151
+
1152
+ CKEDITOR.event.implementOn( View.prototype );
1153
+
1154
+ /**
1155
+ * Class representing the autocomplete model.
1156
+ *
1157
+ * In case you want to modify the model behavior, check out the
1158
+ * {@link CKEDITOR.plugins.autocomplete.view} documentation. It contains
1159
+ * examples of how to easily override the default behavior.
1160
+ *
1161
+ * A model instance is created by the {@link CKEDITOR.plugins.autocomplete#getModel} method.
1162
+ *
1163
+ * **Note:** This class is marked as private, which means that its API might be subject to change in order to
1164
+ * provide further enhancements.
1165
+ *
1166
+ * @class CKEDITOR.plugins.autocomplete.model
1167
+ * @since 4.10.0
1168
+ * @private
1169
+ * @mixins CKEDITOR.event
1170
+ * @constructor Creates the autocomplete model instance.
1171
+ * @param {Function} dataCallback See {@link CKEDITOR.plugins.autocomplete} arguments.
1172
+ */
1173
+ function Model( dataCallback ) {
1174
+ /**
1175
+ * The callback executed by the model when requesting data.
1176
+ * See {@link CKEDITOR.plugins.autocomplete} arguments.
1177
+ *
1178
+ * @readonly
1179
+ * @property {Function}
1180
+ */
1181
+ this.dataCallback = dataCallback;
1182
+
1183
+ /**
1184
+ * Whether the autocomplete is active (i.e. can receive user input like click, key press).
1185
+ * Should be modified by the {@link #setActive} method which fires the {@link #change-isActive} event.
1186
+ *
1187
+ * @readonly
1188
+ */
1189
+ this.isActive = false;
1190
+
1191
+ /**
1192
+ * Indicates the limit of items rendered in the dropdown.
1193
+ *
1194
+ * For falsy values like `0` or `null` all items will be rendered.
1195
+ *
1196
+ * @property {Number} [itemsLimit=0]
1197
+ */
1198
+ this.itemsLimit = 0;
1199
+
1200
+ /**
1201
+ * The ID of the last request for data. Used by the {@link #setQuery} method.
1202
+ *
1203
+ * @readonly
1204
+ * @private
1205
+ * @property {Number} lastRequestId
1206
+ */
1207
+
1208
+ /**
1209
+ * The query string set by the {@link #setQuery} method.
1210
+ *
1211
+ * The query string always has a corresponding {@link #range}.
1212
+ *
1213
+ * @readonly
1214
+ * @property {String} query
1215
+ */
1216
+
1217
+ /**
1218
+ * The range in the DOM where the {@link #query} text is.
1219
+ *
1220
+ * The range always has a corresponding {@link #query}. Both can be set by the {@link #setQuery} method.
1221
+ *
1222
+ * @readonly
1223
+ * @property {CKEDITOR.dom.range} range
1224
+ */
1225
+
1226
+ /**
1227
+ * The query results &mdash; the items to be displayed in the autocomplete panel.
1228
+ *
1229
+ * @readonly
1230
+ * @property {CKEDITOR.plugins.autocomplete.model.item[]} data
1231
+ */
1232
+
1233
+ /**
1234
+ * The ID of the item currently selected in the panel.
1235
+ *
1236
+ * @readonly
1237
+ * @property {Number/String} selectedItemId
1238
+ */
1239
+
1240
+ /**
1241
+ * Event fired when the {@link #data} array changes.
1242
+ *
1243
+ * @event change-data
1244
+ * @param {CKEDITOR.plugins.autocomplete.model.item[]} data The new value.
1245
+ */
1246
+
1247
+ /**
1248
+ * Event fired when the {@link #selectedItemId} property changes.
1249
+ *
1250
+ * @event change-selectedItemId
1251
+ * @param {Number/String} data The new value.
1252
+ */
1253
+
1254
+ /**
1255
+ * Event fired when the {@link #isActive} property changes.
1256
+ *
1257
+ * @event change-isActive
1258
+ * @param {Boolean} data The new value.
1259
+ */
1260
+ }
1261
+
1262
+ Model.prototype = {
1263
+ /**
1264
+ * Gets an index from the {@link #data} array of the item by its ID.
1265
+ *
1266
+ * @param {Number/String} itemId
1267
+ * @returns {Number}
1268
+ */
1269
+ getIndexById: function( itemId ) {
1270
+ if ( !this.hasData() ) {
1271
+ return -1;
1272
+ }
1273
+
1274
+ for ( var data = this.data, i = 0, l = data.length; i < l; i++ ) {
1275
+ if ( data[ i ].id == itemId ) {
1276
+ return i;
1277
+ }
1278
+ }
1279
+
1280
+ return -1;
1281
+ },
1282
+
1283
+ /**
1284
+ * Gets the item from the {@link #data} array by its ID.
1285
+ *
1286
+ * @param {Number/String} itemId
1287
+ * @returns {CKEDITOR.plugins.autocomplete.model.item}
1288
+ */
1289
+ getItemById: function( itemId ) {
1290
+ var index = this.getIndexById( itemId );
1291
+ return ~index && this.data[ index ] || null;
1292
+ },
1293
+
1294
+ /**
1295
+ * Whether the model contains non-empty {@link #data}.
1296
+ *
1297
+ * @returns {Boolean}
1298
+ */
1299
+ hasData: function() {
1300
+ return Boolean( this.data && this.data.length );
1301
+ },
1302
+
1303
+ /**
1304
+ * Sets the {@link #selectedItemId} property.
1305
+ *
1306
+ * @param {Number/String} itemId
1307
+ */
1308
+ setItem: function( itemId ) {
1309
+ if ( this.getIndexById( itemId ) < 0 ) {
1310
+ throw new Error( 'Item with given id does not exist' );
1311
+ }
1312
+
1313
+ this.selectedItemId = itemId;
1314
+ },
1315
+
1316
+ /**
1317
+ * Fires the {@link #change-selectedItemId} event.
1318
+ *
1319
+ * @param {Number/String} itemId
1320
+ */
1321
+ select: function( itemId ) {
1322
+ this.fire( 'change-selectedItemId', itemId );
1323
+ },
1324
+
1325
+ /**
1326
+ * Selects the first item. See also the {@link #select} method.
1327
+ */
1328
+ selectFirst: function() {
1329
+ if ( this.hasData() ) {
1330
+ this.select( this.data[ 0 ].id );
1331
+ }
1332
+ },
1333
+
1334
+ /**
1335
+ * Selects the last item. See also the {@link #select} method.
1336
+ */
1337
+ selectLast: function() {
1338
+ if ( this.hasData() ) {
1339
+ this.select( this.data[ this.data.length - 1 ].id );
1340
+ }
1341
+ },
1342
+
1343
+ /**
1344
+ * Selects the next item in the {@link #data} array. If no item is selected,
1345
+ * it selects the first one. If the last one is selected, it selects the first one.
1346
+ *
1347
+ * See also the {@link #select} method.
1348
+ */
1349
+ selectNext: function() {
1350
+ if ( this.selectedItemId == null ) {
1351
+ this.selectFirst();
1352
+ return;
1353
+ }
1354
+
1355
+ var index = this.getIndexById( this.selectedItemId );
1356
+
1357
+ if ( index < 0 || index + 1 == this.data.length ) {
1358
+ this.selectFirst();
1359
+ } else {
1360
+ this.select( this.data[ index + 1 ].id );
1361
+ }
1362
+ },
1363
+
1364
+ /**
1365
+ * Selects the previous item in the {@link #data} array. If no item is selected,
1366
+ * it selects the last one. If the first one is selected, it selects the last one.
1367
+ *
1368
+ * See also the {@link #select} method.
1369
+ */
1370
+ selectPrevious: function() {
1371
+ if ( this.selectedItemId == null ) {
1372
+ this.selectLast();
1373
+ return;
1374
+ }
1375
+
1376
+ var index = this.getIndexById( this.selectedItemId );
1377
+
1378
+ if ( index <= 0 ) {
1379
+ this.selectLast();
1380
+ } else {
1381
+ this.select( this.data[ index - 1 ].id );
1382
+ }
1383
+ },
1384
+
1385
+ /**
1386
+ * Sets the {@link #isActive} property and fires the {@link #change-isActive} event.
1387
+ *
1388
+ * @param {Boolean} isActive
1389
+ */
1390
+ setActive: function( isActive ) {
1391
+ this.isActive = isActive;
1392
+ this.fire( 'change-isActive', isActive );
1393
+ },
1394
+
1395
+ /**
1396
+ * Sets the {@link #query} and {@link #range} and makes a request for the query results
1397
+ * by executing the {@link #dataCallback} function. When the data is returned (synchronously or
1398
+ * asynchronously, because {@link #dataCallback} exposes a callback function), the {@link #data}
1399
+ * property is set and the {@link #change-data} event is fired.
1400
+ *
1401
+ * This method controls that only the response for the current query is handled.
1402
+ *
1403
+ * @param {String} query
1404
+ * @param {CKEDITOR.dom.range} range
1405
+ */
1406
+ setQuery: function( query, range ) {
1407
+ var that = this,
1408
+ requestId = CKEDITOR.tools.getNextId();
1409
+
1410
+ this.lastRequestId = requestId;
1411
+ this.query = query;
1412
+ this.range = range;
1413
+ this.data = null;
1414
+ this.selectedItemId = null;
1415
+
1416
+ this.dataCallback( {
1417
+ query: query,
1418
+ range: range
1419
+ }, handleData );
1420
+
1421
+ // Note: don't put any executable code here because the callback passed to
1422
+ // this.dataCallback may be executed synchronously or asynchronously
1423
+ // so execution order will differ.
1424
+
1425
+ function handleData( data ) {
1426
+ // Handle only the response for the most recent setQuery call.
1427
+ if ( requestId == that.lastRequestId ) {
1428
+ // Limit number of items (#2030).
1429
+ if ( that.itemsLimit ) {
1430
+ that.data = data.slice( 0, that.itemsLimit );
1431
+ } else {
1432
+ that.data = data;
1433
+ }
1434
+ that.fire( 'change-data', that.data );
1435
+ }
1436
+ }
1437
+ }
1438
+ };
1439
+
1440
+ CKEDITOR.event.implementOn( Model.prototype );
1441
+
1442
+ /**
1443
+ * An abstract class representing one {@link CKEDITOR.plugins.autocomplete.model#data data item}.
1444
+ * A item can be understood as one entry in the autocomplete panel.
1445
+ *
1446
+ * An item must have a unique {@link #id} and may have more properties which can then be used, for example,
1447
+ * in the {@link CKEDITOR.plugins.autocomplete.view#itemTemplate} template or the
1448
+ * {@link CKEDITOR.plugins.autocomplete#getHtmlToInsert} method.
1449
+ *
1450
+ * Example items:
1451
+ *
1452
+ * ```javascript
1453
+ * { id: 345, name: 'CKEditor' }
1454
+ * { id: 'smile1', alt: 'smile', emojiSrc: 'emojis/smile.png' }
1455
+ * ```
1456
+ *
1457
+ * @abstract
1458
+ * @class CKEDITOR.plugins.autocomplete.model.item
1459
+ * @since 4.10.0
1460
+ */
1461
+
1462
+ /**
1463
+ * The unique ID of the item. The ID should not change with time, so two
1464
+ * {@link CKEDITOR.plugins.autocomplete.model#dataCallback}
1465
+ * calls should always result in the same ID for the same logical item.
1466
+ * This can, for example, allow to keep the same item selected when
1467
+ * the data changes.
1468
+ *
1469
+ * **Note:** When using a string as an item, make sure that the string does not
1470
+ * contain any special characters (above all `"[]` characters). This limitation is
1471
+ * due to the simplified way the {@link CKEDITOR.plugins.autocomplete.view}
1472
+ * stores IDs in the DOM.
1473
+ *
1474
+ * @readonly
1475
+ * @property {Number/String} id
1476
+ */
1477
+
1478
+ CKEDITOR.plugins.autocomplete = Autocomplete;
1479
+ Autocomplete.view = View;
1480
+ Autocomplete.model = Model;
1481
+
1482
+ /**
1483
+ * The autocomplete keystrokes used to finish autocompletion with the selected view item.
1484
+ * This setting will set completing keystrokes for each autocomplete plugin respectively.
1485
+ *
1486
+ * To change completing keystrokes individually use the {@link CKEDITOR.plugins.autocomplete#commitKeystrokes} plugin property.
1487
+ *
1488
+ * ```javascript
1489
+ * // Default configuration (9 = Tab, 13 = Enter).
1490
+ * config.autocomplete_commitKeystrokes = [ 9, 13 ];
1491
+ * ```
1492
+ *
1493
+ * Commit keystroke can also be disabled by setting it to an empty array.
1494
+ *
1495
+ * ```javascript
1496
+ * // Disable autocomplete commit keystroke.
1497
+ * config.autocomplete_commitKeystrokes = [];
1498
+ * ```
1499
+ *
1500
+ * @since 4.10.0
1501
+ * @cfg {Number/Number[]} [autocomplete_commitKeystrokes=[9, 13]]
1502
+ * @member CKEDITOR.config
1503
+ */
1504
+ CKEDITOR.config.autocomplete_commitKeystrokes = [ 9, 13 ];
1505
+
1506
+ // Viewport on iOS is moved into iframe parent element because of https://bugs.webkit.org/show_bug.cgi?id=149264 issue.
1507
+ // Once upstream issue is resolved this function should be removed and its concurrences should be refactored to
1508
+ // follow the default code path.
1509
+ function iOSViewportElement( editor ) {
1510
+ return editor.window.getFrame().getParent();
1511
+ }
1512
+
1513
+ function encodeItem( item ) {
1514
+ return CKEDITOR.tools.array.reduce( CKEDITOR.tools.object.keys( item ), function( cur, key ) {
1515
+ cur[ key ] = CKEDITOR.tools.htmlEncode( item[ key ] );
1516
+ return cur;
1517
+ }, {} );
1518
+ }
1519
+
1520
+ /**
1521
+ * Abstract class describing the definition of the [Autocomplete](https://ckeditor.com/cke4/addon/autocomplete) plugin configuration.
1522
+ *
1523
+ * It lists properties used to define and create autocomplete configuration definition.
1524
+ *
1525
+ * Simple usage:
1526
+ *
1527
+ * ```javascript
1528
+ * var definition = {
1529
+ * dataCallback: dataCallback,
1530
+ * textTestCallback: textTestCallback,
1531
+ * throttle: 200
1532
+ * };
1533
+ * ```
1534
+ *
1535
+ * @class CKEDITOR.plugins.autocomplete.configDefinition
1536
+ * @abstract
1537
+ * @since 4.10.0
1538
+ */
1539
+
1540
+ /**
1541
+ * Callback executed to get suggestion data based on the search query. The returned data will be
1542
+ * displayed in the autocomplete view.
1543
+ *
1544
+ * ```javascript
1545
+ * // Returns (through its callback) the suggestions for the current query.
1546
+ * // Note: The itemsArray variable is the example "database".
1547
+ * function dataCallback( matchInfo, callback ) {
1548
+ * // Simple search.
1549
+ * // Filter the entire items array so only the items that start
1550
+ * // with the query remain.
1551
+ * var suggestions = itemsArray.filter( function( item ) {
1552
+ * return item.name.indexOf( matchInfo.query ) === 0;
1553
+ * } );
1554
+ *
1555
+ * // Note: The callback function can also be executed asynchronously
1556
+ * // so dataCallback can do an XHR request or use any other asynchronous API.
1557
+ * callback( suggestions );
1558
+ * }
1559
+ *
1560
+ * ```
1561
+ *
1562
+ * @method dataCallback
1563
+ * @param {CKEDITOR.plugins.autocomplete.matchInfo} matchInfo
1564
+ * @param {Function} callback The callback which should be executed with the matched data.
1565
+ * @param {CKEDITOR.plugins.autocomplete.model.item[]} callback.data The suggestion data that should be
1566
+ * displayed in the autocomplete view for a given query. The data items should implement the
1567
+ * {@link CKEDITOR.plugins.autocomplete.model.item} interface.
1568
+ */
1569
+
1570
+ /**
1571
+ * Callback executed to check if a text next to the selection should open
1572
+ * the autocomplete. See the {@link CKEDITOR.plugins.textWatcher}'s `callback` argument.
1573
+ *
1574
+ * ```javascript
1575
+ * // Called when the user types in the editor or moves the caret.
1576
+ * // The range represents the caret position.
1577
+ * function textTestCallback( range ) {
1578
+ * // You do not want to autocomplete a non-empty selection.
1579
+ * if ( !range.collapsed ) {
1580
+ * return null;
1581
+ * }
1582
+ *
1583
+ * // Use the text match plugin which does the tricky job of doing
1584
+ * // a text search in the DOM. The matchCallback function should return
1585
+ * // a matching fragment of the text.
1586
+ * return CKEDITOR.plugins.textMatch.match( range, matchCallback );
1587
+ * }
1588
+ *
1589
+ * // Returns a position of the matching text.
1590
+ * // It matches with a word starting from the '@' character
1591
+ * // up to the caret position.
1592
+ * function matchCallback( text, offset ) {
1593
+ * // Get the text before the caret.
1594
+ * var left = text.slice( 0, offset ),
1595
+ * // Will look for an '@' character followed by word characters.
1596
+ * match = left.match( /@\w*$/ );
1597
+ *
1598
+ * if ( !match ) {
1599
+ * return null;
1600
+ * }
1601
+ * return { start: match.index, end: offset };
1602
+ * }
1603
+ * ```
1604
+ *
1605
+ * @method textTestCallback
1606
+ * @param {CKEDITOR.dom.range} range Range representing the caret position.
1607
+ */
1608
+
1609
+ /**
1610
+ * @inheritdoc CKEDITOR.plugins.autocomplete#throttle
1611
+ * @property {Number} [throttle]
1612
+ */
1613
+
1614
+ /**
1615
+ * @inheritdoc CKEDITOR.plugins.autocomplete.model#itemsLimit
1616
+ * @property {Number} [itemsLimit]
1617
+ */
1618
+
1619
+ /**
1620
+ * @inheritdoc CKEDITOR.plugins.autocomplete.view#itemTemplate
1621
+ * @property {String} [itemTemplate]
1622
+ */
1623
+
1624
+ /**
1625
+ * @inheritdoc CKEDITOR.plugins.autocomplete#outputTemplate
1626
+ * @property {String} [outputTemplate]
1627
+ */
1628
+
1629
+ /**
1630
+ * Abstract class describing a set of properties that can be used to produce more adequate suggestion data based on the matched query.
1631
+ *
1632
+ * @class CKEDITOR.plugins.autocomplete.matchInfo
1633
+ * @abstract
1634
+ * @since 4.10.0
1635
+ */
1636
+
1637
+ /**
1638
+ * The query string that was accepted by the
1639
+ * {@link CKEDITOR.plugins.autocomplete.configDefinition#textTestCallback config.textTestCallback}.
1640
+ *
1641
+ * @property {String} query
1642
+ */
1643
+
1644
+ /**
1645
+ * The range in the DOM indicating the position of the {@link #query}.
1646
+ *
1647
+ * @property {CKEDITOR.dom.range} range
1648
+ */
1649
+
1650
+ /**
1651
+ * The {@link CKEDITOR.plugins.autocomplete Autocomplete} instance that matched the query.
1652
+ *
1653
+ * @property {CKEDITOR.plugins.autocomplete} autocomplete
1654
+ */
1655
+ } )();