Merging resourceloader branch into trunk. Full documentation is at http://www.mediawi...
[lhc/web/wiklou.git] / resources / mediawiki / legacy / mediawiki.legacy.preview.js
1 /*
2 * Legacy emulation for the now depricated skins/common/preview.js
3 *
4 * Inline ("Live") preview
5 */
6
7 ( function( $, mw ) {
8
9 /* Extension */
10
11 $.extend( true, mw.legacy, {
12
13 /* Functions */
14
15 'doLivePreview': function( e ) {
16 e.preventDefault();
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');
29 if ( !target ) {
30 target = window.location.href;
31 }
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 );
41 }
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', ''); } );
45 } );
46 loadSpinner.remove();
47 $j( mw ).trigger( 'LivePreviewDone', [copyElements] );
48 } );
49 }
50 } );
51
52 /* Initialization */
53
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() {
58 var a = [];
59 if (this.length == 0) return a;
60 var form = this[0];
61 var els = form.elements;
62 if (!els) return a;
63 for(var i=0, max=els.length; i < max; i++) {
64 var el = els[i];
65 var n = el.name;
66 if (!n) continue;
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]});
71 }
72 else if (v !== null && typeof v != 'undefined')
73 a.push({name: n, value: v});
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 * Returns the value of the field element.
87 */
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))
95 return null;
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++) {
103 var op = ops[i];
104 if (op.selected) {
105 var v = op.value;
106 if (!v) // extra pain for IE...
107 v = (op.attributes && op.attributes['value'] &&
108 !(op.attributes['value'].specified))
109 ? op.text : op.value;
110 if (one) return v;
111 a.push(v);
112 }
113 }
114 return a;
115 }
116 return el.value;
117 };
118 $j('#wpPreview').click( doLivePreview );
119 } );
120
121 } )( jQuery, mediaWiki );