summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/res/smw/special.search/search.form.js
blob: 9a525d7ab151be2bdc16e556c25a083fc1eb755b (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
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
/*!
 * @license GNU GPL v2+
 * @since  3.0
 *
 * @author mwjames
 */
( function( $, mw, smw ) {
	'use strict';

	/**
	 * Support extended form in Special:Search
	 *
	 * @since 3.0
	 */
	var form = function() {

		var namespaces = [];
		var ui = new smw.ui();

		// Empty value, inject a hidden non visible char to allow to trigger
		// the search without an input text
		if ( $( '#searchText > input' ).val() === '' ) {
			$('#search').append('<input type="hidden" name="search" value=" " id="search-hidden" />' );
		};

		// If a users enters some data, remove the hidden field when it is not
		// empty.
		$( document ).on( "change keyup", "#searchText > input", function() {
			if ( $( '#searchText > input' ).val() !== '' ) {
				$( "#search-hidden" ).remove();
			} else {
				$('#search').append('<input type="hidden" name="search" value=" " id="search-hidden" />' );
			}
		} );

		$( document ).on( "click", "#smw-search-sort", function(){

			var that = $( this );
			var opts = {
				eSelect: function( data ) {
					if( data && data.length ) {
						that.text( data[0].name );
						that.prop( 'value', data[0].id );
						that.parent().find( 'input[name^=sort]' ).prop( 'value', data[0].id );
						that.trigger( 'change' );
					};
				}
			}

			ui.selectMenu( that, opts );
		} );

		$( document ).on( "click", "#smw-search-forms", function(){

			var that = $( this );
			var opts = {
				label: 'Form',
				search: true,
				orderBy: 'name',
				eSelect: function( data ) {
					if( data && data.length ) {
						that.text( data[0].name );
						that.prop( 'value', data[0].id );
						that.parent().find( 'input[name^=smw-form]' ).prop( 'value', data[0].id );
						that.trigger( 'change' );
					};
				}
			}

			ui.selectMenu( that, opts );
		} );

		$( document ).ready( function() {

			/**
			 * Open form ...
			 */
			$( '#smw-form-open > .smw-input-group' ).each( function() {
				var context = $( this );

				if ( $( "#smw-search-forms select" ).val() === 'open' ) {
					context.find( '.smw-input-field' ).show();
					context.find( '.smw-select-field' ).show();
					context.find( '.smw-button-field' ).show();
				};

				if ( context.find( '.smw-property-input' ).val() === '' ) {
					context.find( '.smw-propertyvalue-input' ).addClass( 'is-disabled' );
				} else {
					context.find( '.smw-propertyvalue-input' ).removeClass( 'is-disabled' );
				};

				if ( context.find( '.smw-propertyvalue-input' ).val() === '' ) {
					context.find( '.smw-propertyvalue-input' ).addClass( 'is-disabled' );
				}
			} )

			/**
			 * Act on changes to the form select
			 */
			$( this ).on( "change", ".smw-select-field", function( event ) {
				var context = $( this ).closest( '.smw-input-group' );
				var length = $( '#smw-form-open .smw-input-group' ) ? $( '#smw-form-open .smw-input-group' ).length : 0;

				// Property input empty, make the value field appear to be disabled
				if ( context.find( '.smw-property-input' ).val() === '' ) {
					context.find( '.smw-propertyvalue-input' ).val( '' );
					context.find( '.smw-propertyvalue-input' ).addClass( 'is-disabled' );
				}

				// Act on the `del` request but only for as long as one group remains
				if ( length > 1 && context.find( '.smw-select-field' ).val() === 'del' ) {
					context.remove();
				}

				// Disable `del` when there are not enough groups available
				if ( length <= 2 ) {
					$( "#smw-form-open > .smw-input-group > .smw-select-field option[value='del']" ).attr( "disabled", "disabled" );
				}
			} );

			/**
			 * Listing to the property select complete event on a field that uses
			 * the autocomplete.
			 */
			$( "#smw-form-open" ).on( "smw.autocomplete.property.select.complete", function( event, opts ) {

				var context = $( this );
				var input = opts.context.closest( '.smw-input-group' ).find( '.smw-propertyvalue-input' );

				// A property was "really" selected! enable the value field
				if ( opts.suggestion && opts.suggestion.value !== '' ) {
					input.removeClass( 'is-disabled' );
					input.data( 'property', opts.suggestion.value );

					// Trigger event to initialize the instance
					context.trigger( 'smw.autocomplete.propertyvalue', { context: opts.context.closest( '.smw-input-group' ) } );
				}
			} );

			/**
			 * Listing to the last property value complete event on a field that
			 * uses the autocomplete and decide whether to clone and create an
			 * empty group.
			 */
			$( "#smw-form-open" ).on( "smw.autocomplete.propertyvalue.select.complete", function( event, opts ) {
				var context = opts.context.closest( '.smw-input-group' );
				var last = $( ".smw-input-group" ).last();

				// Only clone when the autocomplete contains a term and is the last
				// property input value is not empty to avoid cloning empty lines from
				// a repeated confirmation of the same property value autocompletion
				// request
				var hasValues = last.find( '.smw-property-input' ).val() !== '' && last.last().find( '.smw-propertyvalue-input' ).val();

				if ( opts.suggestion && opts.suggestion.value !== '' && hasValues ) {

					// Clone the entire line for a new input
					var clone = context.clone();
					clone.find( "input:text" ).val( "" ).end().appendTo( '#smw-form-open' );

					// We clone which means we have +1 available
					$( "#smw-form-open > .smw-input-group > .smw-select-field option[value='del']" ).prop( "disabled", false );
					clone.find( '.smw-propertyvalue-input' ).addClass( 'is-disabled' );
					context.trigger( 'smw.autocomplete.property', { context: clone } );
				}
			} );

			var initialForm = $( '#smw-searchoptions' ).find( 'input[name^=smw-form]' ).prop( 'value' );

			/**
			 *  Listing to the form select field change
			 */
			$( this ).on( "change", "#smw-search-forms", function( event ) {

				var that = $( this );
				var type = $( '#smw-searchoptions' ).find( 'input[name^=smw-form]' ).prop( 'value' );
				var nsList = $( "#smw-search-forms" ).data( 'nslist' );

				if ( type !== '' ) {
					$( '#smw-searchoptions .divider' ).show();

					// Handle the hide/show of the NS section
					if ( $( '#ns-list' ).css( 'display' ) === 'block' && initialForm === '' ) {
						$( '#ns-list' ).css( 'display', 'none' );
						$( '#smw-search-togglensview input' ).prop( 'value', 'Show' );
						$( 'input[name=ns-list]' ).attr( 'value', 1 );
						initialForm = $( "#smw-search-forms select" ).val();
					} else {
						$( '#smw-search-togglensview input' ).prop(
							'value',
							( $( '#ns-list' ).css( 'display' ) !== 'none' ? 'Hide': 'Show' )
						);
					}
				} else {
					$( '#smw-searchoptions .divider' ).hide();
				}

				// On any change, hide all fields by default and only make fields
				// visible that belong the selected form.
				$( '.smw-input-field' ).hide();
				$( '.smw-select-field' ).hide();
				$( '.smw-button-field' ).hide();
				$( '.smw-form-description' ).hide();

				// Important! The browser will complain about require fields that
				// have been made invisible.
				$( '.smw-input-field' ).find( 'input' ).prop( 'required', false );

				if ( type === 'open' ) {
					var group = $( '#smw-form-open > .smw-input-group' );

					group.find( '.smw-input-field' ).show();
					group.find( '.smw-select-field' ).show();
					group.find( '.smw-button-field' ).show();
				};

				// Restore the NS settings for the once we have forcibly set
				namespaces.map( function( v, ns ) {

					if ( v === true ) {
						$( '#search input[id^=mw-search-ns' + ns + ']' ).prop( 'checked', false );
					};

					namespaces[ns] = false
				} );

				if ( type !== '' ) {

					var fields = $( '#smw-form-' + type + ' > .smw-input-field' )

					fields.show();
					$( '#smw-form-' + type + ' .smw-form-description' ).show();

					fields.each( function() {
						var required = $( this ).find( 'input' ).data( 'required' );

						if ( required ) {
							$( this ).find( 'input' ).prop( "required", true );
						};
					} );

					// Do we have some namespaces that we want to enforce via the
					// form definition?
					if ( nsList.hasOwnProperty( type ) ) {
						nsList[type].map( function( ns ) {
							var checkbox = $( '#search input[id^=mw-search-ns' + ns + ']' );

							// Already checked by a user? Do nothing!
							if ( checkbox.prop( 'checked' ) === false ) {
								namespaces[ns] = true;
								checkbox.prop( 'checked', true );
							};
						} );
					};
				}
			} );
		} );
	};

	// 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' ) {
		smw.load( form );
	};

} )( jQuery, mediaWiki, semanticMediaWiki );