summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/res/smw/util/ext.smw.util.autocomplete.js
blob: a3f20c739b7b7812d7381cf3052d5fe1e5d92612 (plain)
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
/**
 * JavaScript for SMW autocomplete functionality
 *
 * @see https://www.mediawiki.org/wiki/Extension:Semantic_MediaWiki
 *
 * @since 1.8
 * @release 0.1
 *
 * @file
 * @ingroup SMW
 *
 * @licence GNU GPL v2 or later
 * @author mwjames
 */
/*global mediaWiki:true*/
( function( $, mw ) {
	'use strict';

	/**
	 * Default options
	 *
	 */
	var defaults = {
			limit: 10,
			separator: null,
			search: 'property',
			namespace: mw.config.get( 'wgNamespaceIds' ).property
	};

	/**
	 * Handle autocomplete function for various instances
	 *
	 * @var options
	 *
	 * @since: 1.8
	 */
	$.fn.smwAutocomplete = function( settings ){

		// Merge defaults and options
		var options = $.extend( {}, defaults, settings );

		// Specify regular expression
		var regex = new RegExp( options.separator , 'mi' );

		// Helper functions
		function split( val ) {
			return val.split( regex );
		}

		function extractLast( term ) {
			return split( term ).pop();
		}

		function escapeQuestion(term){
			if ( term.substring(0, 1) === "?" ) {
				return term.substring(1);
			} else {
				return term;
			}
		}

		// MW 1.24 changed ui versions
		var version = $.ui ? parseFloat( $.ui.version.substring( 2 ) ) : 1;

		if ( version >= 9 ) {
			// Extending jQuery functions for custom highligting
			$.ui.autocomplete( 'instance' )._renderItem = function( ul, item ) {
				var term_without_q = escapeQuestion(extractLast( this.term ));
				var re = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term_without_q.replace("/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi", "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi");
				var loc = item.label.search(re),
					t = '';
				if (loc >= 0) {
					t = item.label.substr(0, loc) + '<strong>' + item.label.substr(loc, term_without_q.length) + '</strong>' + item.label.substr(loc + term_without_q.length);
				} else {
					t = item.label;
				}
				return $( "<li>" ).append( "<a>" + t + "</a>" ).appendTo( ul );
			};

		} else{
			// Extending jQuery functions for custom highligting
			$.ui.autocomplete.prototype._renderItem = function( ul, item ) {
				var term_without_q = escapeQuestion(extractLast( this.term ));
				var re = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term_without_q.replace("/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi", "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi");
				var loc = item.label.search(re),
					t = '';
				if (loc >= 0) {
					t = item.label.substr(0, loc) + '<strong>' + item.label.substr(loc, term_without_q.length) + '</strong>' + item.label.substr(loc + term_without_q.length);
				} else {
					t = item.label;
				}
				$( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( " <a>" + t + "</a>" )
					.appendTo( ul );
			};
		}

		// Extending jquery functions for custom autocomplete matching
		$.extend( $.ui.autocomplete, {
			filter: function(array, term) {
				var matcher = new RegExp( "\\\b" + $.ui.autocomplete.escapeRegex(term), "i" );
				return $.grep( array, function(value) {
					return matcher.test( value.label || value.value || value );
				});
			}
		} );

		// Autocomplete core
		this.autocomplete( {
			minLength: 2,
			source: function(request, response) {
				$.getJSON(
					mw.config.get( 'wgScriptPath' ) + '/api.php',
					{
						'action': 'opensearch',
						'format': 'json',
						'limit': options.limit,
						'namespace': options.namespace ,
						'search': extractLast( request.term )
					},
					function( data ){

						if ( data.error === undefined ) {
							//remove the word 'Property:' from returned data
							if ( options.search === 'property' ){
								for( var i=0; i < data[1].length; i++ ) {
									data[1][i]= data[1][i].substr( data[1][i].indexOf( ':' ) + 1 );
								}
							}
							response( data[1] );
						} else {
							response ( false );
						}
					}
				);
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
				var terms = this.value;
				terms = split( terms );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push("");
				this.value = terms.join( options.separator !== null ? options.separator : '' );
				return false;
			}
		} );
	};

} )( jQuery, mediaWiki );