-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathsuggest.js
619 lines (513 loc) · 17.9 KB
/
suggest.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// This file contains javascript associated with a autosuggest control
function smc_AutoSuggest(oOptions)
{
this.opt = oOptions;
// Store the handle to the text box.
this.oTextHandle = document.getElementById(this.opt.sControlId);
this.oRealTextHandle = null;
this.oSuggestDivHandle = null;
this.sLastSearch = '';
this.sLastDirtySearch = '';
this.oSelectedDiv = null;
this.aCache = [];
this.aDisplayData = [];
this.sRetrieveURL = 'sRetrieveURL' in this.opt ? this.opt.sRetrieveURL : '%scripturl%action=suggest;suggest_type=%suggest_type%;search=%search%;%sessionVar%=%sessionID%;xml;time=%time%';
// How many objects can we show at once?
this.iMaxDisplayQuantity = 'iMaxDisplayQuantity' in this.opt ? this.opt.iMaxDisplayQuantity : 15;
// How many characters shall we start searching on?
this.iMinimumSearchChars = 'iMinimumSearchChars' in this.opt ? this.opt.iMinimumSearchChars : 3;
// Should selected items be added to a list?
this.bItemList = 'bItemList' in this.opt ? this.opt.bItemList : false;
// Are there any items that should be added in advance?
this.aListItems = 'aListItems' in this.opt ? this.opt.aListItems : [];
this.sItemTemplate = 'sItemTemplate' in this.opt ? this.opt.sItemTemplate : '<input type="hidden" name="%post_name%[]" value="%item_id%"><a href="%item_href%" class="extern" onclick="window.open(this.href, \'_blank\'); return false;">%item_name%</a> <span class="main_icons delete" title="%delete_text%" onclick="return %self%.deleteAddedItem(%item_id%);"></span>';
this.sTextDeleteItem = 'sTextDeleteItem' in this.opt ? this.opt.sTextDeleteItem : '';
this.oCallback = {};
this.bDoAutoAdd = false;
this.iItemCount = 0;
this.oHideTimer = null;
this.bPositionComplete = false;
// Just make sure the page is loaded before calling the init.
addLoadEvent(this.opt.sSelf + '.init();');
}
smc_AutoSuggest.prototype.init = function()
{
// Create a div that'll contain the results later on.
this.oSuggestDivHandle = document.createElement('div');
this.oSuggestDivHandle.className = 'auto_suggest_div';
document.body.appendChild(this.oSuggestDivHandle);
// Create a backup text input.
this.oRealTextHandle = document.createElement('input');
this.oRealTextHandle.type = 'hidden';
this.oRealTextHandle.name = this.oTextHandle.name;
this.oRealTextHandle.value = this.oTextHandle.value;
this.oTextHandle.form.appendChild(this.oRealTextHandle);
// Disable autocomplete in any browser by obfuscating the name.
this.oTextHandle.name = 'dummy_' + Math.floor(Math.random() * 1000000);
this.oTextHandle.autocomplete = 'off';
this.oTextHandle.instanceRef = this;
var fOnKeyDown = function (oEvent) {
return this.instanceRef.handleKey(oEvent);
};
is_opera ? this.oTextHandle.onkeypress = fOnKeyDown : this.oTextHandle.onkeydown = fOnKeyDown;
this.oTextHandle.onkeyup = function (oEvent) {
return this.instanceRef.autoSuggestUpdate(oEvent);
};
this.oTextHandle.onchange = function (oEvent) {
return this.instanceRef.autoSuggestUpdate(oEvent);
};
this.oTextHandle.onblur = function (oEvent) {
return this.instanceRef.autoSuggestHide(oEvent);
};
this.oTextHandle.onfocus = function (oEvent) {
return this.instanceRef.autoSuggestUpdate(oEvent);
};
if (this.bItemList)
{
if ('sItemListContainerId' in this.opt)
this.oItemList = document.getElementById(this.opt.sItemListContainerId);
else
{
this.oItemList = document.createElement('div');
this.oTextHandle.parentNode.insertBefore(this.oItemList, this.oTextHandle.nextSibling);
}
}
if (this.aListItems.length > 0)
for (var i = 0, n = this.aListItems.length; i < n; i++)
this.addItemLink(this.aListItems[i].sItemId, this.aListItems[i].sItemName);
return true;
}
// Was it an enter key - if so assume they are trying to select something.
smc_AutoSuggest.prototype.handleKey = function(oEvent)
{
// Grab the event object, one way or the other
if (!oEvent)
oEvent = window.event;
// Get the keycode of the key that was pressed.
var iKeyPress = 0;
if ('keyCode' in oEvent)
iKeyPress = oEvent.keyCode;
else if ('which' in oEvent)
iKeyPress = oEvent.which;
switch (iKeyPress)
{
// Tab.
case 9:
if (this.aDisplayData.length > 0)
{
if (this.oSelectedDiv != null)
this.itemClicked(this.oSelectedDiv);
else
this.handleSubmit();
}
// Continue to the next control.
return true;
break;
// Enter.
case 13:
if (this.aDisplayData.length > 0 && this.oSelectedDiv != null)
{
this.itemClicked(this.oSelectedDiv);
// Do our best to stop it submitting the form!
return false;
}
else
return true;
break;
// Up/Down arrow?
case 38:
case 40:
if (this.aDisplayData.length && this.oSuggestDivHandle.style.visibility != 'hidden')
{
// Loop through the display data trying to find our entry.
var bPrevHandle = false;
var oToHighlight = null;
for (var i = 0; i < this.aDisplayData.length; i++)
{
// If we're going up and yet the top one was already selected don't go around.
if (this.oSelectedDiv != null && this.oSelectedDiv == this.aDisplayData[i] && i == 0 && iKeyPress == 38)
{
oToHighlight = this.oSelectedDiv;
break;
}
// If nothing is selected and we are going down then we select the first one.
if (this.oSelectedDiv == null && iKeyPress == 40)
{
oToHighlight = this.aDisplayData[i];
break;
}
// If the previous handle was the actual previously selected one and we're hitting down then this is the one we want.
if (bPrevHandle != false && bPrevHandle == this.oSelectedDiv && iKeyPress == 40)
{
oToHighlight = this.aDisplayData[i];
break;
}
// If we're going up and this is the previously selected one then we want the one before, if there was one.
if (bPrevHandle != false && this.aDisplayData[i] == this.oSelectedDiv && iKeyPress == 38)
{
oToHighlight = bPrevHandle;
break;
}
// Make the previous handle this!
bPrevHandle = this.aDisplayData[i];
}
// If we don't have one to highlight by now then it must be the last one that we're after.
if (oToHighlight == null)
oToHighlight = bPrevHandle;
// Remove any old highlighting.
if (this.oSelectedDiv != null)
this.itemMouseOut(this.oSelectedDiv);
// Mark what the selected div now is.
this.oSelectedDiv = oToHighlight;
this.itemMouseOver(this.oSelectedDiv);
}
break;
}
return true;
}
// Functions for integration.
smc_AutoSuggest.prototype.registerCallback = function(sCallbackType, sCallback)
{
switch (sCallbackType)
{
case 'onBeforeAddItem':
this.oCallback.onBeforeAddItem = sCallback;
break;
case 'onAfterAddItem':
this.oCallback.onAfterAddItem = sCallback;
break;
case 'onAfterDeleteItem':
this.oCallback.onAfterDeleteItem = sCallback;
break;
case 'onBeforeUpdate':
this.oCallback.onBeforeUpdate = sCallback;
break;
}
}
// User hit submit?
smc_AutoSuggest.prototype.handleSubmit = function()
{
var bReturnValue = true;
var oFoundEntry = null;
// Do we have something that matches the current text?
for (var i = 0; i < this.aCache.length; i++)
{
if (this.sLastSearch.toLowerCase() == this.aCache[i].sItemName.toLowerCase().substr(0, this.sLastSearch.length))
{
// Exact match?
if (this.sLastSearch.length == this.aCache[i].sItemName.length)
{
// This is the one!
oFoundEntry = {
sItemId: this.aCache[i].sItemId,
sItemName: this.aCache[i].sItemName
};
break;
}
// Not an exact match, but it'll do for now.
else
{
// If we have two matches don't find anything.
if (oFoundEntry != null)
bReturnValue = false;
else
oFoundEntry = {
sItemId: this.aCache[i].sItemId,
sItemName: this.aCache[i].sItemName
};
}
}
}
if (oFoundEntry == null || bReturnValue == false)
return bReturnValue;
else
{
this.addItemLink(oFoundEntry.sItemId, oFoundEntry.sItemName, true);
return false;
}
}
// Positions the box correctly on the window.
smc_AutoSuggest.prototype.positionDiv = function()
{
// Only do it once.
if (this.bPositionComplete)
return true;
this.bPositionComplete = true;
// Put the div under the text box.
var aParentPos = smf_itemPos(this.oTextHandle);
this.oSuggestDivHandle.style.left = aParentPos[0] + 'px';
this.oSuggestDivHandle.style.top = (aParentPos[1] + this.oTextHandle.offsetHeight) + 'px';
this.oSuggestDivHandle.style.width = this.oTextHandle.style.width;
return true;
}
// Do something after clicking an item.
smc_AutoSuggest.prototype.itemClicked = function(oCurElement)
{
// Is there a div that we are populating?
if (this.bItemList)
this.addItemLink(oCurElement.sItemId, oCurElement.innerHTML);
// Otherwise clear things down.
else
this.oTextHandle.value = oCurElement.innerHTML.php_unhtmlspecialchars();
this.oRealTextHandle.value = this.oTextHandle.value;
this.autoSuggestActualHide();
this.oSelectedDiv = null;
}
// Remove the last searched for name from the search box.
smc_AutoSuggest.prototype.removeLastSearchString = function ()
{
// Remove the text we searched for from the div.
var sTempText = this.oTextHandle.value.toLowerCase();
var iStartString = sTempText.indexOf(this.sLastSearch.toLowerCase());
// Just attempt to remove the bits we just searched for.
if (iStartString != -1)
{
while (iStartString > 0)
{
if (sTempText.charAt(iStartString - 1) == '"' || sTempText.charAt(iStartString - 1) == ',' || sTempText.charAt(iStartString - 1) == ' ')
{
iStartString--;
if (sTempText.charAt(iStartString - 1) == ',')
break;
}
else
break;
}
// Now remove anything from iStartString upwards.
this.oTextHandle.value = this.oTextHandle.value.substr(0, iStartString);
}
// Just take it all.
else
this.oTextHandle.value = '';
}
// Add a result if not already done.
smc_AutoSuggest.prototype.addItemLink = function (sItemId, sItemName, bFromSubmit)
{
// Increase the internal item count.
this.iItemCount ++;
// If there's a callback then call it.
if ('oCallback' in this && 'onBeforeAddItem' in this.oCallback && typeof(this.oCallback.onBeforeAddItem) == 'string')
{
// If it returns false the item must not be added.
if (!eval(this.oCallback.onBeforeAddItem + '(' + this.opt.sSelf + ', \'' + sItemId + '\');'))
return;
}
var oNewDiv = document.createElement('div');
oNewDiv.id = 'suggest_' + this.opt.sSuggestId + '_' + sItemId;
setInnerHTML(oNewDiv, this.sItemTemplate.replace(/%post_name%/g, this.opt.sPostName).replace(/%item_id%/g, sItemId).replace(/%item_href%/g, smf_prepareScriptUrl(smf_scripturl) + this.opt.sURLMask.replace(/%item_id%/g, sItemId)).replace(/%item_name%/g, sItemName).replace(/%images_url%/g, smf_images_url).replace(/%self%/g, this.opt.sSelf).replace(/%delete_text%/g, this.sTextDeleteItem));
this.oItemList.appendChild(oNewDiv);
// If there's a registered callback, call it.
if ('oCallback' in this && 'onAfterAddItem' in this.oCallback && typeof(this.oCallback.onAfterAddItem) == 'string')
eval(this.oCallback.onAfterAddItem + '(' + this.opt.sSelf + ', \'' + oNewDiv.id + '\', ' + this.iItemCount + ');');
// Clear the div a bit.
this.removeLastSearchString();
// If we came from a submit, and there's still more to go, turn on auto add for all the other things.
this.bDoAutoAdd = this.oTextHandle.value != '' && bFromSubmit;
// Update the fellow..
this.autoSuggestUpdate();
}
// Delete an item that has been added, if at all?
smc_AutoSuggest.prototype.deleteAddedItem = function (sItemId)
{
var oDiv = document.getElementById('suggest_' + this.opt.sSuggestId + '_' + sItemId);
// Remove the div if it exists.
if (typeof(oDiv) == 'object' && oDiv != null)
{
oDiv.parentNode.removeChild(document.getElementById('suggest_' + this.opt.sSuggestId + '_' + sItemId));
// Decrease the internal item count.
this.iItemCount --;
// If there's a registered callback, call it.
if ('oCallback' in this && 'onAfterDeleteItem' in this.oCallback && typeof(this.oCallback.onAfterDeleteItem) == 'string')
eval(this.oCallback.onAfterDeleteItem + '(' + this.opt.sSelf + ', ' + this.iItemCount + ');');
}
return false;
}
// Hide the box.
smc_AutoSuggest.prototype.autoSuggestHide = function ()
{
// Delay to allow events to propogate through....
this.oHideTimer = setTimeout(this.opt.sSelf + '.autoSuggestActualHide();', 250);
}
// Do the actual hiding after a timeout.
smc_AutoSuggest.prototype.autoSuggestActualHide = function()
{
this.oSuggestDivHandle.style.display = 'none';
this.oSuggestDivHandle.style.visibility = 'hidden';
this.oSelectedDiv = null;
}
// Show the box.
smc_AutoSuggest.prototype.autoSuggestShow = function()
{
if (this.oHideTimer)
{
clearTimeout(this.oHideTimer);
this.oHideTimer = false;
}
this.positionDiv();
this.oSuggestDivHandle.style.visibility = 'visible';
this.oSuggestDivHandle.style.display = '';
}
// Populate the actual div.
smc_AutoSuggest.prototype.populateDiv = function(aResults)
{
// Cannot have any children yet.
while (this.oSuggestDivHandle.childNodes.length > 0)
{
// Tidy up the events etc too.
this.oSuggestDivHandle.childNodes[0].onmouseover = null;
this.oSuggestDivHandle.childNodes[0].onmouseout = null;
this.oSuggestDivHandle.childNodes[0].onclick = null;
this.oSuggestDivHandle.removeChild(this.oSuggestDivHandle.childNodes[0]);
}
// Something to display?
if (typeof(aResults) == 'undefined')
{
this.aDisplayData = [];
return false;
}
var aNewDisplayData = [];
for (var i = 0; i < (aResults.length > this.iMaxDisplayQuantity ? this.iMaxDisplayQuantity : aResults.length); i++)
{
// Create the sub element
var oNewDivHandle = document.createElement('div');
oNewDivHandle.sItemId = aResults[i].sItemId;
oNewDivHandle.className = 'auto_suggest_item';
oNewDivHandle.innerHTML = aResults[i].sItemName;
//oNewDivHandle.style.width = this.oTextHandle.style.width;
this.oSuggestDivHandle.appendChild(oNewDivHandle);
// Attach some events to it so we can do stuff.
oNewDivHandle.instanceRef = this;
oNewDivHandle.onmouseover = function (oEvent)
{
this.instanceRef.itemMouseOver(this);
}
oNewDivHandle.onmouseout = function (oEvent)
{
this.instanceRef.itemMouseOut(this);
}
oNewDivHandle.onclick = function (oEvent)
{
this.instanceRef.itemClicked(this);
}
aNewDisplayData[i] = oNewDivHandle;
}
this.aDisplayData = aNewDisplayData;
return true;
}
// Refocus the element.
smc_AutoSuggest.prototype.itemMouseOver = function (oCurElement)
{
this.oSelectedDiv = oCurElement;
oCurElement.className = 'auto_suggest_item_hover';
}
// Onfocus the element
smc_AutoSuggest.prototype.itemMouseOut = function (oCurElement)
{
oCurElement.className = 'auto_suggest_item';
}
smc_AutoSuggest.prototype.onSuggestionReceived = function (oXMLDoc)
{
var sQuoteText = '';
var aItems = oXMLDoc.getElementsByTagName('item');
this.aCache = [];
for (var i = 0; i < aItems.length; i++)
{
this.aCache[i] = {
sItemId: aItems[i].getAttribute('id'),
sItemName: aItems[i].childNodes[0].nodeValue
};
// If we're doing auto add and we find the exact person, then add them!
if (this.bDoAutoAdd && this.sLastSearch == this.aCache[i].sItemName)
{
var oReturnValue = {
sItemId: this.aCache[i].sItemId,
sItemName: this.aCache[i].sItemName
};
this.aCache = [];
return this.addItemLink(oReturnValue.sItemId, oReturnValue.sItemName, true);
}
}
// Check we don't try to keep auto updating!
this.bDoAutoAdd = false;
// Populate the div.
this.populateDiv(this.aCache);
// Make sure we can see it - if we can.
if (aItems.length == 0)
this.autoSuggestHide();
else
this.autoSuggestShow();
return true;
}
// Get a new suggestion.
smc_AutoSuggest.prototype.autoSuggestUpdate = function ()
{
// If there's a callback then call it.
if ('onBeforeUpdate' in this.oCallback && typeof(this.oCallback.onBeforeUpdate) == 'string')
{
// If it returns false the item must not be added.
if (!eval(this.oCallback.onBeforeUpdate + '(' + this.opt.sSelf + ');'))
return false;
}
this.oRealTextHandle.value = this.oTextHandle.value;
if (isEmptyText(this.oTextHandle))
{
this.aCache = [];
this.populateDiv();
this.autoSuggestHide();
return true;
}
// Nothing changed?
if (this.oTextHandle.value == this.sLastDirtySearch)
return true;
this.sLastDirtySearch = this.oTextHandle.value;
// We're only actually interested in the last string.
var sSearchString = this.oTextHandle.value.replace(/^("[^"]+",[ ]*)+/, '').replace(/^([^,]+,[ ]*)+/, '');
if (sSearchString.substr(0, 1) == '"')
sSearchString = sSearchString.substr(1);
// Stop replication ASAP.
var sRealLastSearch = this.sLastSearch;
this.sLastSearch = sSearchString;
// Either nothing or we've completed a sentance.
if (sSearchString == '' || sSearchString.substr(sSearchString.length - 1) == '"')
{
this.populateDiv();
return true;
}
// Nothing?
if (sRealLastSearch == sSearchString)
return true;
// Too small?
else if (sSearchString.length < this.iMinimumSearchChars)
{
this.aCache = [];
this.autoSuggestHide();
return true;
}
else if (sSearchString.substr(0, sRealLastSearch.length) == sRealLastSearch)
{
// Instead of hitting the server again, just narrow down the results...
var aNewCache = [];
var j = 0;
var sLowercaseSearch = sSearchString.toLowerCase();
for (var k = 0; k < this.aCache.length; k++)
{
if (this.aCache[k].sItemName.substr(0, sSearchString.length).toLowerCase() == sLowercaseSearch)
aNewCache[j++] = this.aCache[k];
}
this.aCache = [];
if (aNewCache.length != 0)
{
this.aCache = aNewCache;
// Repopulate.
this.populateDiv(this.aCache);
// Check it can be seen.
this.autoSuggestShow();
return true;
}
}
// Clean the text handle.
sSearchString = sSearchString.php_to8bit().php_urlencode();
// Get the document.
sendXMLDocument.call(this, this.sRetrieveURL.replace(/%scripturl%/g, smf_prepareScriptUrl(smf_scripturl)).replace(/%suggest_type%/g, this.opt.sSearchType).replace(/%search%/g, sSearchString).replace(/%sessionVar%/g, this.opt.sSessionVar).replace(/%sessionID%/g, this.opt.sSessionId).replace(/%time%/g, new Date().getTime()), '', this.onSuggestionReceived);
return true;
}