-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
view.js
249 lines (192 loc) · 7.03 KB
/
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
/* bender-tags: editor */
/* bender-ckeditor-plugins: autocomplete, caretposition */
( function() {
bender.editors = {
classic: {
name: 'editor1'
},
inline: {
name: 'editor2',
creator: 'inline'
}
};
bender.test( {
'test create element': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor );
assertViewElement( editor, view.createElement() );
},
'test append': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor );
view.append();
assert.areSame( CKEDITOR.document, view.document );
assertViewElement( editor, view.element );
},
'test open': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor );
view.append();
view.open();
assert.isTrue( view.element.hasClass( 'cke_autocomplete_opened' ) );
},
'test close': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor );
view.append();
view.open();
view.close();
assert.isFalse( view.element.hasClass( 'cke_autocomplete_opened' ) );
},
'test create item': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
item = { id: 1, name: 'item' };
assertItemElement( item, view.createItem( item ) );
},
'test get caret rect (classic)': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
position = { top: 2, height: 3, left: 4 },
caretPositionStub = sinon.stub( CKEDITOR.dom.selection.prototype, 'getCaretPosition' ).returns( position ),
editableStub = sinon.stub( CKEDITOR.editable.prototype, 'getParent' ).returns( {
getDocumentPosition: function() {
return {
y: 2,
x: 4
};
}
} );
var rect = view.getCaretRect();
assert.areEqual( 7, rect.bottom );
assert.areEqual( 8, rect.left );
assert.areEqual( 4, rect.top );
// Test fixture down
caretPositionStub.restore();
editableStub.restore();
},
'test get caret rect (inline)': function() {
var editor = this.editors.inline,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
position = { top: 2, height: 3, left: 4 },
caretPositionStub = sinon.stub( CKEDITOR.dom.selection.prototype, 'getCaretPosition' ).returns( position ),
windowStub = sinon.stub( CKEDITOR.document, 'getWindow' ).returns( {
getScrollPosition: function() {
return {
y: 2,
x: 4
};
}
} );
var rect = view.getCaretRect();
assert.areEqual( 7, rect.bottom );
assert.areEqual( 8, rect.left );
assert.areEqual( 4, rect.top );
// Test fixture down
caretPositionStub.restore();
windowStub.restore();
},
'test is item element': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
item = { id: 1, name: 'item' },
itemElement = view.createItem( item );
assert.isTrue( view.isItemElement( itemElement ) );
assert.isFalse( view.isItemElement( CKEDITOR.document.createElement( 'li' ) ) );
},
'test update items': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
items = [ { id: 1, name: 'item1' }, { id: 2, name: 'item2' } ];
view.append();
view.updateItems( items );
assertItemElement( items[ 0 ], view.getItemById( items[ 0 ].id ) );
assertItemElement( items[ 1 ], view.getItemById( items[ 1 ].id ) );
},
'test select item': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
spy = sinon.spy( view, 'scrollElementTo' ),
item1 = { id: 1, name: 'item1' },
item2 = { id: 2, name: 'item2' },
items = [ item1, item2 ];
view.append();
view.updateItems( items );
view.selectedItemId = item1.id;
view.selectItem( item2.id );
assert.areEqual( item2.id, view.selectedItemId );
assert.isTrue( view.getItemById( item2.id ).hasClass( 'cke_autocomplete_selected' ) );
assert.isTrue( spy.calledOnce );
},
'test set position above inside absolute rect': function() {
assertPanePosition( this.editors.classic, {
caretRect: { top: 400, bottom: 410, left: 100 },
editorViewportRect: { top: 0, bottom: 500 },
viewPanelHeight: 100
}, function( element ) {
assert.areEqual( '300px', element.getStyle( 'top' ) );
assert.areEqual( '100px', element.getStyle( 'left' ) );
} );
},
'test set position below inside absolute rect': function() {
assertPanePosition( this.editors.classic, {
caretRect: { top: 100, bottom: 110, left: 50 },
editorViewportRect: { top: 0, bottom: 500 },
viewPanelHeight: 100
}, function( element ) {
assert.areEqual( '110px', element.getStyle( 'top' ) );
assert.areEqual( '50px', element.getStyle( 'left' ) );
} );
},
'test set position above outside absolute rect': function() {
assertPanePosition( this.editors.classic, {
caretRect: { top: 400, bottom: 410, left: 100 },
editorViewportRect: { top: 0, bottom: 300 },
viewPanelHeight: 100
}, function( element ) {
assert.areEqual( '300px', element.getStyle( 'top' ) );
assert.areEqual( '100px', element.getStyle( 'left' ) );
} );
},
'test set position below outside absolute rect': function() {
assertPanePosition( this.editors.classic, {
caretRect: { top: 100, bottom: 110, left: 50 },
editorViewportRect: { top: 200, bottom: 500 },
viewPanelHeight: 100
}, function( element ) {
assert.areEqual( '200px', element.getStyle( 'top' ) );
assert.areEqual( '50px', element.getStyle( 'left' ) );
} );
},
'test attach': function() {
var editor = this.editors.classic,
view = new CKEDITOR.plugins.autocomplete.view( editor ),
spy = sinon.spy( view, 'fire' ),
item1 = { id: 1, name: 'item1' },
item2 = { id: 2, name: 'item2' },
items = [ item1, item2 ];
view.append();
view.updateItems( items );
view.attach();
view.element.fire( 'click', new CKEDITOR.dom.event( { target: view.getItemById( item2.id ).$ } ) );
assert.isTrue( spy.calledWith( 'click-item', item2.id.toString() ) );
}
} );
function assertPanePosition( editor, config, callback ) {
var view = new CKEDITOR.plugins.autocomplete.view( editor ),
elementStub = sinon.stub( CKEDITOR.dom.element.prototype, 'getAbsoluteClientRect' ).returns( config.editorViewportRect );
view.append();
sinon.stub( view.element, 'getSize' ).returns( config.viewPanelHeight );
view.setPosition( config.caretRect );
callback( view.element );
// Test fixture down
elementStub.restore();
}
function assertViewElement( editor, element ) {
var zIndex = editor.config.baseFloatZIndex - 3;
assert.areEqual( '<ul class="cke_autocomplete_panel" style="z-index: ' + zIndex + ';"></ul>', element.$.outerHTML );
}
function assertItemElement( item, itemElement ) {
assert.areEqual( '<li data-id="' + item.id + '">' + item.name + '</li>', itemElement.$.outerHTML );
}
} )();