مدیاویکی:Gadget-DiffPicker.js: تفاوت میان نسخهها
ظاهر
imported>Alpineer صفحهای تازه حاوی «(function ($, mw) { 'use strict'; var cookie = { set: function(name, value){ mw.cookie.set(name, JSON.stringify(value)); }, get:...» ایجاد کرد |
imported>Alpineer بدون خلاصۀ ویرایش |
||
| خط ۱: | خط ۱: | ||
(function ($, mw) { | (function ($, mw) { | ||
'use strict'; | 'use strict'; | ||
var bookmarks = []; | |||
var menuLink = '<li class="n-pcikeddiffs"><a href="#">تفاوتها</a> <span id="diffCounter">(۰)</span></li>'; | |||
$(menuLink).insertAfter('#n-recentchanges'); | |||
var cookie = { | var cookie = { | ||
set: function(name, value){ | set: function(name, value){ | ||
| خط ۶: | خط ۱۴: | ||
}, | }, | ||
get: function(name){ | get: function(name){ | ||
var value = mw.cookie.get(name); | |||
if (value) { | |||
return JSON.parse(value); | |||
} else { | |||
return []; | |||
} | |||
} | } | ||
}; | }; | ||
counter(); | |||
var diff = mw.config.get('wgDiffNewId'); | var diff = mw.config.get('wgDiffNewId'); | ||
| خط ۲۲: | خط ۳۶: | ||
doIt(diff); | doIt(diff); | ||
}); | }); | ||
function counter(){ | |||
var n = cookie.get('pickedDiffs').length; | |||
$('#diffCounter').removeClass(); | |||
if (n > 0) { | |||
$('#diffCounter').addClass('red'); | |||
} | |||
$('#diffCounter').text('(' + toPersianNum(n) + ')'); | |||
} | |||
function doIt(revId){ | function doIt(revId){ | ||
revId = parseInt(revId); | |||
bookmarks = cookie.get('pickedDiffs'); | bookmarks = cookie.get('pickedDiffs'); | ||
var index = bookmarks.indexOf(revId); | var index = bookmarks.indexOf(revId); | ||
| خط ۳۳: | خط ۵۷: | ||
cookie.set('pickedDiffs', bookmarks); | cookie.set('pickedDiffs', bookmarks); | ||
icon(); | icon(); | ||
counter(); | |||
} | } | ||
| خط ۴۳: | خط ۶۸: | ||
$('#diffPeaker-btn').addClass('notPicked'); | $('#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', '.n-pcikeddiffs', function(){ | |||
bookmarks = cookie.get('pickedDiffs'); | |||
if (bookmarks.length > 0) { | |||
openDiffPicker(); | |||
} else { | |||
mw.notify('هیچ تفاوت ذخیره شدهای وجود ندارد.'); | |||
} | |||
}); | |||
$(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)); | }(jQuery, mediaWiki)); | ||
نسخهٔ ۱ دسامبر ۲۰۱۷، ساعت ۱۳:۲۲
(function ($, mw) {
'use strict';
var bookmarks = [];
var menuLink = '<li class="n-pcikeddiffs"><a href="#">تفاوتها</a> <span id="diffCounter">(۰)</span></li>';
$(menuLink).insertAfter('#n-recentchanges');
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 [];
}
}
};
counter();
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(){
var n = cookie.get('pickedDiffs').length;
$('#diffCounter').removeClass();
if (n > 0) {
$('#diffCounter').addClass('red');
}
$('#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', '.n-pcikeddiffs', function(){
bookmarks = cookie.get('pickedDiffs');
if (bookmarks.length > 0) {
openDiffPicker();
} else {
mw.notify('هیچ تفاوت ذخیره شدهای وجود ندارد.');
}
});
$(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));