مدیاویکی:Gadget-DiffPicker.js - ویکیپدیا، دانشنامهٔ آزاد
نکته: برای دیدن تغییرات، ممکن است نیاز باشد که حافظهٔ نهانی مرورگر خود را پس از انتشار پاکسازی کنید. گوگل کروم، فایرفاکس، مایکروسافت اج و سافاری: کلید ⇧ Shift را نگه دارید و روی دکمهٔ Reload در نوار ابزار مرورگر کلیک کنید. برای آگاهی از جزئیات و نحوهٔ پاکسازی حافظهٔ نهانی سایر مرورگرها، صفحهٔ ویکیپدیا:میانگیر مرورگرتان را خالی کنید را ببینید.
(function ($, mw) { 'use strict'; var bookmarks = []; $(function () { $(mw.util.addPortletLink('p-tb', '#', 'تفاوتها', 't-pickeddiffs')) .click(function (e) { e.preventDefault(); bookmarks = cookie.get('pickedDiffs'); if (bookmarks.length > 0) { openDiffPicker(); } else { mw.notify('هیچ تفاوت ذخیره شدهای وجود ندارد.'); } return false; }); counter(); }); var cookie = { set: function(name, value){ mw.cookie.set(name, JSON.stringify(value)); }, get: function(name){ var value = mw.cookie.get(name); if (value) { return JSON.parse(value); } else { return []; } } }; var diff = mw.config.get('wgDiffNewId'); if (diff) { $('.diff-ntitle').prepend('<div id="diffPeaker-btn"></div>'); icon(); } $(document).on('click', '#diffPeaker-btn', function(){ doIt(diff); }); function counter(){ $('#t-pickeddiffs a').html('تفاوتها <span id="diffCounter"></span>'); var n = cookie.get('pickedDiffs').length; $('#diffCounter').removeClass(); if (n > 0) { $('#diffCounter').addClass('red'); } if (n == 0) { n = ''; } $('#diffCounter').text(toPersianNum(n)); } function doIt(revId){ revId = parseInt(revId); bookmarks = cookie.get('pickedDiffs'); var index = bookmarks.indexOf(revId); if (index == -1) { bookmarks.push(revId); } else { bookmarks.splice(index, 1); } cookie.set('pickedDiffs', bookmarks); icon(); counter(); } function icon(){ bookmarks = cookie.get('pickedDiffs'); $('#diffPeaker-btn').removeClass(); if (bookmarks.indexOf(diff) != -1) { $('#diffPeaker-btn').addClass('picked'); } else { $('#diffPeaker-btn').addClass('notPicked'); } } function toPersianNum(num, dontTrim) { var i = 0, dontTrim = dontTrim || false, num = dontTrim ? num.toString() : num.toString().trim(), len = num.length, res = '', pos, persianNumbers = typeof persianNumber == 'undefined' ? ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'] : persianNumbers; for (; i < len; i++) { if (( pos = persianNumbers[num.charAt(i)] )) res += pos; else res += num.charAt(i); } return res; } function insertAtCursor(selector, value) { var cursorPos = $(selector).prop('selectionStart'); var v = $(selector).val(); var textBefore = v.substring(0, cursorPos); var textAfter = v.substring(cursorPos, v.length); $(selector).val(textBefore + value + textAfter); } $(document).on('click', '.DiffList .unpickDiff', function(){ var row = $(this); doIt(row.attr('diff')); row.parents('tr').remove(); }); // Interface var DiffModule = {}; function DiffPicker( config ) { DiffPicker.parent.call( this, config ); } OO.inheritClass( DiffPicker, OO.ui.ProcessDialog ); DiffPicker.static.name = 'DiffPicker'; DiffPicker.static.title = 'گزینشگر تفاوت'; DiffPicker.static.actions = []; if ($('#wpTextbox1').length > 0) { DiffPicker.static.actions.push({ action: 'insert', label: 'درج', flags: 'primary' }); } DiffPicker.static.actions.push({action: 'cancel', label: 'بستن', flags: 'safe'}); DiffPicker.static.actions.push({action: 'clear', label: 'خالی کردن فهرست'}); DiffPicker.prototype.initialize = function () { DiffPicker.parent.prototype.initialize.apply( this, arguments ); this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } ); DiffModule = new OO.ui.ToggleSwitchWidget({ value: true }); var fieldset = new OO.ui.FieldsetLayout(); fieldset.addItems( [ new OO.ui.FieldLayout( DiffModule, { label: 'الگوی تفاوتها' } ) ] ); this.content.$element.append(fieldset.$element); bookmarks = cookie.get('pickedDiffs'); var diffsList = '<table class="DiffList"><tr><th>پیوند تفاوت</th><th>حذف</th></tr>'; for (var i in bookmarks) { diffsList += '<tr>'; diffsList += '<td><a href="https://fa.wikipedia.org/w/index.php?diff=' + bookmarks[i] + '">[[ویژه:تفاوت/' + bookmarks[i] + ']]</a></td>'; diffsList += '<td><img src="https://upload.wikimedia.org/wikipedia/commons/d/d0/OOjs_UI_icon_clear-progressive.svg" class="unpickDiff" diff="' + bookmarks[i] + '"/></td>'; diffsList += '</tr>'; } diffsList += '</table>'; this.content.$element.append(diffsList); this.$body.append( this.content.$element ); }; DiffPicker.prototype.getActionProcess = function ( action ) { var dialog = this; bookmarks = cookie.get('pickedDiffs'); if (action == 'insert' && bookmarks.length > 0) { var out = ''; if (DiffModule.getValue()) { out += '{{تفاوتها|'; for (var i in bookmarks) { out += bookmarks[i]; if (i <= bookmarks.length - 2) { out += '|'; } } out += '}}' } else { for (var i in bookmarks) { out += '[[ویژه:تفاوت/' + bookmarks[i] + ']]'; if (i <= bookmarks.length - 3) { out += '، '; } if (i == bookmarks.length - 2) { out += ' و '; } } } insertAtCursor('#wpTextbox1', out); mw.notify('پیوندهای تفاوت در متن درج شدند.'); } else if (action == 'clear') { cookie.set('pickedDiffs', []); counter(); return new OO.ui.Process( function () { dialog.close( { action: action } ); } ); } else { return new OO.ui.Process( function () { dialog.close( { action: action } ); } ); } return DiffPicker.parent.prototype.getActionProcess.call( this, action ); }; function openDiffPicker() { var windowManager = new OO.ui.WindowManager(); $( 'body' ).append( windowManager.$element ); var dialog = new DiffPicker(); windowManager.addWindows( [ dialog ] ); windowManager.openWindow( dialog ); } }(jQuery, mediaWiki));