forked from wbniv/foswiki
/
plugin.js
126 lines (116 loc) · 3.88 KB
/
plugin.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
/*
Copyright (c) 2012, Modell Aachen, http://modell-aachen.de/
All rights reserved.
*/
// THIRD-PARTY CODE for HTML autocomplete list {{{
/*
* jQuery UI Autocomplete HTML Extension
*
* Copyright 2010, Scott González (http://scottgonzalez.com)
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* http://github.com/scottgonzalez/jquery-ui-extensions
*/
(function( $ ) {
var proto = $.ui.autocomplete.prototype,
initSource = proto._initSource;
function filter( array, term ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
return $.grep( array, function(value) {
return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
});
}
$.extend( proto, {
_initSource: function() {
if ( this.options.html && $.isArray(this.options.source) ) {
this.source = function( request, response ) {
response( filter( this.options.source, request.term ) );
};
} else {
initSource.call( this );
}
},
_renderItem: function( ul, item) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( $( "<a></a>" )[ this.options.html ? "html" : "text" ]( item.label ) )
.appendTo( ul );
}
});
})( jQuery );
// }}}
CKEDITOR.plugins.add( 'qwikiautosuggest',
{
requires : ['dialogui'],
init : function( editor )
{
editor.element.getDocument().appendStyleSheet( CKEDITOR.getUrl(
'_source/' + // @Packager.RemoveLine
'plugins/qwikiautosuggest/css/autosuggest.css'
));
/*
* Add a new method qwikiautosuggest to UI elements in dialogs.
* Possible uses:
*
* element.qwikiautosuggest("init", options):
* Initialize autocompletion for this element. Options is an object
* that should contain at least a "source" element that is a
* URL or a function. The URL may contain the special '$query'
* keyword which will automatically be replaced with the string
* to be autocompleted.
*
* For other options, see the documentation for jQuery UI.
*
* element.qwikiautosuggest("url", url):
* Changes the source URL, implementing the expansion as described
* above.
*
* element.qwikiautosuggest(...):
* Any other invocation is transparently passed through to jQuery UI's
* autocomplete().
*/
CKEDITOR.tools.extend(CKEDITOR.ui.dialog.uiElement.prototype, {
qwikiautosuggest: function(method) {
var jq = $(this.getInputElement().$);
switch (method) {
case "init":
var opts = arguments[1];
// Required so that the list doesn't pop up behind the CKEditor BG overlay -jk
jq.css("z-index", $(this.getDialog().getElement().$.firstChild).css("z-index"));
jq.css("position", "relative");
if (opts.html !== false) opts.html = true;
jq.autocomplete(opts);
var source;
if (opts && typeof(opts.source) == "string")
this.qwikiautosuggest("url", opts.source);
break;
case "url":
var url = arguments[1];
jq.autocomplete("option", "source", function(req, resp) {
$.ajax({
'url': url.replace("$query", req.term),
dataType: 'json',
success: function(data) {
resp($.map(data, function(val) {
var o = {};
o.label = '<div class="autocomplete_label">'+val.label+'</div>'+
'<div class="autocomplete_sublabel">'+val.sublabel+'</div>';
o.value = val.value;
return o;
}));
},
error: function(){ resp([]); }
});
});
break;
default:
jq.autocomplete.apply(jq, arguments);
}
}
});
}
} );
CKEDITOR.qwikiautosuggest = {
topics: FoswikiCKE.getFoswikiVar("VIEWSCRIPTURL") + "/System/AjaxHelper?section=topic;contenttype=text/plain;skin=text;baseweb=all,-Trash*,-System,-TWiki,-Sandbox;input=$query",
attachments: FoswikiCKE.getFoswikiVar("VIEWSCRIPTURL") + "/System/AjaxHelper?section=attachment;contenttype=text/plain;skin=text;input=$query"
};