Index: trunk/phase3/skins/common/preview.js |
— | — | @@ -4,15 +4,10 @@ |
5 | 5 | |
6 | 6 | function doLivePreview( e ) { |
7 | 7 | 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 }; |
16 | 8 | |
| 9 | + var postData = $j('#editform').formToArray(); |
| 10 | + postData.push( { 'name' : 'wpPreview', 'value' : '1' } ); |
| 11 | + |
17 | 12 | // Hide active diff, used templates, old preview if shown |
18 | 13 | var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats', |
19 | 14 | '#catlinks']; |
— | — | @@ -25,8 +20,13 @@ |
26 | 21 | $j('#wikiPreview').before( loadSpinner ); |
27 | 22 | |
28 | 23 | 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, |
31 | 31 | function() { |
32 | 32 | |
33 | 33 | for( var i=0; i<copyElements.length; ++i) { |
— | — | @@ -48,6 +48,76 @@ |
49 | 49 | } ); |
50 | 50 | } |
51 | 51 | |
| 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 | + |
52 | 122 | $j(document).ready( function() { |
53 | 123 | $j('#wpPreview').click( doLivePreview ); |
54 | 124 | } ); |