r99726 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99725‎ | r99726 | r99727 >
Date:20:29, 13 October 2011
Author:jeroendedauw
Status:ok (Comments)
Tags:
Comment:
split up js
Modified paths:
  • /trunk/extensions/Contest/Contest.php (modified) (history)
  • /trunk/extensions/Contest/resources/contest.special.submission.js (modified) (history)
  • /trunk/extensions/Contest/resources/jquery.contestSubmission.js (added) (history)

Diff [purge]

Index: trunk/extensions/Contest/Contest.php
@@ -225,6 +225,7 @@
226226
227227 $wgResourceModules['contest.special.submission'] = $moduleTemplate + array(
228228 'scripts' => array(
 229+ 'jquery.contestSubmission.js',
229230 'contest.special.submission.js',
230231 ),
231232 'dependencies' => array(
Index: trunk/extensions/Contest/resources/contest.special.submission.js
@@ -8,114 +8,6 @@
99
1010 (function( $, mw ) {
1111
12 - /**
13 - * Regex text escaping function.
14 - * Borrowed from http://simonwillison.net/2006/Jan/20/escape/
15 - */
16 - RegExp.escape = function( text ) {
17 - if ( !arguments.callee.sRE ) {
18 - var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ];
19 - arguments.callee.sRE = new RegExp( '(\\' + specials.join('|\\') + ')', 'g' );
20 - }
21 - return text.replace(arguments.callee.sRE, '\\$1');
22 - }
23 -
24 - $.fn.contestSubmission = function() {
25 - var _this = this;
26 - var $this = $( this );
27 -
28 - this.config = {};
29 - this.status = {};
30 -
31 - this.input = null;
32 - this.label = null;
33 -
34 - this.getValue = function() {
35 - return this.input.val();
36 - }
37 -
38 - this.getDomains = function() {
39 - return this.config.domains;
40 - };
41 -
42 -// this.getDomainLinks = function() {
43 -// return this.getDomains().map( function() { return $( '<a />' ).attr( 'href', 'http://' + this ).text( this ); } );
44 -// };
45 -
46 - this.validate = function() {
47 - var domains = _this.getDomains();
48 -
49 - for ( var i = domains.length - 1; i >= 0; i-- ) {
50 - var regex = new RegExp( "^https?://(([a-z0-9]+)\\.)?" + RegExp.escape( domains[i] ) + "/(.*)?$", "gi" );
51 - if ( regex.test( this.getValue() ) ) {
52 - return true;
53 - }
54 - }
55 -
56 - return false;
57 - };
58 -
59 - this.showStatus = function() {
60 - if ( _this.status.valid ) {
61 - _this.input.removeClass( 'error' );
62 - }
63 - else {
64 - _this.input.addClass( 'error' );
65 - }
66 - };
67 -
68 - this.onValueChanged = function() {
69 - _this.status.valid = _this.validate();
70 - _this.showStatus();
71 - };
72 -
73 - this.setup = function() {
74 - var message = $this.attr( 'data-value' ) === '' ? 'contest-submission-new-submission' : 'contest-submission-current-submission';
75 - var domainLinks = [];
76 -
77 - for ( var i = this.config.domains.length - 1; i >= 0; i-- ) {
78 - var link = $( '<a />' ).text( this.config.domains[i] ).attr( {
79 - 'href': 'http://' + this.config.domains[i],
80 - 'target': 'blank'
81 - } );
82 - domainLinks.push( $( '<div />' ).html( link ).html() );
83 - }
84 -
85 - var links = $( '<span />' ).html( '' );
86 -
87 - this.label = $( '<label style="display:block" />' ).attr( {
88 - 'for': this.config.name,
89 - } ).text( mw.msg( message ) ).append(
90 - $( '<br />' ),
91 - mw.msg( 'contest-submission-domains', domainLinks.join( ', ' ) )
92 - );
93 -
94 - this.input = $( '<input />' ).attr( {
95 - 'type': 'text',
96 - 'value': $this.attr( 'data-value' ),
97 - 'name': this.config.name,
98 - 'size': 45,
99 - 'id': this.config.name
100 - } );
101 -
102 - this.html( this.label );
103 - this.append( this.input );
104 -
105 - this.input.keyup( this.onValueChanged );
106 - };
107 -
108 - this.getConfig = function() {
109 - this.config.name = $this.attr( 'data-name' );
110 - this.config.domains = $this.attr( 'data-domains' ).split( '|' );
111 - };
112 -
113 - this.getConfig();
114 - this.setup();
115 - this.onValueChanged();
116 -
117 - return this;
118 - };
119 -
12012 $( document ).ready( function() {
12113
12214 $( '.mw-htmlform-submit' ).button();
Index: trunk/extensions/Contest/resources/jquery.contestSubmission.js
@@ -0,0 +1,115 @@
 2+/**
 3+ * JavasSript for the Contest MediaWiki extension.
 4+ * @see https://www.mediawiki.org/wiki/Extension:Contest
 5+ *
 6+ * @licence GNU GPL v3 or later
 7+ * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
 8+ */
 9+
 10+(function( $, mw ) {
 11+
 12+ /**
 13+ * Regex text escaping function.
 14+ * Borrowed from http://simonwillison.net/2006/Jan/20/escape/
 15+ */
 16+ RegExp.escape = function( text ) {
 17+ if ( !arguments.callee.sRE ) {
 18+ var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ];
 19+ arguments.callee.sRE = new RegExp( '(\\' + specials.join('|\\') + ')', 'g' );
 20+ }
 21+ return text.replace(arguments.callee.sRE, '\\$1');
 22+ }
 23+
 24+ $.fn.contestSubmission = function() {
 25+ var _this = this;
 26+ var $this = $( this );
 27+
 28+ this.config = {};
 29+ this.status = {};
 30+
 31+ this.input = null;
 32+ this.label = null;
 33+
 34+ this.getValue = function() {
 35+ return this.input.val();
 36+ }
 37+
 38+ this.getDomains = function() {
 39+ return this.config.domains;
 40+ };
 41+
 42+ this.validate = function() {
 43+ var domains = _this.getDomains();
 44+
 45+ for ( var i = domains.length - 1; i >= 0; i-- ) {
 46+ var regex = new RegExp( "^https?://(([a-z0-9]+)\\.)?" + RegExp.escape( domains[i] ) + "/(.*)?$", "gi" );
 47+ if ( regex.test( this.getValue() ) ) {
 48+ return true;
 49+ }
 50+ }
 51+
 52+ return false;
 53+ };
 54+
 55+ this.showStatus = function() {
 56+ if ( _this.status.valid ) {
 57+ _this.input.removeClass( 'error' );
 58+ }
 59+ else {
 60+ _this.input.addClass( 'error' );
 61+ }
 62+ };
 63+
 64+ this.onValueChanged = function() {
 65+ _this.status.valid = _this.validate();
 66+ _this.showStatus();
 67+ };
 68+
 69+ this.setup = function() {
 70+ var message = $this.attr( 'data-value' ) === '' ? 'contest-submission-new-submission' : 'contest-submission-current-submission';
 71+ var domainLinks = [];
 72+
 73+ for ( var i = this.config.domains.length - 1; i >= 0; i-- ) {
 74+ var link = $( '<a />' ).text( this.config.domains[i] ).attr( {
 75+ 'href': 'http://' + this.config.domains[i],
 76+ 'target': 'blank'
 77+ } );
 78+ domainLinks.push( $( '<div />' ).html( link ).html() );
 79+ }
 80+
 81+ var links = $( '<span />' ).html( '' );
 82+
 83+ this.label = $( '<label style="display:block" />' ).attr( {
 84+ 'for': this.config.name,
 85+ } ).text( mw.msg( message ) ).append(
 86+ $( '<br />' ),
 87+ mw.msg( 'contest-submission-domains', domainLinks.join( ', ' ) )
 88+ );
 89+
 90+ this.input = $( '<input />' ).attr( {
 91+ 'type': 'text',
 92+ 'value': $this.attr( 'data-value' ),
 93+ 'name': this.config.name,
 94+ 'size': 45,
 95+ 'id': this.config.name
 96+ } );
 97+
 98+ this.html( this.label );
 99+ this.append( this.input );
 100+
 101+ this.input.keyup( this.onValueChanged );
 102+ };
 103+
 104+ this.getConfig = function() {
 105+ this.config.name = $this.attr( 'data-name' );
 106+ this.config.domains = $this.attr( 'data-domains' ).split( '|' );
 107+ };
 108+
 109+ this.getConfig();
 110+ this.setup();
 111+ this.onValueChanged();
 112+
 113+ return this;
 114+ };
 115+
 116+})( window.jQuery, window.mediaWiki );
\ No newline at end of file

Comments

#Comment by Catrope (talk | contribs)   00:55, 14 October 2011

Would be nice to also split up the module into two modules.

#Comment by Jeroen De Dauw (talk | contribs)   00:57, 14 October 2011

Right, now I think about it, that does make sense.

Status & tagging log