MediaWiki:EditConflictAlert/code.js

/* EditConflictAlert * * Tells the user when a page that is currently being edited is updated. * @scope: Any * @author: Dorumin */

(function($, mw) {   if (!$ || !mw || (wgAction != 'edit' && wgAction != 'submit')) return;    var obj = {		content: false,		seenid: false,        curid: false,        newid: false,        wait: false,		diff: false,		processing: false,        interval: window.queryInterval || 5000,		page: mw.config.get('wgPageName'),		ajax: $(' ').attr({ id: 'ajax-indicator', src: mw.config.get('stylepath') + '/common/images/ajax.gif', title: 'Loading...', alt: 'Loading...' }).css({ float: 'center', margin: '2em auto', display: 'block' }),		notify: function {			var self = obj;			/* I had to make my own function. BannerNotification doesn't work in edit pages. */			if ($('#view-diff').length || self.seenid == self.newid) return;			var $notif = $(' \  \ Warning! A new edit was made to this page since you started editing! \					(diff)\ ').hide;			$('#notification-container').append($notif);			$notif.find('.close').click(function { self.seenid = self.newid; $notif.slideUp(200, function {					$notif.remove;				}); });			$notif.slideDown;			$notif.find('#view-diff').click(obj.viewDiff);		},		addContents: function {			$('#diff-modal').closeModal;			$('#close-banner').click;			var self = obj;			self.seenid = self.newid;			var $editBox = $('#wpTextbox1');			var yourText = $editBox.val;			$editBox.val(self.content);			var diff = $(' ').html(' \ The differences (help)\ ' + self.diff);			var myedit = $(' ').html(' \ This is your edit. Copy your changes to the box above!\ \				' + yourText + ' ');			$('.mw-editTools').after(diff, myedit);		},		loadDiff: function {			var self = obj;			$.get('/?diff=' + self.newid + '&oldid=' + self.curid, function(diff) { var el = $(diff).find('.diff'); self.diff = $(' ').append(el.clone).html; $('#ajax-indicator').replaceWith(self.diff); });		},		viewDiff: function {			var self = obj;			$.showCustomModal('Difference between revisions', self.ajax, { width: 1000, id: 'diff-modal', callback: self.loadDiff, buttons: [{ message: 'Close', handler: function { self.seenid = self.newid; $('#diff-modal').closeModal; }				}, {					defaultButton: true, message: 'Update!', handler: self.addContents }]			});		},       query: function {            var self = obj;            var Api = self.Api;			if (self.processing) {				self.wait = true;				return;			}			self.processing = true;            Api.get({ action: 'query', prop: 'revisions', rvprop: 'ids|content', titles: self.page, cb: new Date.getTime }).done(function(d) { self.processing = false; if (self.wait) { self.wait = false; self.query; }				var p = d.query.pages[Object.keys(d.query.pages)[0]]; self.newid = p.revisions[0].revid; if (!self.curid || self.curid == self.newid) return; self.content = p.revisions[0]['*']; $('.banner-notification.notify:has(#view-diff)').remove; self.notify; });       },        init: function {            var self = this;            var Api = self.Api = new mw.Api;            Api.get({ action: 'query', prop: 'revisions', rvprop: 'ids', titles: self.page, cb: new Date.getTime }).done(function(d) { self.curid = d.query.pages[Object.keys(d.query.pages)[0]].revisions[0].revid; });			$('.WikiaMainContent').prepend(' ');           setInterval(self.query, self.interval);        }    };	var interval = setInterval(function { if (!mw || !mw.Api) return; clearInterval(interval); obj.init; }, 250); })(window.jQuery, window.mediaWiki);