2 * Live preview script for MediaWiki
5 window
.doLivePreview = function( e
) {
8 $( mw
).trigger( 'LivePreviewPrepare' );
10 var postData
= $('#editform').formToArray();
11 postData
.push( { 'name' : 'wpPreview', 'value' : '1' } );
13 // Hide active diff, used templates, old preview if shown
14 var copyElements
= ['#wikiPreview', '.templatesUsed', '.hiddencats',
16 var copySelector
= copyElements
.join(',');
18 $.each( copyElements
, function(k
,v
) { $(v
).fadeOut('fast'); } );
20 // Display a loading graphic
21 var loadSpinner
= $('<div class="mw-ajax-loader"/>');
22 $('#wikiPreview').before( loadSpinner
);
24 var page
= $('<div/>');
25 var target
= $('#editform').attr('action');
28 target
= window
.location
.href
;
31 page
.load( target
+ ' ' + copySelector
, postData
,
34 for( var i
=0; i
<copyElements
.length
; ++i
) {
35 // For all the specified elements, find the elements in the loaded page
36 // and the real page, empty the element in the real page, and fill it
37 // with the content of the loaded page
38 var copyContent
= page
.find( copyElements
[i
] ).contents();
39 $(copyElements
[i
]).empty().append( copyContent
);
40 var newClasses
= page
.find( copyElements
[i
] ).prop('class');
41 $(copyElements
[i
]).prop( 'class', newClasses
);
44 $.each( copyElements
, function(k
,v
) {
45 // Don't belligerently show elements that are supposed to be hidden
46 $(v
).fadeIn( 'fast', function() { $(this).css('display', ''); } );
51 $( mw
).trigger( 'LivePreviewDone', [copyElements
] );
55 // Shamelessly stolen from the jQuery form plugin, which is licensed under the GPL.
56 // http://jquery.malsup.com/form/#download
57 $.fn
.formToArray = function() {
59 if (this.length
== 0) return a
;
62 var els
= form
.elements
;
64 for(var i
=0, max
=els
.length
; i
< max
; i
++) {
69 var v
= $.fieldValue(el
, true);
70 if (v
&& v
.constructor == Array
) {
71 for(var j
=0, jmax
=v
.length
; j
< jmax
; j
++)
72 a
.push({name
: n
, value
: v
[j
]});
74 else if (v
!== null && typeof v
!= 'undefined')
75 a
.push({name
: n
, value
: v
});
79 // input type=='image' are not found in elements array! handle it here
80 var $input
= $(form
.clk
), input
= $input
[0], n
= input
.name
;
81 if (n
&& !input
.disabled
&& input
.type
== 'image') {
82 a
.push({name
: n
, value
: $input
.val()});
83 a
.push({name
: n
+'.x', value
: form
.clk_x
}, {name
: n
+'.y', value
: form
.clk_y
});
90 * Returns the value of the field element.
92 $.fieldValue = function(el
, successful
) {
93 var n
= el
.name
, t
= el
.type
, tag
= el
.tagName
.toLowerCase();
94 if (typeof successful
== 'undefined') successful
= true;
96 if (successful
&& (!n
|| el
.disabled
|| t
== 'reset' || t
== 'button' ||
97 (t
== 'checkbox' || t
== 'radio') && !el
.checked
||
98 (t
== 'submit' || t
== 'image') && el
.form
&& el
.form
.clk
!= el
||
99 tag
== 'select' && el
.selectedIndex
== -1))
102 if (tag
== 'select') {
103 var index
= el
.selectedIndex
;
104 if (index
< 0) return null;
105 var a
= [], ops
= el
.options
;
106 var one
= (t
== 'select-one');
107 var max
= (one
? index
+1 : ops
.length
);
108 for(var i
=(one
? index
: 0); i
< max
; i
++) {
112 if (!v
) // extra pain for IE...
113 v
= (op
.attributes
&& op
.attributes
['value'] &&
114 !(op
.attributes
['value'].specified
))
115 ? op
.text
: op
.value
;
125 $(document
).ready( function() {
126 $('#wpPreview').click( doLivePreview
);