summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/res/smw/suggester/ext.smw.suggester.textInput.js
blob: 74b0c9c986d4616c80336586820304661e0d2845 (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
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
/*!
 * This file is part of the Semantic MediaWiki
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @license GNU GPL v2+
 * @since  3.0
 *
 * @author mwjames
 */
( function( $, mw, smw ) {
	'use strict';

	/**
	 * Only when the registered marker are used will the search be activated
	 * to avoid arbitrary matches for non-semantic content.
	 */
	mw.loader.using( [ 'ext.smw.suggester' ], function() {

		/**
		 * Support text input on Special:Search
		 *
		 * @since 3.0
		 */
		var search = function() {

			var context = $( '#searchText > input' ),
				isHidden = false;

			if ( context.length ) {

				// This features is only enabled for SMWSearch hence when the input
				// field contains [[ ... ]] we assume a search via the QueryEngine
				// therefore disable the standard highlighlter as no meaningfull
				// input help will be shown
				context.on( 'keyup keypres mouseenter', function( e ) {

					// MW 1.27 - MW 1.31
					var highlighter = context.parent().find( '.oo-ui-widget' );

					// MW 1.32+
					if ( highlighter.length == 0 ) {
						highlighter = $( '.oo-ui-defaultOverlay > .oo-ui-widget' );
					};

					// Disable (hide) the MW's search input highlighter
					if ( context.val().search( /\[|\[\[|in:|not:|has:|phrase:|::/gi ) > -1 ) {
						highlighter.hide();
						isHidden = true;
					} else if( isHidden ) {
						isHidden = false;
						highlighter.show();
					};
				} );

				var entitySuggester = smw.Factory.newEntitySuggester(
					context
				);

				// Register default tokens
				entitySuggester.registerDefaultTokenList(
					[
						'property',
						'concept',
						'category'
					]
				);

				entitySuggester.registerTokenDefinition(
					'property',
					{
						token: 'has:?',
						beforeInsert: function( token, value ) {
							return value.replace( '?', '' );
						}
					}
				);
			};
		};

		/**
		 * Support text input on Special:Upload
		 *
		 * @since 3.0
		 */
		var upload = function() {

			var context = $( '#wpUploadDescription' );

			if ( context.length ) {

				var entitySuggester= smw.Factory.newEntitySuggester(
				context
				);

				// Register autocomplete default tokens
				entitySuggester.registerDefaultTokenList(
					[
						'property',
						'concept',
						'category'
					]
				);
			};
		};

		/**
		 * Support text input on the wikiEditor textbox
		 *
		 * @since 3.0
		 */
		var wikiEditor = function() {

			var wpTextbox1 = $( '#wpTextbox1' );

			// Attach to the textarea
			if ( wpTextbox1.length ) {

				var entitySuggester = smw.Factory.newEntitySuggester(
					wpTextbox1
				);

				// Register default tokens
				entitySuggester.registerDefaultTokenList(
					[
						'property',
						'concept',
						'category'
					]
				);

				// Register additional definition since the editing can involve
				// different patterns

				// Used in combination with printouts as in:
				//
				// {{#ask: ..
				//  |?p: ...
				// }}
				entitySuggester.registerTokenDefinition(
					'property',
					{
						token: '?p:',
						beforeInsert: function( token, value ) {
							return value.replace( 'p:', '' );
						}
					}
				);

				// Used in combination with #set and #subobject as in:
				//
				// {{#set:
				//  |p: ...
				// }}
				entitySuggester.registerTokenDefinition(
					'property',
					{
						token: '|p:',
						beforeInsert: function( token, value ) {
							return value.replace( 'p:', '' ) + '=';
						}
					}
				);
			};
		};

		function load( callback ) {
			if ( document.readyState == 'complete' ) {
				callback();
			} else {
				window.addEventListener( 'load', callback );
			}
		}

		// Only load when it is Special:Search and the search type supports
		// https://www.semantic-mediawiki.org/wiki/Help:SMWSearch
		if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Search' && mw.config.get( 'wgSearchType' ) == 'SMWSearch' ) {
			load( search );
		};

		if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Upload' ) {
			load( upload );
		};
			
		var wgAction = mw.config.get( 'wgAction' );

		if ( ( wgAction == 'edit' || wgAction == 'submit' ) && mw.config.get( 'wgPageContentModel' ) == 'wikitext'  ) {
			load( wikiEditor );
		};
	} );

} )( jQuery, mediaWiki, semanticMediaWiki );