Користувач:Leh Palych/test.js
Увага: Після публікування слід очистити кеш браузера, щоб побачити зміни.
- Firefox / Safari: тримайте Shift, коли натискаєте Оновити, або натисніть Ctrl-F5 чи Ctrl-Shift-R (⌘-R на Apple Mac)
- Google Chrome: натисніть Ctrl-Shift-R (⌘-Shift-R на Apple Mac)
- Internet Explorer / Edge: тримайте Ctrl, коли натискаєте Оновити, або натисніть Ctrl-F5
- Opera: натисніть Ctrl-F5
$(function () {
var ns = mw.config.get( 'wgNamespaceNumber' );
if( ! [0, 250].includes( ns ) ) {
return;
}
var action = mw.config.get( 'wgAction' );
if ( ns == 0 && action == 'view' ||
ns == 250 && [ 'edit', 'submit' ].includes( action ) ) {
var portletLink = mw.util.addPortletLink(
"p-tb",
"#",
"Створити статтю для слова",
"t-new_page4word",
"Створення/оновлення статті для слова"
);
$( portletLink ).on( "click", function ( e ) {
e.preventDefault();
update_page4word();
});
}
});
var aDic = new Array (
[["Грінченко. Словарь української мови (1924). Том 1.djvu",
"Грінченко. Словарь української мови (1924). Том 2.djvu",
],'Словарь української мови (1924)','Словник, 1924', false],
[["Василь Доманицький. Словарик. Пояснення чужих та не дуже зрозумілих слів. 1906.pdf",
],'Словарик. Пояснення чужих та не дуже зрозумілих слів','Словник, 1906', false],
[["Словник української мови. Том I. А-Ґ. 1927.pdf",
"Словник української мови. Том II. Д-Й. 1927.pdf",
"Словник української мови. Том III. К-Н. 1928.pdf",
],'Словник української мови (1927)','Словник, 1927', false],
[["Грінченко Б. Словник української мови. Том I. А-Ж. 1937.djvu",
],'Словник української мови (1937)','Словник, 1937', false],
[["Дмитро Яворницький. Словник української мови (Т.1, А-К), (1920).djvu",
],'СУМ (Яворницький)','СУМ, Я-1920', false]
);
//-------------------------------------------
function update_page4word()
{
function MyDialog( config ) {
MyDialog.super.call( this, config );
}
OO.inheritClass( MyDialog, OO.ui.ProcessDialog );
MyDialog.static.name = 'myDialog';
MyDialog.static.title = 'Словник';
MyDialog.static.actions = [
{
flags: [ 'primary', 'progressive' ],
label: 'Створити',
action: 'create'
},
{
label: 'Cancel',
flags: [ 'safe', 'close' ]
},
{
flags: [ 'primary', 'progressive' ],
label: 'Перегляд',
action: 'view'
},
];
MyDialog.prototype.initialize = function () {
MyDialog.super.prototype.initialize.call( this );
this.panel = new OO.ui.PanelLayout( {
padded: true,
expanded: false
} );
this.content = new OO.ui.FieldsetLayout();
this.wordInput = new OO.ui.TextInputWidget( {
id: 'dialogWord'
} );
this.field = new OO.ui.FieldLayout( this.wordInput, {
label: 'Введіть слово:',
align: 'top'
} );
this.iconStatus = new OO.ui.IconWidget( {
icon: '',
title: 'Status'
} ),
this.labelMessage = new OO.ui.LabelWidget( {
label: '',
align: 'top'
} );
this.content.addItems( [ this.field, this.iconStatus, this.labelMessage ] );
this.panel.$element.append( this.content.$element );
this.$body.append( this.panel.$element );
this.wordInput.connect( this, { 'change': 'onWordInputChange' } );
this.wordInput.focus();
};
MyDialog.prototype.onWordInputChange = function ( value ) {
this.actions.setAbilities( {
open: !!value.length
} );
};
MyDialog.prototype.getBodyHeight = function () {
return this.panel.$element.outerHeight( true );
};
MyDialog.prototype.getSetupProcess = function ( data ) {
data = data || {};
return MyDialog.super.prototype.getSetupProcess.call( this, data )
.next( function () {
this.wordInput.setValue( data.word );
this.wordInput.focus();
}, this );
};
MyDialog.prototype.getActionProcess = function ( action ) {
var message = this.labelMessage;
var icon = this.iconStatus;
var nbsp = String.fromCharCode(160);
var word = this.wordInput.getValue();
word = word.trim()
if (! word && [ 'view', 'create' ].includes( action )) {
return new OO.ui.Process( function () {
message.setLabel( nbsp+'Введіть слово.');
icon.setIcon('notice');
setTimeout(clearMessage, 3000, icon, message);
}, this );
}
// Перегляд сторінки слова
if ( action === 'view' ) {
return new OO.ui.Process( function () {
var servername = mw.config.get('wgServer');
var path = mw.config.get('wgArticlePath');
path = path.replace('$1', '');
var link = servername + path + word;
window.open( link );
}, this );
}
// Створення/оновлення сторінки слова
if ( action === 'create' ) {
return new OO.ui.Process( function () {
var link, wordCap, ind, dic;
var servername = mw.config.get('wgServer');
var path = mw.config.get('wgArticlePath');
path = path.replace('$1', '');
word = word.toLowerCase();
wordCap = word[0].toUpperCase() + word.slice(1);
link = servername + path + word;
var shablon = "{{слово\n | назва = $1\n$2}}\n";
var textPage = '';
var wordText;
var lFound = false;
var lAdd = false;
for (ind in aDic) aDic[ind][3] = false;
mw.loader.using( [ 'mediawiki.api' ], function () {
var api = new mw.Api();
var params;
params = {
action: 'query',
titles: word,
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
formatversion: '2',
format: 'json'
};
// Check page's presence
api.get( params ).done( function ( data ) {
var pages, aInd;
var no_page = false;
pages = data.query.pages;
ind = pages.length-1;
var revs = pages[ind].revisions;
if(typeof revs !== 'undefined'){
ind = revs.length-1;
wordText = revs[ind].slots.main.content;
for (var idic in aDic) {
aInd = aDic[idic][0];
for (var iind in aInd) {
if (wordText.includes(aInd[iind])) {
lFound = true;
aDic[idic][3] = true;
}
}
}
}
else { no_page = true }
if( no_page || lFound ) {
if( no_page) {
textPage = shablon;
}
if( lFound ) {
textPage = wordText + '\n';
}
params = {
action: 'query',
titles: '',
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
formatversion: '2',
format: 'json'
};
for( ind in aDic ) {
dic = aDic[ind];
if(! dic[3]) {
params.titles += dic[1] + '/' + word + '|';
params.titles += dic[1] + '/' + wordCap + '|';
}
}
params.titles = params.titles.slice(0, -1);
api.get( params ).done( function ( data ) {
var pages, wordPage, ind, last, revs, title, num;
var pos1, pos2, lUpCase, nameNewPage;
var absent = true;
for(ind in aDic) {
if(aDic[ind][3]) {
absent = false;
break;
}
}
pages = data.query.pages;
for(ind in pages) {
revs = pages[ind].revisions;
if(typeof revs !== 'undefined'){
title = pages[ind].title;
pos1 = title.indexOf('/' + word);
lUpCase = pos1 == -1;
if (lUpCase) {
pos1 = title.indexOf('/' + wordCap);
nameNewPage = wordCap;
}
else {
nameNewPage = word;
}
if( no_page) {
textPage = textPage.replace("$1", nameNewPage);
if (lUpCase) {
textPage = textPage.replace("$2", " | upcase = 1\n");
}
else {
textPage = textPage.replace("$2", "");
}
}
title = title.slice(0, pos1);
for(var jind in aDic){
if(aDic[jind][1] == title) {
num = jind;
break;
}
}
absent = false;
last = revs.length-1;
wordPage = revs[last].slots.main.content;
pos1 = wordPage.indexOf('<pages index');
pos2 = wordPage.indexOf('/>', pos1+1);
if( pos1 > -1 ) {
if( lAdd || lFound ) textPage += '\n\n';
textPage += wordPage.slice(pos1, pos2+2) + '\n';
textPage += ': {{збірка|'+ aDic[num][2] +'|1}}' + '\n';
lAdd = true;
}
}
}
// Create/Update word's page
if( lAdd ) {
params = {
action: 'edit',
title: word,
text: textPage,
summary : '.',
contentformat: 'text/x-wiki',
format: 'json'
},
api.postWithToken( 'csrf', params ).done( function ( data ) {
message.setLabel( nbsp+'Створена/оновлена сторінка');
icon.setIcon('check');
setTimeout(clearMessage, 3000, icon, message);
} );
}
else {
if( absent ) {
message.setLabel( nbsp+'Немає слова в словниках.');
icon.setIcon('notice');
}
else {
message.setLabel( nbsp+'Сторінка вже є. Немає доповнень.');
icon.setIcon('alert');
}
setTimeout(clearMessage, 3000, icon, message);
}
} ); // API новий вміст сторінки
}
else {
message.setLabel( nbsp+'Сторінка не зі словника Вікіджерел.');
icon.setIcon('cancel');
setTimeout(clearMessage, 3000, icon, message);
}
} ); // API перевірка наявності сторінки слова
} );
}, this );
}
return MyDialog.super.prototype.getActionProcess.call( this, action );
};
MyDialog.prototype.getTeardownProcess = function ( data ) {
return MyDialog.super.prototype.getTeardownProcess.call( this, data )
.first( function () {
}, this );
};
var windowManager = new OO.ui.WindowManager();
$( document.body ).append( windowManager.$element );
var myDialog = new MyDialog({
size: 'small',
modal: true
});
windowManager.addWindows( [ myDialog ] );
var val, startPos, endPos
var tbox = document.getElementById("wpTextbox1");
if(tbox !== null){
startPos = tbox.selectionStart;
endPos = tbox.selectionEnd;
if (endPos > startPos) val = tbox.value.substring(startPos, endPos);
}
else val = "";
windowManager.openWindow( myDialog, { word: val } );
function clearMessage(icon, message) {
icon.setIcon('');
message.setLabel('');
}
}