r102888 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102887‎ | r102888 | r102889 >
Date:11:48, 13 November 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added image preview stuff
Modified paths:
  • /trunk/extensions/SemanticForms/SemanticForms.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_TextAreaWithAutocompleteInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_TextInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_TextWithAutocompleteInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/libs/SF_imagePreview.js (added) (history)
  • /trunk/extensions/SemanticForms/specials/SF_UploadWindow2.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/SemanticForms.php
@@ -236,6 +236,9 @@
237237 'scripts' => 'libs/SF_wikieditor.js',
238238 'styles' => 'skins/SF_wikieditor.css',
239239 ),
 240+ 'ext.semanticforms.imagepreview' => $sfgResourceTemplate + array(
 241+ 'scripts' => 'libs/SF_imagePreview.js',
 242+ ),
240243 );
241244 }
242245
Index: trunk/extensions/SemanticForms/specials/SF_UploadWindow2.php
@@ -385,11 +385,16 @@
386386 $basename = str_replace( '_', ' ', $basename );
387387 // UTF8-decoding is needed for IE
388388 $basename = utf8_decode( $basename );
389 - $output = ' <script type="text/javascript">' . "\n";
 389+
 390+ $output .= <<<END
 391+ <script type="text/javascript">
 392+ var input = parent.window.jQuery( parent.document.getElementById("{$this->mInputID}") );
 393+END;
 394+
390395 if ( $this->mDelimiter == null ) {
391396 $output .= <<<END
392 - parent.document.getElementById("{$this->mInputID}").value = '$basename';
393 -
 397+ input.val( '$basename' );
 398+ input.change();
394399 END;
395400 } else {
396401 $output .= <<<END
@@ -399,14 +404,18 @@
400405 // both a delimiter and a file name; and add on a delimiter
401406 // at the end in any case
402407 var cur_value = parent.document.getElementById("{$this->mInputID}").value;
 408+
403409 if (cur_value === '') {
404 - parent.document.getElementById("{$this->mInputID}").value = '$basename' + '{$this->mDelimiter} ';
 410+ input.val( '$basename' + '{$this->mDelimiter} ' );
 411+ input.change();
405412 } else {
406413 var last_char = cur_value.charAt(cur_value.length - 1);
407414 if (last_char == '{$this->mDelimiter}' || last_char == ' ') {
408415 parent.document.getElementById("{$this->mInputID}").value += '$basename' + '{$this->mDelimiter} ';
 416+ input.change();
409417 } else {
410418 parent.document.getElementById("{$this->mInputID}").value += '{$this->mDelimiter} $basename{$this->mDelimiter} ';
 419+ input.change();
411420 }
412421 }
413422
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_TextInput.php
@@ -42,7 +42,7 @@
4343 return array( '_wpg' );
4444 }
4545
46 - public static function uploadLinkHTML( $input_id, $delimiter = null, $default_filename = null ) {
 46+ public static function uploadableHTML( $input_id, $delimiter = null, $default_filename = null, $cur_value = '', $other_args = array() ) {
4747 $upload_window_page = SpecialPage::getPage( 'UploadWindow' );
4848 $query_string = "sfInputID=$input_id";
4949 if ( $delimiter != null ) {
@@ -60,13 +60,48 @@
6161 $style = '';
6262 }
6363
 64+ $cssClasses = array( 'sfFancyBox', 'sfUploadable' );
 65+
 66+ $showPreview = array_key_exists( 'imagepreview', $other_args ) && trim( $other_args['imagepreview'] == 'on' );
 67+
 68+ if ( $showPreview ) {
 69+ $cssClasses[] = 'sfImagePreview';
 70+ }
 71+
6472 $linkAttrs = array(
6573 'href' => $upload_window_url,
66 - 'class' => 'sfFancyBox',
 74+ 'class' => implode( ' ', $cssClasses ),
6775 'title' => $upload_label,
68 - 'rev' => $style
 76+ 'rev' => $style,
 77+ 'data-input-id' => $input_id
6978 );
 79+
7080 $text = "\t" . Xml::element( 'a', $linkAttrs, $upload_label ) . "\n";
 81+
 82+ if ( $showPreview ) {
 83+ $previewImage = null;
 84+
 85+ if ( $cur_value !== '' ) {
 86+ $imageTitle = Title::newFromText( $cur_value, NS_FILE );
 87+
 88+ if ( !is_null( $imageTitle ) && $imageTitle->getNamespace() == NS_FILE && $imageTitle->exists() ) {
 89+ $imagePage = new ImagePage( $imageTitle );
 90+ $url = $imagePage->getDisplayedFile()->transform( array( 'width' => 200 ) )->getURL();
 91+
 92+ $previewImage = Html::element(
 93+ 'img',
 94+ array( 'src' => $url )
 95+ );
 96+ }
 97+ }
 98+
 99+ $text .= Html::rawElement(
 100+ 'div',
 101+ array( 'id' => $input_id . '_imagepreview' ),
 102+ $previewImage
 103+ );
 104+ }
 105+
71106 return $text;
72107 }
73108
@@ -146,7 +181,8 @@
147182 } else {
148183 $default_filename = '';
149184 }
150 - $text .= self::uploadLinkHTML( $input_id, $delimiter, $default_filename );
 185+
 186+ $text .= self::uploadableHTML( $input_id, $delimiter, $default_filename, $cur_value, $other_args );
