r82191 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82190‎ | r82191 | r82192 >
Date:21:02, 15 February 2011
Author:mah
Status:ok
Tags:
Comment:
(bug 27435) Wrap with jquery bit per Roan's suggestion
Modified paths:
  • /trunk/phase3/skins/common/preview.js (modified) (history)

Diff [purge]

Index: trunk/phase3/skins/common/preview.js
@@ -1,127 +1,128 @@
22 /**
33 * Live preview script for MediaWiki
44 */
 5+(function( $ ) {
 6+ window.doLivePreview = function( e ) {
 7+ e.preventDefault();
58
6 -window.doLivePreview = function( e ) {
7 - e.preventDefault();
 9+ $( mw ).trigger( 'LivePreviewPrepare' );
810
9 - $( mw ).trigger( 'LivePreviewPrepare' );
 11+ var postData = $('#editform').formToArray();
 12+ postData.push( { 'name' : 'wpPreview', 'value' : '1' } );
1013
11 - var postData = $('#editform').formToArray();
12 - postData.push( { 'name' : 'wpPreview', 'value' : '1' } );
 14+ // Hide active diff, used templates, old preview if shown
 15+ var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats',
 16+ '#catlinks'];
 17+ var copySelector = copyElements.join(',');
1318
14 - // Hide active diff, used templates, old preview if shown
15 - var copyElements = ['#wikiPreview', '.templatesUsed', '.hiddencats',
16 - '#catlinks'];
17 - var copySelector = copyElements.join(',');
 19+ $.each( copyElements, function(k,v) { $(v).fadeOut('fast'); } );
1820
19 - $.each( copyElements, function(k,v) { $(v).fadeOut('fast'); } );
 21+ // Display a loading graphic
 22+ var loadSpinner = $('<div class="mw-ajax-loader"/>');
 23+ $('#wikiPreview').before( loadSpinner );
2024
21 - // Display a loading graphic
22 - var loadSpinner = $('<div class="mw-ajax-loader"/>');
23 - $('#wikiPreview').before( loadSpinner );
 25+ var page = $('<div/>');
 26+ var target = $('#editform').attr('action');
2427
25 - var page = $('<div/>');
26 - var target = $('#editform').attr('action');
 28+ if ( !target ) {
 29+ target = window.location.href;
 30+ }
2731
28 - if ( !target ) {
29 - target = window.location.href;
30 - }
 32+ page.load( target + ' ' + copySelector, postData,
 33+ function() {
3134
32 - page.load( target + ' ' + copySelector, postData,
33 - function() {
 35+ for( var i=0; i<copyElements.length; ++i) {
 36+ // For all the specified elements, find the elements in the loaded page
 37+ // and the real page, empty the element in the real page, and fill it
 38+ // with the content of the loaded page
 39+ var copyContent = page.find( copyElements[i] ).contents();
 40+ $(copyElements[i]).empty().append( copyContent );
 41+ var newClasses = page.find( copyElements[i] ).attr('class');
 42+ $(copyElements[i]).attr( 'class', newClasses );
 43+ }
3444
35 - for( var i=0; i<copyElements.length; ++i) {
36 - // For all the specified elements, find the elements in the loaded page
37 - // and the real page, empty the element in the real page, and fill it
38 - // with the content of the loaded page
39 - var copyContent = page.find( copyElements[i] ).contents();
40 - $(copyElements[i]).empty().append( copyContent );
41 - var newClasses = page.find( copyElements[i] ).attr('class');
42 - $(copyElements[i]).attr( 'class', newClasses );
43 - }
 45+ $.each( copyElements, function(k,v) {
 46+ // Don't belligerently show elements that are supposed to be hidden
 47+ $(v).fadeIn( 'fast', function() { $(this).css('display', ''); } );
 48+ } );
4449
45 - $.each( copyElements, function(k,v) {
46 - // Don't belligerently show elements that are supposed to be hidden
47 - $(v).fadeIn( 'fast', function() { $(this).css('display', ''); } );
 50+ loadSpinner.remove();
 51+
 52+ $( mw ).trigger( 'LivePreviewDone', [copyElements] );
4853 } );
 54+ };
4955
50 - loadSpinner.remove();
 56+ // Shamelessly stolen from the jQuery form plugin, which is licensed under the GPL.
 57+ // http://jquery.malsup.com/form/#download
 58+ $.fn.formToArray = function() {
 59+ var a = [];
 60+ if (this.length == 0) return a;
5161
52 - $( mw ).trigger( 'LivePreviewDone', [copyElements] );
53 - } );
54 -};
 62+ var form = this[0];
 63+ var els = form.elements;
 64+ if (!els) return a;
 65+ for(var i=0, max=els.length; i < max; i++) {
 66+ var el = els[i];
 67+ var n = el.name;
 68+ if (!n) continue;
5569
56 -// Shamelessly stolen from the jQuery form plugin, which is licensed under the GPL.
57 -// http://jquery.malsup.com/form/#download
58 -$.fn.formToArray = function() {
59 - var a = [];
60 - if (this.length == 0) return a;
61 -
62 - var form = this[0];
63 - var els = form.elements;
64 - if (!els) return a;
65 - for(var i=0, max=els.length; i < max; i++) {
66 - var el = els[i];
67 - var n = el.name;
68 - if (!n) continue;
69 -
70 - var v = $.fieldValue(el, true);
71 - if (v && v.constructor == Array) {
72 - for(var j=0, jmax=v.length; j < jmax; j++)
73 - a.push({name: n, value: v[j]});
 70+ var v = $.fieldValue(el, true);
 71+ if (v && v.constructor == Array) {
 72+ for(var j=0, jmax=v.length; j < jmax; j++)
 73+ a.push({name: n, value: v[j]});
 74+ }
 75+ else if (v !== null && typeof v != 'undefined')
 76+ a.push({name: n, value: v});
7477 }
75 - else if (v !== null && typeof v != 'undefined')
76 - a.push({name: n, value: v});
77 - }
7878
79 - if (form.clk) {
80 - // input type=='image' are not found in elements array! handle it here
81 - var $input = $(form.clk), input = $input[0], n = input.name;
82 - if (n && !input.disabled && input.type == 'image') {
83 - a.push({name: n, value: $input.val()});
84 - a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
 79+ if (form.clk) {
 80+ // input type=='image' are not found in elements array! handle it here
 81+ var $input = $(form.clk), input = $input[0], n = input.name;
 82+ if (n && !input.disabled && input.type == 'image') {
 83+ a.push({name: n, value: $input.val()});
 84+ a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
 85+ }
8586 }
86 - }
87 - return a;
88 -};
 87+ return a;
 88+ };
8989
90 -/**
91 - * Returns the value of the field element.
92 - */
93 -$.fieldValue = function(el, successful) {
94 - var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
95 - if (typeof successful == 'undefined') successful = true;
 90+ /**
 91+ * Returns the value of the field element.
 92+ */
 93+ $.fieldValue = function(el, successful) {
 94+ var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
 95+ if (typeof successful == 'undefined') successful = true;
9696
97 - if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
98 - (t == 'checkbox' || t == 'radio') && !el.checked ||
99 - (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
100 - tag == 'select' && el.selectedIndex == -1))
101 - return null;
 97+ if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
 98+ (t == 'checkbox' || t == 'radio') && !el.checked ||
 99+ (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
 100+ tag == 'select' && el.selectedIndex == -1))
 101+ return null;
102102
103 - if (tag == 'select') {
104 - var index = el.selectedIndex;
105 - if (index < 0) return null;
106 - var a = [], ops = el.options;
107 - var one = (t == 'select-one');
108 - var max = (one ? index+1 : ops.length);
109 - for(var i=(one ? index : 0); i < max; i++) {
110 - var op = ops[i];
111 - if (op.selected) {
112 - var v = op.value;
113 - if (!v) // extra pain for IE...
114 - v = (op.attributes && op.attributes['value'] &&
115 - !(op.attributes['value'].specified))
116 - ? op.text : op.value;
117 - if (one) return v;
118 - a.push(v);
 103+ if (tag == 'select') {
 104+ var index = el.selectedIndex;
 105+ if (index < 0) return null;
 106+ var a = [], ops = el.options;
 107+ var one = (t == 'select-one');
 108+ var max = (one ? index+1 : ops.length);
 109+ for(var i=(one ? index : 0); i < max; i++) {
 110+ var op = ops[i];
 111+ if (op.selected) {
 112+ var v = op.value;
 113+ if (!v) // extra pain for IE...
 114+ v = (op.attributes && op.attributes['value'] &&
 115+ !(op.attributes['value'].specified))
 116+ ? op.text : op.value;
 117+ if (one) return v;
 118+ a.push(v);
 119+ }
119120 }
 121+ return a;
120122 }
121 - return a;
122 - }
123 - return el.value;
124 -};
 123+ return el.value;
 124+ };
125125
126 -$(document).ready( function() {
127 - $('#wpPreview').click( doLivePreview );
128 -} );
 126+ $(document).ready( function() {
 127+ $('#wpPreview').click( doLivePreview );
 128+ } );
 129+}) ( jQuery );

Follow-up revisions

RevisionCommit summaryAuthorDate
r822201.17wmf1: MFT r80495, r80765, r81177, r82000, r82155, r82156, r82191, r82200,...catrope07:23, 16 February 2011
r85151MFT: r82000, r82004, r82020, r82025, r82038, r82039, r82048, r82070, r82081, ...demon20:39, 1 April 2011

Status & tagging log