2 * Legacy emulation for the now depricated skins/common/preview.js
4 * Inline ("Live") preview
11 $.extend( true, mw
.legacy
, {
15 'doLivePreview': function( e
) {
17 $j( mw
).trigger( 'LivePreviewPrepare' );
18 var postData
= $j('#editform').formToArray();
19 postData
.push( { 'name' : 'wpPreview', 'value' : '1' } );
20 // Hide active diff, used templates, old preview if shown
21 var copyElements
= ['#wikiPreview', '.templatesUsed', '.hiddencats', '#catlinks'];
22 var copySelector
= copyElements
.join(',');
23 $j
.each( copyElements
, function(k
,v
) { $j(v
).fadeOut('fast'); } );
24 // Display a loading graphic
25 var loadSpinner
= $j('<div class="mw-ajax-loader"/>');
26 $j('#wikiPreview').before( loadSpinner
);
27 var page
= $j('<div/>');
28 var target
= $j('#editform').attr('action');
30 target
= window
.location
.href
;
32 page
.load( target
+ ' ' + copySelector
, postData
, function() {
33 for ( var i
=0; i
<copyElements
.length
; ++i
) {
34 // For all the specified elements, find the elements in the loaded page
35 // and the real page, empty the element in the real page, and fill it
36 // with the content of the loaded page
37 var copyContent
= page
.find( copyElements
[i
] ).contents();
38 $j(copyElements
[i
]).empty().append( copyContent
);
39 var newClasses
= page
.find( copyElements
[i
] ).attr('class');
40 $j(copyElements
[i
]).attr( 'class', newClasses
);
42 $j
.each( copyElements
, function(k
,v
) {
43 // Don't belligerently show elements that are supposed to be hidden
44 $j(v
).fadeIn( 'fast', function() { $j(this).css('display', ''); } );
47 $j( mw
).trigger( 'LivePreviewDone', [copyElements
] );
54 $( document
).ready( function() {
55 // Shamelessly stolen from the jQuery form plugin, which is licensed under the GPL.
56 // http://jquery.malsup.com/form/#download
57 $j
.fn
.formToArray = function() {
59 if (this.length
== 0) return a
;
61 var els
= form
.elements
;
63 for(var i
=0, max
=els
.length
; i
< max
; i
++) {
67 var v
= $j
.fieldValue(el
, true);
68 if (v
&& v
.constructor == Array
) {
69 for(var j
=0, jmax
=v
.length
; j
< jmax
; j
++)
70 a
.push({name
: n
, value
: v
[j
]});
72 else if (v
!== null && typeof v
!= 'undefined')
73 a
.push({name
: n
, value
: v
});
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
});
86 * Returns the value of the field element.
88 $j
.fieldValue = function(el
, successful
) {
89 var n
= el
.name
, t
= el
.type
, tag
= el
.tagName
.toLowerCase();
90 if (typeof successful
== 'undefined') successful
= true;
91 if (successful
&& (!n
|| el
.disabled
|| t
== 'reset' || t
== 'button' ||
92 (t
== 'checkbox' || t
== 'radio') && !el
.checked
||
93 (t
== 'submit' || t
== 'image') && el
.form
&& el
.form
.clk
!= el
||
94 tag
== 'select' && el
.selectedIndex
== -1))
96 if (tag
== 'select') {
97 var index
= el
.selectedIndex
;
98 if (index
< 0) return null;
99 var a
= [], ops
= el
.options
;
100 var one
= (t
== 'select-one');
101 var max
= (one
? index
+1 : ops
.length
);
102 for(var i
=(one
? index
: 0); i
< max
; i
++) {
106 if (!v
) // extra pain for IE...
107 v
= (op
.attributes
&& op
.attributes
['value'] &&
108 !(op
.attributes
['value'].specified
))
109 ? op
.text
: op
.value
;
118 $j('#wpPreview').click( doLivePreview
);
121 } )( jQuery
, mediaWiki
);