r63120 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63119‎ | r63120 | r63121 >
Date:15:21, 1 March 2010
Author:catrope
Status:ok
Tags:
Comment:
Storyboard: Cleanup for r63105
* Move JS inserted in storyboard tag hook to its own file, jQuery -> $j, obj -> $storyboard
* Move the width/height parsing logic into its own function, replace string kung-fu with a simple regex and allow em and ex suffixes as well as px and %
* Use Html::element() rather than raw HTML. This also closes the <div>, which was not the case before
* svn:keywords=Id for ApiStoryReview.php
Modified paths:
  • /trunk/extensions/Storyboard/api/ApiStoryReview.php (modified) (history)
  • /trunk/extensions/Storyboard/tags/Storyboard/Storyboard_body.php (modified) (history)
  • /trunk/extensions/Storyboard/tags/Storyboard/storyboard.js (added) (history)

Diff [purge]

Index: trunk/extensions/Storyboard/api/ApiStoryReview.php
@@ -72,6 +72,6 @@
7373 }
7474
7575 public function getVersion() {
76 - return __CLASS__ . ': $Id: $';
 76+ return __CLASS__ . ': $Id$';
7777 }
7878 }
\ No newline at end of file
Property changes on: trunk/extensions/Storyboard/api/ApiStoryReview.php
___________________________________________________________________
Name: svn:keywords
7979 + Id
Index: trunk/extensions/Storyboard/tags/Storyboard/storyboard.js
@@ -0,0 +1,26 @@
 2+/**
 3+ * JavaScript added for <storyboard> tags
 4+ */
 5+
 6+$j( document ).ready( function(){
 7+ $j( '#storyboard' ).ajaxScroll( {
 8+ updateBatch: updateStoryboard,
 9+ batchSize: 5,
 10+ batchNum: 1
 11+ } );
 12+});
 13+
 14+function updateStoryboard( $storyboard ){
 15+ $j.getJSON( wgScriptPath + '/api.php',
 16+ {
 17+ 'action': 'query',
 18+ 'list': 'stories',
 19+ 'stcontinue': $storyboard.attr( 'offset' ),
 20+ 'stlimit': 5,
 21+ 'format': 'json'
 22+ },
 23+ function( data ) {
 24+ // TODO: use data to create stories html
 25+ }
 26+ );
 27+}
Index: trunk/extensions/Storyboard/tags/Storyboard/Storyboard_body.php
@@ -18,71 +18,39 @@
1919 public static function render( $input, $args, $parser, $frame ) {
2020 global $wgOut, $wgJsMimeType, $wgScriptPath, $egStoryboardScriptPath, $egStoryboardWidth, $egStoryboardHeight;
2121
22 - $wgOut->addStyle($egStoryboardScriptPath . '/tags/Storyboard/storyboard.css');
 22+ $wgOut->addStyle( $egStoryboardScriptPath . '/tags/Storyboard/storyboard.css' );
2323 $wgOut->includeJQuery();
24 - $wgOut->addScriptFile($egStoryboardScriptPath . '/tags/Storyboard/jquery.ajaxscroll.js');
 24+ // TODO: Combine+minfiy JS files, add switch to use combined+minified version
 25+ $wgOut->addScriptFile( $egStoryboardScriptPath . '/tags/Storyboard/jquery.ajaxscroll.js' );
 26+ $wgOut->addScriptFile( $egStoryboardScriptPath . '/tags/Storyboard/storyboard.js' );
2527
26 - $widthGiven = array_key_exists('width', $args)
27 - && (is_numeric($args['width'])
28 - || (strlen($args['width']) > 1
29 - && is_numeric(substr($args['width'], 0, strlen($args['width']) - 1))
30 - && substr($args['width'], strlen($args['width']) == '%'
31 - )
32 - )
33 - );
34 - $width = $widthGiven ? $args['width'] : $egStoryboardWidth;
35 -
36 - $heightGiven = array_key_exists('height', $args)
37 - && (is_numeric($args['height'])
38 - || (strlen($args['height']) > 1
39 - && is_numeric(substr($args['height'], 0, strlen($args['height']) - 1))
40 - && substr($args['height'], strlen($args['height']) == '%'
41 - )
42 - )
43 - );
44 - $height = $heightGiven ? $args['height'] : $egStoryboardHeight;
45 -
46 - TagStoryboard::addPxWhenNeeded($width);
47 - TagStoryboard::addPxWhenNeeded($height);
48 -
49 - $output = <<<EOT
50 -<div class="ajaxscroll" id="storyboard" style="height: $height; width: $width;">
51 -<script type="$wgJsMimeType"> /*<![CDATA[*/
52 -jQuery(function(){
53 - jQuery('#storyboard').ajaxScroll({
54 - updateBatch: updateStoryboard,
55 - batchSize: 5,
56 - batchNum: 1
57 - });
58 -});
59 -function updateStoryboard(obj){
60 - jQuery.getJSON('$wgScriptPath/api.php',
61 - {
62 - 'action': 'query',
63 - 'list': 'stories',
64 - 'stcontinue': obj.attr( 'offset' ),
65 - 'stlimit': '5',
66 - 'format': 'json'
67 - },
68 - function( data ) {
69 - // TODO: use data to create stories html
70 - }
71 - );
72 -}
73 -/*]]>*/ </script>
74 -EOT;
 28+ $width = self::getDimension( $args, 'width', $egStoryboardWidth );
 29+ $height = self::getDimension( $args, 'height', $egStoryboardHeight );
7530
76 - return array($output, 'noparse' => 'true', 'isHTML' => 'true');
 31+ $output = Html::element( 'div', array(
 32+ 'class' => 'ajaxscroll',
 33+ 'id' => 'storyboard', // FIXME: Causes id conflicts for multiple <storyboard> tags
 34+ 'style' => "height: $height; width: $width;"
 35+ )
 36+ );
 37+ return array( $output, 'noparse' => 'true', 'isHTML' => 'true' );
7738 }
7839
7940 /**
80 - * Adds 'px' to a width or height value when it's not there yet, and it's not a percentage.
81 - * @param string $value
 41+ * Get the width or height from an arguments array, or use the default value if not specified or not valid
 42+ * @param $arr Array of arguments
 43+ * @param $name Key in $array
 44+ * @param $default Default value to use if $arr[$name] is not set or not valid
8245 */
83 - private static function addPxWhenNeeded(&$value){
84 - $hasPx = strrpos( $value, 'px' ) === strlen( $value ) - 2;
85 - $hasPercent = strrpos( $value, '%' ) === strlen( $value ) - 1;
86 - if (!$hasPx && !$hasPercent) $value .= 'px';
 46+ private static function getDimension( $arr, $name, $default ) {
 47+ $value = $default;
 48+ if ( isset( $arr[$name] ) && preg_match( '/\d+(\.\d+)?%?/', $arr[$name] ) ) {
 49+ $value = $arr[$name];
 50+ }
 51+ if ( !preg_match( '/(px|ex|em|%)$/', $value ) ) {
 52+ $value .= 'px';
 53+ }
 54+ return $value;
8755 }
8856
8957 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r63105Modifications for getting storyboard content + support for width and height p...jeroendedauw23:27, 28 February 2010

Status & tagging log