r65384 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65383‎ | r65384 | r65385 >
Date:13:46, 21 April 2010
Author:werdna
Status:ok (Comments)
Tags:
Comment:
Live Preview: Do a better job of emulating the edit form submission. Fixes Live Preview in LiquidThreads NewMessages pages.
Modified paths:
  • /trunk/phase3/skins/common/preview.js (modified) (history)

Diff [purge]

Index: trunk/phase3/skins/common/preview.js
@@ -4,15 +4,10 @@
55
66 function doLivePreview( e ) {
77 e.preventDefault();
8 - var previewText = $j('#wpTextbox1').val();
9 -
10 - var editToken = $j( '[name="wpEditToken"]' ).attr( 'value' );
11 - var editTime = $j( '[name="wpEdittime"]' ).attr( 'value' );
12 - var startTime = $j( '[name="wpStarttime"]' ).attr( 'value' );
13 -
14 - var postData = { 'action' : 'submit', 'wpTextbox1' : previewText, 'wpPreview' : true,
15 - 'wpEditToken' : editToken, 'wpEdittime': editTime, 'wpStarttime': startTime, 'title' : wgPageName };
168
 9+ var postData = $j('#editform').formToArray();
 10+ postData.push( { 'name' : 'wpPreview', 'value' : '1' } );
 11+
1712 // Hide active diff, used templates, old preview if shown
1813 var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats',
1914 '#catlinks'];
@@ -25,8 +20,13 @@
2621 $j('#wikiPreview').before( loadSpinner );
2722
2823 var page = $j('<div/>');
29 - page.load( wgScript+'?action=submit '+copySelector,
30 - postData,
 24+ var target = $j('#editform').attr('action');
 25+
 26+ if ( !target ) {
 27+ target = window.location.href;
 28+ }
 29+
 30+ page.load( target, postData,
3131 function() {
3232
3333 for( var i=0; i<copyElements.length; ++i) {
@@ -48,6 +48,76 @@
4949 } );
5050 }
5151
 52+// Shamelessly stolen from the jQuery form plugin, which is licensed under the GPL.
 53+// http://jquery.malsup.com/form/#download
 54+$j.fn.formToArray = function() {
 55+ var a = [];
 56+ if (this.length == 0) return a;
 57+
 58+ var form = this[0];
 59+ var els = form.elements;
 60+ if (!els) return a;
 61+ for(var i=0, max=els.length; i < max; i++) {
 62+ var el = els[i];
 63+ var n = el.name;
 64+ if (!n) continue;
 65+
 66+ var v = $j.fieldValue(el, true);
 67+ if (v && v.constructor == Array) {
 68+ for(var j=0, jmax=v.length; j < jmax; j++)
 69+ a.push({name: n, value: v[j]});
 70+ }
 71+ else if (v !== null && typeof v != 'undefined')
 72+ a.push({name: n, value: v});
 73+ }
 74+
 75+ if (form.clk) {
 76+ // input type=='image' are not found in elements array! handle it here
 77+ var $input = $(form.clk), input = $input[0], n = input.name;
 78+ if (n && !input.disabled && input.type == 'image') {
 79+ a.push({name: n, value: $input.val()});
 80+ a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
 81+ }
 82+ }
 83+ return a;
 84+};
 85+
 86+/**
 87+ * Returns the value of the field element.
 88+ */
 89+$j.fieldValue = function(el, successful) {
 90+ var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
 91+ if (typeof successful == 'undefined') successful = true;
 92+
 93+ if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
 94+ (t == 'checkbox' || t == 'radio') && !el.checked ||
 95+ (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
 96+ tag == 'select' && el.selectedIndex == -1))
 97+ return null;
 98+
 99+ if (tag == 'select') {
 100+ var index = el.selectedIndex;
 101+ if (index < 0) return null;
 102+ var a = [], ops = el.options;
 103+ var one = (t == 'select-one');
 104+ var max = (one ? index+1 : ops.length);
 105+ for(var i=(one ? index : 0); i < max; i++) {
 106+ var op = ops[i];
 107+ if (op.selected) {
 108+ var v = op.value;
 109+ if (!v) // extra pain for IE...
 110+ v = (op.attributes && op.attributes['value'] &&
 111+ !(op.attributes['value'].specified))
 112+ ? op.text : op.value;
 113+ if (one) return v;
 114+ a.push(v);
 115+ }
 116+ }
 117+ return a;
 118+ }
 119+ return el.value;
 120+};
 121+
52122 $j(document).ready( function() {
53123 $j('#wpPreview').click( doLivePreview );
54124 } );

Follow-up revisions

RevisionCommit summaryAuthorDate
r65385MFT r65384werdna13:49, 21 April 2010
r65388Fix regression in r65384 with document.write() compatibilitywerdna13:53, 21 April 2010

Comments

#Comment by Trevor Parscal (WMF) (talk | contribs)   17:46, 12 January 2011

Could we move the formToArray to portion of this code to resources/jquery/jquery.formToArray.js - that way everyone can enjoy it, and we don't end up having someone else do just that and have 2 versions of the same thing being sent to the client...

#Comment by Trevor Parscal (WMF) (talk | contribs)   17:50, 12 January 2011

Otherwise, this looks good to me. Seems to work well.

#Comment by Werdna (talk | contribs)   18:26, 18 January 2011

Added the whole library in r80504.

#Comment by Trevor Parscal (WMF) (talk | contribs)   19:23, 19 January 2011

Awesome!

Status & tagging log