151187 }
152188 $spanClass = 'inputSpan';
153189 if ( $inputType !== '' ) {
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_TextAreaWithAutocompleteInput.php
@@ -139,7 +139,7 @@
140140 } else {
141141 $default_filename = '';
142142 }
143 - $text .= self::uploadLinkHTML( $input_id, $delimiter, $default_filename );
 143+ $text .= self::uploadableHTML( $input_id, $delimiter, $default_filename, $cur_value, $other_args );
144144 }
145145
146146 $spanClass = 'inputSpan';
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_TextWithAutocompleteInput.php
@@ -184,7 +184,7 @@
185185 } else {
186186 $default_filename = '';
187187 }
188 - $text .= self::uploadLinkHTML( $input_id, $delimiter, $default_filename );
 188+ $text .= self::uploadableHTML( $input_id, $delimiter, $default_filename, $cur_value, $other_args );
189189 }
190190
191191 $spanClass = 'inputSpan';
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -284,6 +284,7 @@
285285 }
286286 $output->addModules( 'ext.semanticforms.main' );
287287 $output->addModules( 'ext.semanticforms.fancybox' );
 288+ $output->addModules( 'ext.semanticforms.imagepreview' );
288289 $output->addModules( 'ext.semanticforms.autogrow' );
289290 $output->addModules( 'ext.semanticforms.submit' );
290291 $output->addModules( 'ext.smw.tooltips' );
@@ -395,6 +396,7 @@
396397 $scripts[] = "$sfgScriptPath/libs/jquery-ui/jquery.ui.mouse.min.js";
397398 $scripts[] = "$sfgScriptPath/libs/jquery-ui/jquery.ui.sortable.min.js";
398399 $scripts[] = "$sfgScriptPath/libs/jquery.fancybox.js";
 400+ $scripts[] = "$sfgScriptPath/libs/SF_imagePreview.js";
399401 $scripts[] = "$sfgScriptPath/libs/SF_autogrow.js";
400402 $scripts[] = "$sfgScriptPath/libs/SF_submit.js";
401403 $scripts[] = "$sfgScriptPath/libs/SemanticForms.js";
Index: trunk/extensions/SemanticForms/libs/SF_imagePreview.js
@@ -0,0 +1,67 @@
 2+/**
 3+ * JavasSript for the Semantic Forms MediaWiki extension.
 4+ *
 5+ * @licence GNU GPL v3+
 6+ * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
 7+ */
 8+
 9+(function( $ ) {
 10+
 11+ var _this = this;
 12+
 13+ this.getPreviewImage = function( args, callback ) {
 14+ $.getJSON(
 15+ wgScriptPath + '/api.php',
 16+ {
 17+ 'action': 'query',
 18+ 'format': 'json',
 19+ 'prop': 'imageinfo',
 20+ 'iiprop': 'url',
 21+ 'titles': 'File:' + args.title,
 22+ 'iiurlwidth': args.width
 23+ },
 24+ function( data ) {
 25+ if ( data.query && data.query.pages ) {
 26+ var pages = data.query.pages;
 27+
 28+ for ( p in pages ) {
 29+ var info = pages[p].imageinfo;
 30+ for ( i in info ) {
 31+ callback( info[i].thumburl );
 32+ return;
 33+ }
 34+ }
 35+ }
 36+ callback( false );
 37+ }
 38+ );
 39+ };
 40+
 41+ $( document ).ready( function() {
 42+ $( '.sfImagePreview' ).each( function( index, domElement ) {
 43+ var $uploadLink = $( domElement );
 44+ var inputId = $uploadLink.attr( 'data-input-id' );
 45+ var $input = $( '#' + inputId );
 46+
 47+ $input.change( function() {
 48+ _this.getPreviewImage(
 49+ {
 50+ 'title': $input.val(),
 51+ 'width': 200
 52+ },
 53+ function( url ) {
 54+ $previewDiv = $( '#' + inputId + '_imagepreview' );
 55+
 56+ if ( url === false ) {
 57+ $previewDiv.html( '' );
 58+ }
 59+ else {
 60+ $previewDiv.html( $( '<img />' ).attr( { 'src': url } ) );
 61+ }
 62+ }
 63+ );
 64+ } );
 65+ } );
 66+ } );
 67+
 68+})( window.jQuery );
Property changes on: trunk/extensions/SemanticForms/libs/SF_imagePreview.js
___________________________________________________________________
Added: svn:eol-style
169 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r102990Follow-up to r102888 - changed parameter for enabling preview from "imageprev...yaron16:11, 14 November 2011
r103422Another follow-up to r102888 - changed parameter from 'imagepreview' to 'imag...yaron00:21, 17 November 2011
r114649Fix for r102888 - $output should be initialized correctlyyaron02:06, 2 April 2012