r114250 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114249‎ | r114250 | r114251 >
Date:10:58, 20 March 2012
Author:amire80
Status:ok
Tags:i18review 
Comment:
Refactoring ext.translate.special.pagetranslation.js to make it reusable: The generic autocompletion funcionality is in ext.translate.multiselectautocomplete.js and ext.translate.special.pagetranslation.js only applies it to Special:PageTranslation.
Modified paths:
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/resources/ext.translate.multiselectautocomplete.js (added) (history)
  • /trunk/extensions/Translate/resources/ext.translate.special.pagetranslation.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -249,11 +249,19 @@
250250 ),
251251 ) + $resourcePaths;
252252
 253+$wgResourceModules['ext.translate.multiselectautocomplete'] = array(
 254+ 'scripts' => 'resources/ext.translate.multiselectautocomplete.js',
 255+ 'dependencies' => array(
 256+ 'jquery.ui.autocomplete',
 257+ ),
 258+ 'position' => 'top',
 259+) + $resourcePaths;
 260+
253261 $wgResourceModules['ext.translate.special.pagetranslation'] = array(
254262 'scripts' => 'resources/ext.translate.special.pagetranslation.js',
255263 'styles' => 'resources/ext.translate.special.pagetranslation.css',
256264 'dependencies' => array(
257 - 'jquery.ui.autocomplete',
 265+ 'ext.translate.multiselectautocomplete',
258266 ),
259267 'position' => 'top',
260268 ) + $resourcePaths;
Index: trunk/extensions/Translate/resources/ext.translate.special.pagetranslation.js
@@ -7,76 +7,5 @@
88 * Credits: http://jqueryui.com/demos/autocomplete/#multiple
99 */
1010 jQuery( function( $ ) {
11 - "use strict"
12 -
13 - $.widget( "ui.multiselectautocomplete", {
14 - options: {
15 - inputbox: null, // a jQuery selector for the input box where selections are written.
16 - // @TODO can have more options.
17 - },
18 - _create: function() {
19 - var self = this,
20 - select = this.element.hide(),
21 - options = this.options;
22 - function split( val ) {
23 - return val.split( /,\s*/ );
24 - }
25 -
26 - var input = this.input = $( options.inputbox ).autocomplete( {
27 - delay: 0,
28 - minLength: 0,
29 - source: function( request, response ) {
30 - var term = split( request.term ).pop();
31 - var matcher = new RegExp( $.ui.autocomplete.escapeRegex( term ), "i" );
32 - response( select.children( "option" ).map( function() {
33 - var text = $( this ).text();
34 - var value = $( this ).val();
35 - var term = split( request.term ).pop();
36 - if ( this.value && ( !request.term || matcher.test(text) ) ) {
37 - return {
38 - label: text.replace(
39 - new RegExp(
40 - "(?![^&;]+;)(?!<[^<>]*)(" +
41 - $.ui.autocomplete.escapeRegex( term ) +
42 - ")(?![^<>]*>)(?![^&;]+;)", "gi"
43 - ), "<strong>$1</strong>" ),
44 - value: value,
45 - option: this
46 - };
47 - }
48 - } ) );
49 - },
50 - select: function( event, ui ) {
51 - ui.item.option.selected = true;
52 - self._trigger( "selected", event, {
53 - item: ui.item.option
54 - });
55 - var terms = split( $(this).val() );
56 - // remove the current input
57 - terms.pop();
58 - // add the selected item
59 - terms.push( ui.item.value );
60 - // add placeholder to get the comma-and-space at the end
61 - terms.push( "" );
62 - $( this ).val( terms.join( ", " ) );
63 - return false;
64 - }
65 - } );
66 -
67 - input.data( "autocomplete" )._renderItem = function( ul, item ) {
68 - return $( "<li>" )
69 - .data( "item.autocomplete", item )
70 - .append( "<a>" + item.label + "</a>" )
71 - .appendTo( ul );
72 - };
73 - }, // End of _create
74 -
75 - destroy: function() {
76 - this.input.remove();
77 - this.element.show();
78 - $.Widget.prototype.destroy.call( this );
79 - }
80 - } );
81 -
8211 $( "#wpUserLanguage" ).multiselectautocomplete( { inputbox : '#tpt-prioritylangs' } );
8312 } );
Index: trunk/extensions/Translate/resources/ext.translate.multiselectautocomplete.js
@@ -0,0 +1,80 @@
 2+/*
 3+ * @author Santhosh Thottingal
 4+ * jQuery autocomplete based multiple selector for input box.
 5+ * Autocompleted values will be available in input filed as comma separated values.
 6+ * The values for autocompletion is from the language selector in this case.
 7+ * The input field is created in PHP code.
 8+ * Credits: http://jqueryui.com/demos/autocomplete/#multiple
 9+ */
 10+jQuery( function( $ ) {
 11+ "use strict"
 12+
 13+ $.widget( "ui.multiselectautocomplete", {
 14+ options: {
 15+ inputbox: null, // a jQuery selector for the input box where selections are written.
 16+ // @TODO can have more options.
 17+ },
 18+ _create: function() {
 19+ var self = this,
 20+ select = this.element.hide(),
 21+ options = this.options;
 22+ function split( val ) {
 23+ return val.split( /,\s*/ );
 24+ }
 25+
 26+ var input = this.input = $( options.inputbox ).autocomplete( {
 27+ delay: 0,
 28+ minLength: 0,
 29+ source: function( request, response ) {
 30+ var term = split( request.term ).pop();
 31+ var matcher = new RegExp( $.ui.autocomplete.escapeRegex( term ), "i" );
 32+ response( select.children( "option" ).map( function() {
 33+ var text = $( this ).text();
 34+ var value = $( this ).val();
 35+ var term = split( request.term ).pop();
 36+ if ( this.value && ( !request.term || matcher.test(text) ) ) {
 37+ return {
 38+ label: text.replace(
 39+ new RegExp(
 40+ "(?![^&;]+;)(?!<[^<>]*)(" +
 41+ $.ui.autocomplete.escapeRegex( term ) +
 42+ ")(?![^<>]*>)(?![^&;]+;)", "gi"
 43+ ), "<strong>$1</strong>" ),
 44+ value: value,
 45+ option: this
 46+ };
 47+ }
 48+ } ) );
 49+ },
 50+ select: function( event, ui ) {
 51+ ui.item.option.selected = true;
 52+ self._trigger( "selected", event, {
 53+ item: ui.item.option
 54+ });
 55+ var terms = split( $(this).val() );
 56+ // remove the current input
 57+ terms.pop();
 58+ // add the selected item
 59+ terms.push( ui.item.value );
 60+ // add placeholder to get the comma-and-space at the end
 61+ terms.push( "" );
 62+ $( this ).val( terms.join( ", " ) );
 63+ return false;
 64+ }
 65+ } );
 66+
 67+ input.data( "autocomplete" )._renderItem = function( ul, item ) {
 68+ return $( "<li>" )
 69+ .data( "item.autocomplete", item )
 70+ .append( "<a>" + item.label + "</a>" )
 71+ .appendTo( ul );
 72+ };
 73+ }, // End of _create
 74+
 75+ destroy: function() {
 76+ this.input.remove();
 77+ this.element.show();
 78+ $.Widget.prototype.destroy.call( this );
 79+ }
 80+ } );
 81+} );
Property changes on: trunk/extensions/Translate/resources/ext.translate.multiselectautocomplete.js
___________________________________________________________________
Added: svn:eol-style
182 + native

Sign-offs

UserFlagDate
Nikerabbitinspected07:30, 21 March 2012

Follow-up revisions

RevisionCommit summaryAuthorDate
r114254Remove comment related to the code that was moved to a new file in r114250santhosh11:32, 20 March 2012

Status & tagging log